Why clean up?#
A successful system build produces a system closure in /nix/store. Switching the system profile creates selectable generations. References from older generations protect their dependencies, allowing those configurations to be used later. Repeatedly switching to the same output does not necessarily produce different software versions.
- Benefit: you can select an older version from the boot menu to roll back at any time.
- Cost: disk space can fill up quickly.
Stage 1: manual cleanup#
1. Clean up old versions (generations)
# 查看目前的版本
nixos-rebuild list-generations
# 删除旧版本(保留当前版本)
sudo nix-env --delete-generations old --profile /nix/var/nix/profiles/system2. Garbage collection
Deleting old generations only removes references. To actually free disk space, run:
sudo nix-collect-garbage
# 另一个更激进的选择:会先删除找到的旧 profile 世代
# sudo nix-collect-garbage -d-d: first deletes old generations of the profiles it finds, then collects objects not referenced by any GC root. It does not simply remove software outside the current system, and it does not retain the latest two or three generations.
Stage 2: automated maintenance#
Configure automatic cleanup in configuration.nix so you no longer need to run the commands manually.
{
# 自动垃圾回收
nix.gc = {
automatic = true;
dates = "weekly";
options = "--delete-older-than 7d";
};
# 自动优化存储 (将相同的文件硬链接到一起)
nix.settings.auto-optimise-store = true;
}Stage 3: the art of rolling back#
1. Temporary rollback without rebooting
If you find a problem with the configuration you just installed, you do not need to reboot:
sudo nixos-rebuild switch --rollback2. Roll back from the boot menu
If the system cannot reach the desktop, select the previous version directly from the GRUB/systemd-boot menu.
Advice for Arch users#
Do not clear caches as frequently as you might on Arch. Nix has a distinctive storage model; retaining the last 2-3 generations can sometimes save you.
Summary#
- Generation: a system configuration closure and historical profile reference, not a snapshot of user files, databases, or an entire filesystem.
- GC: reclaims store objects not protected by GC roots. Dependencies that still have references are retained.
- Optimize: reduces the disk space used by duplicate files.
Rollback prerequisites
Deleting old generations may remove store contents needed by their boot entries. Rolling back configuration does not undo data written by applications or database migrations; plan system changes and data backups separately. switch --rollback activates the old configuration immediately and is an operation that actually applies changes.
References & further reading
Adapted from technical notes. Examples use generic names.
Continue along a reading path
Nix commands and long-term maintenance
- 01A NixOS learning roadmap: foundations, Flakes, user configuration, and beyond
- 02Nix commands explained: builds, environments, profiles, and systems
- 03nh: a unified workflow for Nix systems, users, and cleanup
- 04Garbage collection, store optimization, and rollback generations
- 05NixOS configuration organization, maintenance, and troubleshooting