LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

nh-clean

removes old Nix profile generations and runs garbage collection

TLDR

Clean all profiles and garbage-collect the store
$ nh clean all
copy
Clean only the current user's profiles
$ nh clean user
copy
Keep at least the last 5 generations and anything from the past week
$ nh clean all --keep [5] --keep-since [7d]
copy
Preview what would be removed
$ nh clean all --dry
copy
Ask for confirmation before deleting
$ nh clean all --ask
copy
Clean a specific profile
$ nh clean profile [/nix/var/nix/profiles/system]
copy
Remove generations but skip garbage collection
$ nh clean user --no-gc
copy
Run store optimisation after garbage collection
$ nh clean all --optimise
copy

SYNOPSIS

nh clean all|user [options]nh clean profile [options] profile

DESCRIPTION

nh clean is a more thorough replacement for nix-collect-garbage. It deletes old generations of Nix profiles according to count and age rules, removes orphaned gcroots and aged-out direnv gcroots, then runs nix store gc to free space in the Nix store.Unlike nix-collect-garbage, it can keep a minimum number of generations and a time window at the same time, and shows a plan of what it will delete before acting.

PARAMETERS

all

Clean all profiles: system profiles, per-user profiles and every user's XDG profiles. Usually needs root, which nh obtains itself.
user
Clean the current user's profiles (~/.local/state/nix/profiles and /nix/var/nix/profiles/per-user/user). Refuses to run as root.
profile path
Clean one specific profile. Gcroots are not touched in this mode.
-k, --keep n
Keep at least this number of generations. Default: 1.
-K, --keep-since duration
Keep gcroots and generations newer than this duration (humantime format, e.g. 3d, 2weeks, 12h). Default: 0h.
-n, --dry
Only print what would be done, without doing it.
-a, --ask
Ask for confirmation before proceeding.
--no-gc
Don't run nix store gc after removing generations.
--no-gcroots
Don't clean gcroots.
--no-direnv
Don't clean gcroots created by direnv / nix-direnv.
--keep-one
Keep at least one gcroot per direnv project.
--optimise
Run nix-store --optimise after garbage collection.
--max size
Pass --max to nix store gc, stopping after freeing this many bytes.
-x, --cross-filesystems
Cross filesystem boundaries when scanning for gcroots.

INSTALL

nix profile install nixpkgs#nh
copy

CAVEATS

--keep takes a number of generations, not a duration; use --keep-since for time-based retention. Both rules are applied together: a generation is kept if either rule keeps it. The currently active generation is never removed.On NixOS, programs.nh.clean.enable can run nh clean periodically; do not enable it together with nix.gc.automatic.

SEE ALSO

RESOURCES

Copied to clipboard
Kai