详细介绍 Nix 包管理器的所有核心命令及其参数。
Nix 命令概述#
新 CLI 示例按 Nix 2.34 参考手册校正;旧版本可能使用不同的 profile 选择语法和 Flake 更新参数。系统重建程序、Home Manager 与 Nix 是不同工具,选项不可互换。以下代码是命令参考,不是一次要全部执行的脚本。
Nix 命令分为两大类:
- 经典命令(nix-build, nix-shell 等)- 稳定
- 新命令(nix build, nix develop 等)- 需要
nix-command实验性特性
新命令(Flakes 时代)#
nix build - 构建包
# 基本用法
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 - 开发环境
# 进入开发环境
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 - 临时运行
# 运行包(不安装)
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 - 临时环境
# 创建临时环境
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 管理
# 查看信息
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 - 用户级安装(实验性)
# 安装包
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 # 查看历史经典命令#
nix-build - 传统构建
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 - 传统 shell
# 进入构建环境
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 - 包管理(不推荐)
# 查询
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 - 存储管理
# 查询
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/... # 修复高级命令#
nix copy - 复制包
# 推送到远程缓存
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 - 派生操作
# 显示派生信息
nix derivation show nixpkgs#hello
# 从文件
nix derivation show ./resultnix eval - 表达式求值
# 求值 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 - 格式化
nix fmt # 使用 flake 定义的格式化器
nix fmt -- --check # 检查不格式化
nix fmt -- --write-mode inplace # 直接修改nix log - 查看日志
nix log nixpkgs#hello # 查看构建日志
nix log /nix/store/...-hello # 从路径
nix log .#my-package --refresh # 强制刷新nix path-info - 路径信息
nix path-info nixpkgs#hello # 基本信息
nix path-info -Sh nixpkgs#hello # 大小(人类可读)
nix path-info -r nixpkgs#hello # 所有依赖nix print-dev-env - 打印环境变量
nix print-dev-env # 打印开发环境变量
nix print-dev-env -f shell.nix # 从文件
nix print-dev-env .#devShell # 从 flakenix repl - 交互式环境
nix repl # 进入 repl
nix repl --expr 'import <nixpkgs> {}' # 加载传统搜索路径 nixpkgs
nix repl . # 加载当前 flake在 repl 中:
> pkgs = import <nixpkgs> {}
> pkgs.hello
> :q # 退出
> :l <nixpkgs> # 加载NixOS 相关命令#
nixos-rebuild - 系统重建
# 基本命令
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 - 安装
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 - 生成配置
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 - 版本信息
nixos-version # 当前版本
nixos-version --revision # git 修订版
nixos-version --help # 可用格式和选项按安装版本核对Home Manager 命令#
# 激活
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 # 显示更新日志常用选项和环境变量#
全局选项
# 详细/安静
-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 # 禁用二进制缓存环境变量
# 缓存目录按 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#hello快速参考卡#
日常开发
| 操作 | 命令 |
|---|---|
| 构建项目 | nix build |
| 进入开发环境 | nix develop |
| 临时运行工具 | nix run nixpkgs#tool |
| 更新依赖 | nix flake update |
| 查看可用输出 | nix flake show |
系统管理
| 操作 | 命令 |
|---|---|
| 应用配置 | sudo nixos-rebuild switch |
| 升级系统 | sudo nixos-rebuild switch --upgrade |
| 查看历史 | sudo nixos-rebuild list-generations |
| 回滚 | sudo nixos-rebuild switch --rollback |
| 清理垃圾 | nix-collect-garbage -d |
包管理
| 操作 | 命令 |
|---|---|
| 搜索包 | nix search nixpkgs term |
| 安装到用户 | nix profile install nixpkgs#pkg |
| 移除 | nix profile remove pkg |
| 列出已安装 | nix profile list |
完整示例脚本#
#!/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[] | " - " + .)
'
}使用边界与校正说明#
nix shell 主要提供 PATH 中的工具,不等于 nix develop 的完整构建环境。--print-out-paths 输出路径但仍会构建;--no-link 控制链接是否创建。nix fmt 后的 --check 等参数属于实际 formatter,是否支持需检查项目 formatter。旧 --update-input 示例是历史兼容接口,现代 Nix 可用 nix flake update nixpkgs。SSH store URI 含 ? 等字符时应加引号。手工 nix-store --delete、profile 删除、系统安装和激活均会改变状态;确认目标、备份与保留世代后才执行。nixos-rebuild test 实际启停服务,VM 构建、dry-build 和 flake check 都不能替代实际应用行为的验证。
参考与延伸
根据技术笔记重新整理,示例使用通用名称。