Background: Historical baggage has left Nix with two command-line interfaces:
nix-xxx, the older standalone commands, andnix xxx, the newer unified subcommands. Enablingnix-commandandflakesmakes the new CLI available, but the old commands are still used for older profiles, channels, and specific administrative operations. Parts of the new CLI remain experimental; consult the help for your installed version.
1. The everyday survival kit: just remember these 4#
For everyday use, start by understanding these four types of operation and their effects:
| Scenario | Command | Explanation |
|---|---|---|
| Apply configuration changes | sudo nixos-rebuild switch |
Run after editing configuration.nix or flake.nix. With Flakes, this is usually sudo nixos-rebuild switch --flake .#主机名 |
| Update all system software | sudo nixos-rebuild switch --upgrade |
For a channels-based configuration, update the channel, then build and activate; for a Flake workflow, update the lock first. |
| Undo a broken system change | sudo nixos-rebuild switch --rollback |
Roll back immediately to the previous generation. A reboot is not required, but is recommended to verify the result. |
| Clean up when the disk is full | sudo nix-collect-garbage -d |
Delete all old generations and reclaim space in /nix/store. Afterward, you cannot roll back to the deleted generations. |
2. Old and new CLI counterparts#
After enabling experimental-features = [ "nix-command" "flakes" ], the new subcommands provide corresponding functionality. Profile formats and dependency sources differ, however, so the commands in this table are not unconditional drop-in replacements.
| Old CLI | New CLI | Purpose |
|---|---|---|
nix-env -iA nixpkgs.hello |
nix profile add nixpkgs#hello |
Install user-level software |
nix-env -e hello |
nix profile remove hello |
Remove user-level software |
nix-env --list-generations |
nix profile history |
View generation history |
nix-build |
nix build |
Build a derivation |
nix-shell -p python3 |
nix shell nixpkgs#python3 |
Enter a temporary dependency environment |
nix-channel --update |
nix flake update |
Update a channel or a Flake's locked inputs, respectively (these are different data sources) |
nix-instantiate --eval |
nix eval |
Evaluate a Nix expression |
** Note the exceptions**:
nixos-rebuild(system management) andnix-collect-garbage(garbage collection) handle system rebuilding and profile-history cleanup combined with GC.nix-collect-garbagealso works on non-NixOS systems, whilenix store gchandles only unreferenced store objects.
3. Common new commands for development and testing#
- Run a program temporarily, without installing it:
nix run nixpkgs#cowsay -- "Hello NixOS" - Enter a temporary shell environment:
nix shell nixpkgs#python3 nixpkgs#postgresql - Enter the project's development environment (requires a
flake.nixin the project root withdevShellsconfigured):nix develop
4. Advanced generation management#
A generation is like a game's "save point." switch/boot operations that update the system profile can record a new save point; build/dry-build alone do not. Old generations are not deleted automatically.
| Operation | Command |
|---|---|
| List all system generations | sudo nixos-rebuild list-generations |
| Switch to a specific generation (for example, number 50) | sudo nix-env --profile /nix/var/nix/profiles/system --switch-generation 50, then run that profile's bin/switch-to-configuration switch to activate it |
| Delete a specific generation | sudo nix-env --profile /nix/var/nix/profiles/system --delete-generations 50 51 |
| Keep only the latest 5 generations | sudo nix-env --profile /nix/var/nix/profiles/system --delete-generations +5 |
| Keep generations from the past 30 days | sudo nix-collect-garbage --delete-older-than 30d (recommended; safer than -d) |
| Roll back through GRUB at boot | After rebooting, select "NixOS - All configurations" in the GRUB menu |
Store GC and deleting profile history
The new nix CLI includes nix store gc to clean the underlying store directory, but it does not automatically delete old generations for you. If you use only nix store gc, the old generations remain, so their packages are not considered "unreferenced" and cannot be removed.nix-collect-garbage collects only unreachable objects by default. Adding -d or --delete-older-than first deletes old history from the profiles it finds. Running it as root may affect multiple users' profiles; you must understand the scope of its scan.
History interfaces for the system profile
The new nix profile provides interfaces for the new profile workflow. When handling NixOS system-level configurations at /nix/var/nix/profiles/system, the old and new commands differ as follows:
- If you want to clean up with the new commands, this is an alternative:
You can usenix profile wipe-historyin place ofnix-env.
# 新命令:删除 30 天前的非当前历史(先确认本机版本支持此 profile)
sudo nix profile wipe-history --profile /nix/var/nix/profiles/system --older-than 30d- Why keep the traditional history interfaces and GC tools:
Retaining generations by age or by count is a maintenance-policy choice.nix-collect-garbage --delete-older-than 30dcombines "delete generations" and "reclaim space" in one operation. With the new commands, these are two separate steps: first delete generations withnix profile wipe-history, then reclaim space withnix store gc. This makes it easier to control history-deletion scope and space reclamation separately.
5. Declarative automatic maintenance: put it in your configuration#
To avoid running cleanup commands manually, enable automatic maintenance directly in configuration.nix:
{ config, pkgs, ... }:
{
# 1. 限制 GRUB 启动菜单中保留的世代数量(避免启动列表过长)
boot.loader.grub.configurationLimit = 20;
# 2. 开启新版命令与 Flakes
nix.settings.experimental-features = [ "nix-command" "flakes" ];
# 3. 自动垃圾回收(每周清理 30 天前的世代)
nix.gc = {
automatic = true;
dates = "weekly";
options = "--delete-older-than 30d";
};
# 4. 自动优化存储(硬链接去重,节省空间)
nix.settings.auto-optimise-store = true;
# 5. (可选) 构建期间尝试 GC 的可用空间阈值,不是容量硬限制
nix.extraOptions = ''
min-free = ${toString (5 * 1024 * 1024 * 1024)}
max-free = ${toString (10 * 1024 * 1024 * 1024)}
'';
}6. Remember commands by responsibility#
- Everyday system management:
nixos-rebuild. Temporary programs and development environments:nix run,nix shell, andnix develop. - Profile, channel, and older-expression maintenance:
nix-env,nix-build,nix-shell, andnix-channelstill have uses, depending on your workflow. - Installation and recovery:
nixos-install,nixos-enter, andnixos-generate-configmay still be useful for recovery after installation.
Configuration files are a good fit for stable system and user environments; imperative environments are useful for temporary exploration. Garbage collection does not automatically remove all installed software, nor does it preserve rollback points that have already been removed.
The command table uses the current Nix 2.34 interface. nix profile install is a compatibility alias for add; older versions may have only the old name. A package output being in the store does not mean it is permanently installed. Outputs may remain in the store after a temporary environment ends and later be reclaimed by GC.
References & further reading
- NixOS manual ↗
- Nix language manual ↗
- Nix: garbage collection and profile scope ↗
- Nix: wipe-history ↗
- Nix: profile add ↗
Adapted from technical notes. Examples use generic names.
Continue along a reading path
NixOS system foundations and configuration
- 01NixOS: the foundations of a declarative system
- 02Installing NixOS: installation media, partitions, and initial configuration
- 03NixOS configuration basics: options and modules
- 04Managing a system with Flakes and Home Manager
- 05The NixOS command line: old and new interfaces and everyday maintenance
- 06NixOS generation management and garbage collection
- 07Specialisation: multiple configuration variants in one generation
- 08If configuration is reproducible, why do apps still forget?