Deleted /nix/var/nix/db/

I need a new /nix/var/nix/db/. Where can I extract, recreate or download this one? I will not make a fresh install of the whole OS.

I deleted it after I once again didn’t find out how to upgrade to the next version and I’m stuck at 2.24.14.

nix-channel --add nixos-25.05 release nixos-25.05.804745.7284e2decc98 nixos && sudo nix-channel --update nixos

and then

nixos-rebuild switch --upgrade

didn’t work. So I thought I need to delete the cache. Unfortunately I was too reckless and didn’t make a backup of the DB. So, is there a way to recreate the database? Also, HOW to upgrade to a new version? This should be easy to find, but it isn’t, it was a pain the last time as well.

ls /nix/var/nix/db/
big-lock db.sqlite-shm db.sqlite-wal reserved schema

Edit: I was looking for the path with search, but by looking for Nix database I found some hints.

This seems to be a bigger issue with that database getting corrupted and it’s not easy to recreate.

I also didn’t know that I should backup /nix/var, /nix contains the store.

Edit2: What is the db good for … Restore a backup of /nix/store - #6 by LnL7

After having had a few corruptions, I now back up /nix/var (and exclude /nix/store).

I was once able to manually run some SQL to fix a breakage, but it was a last ditch effort and highly specific to the issue I was facing.

I think you might be best off running nixos-install, which will lose all your generations but get you back up and running relatively quickly.

1 Like

Thanks, but I still don’t trust this. I don’t know what will be lost by that.

I also don’t get what would be so difficult, when I still have my Nix store and only want the current state back in there.

I’m trying to rebuild the db and will at least wait till I have more information. Recreating that db should be an option anyways.

touch /nix/var/nix/db.sqlite
sqlite3 /nix/var/nix/db.sqlite “.recover”
sqlite3 /nix/var/nix/db.sqlite “PRAGMA integrity_check;”

This worked so far. I need the permissions and the pattern or scheme of what’s supposed to be in there.

The nix db contains state not available from the store itself, regarding things like where the data came from and why it should (or shouldn’t) be trusted. That’s why it exists. You can just tell it to blindly trust everything it stumbles across, but that’s not a restoration, even though it functions. I know the nixos install media do something similar since they’re essentially “creating” a prepopulated store.

I really do recommend obliterating /nix/store and building your system over again with nixos-install. Everything in /nix/store should be completely replaceable so long as you still have the nix expressions it was built with. Everything untouched by a normal nixos-rebuild is also untouched by this process (except that nixos-install will install the bootloader, anyway). Trying to save your store is going at things backwards. The store is fundamentally a cache. Regenerate it.

3 Likes

I’m still reading through the discussion on Github, and won’t make a decission before I’m done with it.

In regards to the db you kinda refuted your own point. Since everything installed is something I already trusted, there’s no reason to not just put it back into the db.

Anyways, if I can really just rest restore the store, maybe I’ll do that. It’s still absurd, though.

They already lost all their generations by deleting /nix/var/.

What’s absurd is deleting the source-of-truth and expecting that to not cause problems.

6 Likes

Great, you just derailed the conversation. If you don’t want to help, then don’t post here.

Obviously I didn’t know that the db was “the source-of-truth”.

Absurd is it to me that I still have all the store, but the db was everything, but it also has no automatic backup. I still didn’t get an explanation why it’s such a big deal that the db is gone, when I’m trusting the store.

Yes, there’s no auto-backup. Would be a nice feature request.