Restore a backup of /nix/store

My old MacOS workstation where I used Nix as sort of package manager (brew replacement) died. I have a copy of (only) /nix/store from it. It is not really important since I can just reinstall everything I need on new machine but for the sake of learning something new: how could I take say /mnt/nix/store (the backup) and move everything to my new /nix/store? I mean I can just copy it over, but as far as I am aware Nix has some sqlite database. Also all the stuff should land in my .nix-profile. Can that be done with “nix copy” somehow?

Yes, I am aware of NixOS and declarative package management out of some configuration.nix. I have a toy NixOS machine to play around, but I am not yet confident enough to use it for my everyday tasks.

You should be able to just rsync --hard-links --archive all the stuff over, then create a symlink from ~/.nix-profile to /nix/var/nix/profiles/per-user/${USER}/profile, ~/.nix-defexpr/channels to /nix/var/nix/profiles/per-user/${USER}/channels, that should roughly be it.

Though this is an assumption, I have never tried it!

Also this does not consider anything that is special to macOS! I do not own such a thing, nor did I ever.

Nix allows you to share packages between local machines: Introduction

You might have to hack into nix-serve for this task, since the store you’re trying to use isn’t installed on a machine: GitHub - edolstra/nix-serve: A standalone Nix binary cache server

I don’t think you need to hack nix-serve. Instead you can run a shell under unshare and mount the backup nix store at /nix/store while nix-serve is running. That’s what I have done to restore from a backup

Let me perhaps rephrase my question, what is /nix/var/nix/db/db.sqlite for?
Based on Rebuild sqlite db from scratch? · Issue #3091 · NixOS/nix · GitHub I guess it’s just a cache.
Then what (exactly) happens when there is something in /nix/store but not in the sqlite database?

Unshare is a cool thing, but I don’t have that on MacOS. So ok I guess it is safe (but not very elegant)
if I just manually copy/rsync into /nix/store. And then I can just ‘nix-store --add /nix/store/*’.

Then I still have the problem that the stuff is not in my .nix-profile. So I was thinking about going through all *.drv files, sorting the stuff and then re-realizing the latest version of each program (hope I am using the right terminology). Which since everything is already present shouldn’t do anything except add that symlinks.

1 Like

Let me perhaps rephrase my question, what is /nix/var/nix/db/db.sqlite for?

I tracks valid paths (ie. what’s installed) among other things. If you start with an empty database nix will remove everything that’s not registered when you start a garbage collect. This is because nix can’t validate the content or track any of it’s dependencies without it.