Nix-env --delete-generations not deleting anything

I’m trying to delete several specific generations I no longer need, but nix-env --delete-generations followed by nix-store --gc is having no effect. Can anyone see if I’m doing something wrong?

I try it first as a normal user, then a second time using sudo. No effect either way.

> sudo sh scripts/nix-env-list-generations.sh 
[sudo] password for bgibson: 
  98   2021-11-22 05:14:49   
  99   2021-11-23 19:04:50   
 100   2021-11-23 19:37:55   
 101   2021-11-24 00:16:13   
 102   2021-11-24 12:28:22   
 103   2021-11-26 04:41:53   
 104   2021-11-27 04:41:41   
 105   2021-11-27 14:17:01   
 106   2021-11-28 04:41:28   
 107   2021-11-30 04:44:46   (current)
 108   2021-12-01 17:09:42   

> nix-env --delete-generations 102 103 104 105 106

> sudo sh scripts/nix-env-list-generations.sh
  98   2021-11-22 05:14:49   
  99   2021-11-23 19:04:50   
 100   2021-11-23 19:37:55   
 101   2021-11-24 00:16:13   
 102   2021-11-24 12:28:22   
 103   2021-11-26 04:41:53   
 104   2021-11-27 04:41:41   
 105   2021-11-27 14:17:01   
 106   2021-11-28 04:41:28   
 107   2021-11-30 04:44:46   (current)
 108   2021-12-01 17:09:42   

> nix-store --gc
finding garbage collector roots...
removing stale link from '/nix/var/nix/gcroots/auto/igcwj2l6m1iyirmbaip7mrgz6zw9ky8b' to '/tmp/nix-build-1878712-0/result'
removing stale link from '/nix/var/nix/gcroots/auto/19jq2b5xgd8188wz1h73z895zm4kws7d' to '/tmp/nix-build-2752175-0/result'
removing stale link from '/nix/var/nix/gcroots/auto/5m0spkff3vn5qkn56ginv6pmdfzd9jdq' to '/tmp/nixos-rebuild.BkL0t6/nix.drv'
removing stale link from '/nix/var/nix/gcroots/auto/n3p4cvh8hpgmg7kmical4nm73hn03pav' to '/tmp/nixos-rebuild.BkL0t6/nix'
deleting garbage...
deleting '/nix/store/xv6lsgwbyphz863lrnkbnxrkk9jf5knv-themechanger-0.10.2.drv'
deleting '/nix/store/x86r6y0nwhrmaf75vbvwy6s65hnyfinr-source.drv'
deleting '/nix/store/sl1vvcyay6haz998d3vghr7wpk0fb389-qtstyleplugin-kvantum-0.20.2.drv'
deleting '/nix/store/cklkxadyj5rdyc1s405n385yjkfjw1cd-source.drv'
deleting '/nix/store/0kjcrmvcjvbw103iwbf1xsrqzy9xfh32-nixos-21.11'
deleting '/nix/store/y2fcdnys7js6dz0vxx11sq928h9gnbkm-latte-dock-0.10.0.drv'
deleting '/nix/store/lh25nd3lsg04cdvz2idprarl9jgwr6lp-0001-close-user-autostart.patch'
deleting '/nix/store/7yfvzdby4m01qinxxcahx3621ifv58m6-latte-dock-0.10.0.tar.xz.drv'
deleting '/nix/store/ik5bxl5iqm7kn1kg28267nz2gzf1rw9j-nixexprs.tar.xz'
deleting '/nix/store/trash'
deleting unused links...
note: currently hard linking saves 17596.71 MiB
9 store paths deleted, 0.02 MiB freed

> sudo sh scripts/nix-env-list-generations.sh
  98   2021-11-22 05:14:49   
  99   2021-11-23 19:04:50   
 100   2021-11-23 19:37:55   
 101   2021-11-24 00:16:13   
 102   2021-11-24 12:28:22   
 103   2021-11-26 04:41:53   
 104   2021-11-27 04:41:41   
 105   2021-11-27 14:17:01   
 106   2021-11-28 04:41:28   
 107   2021-11-30 04:44:46   (current)
 108   2021-12-01 17:09:42   
 
[hmm, didn't work, let's try again with sudo...]

> sudo nix-env --delete-generations 102 103 104 105 106                                         

> sudo sh scripts/nix-env-list-generations.sh
  98   2021-11-22 05:14:49   
  99   2021-11-23 19:04:50   
 100   2021-11-23 19:37:55   
 101   2021-11-24 00:16:13   
 102   2021-11-24 12:28:22   
 103   2021-11-26 04:41:53   
 104   2021-11-27 04:41:41   
 105   2021-11-27 14:17:01   
 106   2021-11-28 04:41:28   
 107   2021-11-30 04:44:46   (current)
 108   2021-12-01 17:09:42   

> sudo nix-store --gc                                                                           
finding garbage collector roots...
deleting garbage...
deleting '/nix/store/trash'
deleting unused links...
note: currently hard linking saves 17596.71 MiB
0 store paths deleted, 0.00 MiB freed

> sudo sh scripts/nix-env-list-generations.sh
  98   2021-11-22 05:14:49   
  99   2021-11-23 19:04:50   
 100   2021-11-23 19:37:55   
 101   2021-11-24 00:16:13   
 102   2021-11-24 12:28:22   
 103   2021-11-26 04:41:53   
 104   2021-11-27 04:41:41   
 105   2021-11-27 14:17:01   
 106   2021-11-28 04:41:28   
 107   2021-11-30 04:44:46   (current)
 108   2021-12-01 17:09:42   

[hmm, what now?]

if those are generations of the system profile (ie, generated by nixos-rebuild) you’ll have to tell nix-env to work on the system profile rather than the profile of the executing user: nix-env -p /nix/var/nix/profiles/system. nix-env does not report errors when asked to delete generation numbers that don’t currently exist, so if this was indeed such a mixup the commands wouldn’t have told you.

3 Likes