Nix’s sqlite db semantics

Are the semantics of the db written down anywhere? I’m thinking about the Intensional Store RFC as well as things like triggers and realized I don’t have a clear understanding of the DB’s role and semantics.

The DB is mainly there to keep some information in cache and speed up operations like garbage-collection. You can find its schema over here: https://github.com/NixOS/nix/blob/f435634a29551754d5f7303b0a60cd8fe2df2079/src/libstore/schema.sql

One important thing to know is that the DB isn’t part of the public interface and can change at any time. Usually when that happens we try really hard to make the change forward and backward compatible.

If the sqlite db gets into a bad state or is otherwise corrupted/lost, is there a way to regenerate it from scratch? Or a runbook of what to do in this scenario?

5 Likes

That would be nice.
I have just created an empty sqlite.db with the scheme and no data.
Bad luck if you have no sqlite3 binary around.

You could try deleting /nix/var/nix/db/{db.sqlite,schema}. From skimming the code it looks like if the schema file is missing it will create a new db, though I haven’t tested, and I don’t know if there’s any problem with creating a fresh db while having an existing store (e.g. does it properly realize the db doesn’t reflect the store state?)

1 Like