Home-manager and garbage collection

I have the home-manager nixos module integrated into my system flake (as described in the manual.

This means that home-manager generations are created together with system generations as part of system rebuild, for all relevant users.

However, today I discovered a few hundred of these home-manager generations in my gc roots, under my and several other users’ ~/.local/state/nix/profiles directory, going back a long way. It seems these profile generations aren’t considered by the nix gc service, which I invoke with a --delete-older-than 10d parameter.

Running nix-collect-garbage --delete-older-than 10d as each user solves it, once. Apparently, this is the recommended solution too: home-manager has an equivalent nix gc setting that creates a per-user service for every user to do this.

This all seems very silly. It seems to me that:

  • Perhaps these generation links don’t need to be created in the first place, surely they can be directly linked to the system generation that created them, since they’re tied together anyway at least in this deployment mode?
  • Surely it’s a reasonable expectation that the integration that allows them to be created together with system generations can also arrange for them to be removed the same way, even if that’s not currently done due to some oversight or mistake?
  • Perhaps part of the issue is that home-manager is using the new-style XDG profile directories by default, but the nix-collect-garbage script is still invoking the older nix-store … gc commands, that have the older defaults? I will test, but I’m not sure that the gc would clean user profile generations regardless of path confusions.

Comments and more details welcome

Appears to be an intentionally introduced change in nix itself. We’re not sure why it was done, it seems like a bad idea: `nix-collect-garbage -d` does not clean up user profiles in XDG directories when run as root · Issue #8508 · NixOS/nix · GitHub


Yeah, that’s exactly what this is.

I will try and see whether setting the nix option use-xdg-base-directories helps, by making the older nix commands look there too.