~/ ~/documents ~/software ~/pictures github (opens in new tab)

Take Out the Rubbish

The Nix store (/nix/store) is the heart of how Nix works. Every package, configuration, and build result is stored here, each in its own unique, cryptographically hashed directory. This means you can have multiple versions of the same package, or even conflicting dependencies, all living side by side without interfering with each other. The store is what enables Nix’s famous reproducibility and atomic upgrades/rollbacks. Nix never deletes anything unless you tell it to. Old packages, outdated configs, failed builds—they all pile up. This is great for rollbacks and reproducibility, but not always so great for your disk space.

Free disk space by running the garbage collector:

nix-collect-garbage

It’s common to make use of the --delete-old or --delete-older-than flags:

• –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-than period 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 as 30d, 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.

You don’t have to remember to run garbage collection yourself, Nix can do it for you. With the right settings, Nix will keep an eye on your disk space and take out the rubbish automatically:

# In your nix.conf or configuration.nix
gc = {
    automatic = lib.mkForce true;
    dates = lib.mkForce "weekly";
    options = lib.mkForce "--delete-older-than 7d";
};

# Minimum free space before triggering GC
min-free = builtins.toString (30 * 1024 * 1024 * 1024); # 30 GB
min-free-check-interval = lib.mkForce 1;

Leverage these settings, and your system will stay tidy with minimal effort.


See also: