How does the garbage collector determine what to delete from /nix/store?

Why does the garbage collector not delete the package in this scenario?

  1. Add a single package to configuration.nix
  2. Rebuild
  3. Delete that single added package
  4. Rebuild
  5. Run nix-collect-garbage -d as root

I expected that it would be deleted as it’s not part of the current generation.

The profile that’s set with configuration.nix is owned by root. You have to run nix-collect-garbage -d as root with sudo to get it to delete old system generations.

Seems like you don’t remove the generation. This single package is still linked to one of your old generations. Try to check with:
sudo nix-env -p /nix/var/nix/profiles/system --list-generations

If you remove all other generations with --delete-generations switch, you’ll probably see it’ll be the victim of new garbage collection.

2 Likes

I see. So there are actually three “users” with profiles - me, root, and system? And I have to refer to /nix/var/nix/profiles/system to reference the system profile. That makes sense