nix-darwin icon indicating copy to clipboard operation
nix-darwin copied to clipboard

Deleting old generations

Open happysalada opened this issue 5 years ago • 21 comments

Is there a way to manually delete old generations?

for me running nix-collect-garbage -d doesn't seem to delete old generations.

after having run it, when I run darwin-rebuild --list-generations I still get a list of all the generations.

Whereas for home-manager for example it deletes old generations.

I'm using fish shell. I'm on the master channel for nix-darwin darwin https://github.com/LnL7/nix-darwin/archive/master.tar.gz, and I have updated nix-darwin to the latest.

Let me know if you need any more informations.

happysalada avatar Oct 21 '20 23:10 happysalada

Same issue.

c4710n avatar Oct 23 '20 06:10 c4710n

I assume this is happening with an installation that uses the nix-daemon.

On a multi user installation permissions of profiles is split between root and per user profiles. In this case the root profile manages the version of nix that's installed. Only per user profiles are owned by each user respectively so packages can be installed without privileges. The nix-darwin system profile also falls under root since it takes a similar place as the initial root profile.

The darwin-rebuild command automatically uses sudo in the appropriate places when switching to a new system, but nix-collect-garbage doesn't and thus won't have permissions to touch the system generations unless executed with sudo explicitly.

LnL7 avatar Oct 24 '20 17:10 LnL7

As you assumed, I am using nix-daemon.

After running sudo nix-collect-garbage -d, the old generations is cleaned up.

c4710n avatar Oct 25 '20 00:10 c4710n

Same here. And of course, it worked with sudo. Should the documentation of this go in the wiki? I don't mind making a PR, I just don't know how to contribute to the wiki.

happysalada avatar Oct 25 '20 01:10 happysalada

The wiki on github is a separate repo linked at in the right banner https://github.com/LnL7/nix-darwin.wiki.git. I'm a bit surprised that contributing isn't exposed in the webui however.

LnL7 avatar Oct 25 '20 08:10 LnL7

Is that repo public? That repo's address yields a 404 to me. Do you think it could be a problem with the repo's name? I've always open a PR from the github UI (to contribute from a fork). I don't know of any other way to do that.

happysalada avatar Oct 25 '20 09:10 happysalada

It should be, but that's git only. Seems there's no way to contribute to github wikis without access to the repo which is disappointing.

Given this I would propose you make an issue (or comment here) with the content you would like to add.

LnL7 avatar Oct 25 '20 11:10 LnL7

Actually I just found an option to limit access to collaborators, perhaps it's accessible now?

LnL7 avatar Oct 25 '20 11:10 LnL7

Perhaps I'm not a collaborator, because I still get a 404.

I quite like what you wrote actually, I think it's a perfect explanation. So as a contribution, I would add a page named Deleting old generations with the following.

Deleting old generations

TLDR: sudo nix-collect-garbage -d

On a multi user installation permissions of profiles is split between root and per user profiles. In this case the root profile manages the version of nix that's installed. Only per user profiles are owned by each user respectively so packages can be installed without privileges. The nix-darwin system profile also falls under root since it takes a similar place as the initial root profile.

The darwin-rebuild command automatically uses sudo in the appropriate places when switching to a new system, but nix-collect-garbage doesn't and thus won't have permissions to touch the system generations unless executed with sudo explicitly.

happysalada avatar Oct 26 '20 01:10 happysalada

@happysalada I have permission for that, so I added it. https://github.com/LnL7/nix-darwin/wiki/Deleting-old-generations Feel free to edit if you want.

For the pureness of wiki, I will not mention you at wiki. Thanks for your summary. ;)

c4710n avatar Oct 26 '20 03:10 c4710n

@c4710n awesome thanks! No need for the mention.

happysalada avatar Oct 26 '20 03:10 happysalada

This appears to have changed behavior recently when running sudo nix-collect-garbage -d the following warning now appears

warning: $HOME ('/Users/raphael') is not owned by you, falling back to the one defined in the 'passwd' file ('/var/root')

and it doesn't delete old darwin generations anymore. (without sudo doesn't work either).

happysalada avatar Apr 28 '23 04:04 happysalada

for anyone else ending up here, here is what worked for me. you have to use the nix-collect-garbage produced by nix-darwin, so this will be

sudo ./result/sw/bin/nix-collect-garbage -d

happysalada avatar Apr 28 '23 04:04 happysalada

Hitting the same thing as https://github.com/LnL7/nix-darwin/issues/237#issuecomment-1526952371

Can a maintainer please reopen this until nix-collect-garbage -d is fixed to once again delete old generations?

jab avatar Feb 10 '24 15:02 jab

for anyone else ending up here, here is what worked for me. you have to use the nix-collect-garbage produced by nix-darwin

This didn't work for me:

nix-darwin on  master
❯ ./result-2/sw/bin/nix-collect-garbage -d
removing old generations of profile /Users/jab/.local/state/nix/profiles/channels
removing old generations of profile /nix/var/nix/profiles/per-user/jab/profile
removing old generations of profile /nix/var/nix/profiles/per-user/jab/channels
removing old generations of profile /nix/var/nix/profiles/per-user/jab/profile
removing old generations of profile /nix/var/nix/profiles/per-user/jab/channels
finding garbage collector roots...
deleting garbage...
deleting '/nix/store/l2hbrnxdskw4zafnsdyk2b18nbdikdp1-stdenv-darwin'
...
deleting unused links...
note: currently hard linking saves 6215.80 MiB
17 store paths deleted, 8.93 MiB freed

nix-darwin on  master took 5s
❯ darwin-rebuild --list-generations | wc -l   # did not remove old generations
      59

jab avatar Mar 17 '24 13:03 jab

Using nix-darwin via a flake, the following worked for me:

  1. without sudo, nix-collect-garbage -d; this seemed to remove old profiles' garbage collector roots
  2. sudo ./result/sw/bin/nix-collect-garbage -d was then able to clean things up

StephenWithPH avatar Apr 02 '24 13:04 StephenWithPH

Thanks @StephenWithPH! That worked for me too. Great to have a workaround while a fix for this is still pending.

jab avatar Apr 02 '24 15:04 jab

@StephenWithPH 's solution is working great - is there a way to tell nix-collect-garbage to only consider up to a certain generation in this case? I'd ideally like to keep the last one or two generations around in case a new generation is broken.

remi-gelinas avatar Jun 19 '24 17:06 remi-gelinas

You can use nix-collect-garbage --delete-older-than 30d to only delete configurations older than 30 days

Samasaur1 avatar Jun 22 '24 18:06 Samasaur1

I've tried sudo nix-collect-garbage -d and still had nix store paths I can't remove cause they're in old home manager generations. I used home-manager expire-generations "-1 days" to get rid of them. Then the garbage collect went to town on them.

arichtman avatar Jun 27 '24 10:06 arichtman

Ah yeah, if you're using standalone home-manager then you'll need to use the home-manager command (I'm using the nix-darwin module)

Samasaur1 avatar Jun 27 '24 16:06 Samasaur1