# Home-manager and garbage collection

**URL:** https://discourse.nixos.org/t/home-manager-and-garbage-collection/41715
**Category:** Help
**Created:** [March 18, 2024, 4:56am UTC](https://discourse.nixos.org/t/home-manager-and-garbage-collection/41715 "2024-03-18T04:56:03Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![uep](https://discourse.nixos.org/user_avatar/discourse.nixos.org/uep/32/5167_2.png) [@uep](https://discourse.nixos.org/u/uep)
#### Post date: [March 18, 2024, 4:56am UTC](https://discourse.nixos.org/t/home-manager-and-garbage-collection/41715/1 "2024-03-18T04:56:04Z")

</div>

I have the home-manager nixos module integrated into my system flake (as described in [the manual](https://nix-community.github.io/home-manager/index.xhtml#sec-flakes-nixos-module).

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

---

<div class="post-metadata">

### Author: ![jade](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/j/a8b319/32.png) [@jade](https://discourse.nixos.org/u/jade)
#### Post date: [March 18, 2024, 5:00am UTC](https://discourse.nixos.org/t/home-manager-and-garbage-collection/41715/2 "2024-03-18T05:00:31Z")

</div>

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](https://github.com/NixOS/nix/issues/8508)

---

<div class="post-metadata">

### Author: ![uep](https://discourse.nixos.org/user_avatar/discourse.nixos.org/uep/32/5167_2.png) [@uep](https://discourse.nixos.org/u/uep)
#### Post date: [March 18, 2024, 5:05am UTC](https://discourse.nixos.org/t/home-manager-and-garbage-collection/41715/3 "2024-03-18T05:05:50Z")

</div>

Yeah, that’s exactly what this is.

I will try and see whether setting the nix option [`use-xdg-base-directories`](https://nixos.org/manual/nix/stable/command-ref/conf-file#conf-use-xdg-base-directories) helps, by making the older nix commands look there too.

---

<div class="post-metadata">

### Author: ![SpiderUnderUrBed](https://discourse.nixos.org/user_avatar/discourse.nixos.org/spiderunderurbed/32/36822_2.png) [@SpiderUnderUrBed](https://discourse.nixos.org/u/SpiderUnderUrBed)
#### Post date: [March 28, 2026, 10:08pm UTC](https://discourse.nixos.org/t/home-manager-and-garbage-collection/41715/4 "2026-03-28T22:08:37Z")

</div>

Is there still no dedicated command for cleaning up prior home-manager profiles? with gc?

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [March 28, 2026, 10:14pm UTC](https://discourse.nixos.org/t/home-manager-and-garbage-collection/41715/5 "2026-03-28T22:14:25Z")

</div>

`sudo nixos-collect-garbage`. but you are not allowed to update to nix 2.14 or newer _ever_.

Alternatively loop over all users and run the command _as them_.

---

<div class="post-metadata">

### Author: ![drupol](https://discourse.nixos.org/user_avatar/discourse.nixos.org/drupol/32/9471_2.png) [@drupol](https://discourse.nixos.org/u/drupol)
#### Post date: [March 28, 2026, 10:23pm UTC](https://discourse.nixos.org/t/home-manager-and-garbage-collection/41715/6 "2026-03-28T22:23:12Z")

</div>

nh does it pretty well IIRC: [GitHub - nix-community/nh: Yet another Nix CLI helper. [Maintainers=@NotAShelf, @faukah] · GitHub](https://github.com/nix-community/nh)
