For single-user installation
Run nix-collect-garbage -d
as normal.
For multi-user installation
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.