nix-collect-garbage(1)
π nix-collect-garbage β from the General Commands Manual π
okay bestie, gather round, because this one is literally the cleanup queen of your whole system π§Ήβ¨
β¨ NAME β¨
nix-collect-garbage β delete unreachable store objects π
πΈ SYNOPSIS πΈ
nix-collect-garbage [--delete-old] [-d] [--delete-older-than period]
[--max-freed bytes] [--dry-run]
π¦ DESCRIPTION π¦
so basically, nix-collect-garbage is mostly just an alias of nix-store --gc. like, that's its whole personality at baseline. what it does is delete all the unreachable store objects in the Nix store, which is the vibe-coded way of saying it cleans up your system π«Ά
but β and this is the part that makes it the main character β it also gives you two extra options, --delete-old and --delete-older-than, which also delete old profiles. and that matters, because deleting profiles means potentially way more store objects can get deleted (profiles are garbage collection roots too, iykyk). these options are the equivalent of running nix-env --delete-generations with various augments on multiple profiles, before running nix-collect-garbage (or just nix-store --gc) with no flags at all.
β οΈ Note β deleting previous configurations makes rollbacks to them impossible. like. gone gone. no takebacks π
and fr fr, these flags should be used with care, because they potentially delete generations of profiles that other users on the system are using. not just yours. be a considerate bestie π
π Locations searched for profiles
nix-collect-garbage cannot know about all profiles β that information just doesn't exist. periodt. instead, it looks in a few locations, and acts on all the profiles it finds there:
- The default profile locations as specified in the profiles section of the manual.
- > β οΈ NOTE β Not stable; subject to change.
Do not rely on this functionality; it just exists for migration purposes and may change in the future. These deprecated paths remain a private implementation detail of Nix.
$NIX_STATE_DIR/profiles and $NIX_STATE_DIR/profiles/per-user.
With the exception of $NIX_STATE_DIR/profiles/per-user/root and $NIX_STATE_DIR/profiles/default, these directories are no longer used by other commands. nix-collect-garbage looks there anyways in order to clean up profiles from older versions of Nix. it's giving archaeology πΊβ¨
π OPTIONS π
these options are for deleting old profiles prior to deleting unreachable store objects, fyi.
--delete-old,-dβ Delete all old generations of profiles. ποΈ
This is the equivalent of invoking nix-env --delete-generations old on each found profile.
--delete-older-thanperiod β Delete all generations of profiles older than the specified amount (except for the generations that were active at that point in time). period is a value such as30d, which would mean 30 days. β³
This is the equivalent of invoking nix-env --delete-generations <period> on each found profile. See the documentation of that command for additional information about the period argument.
--max-freedbytes β Keep deleting paths until at least bytes bytes have been deleted, then stop. The argument bytes can be followed by the multiplicative suffixK,M,GorT, denoting KiB, MiB, GiB or TiB units. π₯
β¨ COMMON OPTIONS β¨
okay bestie, so most Nix commands take this exact little squad of flags π like, whatever Nix command you're running, these are the universal ones. here's every single one, no cap:
--helpβ prints out a summary of the command syntax and then dips. short, sweet, iconic. π--versionβ prints out the Nix version number on standard output and exits. like a lil name tag for ur install β¨--verbose/-vβ turns the volume UP on diagnostic messages printed to standard error. here's the important deets: for each Nix operation, whatever goes to standard output is well-defined, and any diagnostic info goes to standard error, never to standard output. periodt. π
you can stack this option over and over (specify it repeatedly) and it just keeps climbing. rn these are the verbosity levels that exist:
- 0 "Errors only" β only prints messages explaining why the Nix invocation failed. that's it. that's the whole vibe. π
- 1 "Informational" β prints useful messages about what Nix is doing. this is the default, bestie π«Ά
- 2 "Talkative" β prints more informational messages. she's chatty now.
- 3 "Chatty" β prints even more informational messages. omg she's still going π
- 4 "Debug" β prints debug information. nerd mode activated π₯
- 5 "Vomit" β prints vast amounts of debug information. literally the name. be warned. π
--quietβ turns the volume DOWN on diagnostic messages printed to standard error. it's the exact inverse option to-v/--verbose. π¦
you can also specify this one repeatedly, and it follows the same verbosity levels list from up top. ^
--log-formatformat β lets you change how the log output is formatted, where format is one of these: πrawβ the raw format, as outputted bynix-build.internal-jsonβ outputs the logs in a structured manner.
Warning β οΈ while the schema itself is relatively stable, the format of the error-messages (namely of the msg-field) can change between releases. so like, don't hardcode ur whole life to it, bestie.
barβ only displays a progress bar during the builds.bar-with-logsβ displays the raw logs, with the progress bar at the bottom. cute lil multitasker πΈ
--no-build-output/-Qβ by default, output written by builders to standard output and standard error gets echoed to the Nix command's standard error. this option suppresses that behaviour entirely. only note tho: the builder's standard output and error are always written to a log file inprefix/nix/var/log/nix, so nothing is actually lost π--max-jobs/-jnumber β sets the max number of build jobs Nix will run in parallel to number. you can also sayautoto use the number of CPUs in the system β¨ the default comes from themax-jobsconfiguration setting, which itself defaults to1. a higher value is useful on SMP systems or to exploit I/O latency. π₯
setting it to 0 disallows building on the local machine β useful when you want builds to happen only on remote builders. main character remote-only energy π
--coresβ sets the value of theNIX_BUILD_CORESenvironment variable in the invocation of builders. builders can use this variable at their discretion to control the max amount of parallelism. for instance, in Nixpkgs, if the derivation attributeenableParallelBuildingis set totrue, the builder passes the-jNflag to GNU Make. it defaults to the value of thecoresconfiguration setting, if set, or1otherwise. the value0means the builder should use all available CPU cores in the system π¦--max-silent-timeβ sets the max number of seconds that a builder can go without producing any data on standard output or standard error. the default is specified by themax-silent-timeconfiguration setting.0means no time-out. silence is golden but not forever bestie π--timeoutβ sets the max number of seconds that a builder can run. the default is specified by thetimeoutconfiguration setting.0means no timeout. β°--keep-going/-kβ keeps going in case of failed builds, to the greatest extent possible. that is, if building an input of some derivation fails, Nix will still build the other inputs, but not the derivation itself. without this option, Nix stops if any build fails (except for builds of substitutes), possibly killing builds in progress (in case of parallel or distributed builds). she persists π--keep-failed/-Kβ specifies that in case of a build failure, the temporary directory (usually in/tmp) where the build takes place should not be deleted. the path of the build directory gets printed as an informational message. forensics mode fr π--fallbackβ whenever Nix attempts to build a derivation for which substitutes are known for each output path, but realising the output paths through the substitutes fails, fall back on building the derivation. π«Ά
the most common scenario where this is useful: you've registered substitutes to do binary distribution from, say, a network repository, and if that repository is down, the realisation of the derivation will fail. with this option, Nix builds the derivation instead. so installation from binaries falls back on installation from source. β¨
this option is not the default, because it's generally not desirable for a transient failure in getting the substitutes to lead to a full build from source (with all the related consumption of resources). so, like, use it with intention, bestie.
--readonly-modeβ when this option is used, no attempt is made to open the Nix database. most Nix operations do need database access, so those operations will fail. it's giving "look, don't touch" π--argname value β accepted bynix-env,nix-instantiate,nix-shellandnix-build. when evaluating Nix expressions, the expression evaluator automatically tries to call functions that it encounters. it can automatically call functions for which every argument has a default value (e.g.,{ argName ? defaultValue }: ...). πΈ
with --arg, you can also call functions that have arguments without a default value (or override a default value). that is, if the evaluator encounters a function with an argument named name, it will call it with value value. π
for instance, the top-level default.nix in Nixpkgs is actually a function:
{ # The system (e.g., `i686-linux') for which to build the packages.
system ? builtins.currentSystem
...
}: ...
so if you call this Nix expression (e.g., when you do nix-env --install --attr pkgname), the function gets called automatically using the value builtins.currentSystem for the system argument. you can override this using --arg, e.g., nix-env --install --attr pkgname --arg system "i686-freebsd". (note that since the argument is a Nix string literal, you have to escape the quotes.) iykyk π
--arg-from-filename path β pass the contents of file path as the argument name to Nix functions. π--arg-from-stdinname β pass the contents of stdin as the argument name to Nix functions. pipe it in, bestie π«Ά--argstrname value β this option is like--arg, only the value is not a Nix expression but a string. so instead of--arg system "i686-linux"(the outer quotes are to keep the shell happy) you can just say--argstr system i686-linux. so much less escaping drama, obsessed β¨--attr/-AattrPath β selects an attribute from the top-level Nix expression being evaluated. (nix-env,nix-instantiate,nix-buildandnix-shellonly.) the attribute path attrPath is a sequence of attribute names separated by dots. for instance, given a top-level Nix expressione, the attribute pathxorg.xorgserverwould cause the expressione.xorg.xorgserverto be used. seenix-env --installfor some concrete examples. π₯
in addition to attribute names, you can also specify array indices. for instance, the attribute path foo.3.bar selects the bar attribute of the fourth element of the array in the foo attribute of the top-level expression. (yes bestie, counting from zero, it's giving programmer) π
--eval-storestore-url β the URL to the Nix store to use for evaluation, i.e. where to store derivations (.drvfiles) and inputs referenced by them. π--expr/-Eβ interpret the command line arguments as a list of Nix expressions to be parsed and evaluated, rather than as a list of file names of Nix expressions. (nix-instantiate,nix-buildandnix-shellonly.) π¦
for nix-shell, this option is commonly used to give you a shell in which you can build the packages returned by the expression. if you want to get a shell which contains the built packages ready for use, give your expression to the nix-shell --packages convenience flag instead. π
-I/--includepath β add an entry to the list of search paths used to resolve lookup paths. this option may be given multiple times. β¨
paths added through -I take precedence over the nix-path configuration setting and the NIX_PATH environment variable. so they win, periodt π
--impureβ allow access to mutable paths and repositories. living dangerously (but sanctioned) π--optionname value β set the Nix configuration option name to value. this overrides settings in the Nix configuration file (seenix.conf5). πΈ--repairβ fix corrupted or missing store paths by redownloading or rebuilding them. β οΈ note that this is slow because it requires computing a cryptographic hash of the contents of every path in the closure of the build. also note the warning undernix-store --repair-path. you've been warned, bestie π
Note π see man nix.conf for overriding configuration settings with command line flags.
β¨ COMMON ENVIRONMENT VARIABLES β¨
Most Nix commands are like, βbestie, I read these env varsβ, so hereβs the tea. π
πΈ Configuration environment variables πΈ
These ones affect how Nix loads its configuration. The full deets live in the configuration file documentation, but hereβs the vibe:
NIX_CONF_DIRβ overrides the system configuration directory. periodt.NIX_USER_CONF_FILESβ overrides the user configuration file locations. slay.NIX_CONFIGβ provides configuration settings inline. iconic.
ποΈ Store setting environment variables ποΈ
NIX_IGNORE_SYMLINK_STOREβ Normally, the Nix store directory (typically/nix/store) is not allowed to contain any symlink components. This is to prevent βimpureβ builds. Builders sometimes βcanonicaliseβ paths by resolving all symlink components. So builds on different machines (where/nix/storeresolves to different locations) could yield different results. This is generally not a problem, except when builds are deployed to machines where/nix/storeresolves differently. If you are sure youβre not going to do that, you can setNIX_IGNORE_SYMLINK_STOREto1.
Note that if youβre symlinking the Nix store so that you can put it on another file system than the root file system, on Linux youβre better off using bind mount points, e.g.:
$ mkdir /nix
$ mount -o bind /mnt/otherdisk/nix /nix
Consult the mount 8 manual page for details, bestie. π«Ά
NIX_STORE_DIRβ Overrides the location of the Nix store. On Unix, the default is/nix/store, set at compile time by thelibstore:store-dirbuild option. On Windows, there is no compile-time setting; the default is%PROGRAMDATA%\nix\store, determined at runtime using the%PROGRAMDATA%Known Folder.
See the Store Types chapter; each store has a store setting linking back to this as its default.
NIX_LOG_DIRβ Overrides the location of the Nix log directory. On Unix, the default is/nix/var/log/nix, set at compile time by thelog-dirbuild option. On Windows, there is no compile-time setting; the default is%PROGRAMDATA%\nix\log, determined at runtime using the%PROGRAMDATA%Known Folder.
The Local Store, Local Daemon Store, and Experimental SSH Store with filesystem mounted have per-store settings that override this.
NIX_STATE_DIRβ Overrides the location of the Nix state directory. On Unix, the default is${localstatedir}/nix, wherelocalstatediris a compile-time build option defaulting to/nix/var. On Windows, there is no compile-time setting; the default is%PROGRAMDATA%\nix\state, determined at runtime using the%PROGRAMDATA%Known Folder.
The Local Store, Local Daemon Store, and Experimental SSH Store with filesystem mounted have per-store settings that override this.
NIX_DAEMON_SOCKET_PATHβ Overrides the path to the Unix domain socket used to communicate with the Nix daemon. Defaults todaemon-socket/socketwithin the state directory (seeNIX_STATE_DIR).
See the Local Daemon Store documentation for details on how the socket path is resolved.
TMPDIRβ Use the specified directory to store temporary files. In particular, this includes temporary build directories; these can take up substantial amounts of disk space. The default is/tmp.NIX_REMOTEβ Overrides the store setting.
π§ Nix language evaluator settings π§
NIX_SHOW_STATSβ If set to1, Nix will print some evaluation statistics, such as the number of values allocated.NIX_COUNT_CALLSβ If set to1, Nix will print how often functions were called during Nix expression evaluation. This is useful for profiling your Nix expressions.GC_INITIAL_HEAP_SIZEβ If Nix has been configured to use the Boehm garbage collector, this variable sets the initial size of the heap in bytes. It defaults to384 MiB. Setting it to a low value reduces memory consumption, but will increase runtime due to the overhead of garbage collection.NIX_PATHβ A colon-separated list of search path entries used to resolve lookup paths.
This environment variable overrides the value of the nix-path configuration setting.
It can be extended using the -I option.
Example:
$ export NIX_PATH=/home/eelco/Dev:nixos-config=/etc/nixos
If NIX_PATH is set to an empty string, resolving search paths will always fail.
Example:
$ NIX_PATH= nix-instantiate --eval '<nixpkgs>'
error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I)
π User Directories π
Nix supports per-user configuration, state, and cache directories. The following environment variables override the locations for these directories:
- user configuration directory:
NIX_CONFIG_HOME - user state directory:
NIX_STATE_HOME - user cache directory:
NIX_CACHE_HOME
When these are not set, the defaults depend on the platform:
- On Unix, the XDG base directories:
$XDG_CONFIG_HOME/nix,$XDG_STATE_HOME/nix,$XDG_CACHE_HOME/nix - On Windows, the Windows Known Folders:
%APPDATA%\nix\config,%LOCALAPPDATA%\nix\state,%LOCALAPPDATA%\nix\cache
For backwards compatibility, legacy Nix commands (e.g. nix-env, nix-channel) use dotfiles in $HOME instead of these directories unless use-xdg-base-directories is enabled. New Nix commands (experimental) use the proper directories by default.
When use-xdg-base-directories is enabled, the configuration directory is resolved as:
$NIX_CONFIG_HOME, if it is defined- Otherwise, the platform default (e.g.
$XDG_CONFIG_HOME/nixon Unix)
Likewise for the state and cache directories.
π¦ Miscellaneous environment variables π¦
IN_NIX_SHELLβ Indicator that tells if the current environment was set up bynix-shell. It can have the valuespureorimpure.
π Appendix: OS-specific conventions π
This information is not Nix-specific, but is referenced above.
π Unix: XDG Base Directories π
The XDG Base Directory Specification defines standard locations for user-specific configuration, state, and cache files on Unix systems.
The following environment variables are used:
XDG_CONFIG_HOME(default~/.config)XDG_STATE_HOME(default~/.local/state)XDG_CACHE_HOME(default~/.cache)XDG_CONFIG_DIRS(default/etc/xdg) β colon-separated list of additional configuration base directories, searched afterXDG_CONFIG_HOME
πͺ Windows: Known Folders πͺ
On Windows, Known Folders provide standard locations for application data on Windows.
The relevant folders are:
%APPDATA%β per-user roaming application data%LOCALAPPDATA%β per-user local application data%PROGRAMDATA%β system-wide application data
π Example π
To delete from the Nix store everything that is not used by the current generations of each profile, do:
$ nix-collect-garbage -d
Thatβs the whole page, bestie. The footer says nix-collect-garbage(1) with its source/version and date, so consider this your cute sign-off. Stay slay. π
β¨
π¦ where this page comes from
- fedora 44: nix-legacy 2.35.3-2.fc44 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms