List and delete NixOS generations

Hi everybody,

This seems like an obvious questions but I can’t find in manual / on discourse how to list all NixOS generations on my laptop and delete some.

I know about nix store gc but I’d like to for example delete generations from yesterday when I was experimenting but keep a generation from the day before.

I found some mention of nix-env that can do that but I’d prefer to use nix (the new version) if possible.

Thank you.

4 Likes

Go to /nix/var/nix/profiles/ and remove symlinks for generations you want to delete, then nix store gc.

Indeed I didn’t see an obvious place where this is documented - either I didn’t look in the right place yet or that might be something that’s worth improving :slight_smile:

5 Likes

List generations: nix profile history --profile /nix/var/nix/profiles/system
Delete generations: sudo nix profile wipe-history --profile /nix/var/nix/profiles/system --older-than 14d.
Manuals: man nix3-profile-history and man nix3-profle-wipe-history.

17 Likes

Thank you very much both! I’ve just rebuilt twice in a row and nix profile history is showing only one new number - not two. So it’s working as I hoped it will.

Is it possible to see if I’m on the latest generation? For example if I run nixos-rebuild boot --flake ... I shouldn’t be and if I run nixos-rebuild switch --flake ... I should be.

You could check if /run/current-system/ and /run/booted-system/ point to the same system as /nix/var/nix/profiles/system

1 Like

Maybe something like this:

diff <(ls -l /run/current-system/ | sort) <(ls -l /run/booted-system/ | sort)

Hi folks,

Could you please confirm that this is supported / safe method? Asking since this is not a CLI command and I worry that I that brake something by messing with symlinks.

And I don’t want to use sudo nix profile wipe-history --profile /nix/var/nix/profile/system --older-than 14d since I want to keep a few older generations.

Thank you.

3 Likes

Deleting symlinks from /nix/var/nix/profiles/ is mostly safe. It might leave you with some broken entries in Grub, but those should be cleaned up with the next nixos-rebuild {boot|switch}.

4 Likes

Thank you very much!

I’m confused:

Why do I get an empty result when I run nix profile list or nix profile history, even though I have 14 generations on my machine?

$ ls -l /nix/var/nix/profiles | sort
drwxr-xr-x 1 root root  8 Apr 16 22:01 per-user
lrwxrwxrwx 1 root root 14 Apr 27 03:53 system -> system-14-link
lrwxrwxrwx 1 root root 43 Apr 22 10:20 default -> /nix/var/nix/profiles/per-user/root/profile
lrwxrwxrwx 1 root root 87 Apr 16 22:14 system-1-link -> /nix/store/fxc44sjh02dcmv750yc2jmvq562y3ay6-nixos-system-foo-25.05.20250405.1fc1117
lrwxrwxrwx 1 root root 87 Apr 19 00:32 system-2-link -> /nix/store/n15j65mmbiqy7chpmklvnmiz878v9bis-nixos-system-foo-25.05.20250405.1fc1117
lrwxrwxrwx 1 root root 87 Apr 22 23:22 system-3-link -> /nix/store/lndlyba1djd1gx47x4mvrj3cjwgg0rad-nixos-system-foo-25.05.20250405.1fc1117
lrwxrwxrwx 1 root root 87 Apr 23 08:28 system-4-link -> /nix/store/f4bqm0gq51m7a4ix5jsbj5rnqv7zlnbv-nixos-system-foo-25.05.20250423.d916277
lrwxrwxrwx 1 root root 87 Apr 24 23:51 system-5-link -> /nix/store/4nlv8gcn9h2m4ga4pa0v139i2cagxmsh-nixos-system-foo-25.05.20250423.d916277
lrwxrwxrwx 1 root root 87 Apr 25 00:35 system-6-link -> /nix/store/38z3nm53z1xpkm042h1s912y5hb8lki7-nixos-system-foo-25.05.20250423.d916277
lrwxrwxrwx 1 root root 87 Apr 25 21:33 system-7-link -> /nix/store/f2jb9yvs7g12kjcxi9rlh523ybk5c7kf-nixos-system-foo-25.05.20250423.d916277
lrwxrwxrwx 1 root root 87 Apr 26 01:22 system-8-link -> /nix/store/jzh5mq9gg3f1jr72bw6kw0znx8kb5w0q-nixos-system-foo-25.05.20250423.d916277
lrwxrwxrwx 1 root root 87 Apr 26 01:30 system-9-link -> /nix/store/qa5ic9vg4l7c35q5gaklzrcb3pmngm2q-nixos-system-foo-25.05.20250423.d916277
lrwxrwxrwx 1 root root 87 Apr 27 01:41 system-10-link -> /nix/store/waf1bf3y8zncfn7wjg34yv8rs3j45wq9-nixos-system-foo-25.05.20250423.d916277
lrwxrwxrwx 1 root root 87 Apr 27 02:02 system-11-link -> /nix/store/27x6ycixj55184c82cl810ymc085vmyg-nixos-system-foo-25.05.20250423.d916277
lrwxrwxrwx 1 root root 87 Apr 27 02:31 system-12-link -> /nix/store/5di8pvr27ppvwlv50bs28866xqvbpl71-nixos-system-foo-25.05.20250423.d916277
lrwxrwxrwx 1 root root 87 Apr 27 03:15 system-13-link -> /nix/store/99jdjyxsngfd72kzjqpvlls97i9lqgyr-nixos-system-foo-25.05.20250423.d916277
lrwxrwxrwx 1 root root 87 Apr 27 03:22 system-14-link -> /nix/store/msn1j8971a7r30g5abhaj8m34d31y6vw-nixos-system-foo-25.05.20250423.d916277

Shouldn’t one or both commands list all the systems linked to in /nix/var/nix/profiles?

No, those are system generations, so you need to run commands as root.
And those generations are maintained with nix-env, so you need to run sudo nix-env --list-generations -p /nix/var/nix/profiles/system.
But you may prefer the more detailed output of nixos-rebuild list-generations.

2 Likes

Very cool! That does the trick. – Thank you! :1st_place_medal:

Quick follow-up question: How would I be able to identify, which generation an application in the Nix store belongs to, e.g.

nix-store -q --tree /nix/store/4ddg20bjnf9gf9pb7q92h8lhfgv528gi-ferdium-7.0.0

The hash is not one of the systems in /nix/var/nix/profiles. :confused:

sudo nix-store --query --roots <path> will print all gcroots that point at that path.

EDIT: fixed typos

1 Like

sudo nix-store --query --roots /nix/store/... works. – Thanks again! :trophy: