Skip to content

NixOS

Garbage

nix-build and profile generations are stored GC roots. nix-build results in a GC root referrer to a specific library version for that build. After an upgrade, delete the previous build so the garbage collector will remove the corresponding derivation, as well as if we want old dependencies cleaned up.

Manipulating the nix-env profile will create further generations. Old generations refer to old software, thus increasing duplication in the nix store after an upgrade.

Garbage Collection Steps

  • nix-channel --update download a new version of the nixpkgs channel, which holds the description of all the software.

  • nixos-rebuild --switch to upgrade

  • Remove all indirect roots generated by nix-build: beware, as this will result in dangling symlinks. A smarter strategy would also remove the target of those symlinks.

  • nix-collect-garbage -d the -d option of is used to delete old generations of all profiles, then collect garbage. You lose the ability to rollback to any previous generation. Ensure the new generation is working well before running this command.

The four steps are shown below:

nix-channel --update

nix-env -u --always

rm /nix/var/nix/gcroots/auto/*

nix-collect-garbage -d