How to remove old system generations?

How can I completely remove old nixos-generations?

I know that I can delete old generations with nix-collect-garbage or even nix-collect-garbage -d. But this seems not to work correctly for system generations.
So: How can I really list and remove old NixOS-system generations?

Details:

  • nix-collect-garbage -d
    was run (as root)
  • nix-env -p /nix/var/nix/profiles/system --list-generations
    lists exactly 1 configuration
  • /boot/grub/grub.cfg
    contains exactly 1 configuration (“NixOS - Default”)
  • but /nix/store contains e.g. several old linux-kernels, gcc etc.

Checking the reverse-dependencies of the old linux-kernels returns e.g.:

# nix-store --query --referrers /nix/store/0n0j02amv1vayml7qr7nlrl8rzrycgyf-linux-4.19.69
/nix/store/0n0j02amv1vayml7qr7nlrl8rzrycgyf-linux-4.19.63
/nix/store/ygcr4jynnr4cp1y0dxs1s8chx1x9k900-kernel-modules
/nix/store/aymqf7y6swzrnm0c467ghklcgk8b9ajz-nixos-system-MYHOST-19.03.173228.93ea6db29cc

# nix-store --query --referrers /nix/store/832wnm2846j4avwicy1n7sar6yaijvnr-linux-4.19.69
/nix/store/832wnm2846j4avwicy1n7sar6yaijvnr-linux-4.19.69
/nix/store/z81yd4pg3biy8zw8c675fa3qzycq1s0v-kernel-modules
/nix/store/100y8581l6qffgl9x8ligwm6k6731941-nixos-system-MYHOST-19.03.173408.bd6ba87381e

# nix-store --query --referrers f38dx1144qc28hk1ji10f4klgmq4rp3f-linux-4.19.87
/nix/store/f38dx1144qc28hk1ji10f4klgmq4rp3f-linux-4.19.87
/nix/store/yimd00zkkf2wpkybfcz7iax3xr0m9s7f-kernel-modules
/nix/store/4fw2cigw4680cq6jh1g4xj11905579pz-nixos-system-MYHOST-19.09.1529.808d3c6d123

So, it seems that there are still some old nixos-system-generations, but they are not included in the list of generations and are not deleted from the garbage-collector.
How can I list and delete them?

2 Likes

Strange. Run nix-store --query --roots /nix/store/... on any path to see what’s keeping it alive. They must be referred to by some root or else the GC would have worked.

5 Likes

There seem to be “result”-symlinks in strange places, linking to old generations.
Looks like they were left over from nixos-rebuild build-tests.

Didn’t expect that, so I guess NixOS 23.11 manual | Nix & NixOS should be improved to document this behavior there.

1 Like

FWIW, it is explained in the nixos-rebuild man page :stuck_out_tongue: