A detailed guide to all core Nix package-manager commands and their arguments.
Overview of Nix commands#
The new CLI examples have been corrected against the Nix 2.34 reference manual; older versions may use different profile-selection syntax and Flake-update arguments. The system rebuild program, Home Manager, and Nix are separate tools, and their options are not interchangeable. The code below is a command reference, not a script to run in full at once.
Nix commands fall into two main categories:
- Classic commands (nix-build, nix-shell, etc.) - stable
- New commands (nix build, nix develop, etc.) - require the
nix-commandexperimental feature
New commands in the Flakes era#
nix build - build packages
# 基本用法
nix build <installable>
# 常用选项
nix build nixpkgs#hello # 从 nixpkgs 构建
nix build .#my-package # 从当前 flake 构建
nix build .#packages.x86_64-linux.hello # 完整路径
# 详细输出
nix build .#hello --print-build-logs # 显示构建日志
nix build .#hello -L # 简写
# 输出选项
nix build .#hello --out-link ./result # 指定输出链接
nix build .#hello --no-link # 不创建链接
nix build .#hello --print-out-paths # 构建并打印输出路径
# 并行构建
nix build .#hello --max-jobs 4 # 并行作业数
nix build .#hello --cores 8 # 每个作业使用的核心数
# 重构建
nix build .#hello --rebuild # 强制重新构建
nix build .#hello --repair # 修复损坏的存储nix develop - development environments
# 进入开发环境
nix develop # 进入默认 devShell
nix develop .#devShells.x86_64-linux.default # 完整路径
nix develop .#python-env # 进入特定环境
# 执行命令后退出
nix develop --command make
nix develop .#rust --command cargo build
nix develop -c rustc --version # 简写
# 配置文件
nix develop -f shell.nix # 使用传统 shell.nix
nix develop -f default.nix # 使用任意 nix 文件
# 选项
nix develop --phase configure # 进入特定阶段(构建时)
nix develop --ignore-environment # 忽略外部 PATH
nix develop --keep HOME # 保留特定变量nix run - run temporarily
# 运行包(不安装)
nix run nixpkgs#htop # 运行 htop
nix run nixpkgs#cowsay -- "文本" # 带参数
nix run .#my-script # 运行项目中的脚本
# 常用示例
nix run nixpkgs#nodejs -- --version
nix run nixpkgs#python311 -- -c "print('hi')"
nix run github:owner/repo#app # 从远程运行
# 调试
nix run .#app -L # 显示日志
nix run .#app --rebuild # 强制重建nix shell - temporary environments
# 创建临时环境
nix shell nixpkgs#git # 进入带 git 的环境
nix shell nixpkgs#{git,vim,htop} # 多个包
nix shell .#my-env # 使用 flake 定义的环境
# 执行命令
nix shell nixpkgs#nodejs nixpkgs#yarn --command npm install
nix-shell -p nodejs yarn git # 传统临时环境;新 nix shell 没有 -p
# 核心/Clang 环境
nix shell nixpkgs#stdenv.cc --command cc --version # 临时使用编译器nix flake - Flake management
# 查看信息
nix flake info # 显示 flake 元数据
nix flake metadata # 同上
nix flake show # 显示所有输出
# 编辑
nix flake edit # 打开 flake.nix
nix flake init # 初始化 flake
nix flake init -t templates#simple # 使用模板
# 锁定依赖
nix flake lock # 创建/更新 flake.lock
nix flake lock --update-input nixpkgs # 更新特定输入
nix flake lock --override-input nixpkgs github:NixOS/nixpkgs/abc123
# 更新
nix flake update # 更新所有输入
nix flake update nixpkgs # 更新特定输入
# 检查
nix flake check # 检查 flake 有效性
nix flake check --no-build # 不实际构建
# 其他
nix flake clone <flake-url> # 克隆 flake 仓库
nix flake metadata --json # JSON 输出nix profile - user-level installation (experimental)
# 安装包
nix profile install nixpkgs#hello
nix profile install .#my-app # 从 flake 安装
# 查看已安装
nix profile list
# 升级
nix profile upgrade hello # 升级特定包
nix profile upgrade --all # 升级所有未固定来源的包
# 移除
nix profile remove hello # 按名称
nix profile remove --regex '^hello.*' # 先 list 再按明确匹配移除;旧版曾使用索引
# 切换
nix profile diff-closures # 查看变更
nix profile rollback # 回滚
nix profile rollback 5 # 回滚到第5代
# 历史
nix profile history # 查看历史Classic commands#
nix-build - traditional builds
nix-build '<nixpkgs>' -A hello # 构建
nix-build '<nixpkgs>' -A hello --dry-run # 只检查
nix-build ./default.nix # 构建本地文件
# 输出选项
nix-build -A hello -o ./hello-result # 自定义输出链接
nix-build -A hello --no-out-link # 不创建链接
# 并行
nix-build -A hello --max-jobs 4
nix-build -A hello --cores 8nix-shell - traditional shells
# 进入构建环境
nix-shell -A hello '<nixpkgs>' # 包的构建环境
nix-shell '<nixpkgs>' -A hello # 同上
# 安装包到临时环境
nix-shell -p git vim htop # 临时装上这些包
nix-shell -p python311 nodejs # 多个包
# 使用文件
nix-shell shell.nix # 使用 shell.nix
nix-shell -p git --run "git --version" # 执行命令后退出
# 调试
nix-shell -A hello --pure # 纯净环境
nix-shell -A hello --keep HOME # 保留 HOMEnix-env - package management (not recommended)
# 查询
nix-env -qaA nixpkgs.hello # 查询包
nix-env -qaP --description # 所有可用包
# 安装
nix-env -iA nixpkgs.hello # 安装
nix-env -e hello # 卸载
# 升级
nix-env -u hello # 升级
nix-env -u # 升级所有
nix-env --list-generations # 列出代
nix-env --rollback # 回滚
# 查询已安装
nix-env -qP # 已安装包;-qaP 查询所有可用包
nix-env --delete-generations +10 # 删除老代nix-store - store management
# 查询
nix-store -q --references /nix/store/...hello # 依赖
nix-store -q --referrers /nix/store/...hello # 反向依赖
nix-store -q --deriver /nix/store/...hello # 派生文件
nix-store -q --graph /nix/store/...hello # 依赖图
# 操作
nix-store --gc # 垃圾回收
nix-store --gc --print-roots # 显示根
nix-store --delete /nix/store/... # 删除包
nix-store --optimise # 去重
# 其他
nix-store --verify # 验证
nix-store --repair-path /nix/store/... # 修复Advanced commands#
nix copy - copy packages
# 推送到远程缓存
nix copy --to ssh://user@host /nix/store/...hello
nix copy --to ssh-ng://user@host nixpkgs#hello
# 从远程拉取
nix copy --from file:///backup /nix/store/...hello
# SSH 方式
nix copy --to 'ssh://user@host?ssh-key=/home/user/.ssh/id_ed25519' ./result
# 压缩传输
nix copy --to 'ssh://host?compress=true' /nix/store/...hellonix derivation - derivation operations
# 显示派生信息
nix derivation show nixpkgs#hello
# 从文件
nix derivation show ./resultnix eval - evaluate expressions
# 求值 Nix 表达式
nix eval --impure --expr builtins.currentSystem
nix eval --impure --json --expr builtins.currentSystem
# 从 flake
nix eval nixpkgs#lib.version
nix eval .#nixosConfigurations.workstation.config.networking.hostName
# 求值文件
nix eval --file default.nix
# 原始输出
nix eval --impure --raw --expr builtins.currentSystemnix fmt - formatting
nix fmt # 使用 flake 定义的格式化器
nix fmt -- --check # 检查不格式化
nix fmt -- --write-mode inplace # 直接修改nix log - view logs
nix log nixpkgs#hello # 查看构建日志
nix log /nix/store/...-hello # 从路径
nix log .#my-package --refresh # 强制刷新nix path-info - path information
nix path-info nixpkgs#hello # 基本信息
nix path-info -Sh nixpkgs#hello # 大小(人类可读)
nix path-info -r nixpkgs#hello # 所有依赖nix print-dev-env - print environment variables
nix print-dev-env # 打印开发环境变量
nix print-dev-env -f shell.nix # 从文件
nix print-dev-env .#devShell # 从 flakenix repl - interactive environment
nix repl # 进入 repl
nix repl --expr 'import <nixpkgs> {}' # 加载传统搜索路径 nixpkgs
nix repl . # 加载当前 flakeInside the repl:
> pkgs = import <nixpkgs> {}
> pkgs.hello
> :q # 退出
> :l <nixpkgs> # 加载NixOS commands#
nixos-rebuild - rebuild the system
# 基本命令
sudo nixos-rebuild switch # 应用并激活
sudo nixos-rebuild build # 构建但不激活
sudo nixos-rebuild test # 实际激活,不改启动默认;并非无副作用测试
sudo nixos-rebuild dry-build # 规划所需构建,不编译、不激活
sudo nixos-rebuild dry-activate # 检查会做什么
# Flakes
sudo nixos-rebuild switch --flake /etc/nixos#workstation
sudo nixos-rebuild switch --flake .#laptop
# 历史管理
sudo nixos-rebuild list-generations # 列出历史
sudo nixos-rebuild switch --rollback # 回滚并实际激活上一世代
# 任意历史世代先用 nix-env --profile /nix/var/nix/profiles/system --list-generations 核对
# 经确认后可运行仍存在的历史系统激活脚本(下文示例中的 42 是占位世代)
sudo /nix/var/nix/profiles/system-42-link/bin/switch-to-configuration switch
# 升级
sudo nixos-rebuild switch --upgrade
sudo nixos-rebuild switch --upgrade -I nixos=https://channels.nixos.org/nixos-unstable # 传统 channel 示例
# 其他选项
sudo nixos-rebuild switch --fast # 跳过某些检查
sudo nixos-rebuild switch -p test # 使用另一个 system profile;仍实际激活,可能更新启动项
# 构建 VM
sudo nixos-rebuild build-vm --flake .#workstation
./result/bin/run-*-vmnixos-install - installation
sudo nixos-install --root /mnt
sudo nixos-install --flake .#workstation --root /mnt
# 选项
sudo nixos-install --no-channel-copy
sudo nixos-install --no-root-passwdnixos-generate-config - generate configuration
sudo nixos-generate-config --root /mnt # 安装时
sudo nixos-generate-config --show-hardware-config # 显示硬件配置
sudo nixos-generate-config --dir ./my-config # 指定目录nixos-enter - chroot
sudo nixos-enter # 进入 /mnt
sudo nixos-enter --root /mnt # 指定根
sudo nixos-enter -c "nixos-rebuild switch" # 执行命令nixos-version - version information
nixos-version # 当前版本
nixos-version --revision # git 修订版
nixos-version --help # 可用格式和选项按安装版本核对Home Manager commands#
# 激活
home-manager switch # 应用配置
home-manager switch --flake .#alice
home-manager build # 只构建
home-manager build --flake .#alice
# 历史
home-manager generations # 列出代
# 查看 generations 输出后,运行目标世代的 activate 脚本
# 回退上一世代:home-manager switch --rollback(按当前版本 help 核对)
# 任意历史世代使用其 activate;没有通用 switch --generation 参数
/nix/store/<hash>-home-manager-generation/activate
# 配置
home-manager edit # 编辑配置
home-manager news # 显示更新日志Common options and environment variables#
Global options
# 详细/安静
-v, --verbose # 详细
-vv # 更详细
--debug # 调试
--quiet # 安静
# 并行
-j, --max-jobs N # 并行作业数
--cores N # 每作业核心数
# 输出
-o, --out-link PATH # 构建命令的输出链接
--no-link # 新 nix build 不创建链接;旧 nix-build 使用 --no-out-link
--json # JSON 输出
# 网络
--offline # 离线模式
--option substitute false # 禁用二进制缓存Environment variables
# 缓存目录按 XDG 规范
export XDG_CACHE_HOME="$HOME/.cache"
# NIX_CACHE_DIR 并非这里可通用假定的受支持设置
# 构建器里的 NIX_BUILD_CORES 由 Nix --cores / cores 设置派生
# 调用客户端时使用:
# nix build .#my-package --cores 8
# 临时指定缓存(自建缓存还需正确受信任签名公钥)
export NIX_CONFIG="extra-substituters = https://cache.nixos.org https://cache.example.com"
# 不假定 NIX_SUBSTITUTERS 是受支持的配置环境变量
# 配置
export NIX_CONF_DIR=$HOME/.config/nix
export NIX_USER_CONF_FILES=$HOME/.config/nix/nix.conf
# store URI 用 CLI --store 指定,不随意重定向物理 store 目录
# nix path-info --store /nix/store nixpkgs#helloQuick reference#
Everyday development
| Operation | Command |
|---|---|
| Build a project | nix build |
| Enter a development environment | nix develop |
| Run a tool temporarily | nix run nixpkgs#tool |
| Update dependencies | nix flake update |
| View available outputs | nix flake show |
System administration
| Operation | Command |
|---|---|
| Apply configuration | sudo nixos-rebuild switch |
| Upgrade the system | sudo nixos-rebuild switch --upgrade |
| View history | sudo nixos-rebuild list-generations |
| Roll back | sudo nixos-rebuild switch --rollback |
| Collect garbage | nix-collect-garbage -d |
Package management
| Operation | Command |
|---|---|
| Search packages | nix search nixpkgs term |
| Install for the user | nix profile install nixpkgs#pkg |
| Remove | nix profile remove pkg |
| List installed packages | nix profile list |
Complete example script#
#!/usr/bin/env bash
# nix-helpers.sh
# 快速搜索并安装
nii() {
local pkg=$1
nix profile install "nixpkgs#$pkg"
}
# 临时运行
nrun() {
nix run "nixpkgs#$1" -- "${@:2}"
}
# 进入任意包的构建环境
nsh() {
nix-shell -p "$@"
}
# 清理会删旧世代,确认保留策略后再调用
nclean() {
nix-collect-garbage --delete-older-than 30d
nix-store --optimise
}
# 更新系统并清理
nupgrade() {
sudo nixos-rebuild switch --upgrade
# 确认新系统工作、旧世代保留策略无误后,单独运行 nclean
}
# 显示 flake 包输出:一次读取并保留纯 system 键
ntree() {
nix flake show --json | jq -r '
.packages | to_entries[] |
.key + ":", (.value | keys[] | " - " + .)
'
}Usage boundaries and corrections#
nix shell primarily provides tools on PATH; it is not the full build environment provided by nix develop. --print-out-paths prints output paths but still performs a build; --no-link controls whether a link is created. Arguments after nix fmt, such as --check, belong to the actual formatter, so check whether the project's formatter supports them. Older --update-input examples use a legacy compatibility interface; modern Nix supports nix flake update nixpkgs. Quote SSH store URIs containing characters such as ?. Manual nix-store --delete operations, profile deletion, system installation, and activation all change state. Confirm the target, backups, and generations to retain before running them. nixos-rebuild test actually starts and stops services; VM builds, dry-build, and flake check cannot replace validation of actual activation behavior.
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