How to 100% reinstall nixos in-place?

So first of all, for future reference, this is why you don’t ever manually modify /nix/store ever ever ever. You could have used nix-store --verify --check-contents --repair to check your store for corrupted files and attempt to repair them from the binary cache.

With that said, this is going to be very difficult to fix. Do nix-store --verify --check-contents --repair to repair as much as you can, but it’s not going to fix everything. Anything that’s generated locally, critically including those .drv paths, will not be repaired. These paths will have to be deleted and purged from the nix DB, but the DB won’t allow you to do that unless everything that depends on them is also deleted and purged.

nix-store --query --referrers-closure /nix/store/mdchwscx6l2kkvjpw8l4f29k9qizpfl1-rust-rover-2024.3.4.drv \
  | xargs nix-store --delete --no-keep-derivations

If you’ve also corrupted non-.drv paths, then you’ll likely have to delete GC roots from the system before those will be allowed to be deleted.

From here you can hopefully rebuild. If these are your system generations that you’ve been messing with, then you must rebuild your system before you reboot, or it will likely fail to boot.

I can’t stress enough how extreme of a circumstance this is, and I strongly urge you to read the manuals and understand the commands I’ve given before you run them. This is why you don’t manually modify the store. Always use commands like nix-store or nix-collect-garbage to make modifications to it.

2 Likes