nix 工具使用 · 选择命令接口并进入临时环境至准备 Flake、软件源与输入锁定
nix 工具使用
选择命令接口并进入临时环境
带空格的 nix shell/build/store 与带连字符的 nix-shell/nix-build/nix-store 是不同接口。先确认项目使用 Flake 还是传统 Nix 表达式,再选环境与构建命令。
nix · 一款强大的包管理器,让包管理变得可靠、可重现且可声明
nix中的某些功能(如nix-command,flake)还是实验性功能,需启用实验特性。 部分子命令如build、develop、flake、registry、profile、search、repl、store、edit、why-depends等均有其独立的用法说明。 另请参阅:nix classic。 更多信息:https://nix.dev/manual/nix/stable/command-ref/new-cli/nix。
- 新建配置以启用实验接口(下例重写 nix.conf;已有配置应合并选项,不能直接覆盖):
mkdir {{[-p|--parents]}} ~/.config/nix; echo 'experimental-features = nix-command flakes' > ~/.config/nix/nix.conf- 根据名称或描述在 nixpkgs 中搜索软件包:
nix search nixpkgs {{search_term}}- 启动一个 shell,使其可以访问 nixpkgs 中指定的软件包:
nix shell {{nixpkgs#pkg1 nixpkgs#pkg2 nixpkgs#pkg3 ...}}- 从 nixpkgs 中永久安装某些软件包:
nix profile install {{nixpkgs#pkg1 nixpkgs#pkg2 nixpkgs#pkg3 ...}}- 从 Nix 存储中移除未使用的路径以释放空间:
参见 nix store。
- 启动一个用于评估 Nix 表达式的交互式环境:
参见 nix repl。
- 显示特定子命令的帮助信息:
nix help {{subcommand}}nix classic · Nix 包管理器经典、稳定的接口,使得包管理变得可靠、可复现且可声明
部分 Nix 命令,例如
nix-build、nix-shell、nix-env和nix-store,拥有各自的页面。 另请参阅:nix。 更多信息:https://nixos.org/。
- 通过包名在 nixpkgs 中搜索包:
nix-env {{[-qaP|--query --available --attr-path]}} {{搜索词正则表达式}}- 启动一个包含指定可用的 shell:
nix-shell {{[-p|--packages]}} {{包1 包2 包3 ...}}- 永久安装某些包:
nix-env {{[-iA|--install --attr]}} {{nixpkgs.包1 nixpkgs.包2 ...}}- 以树状格式展示存储路径(包)的所有依赖:
nix-store {{[-q|--query]}} --tree /nix/store/{{校验和-包名-版本.扩展名}}- 更新频道(仓库源):
参见 nix-channel。
- 从 Nix 存储中移除未使用的路径:
nix-collect-garbagenix shell · 启动一个包含指定软件包的交互式 Shell 环境
另请参阅:
nix-shell,nix flake。 更多信息:https://manned.org/nix3-shell。
- 从
nixpkgs中选取部分软件包启动交互式 Shell:
nix shell {{nixpkgs#包1 nixpkgs#软件包组.包2 ...}}- 使用旧版
nixpkgs(21.05)中的软件包启动 Shell:
nix shell {{nixpkgs/nixos-21.05#软件包}}- 从当前目录的 flake 使用“默认软件包”启动 Shell(若发生构建则显示日志):
nix shell -L- 使用GitHub上 flake 中的软件包启动 Shell:
nix shell {{github:所有者/仓库#软件包}}- 在含指定软件包的 Shell 环境中运行命令:
nix shell {{nixpkgs#软件包}} -c {{某命令 --某参数 '其他参数内容'}}nix develop · 运行一个提供派生构建环境的 Bash 终端
更多信息:https://nix.dev/manual/nix/stable/command-ref/new-cli/nix3-develop.html。
- 启动一个包含 nixpkgs 中某个程序包所有依赖项的终端:
nix develop {{nixpkgs#程序包名}}- 在当前目录的 flake 中启动默认程序包的开发终端:
nix develop- 在该终端中配置并构建源代码:
configurePhase; buildPhasenix run · 从 Nix 软件包配置中运行一个应用程序
另请参阅:
nix flake。 更多信息:https://nix.dev/manual/nix/stable/command-ref/new-cli/nix3-run.html。
- 运行当前目录下软件包配置中的默认应用程序:
nix run- 运行名称与 nixpkgs 中软件包名称匹配的命令(如果需要该软件包中的其他命令,请参阅
tldr nix shell):
nix run nixpkgs#{{软件包}}- 使用提供的参数运行命令:
nix run nixpkgs#{{vim}} -- {{文件路径}}- 从远程代码库运行:
nix run {{远程仓库}}:{{所有者}}/{{代码库}}- 使用指定的标签、修订版本或分支从远程代码库运行:
nix run {{远程仓库}}:{{所有者}}/{{代码库}}/{{引用}}- 从远程代码库指定子目录和程序运行:
nix run "{{远程仓库}}:{{所有者}}/{{代码库}}?dir={{目录名}}#{{程序}}"- 运行 GitHub 拉取请求中的软件包配置:
nix run github:{{所有者}}/{{代码库}}/pull/{{编号}}/headnix-shell · 基于一个 Nix 表达式启动交互式 shell
另请参阅:
nix shell.3。 更多信息:https://nix.dev/manual/nix/stable/command-ref/nix-shell.html。
- 使用当前目录下的
shell.nix或default.nix文件中的 Nix 表达式启动 shell:
nix-shell- 在非交互式 shell 中运行命令并退出:
nix-shell --run "{{命令}} {{参数1 参数2 ...}}"- 使用当前目录中的
default.nix文件中的表达式启动:
nix-shell {{default.nix}}- 从 nixpkgs 加载包后启动 shell:
nix-shell {{[-p|--packages]}} {{包1 包2 ...}}- 通过指定的 nixpkgs 版本加载包后启动 shell:
nix-shell {{[-p|--packages]}} {{包1 包2 ...}} {{[-I|--include]}} nixpkgs={{https://github.com/NixOS/nixpkgs/archive/nixpkgs版本号.tar.gz}}- 在特定的解释器中执行脚本文件的其余部分,(适用于
#!-scripts,详见 https://nixos.org/manual/nix/stable/#use-as-a-interpreter):
nix-shell -i {{解释器}} {{[-p|--packages]}} {{包1 包2 ...}}接口对照: 本节是带连字符的传统 nix-shell;带空格的新接口见 nix shell。
准备 Flake、软件源与输入锁定
新建 Flake 后先看输出和元数据,再更新输入或管理注册表;传统频道单独保留。
nix flake init · 基于模板在当前目录下创建一个 flake
更多信息:https://nix.dev/manual/nix/stable/command-ref/new-cli/nix3-flake-init.html。
- 在当前目录下使用默认模板创建新的 flake:
nix flake init- 使用指定的模板创建新的 flake(关于模板信息,参见
nix flake show):
nix flake init {{[-t|--template]}} templates#{{your_template}}nix flake show · 展示由 flake 提供的输出项
更多信息:https://nix.dev/manual/nix/stable/command-ref/new-cli/nix3-flake-show.html。
- 显示当前目录下 flake 的全部输出:
nix flake show- 显示位于 GitHub 上的某个 flake 的全部输出,并以单行 JSON 格式打印:
nix flake show {{github:owner/repo}} --json --no-pretty- 显示位于 GitHub 上的某个 flake 的全部
legacyPackages输出,并以多行缩进的 JSON 格式打印:
nix flake show {{github:owner/repo}} --json --pretty --legacy- 列出可用于
nix flake init的所有 flake 模板:
nix flake show templatesnix flake info · 显示 flake 的元数据信息
更多信息:https://nix.dev/manual/nix/stable/command-ref/new-cli/nix3-flake-info。
- 显示当前目录下的 flake 元数据信息:
nix flake info- 以单行 JSON 格式显示来自 GitHub 的 flake 元数据信息:
nix flake info {{github:owner/repo}} --json --no-pretty- 以多行缩进 JSON 格式显示来自 GitHub 的 flake 元数据信息:
nix flake info {{github:owner/repo}} --json --prettynix flake · 管理 Nix Flakes
此命令也有关于其子命令的文件,例如:
init、show、info。 更多信息:https://nix.dev/manual/nix/stable/command-ref/new-cli/nix3-flake.html。
- 更新当前目录下 Flake 的所有输入(依赖项):
nix flake update- 仅更新当前目录下 Flake 的指定输入(依赖项):
nix flake update {{输入项}}- 克隆包含 Flake 的 GitHub 仓库:
nix flake clone {{github:所有者/仓库名#属性}}- 为指定目录中的 Flake 创建缺失的锁文件记录:
nix flake lock {{flake路径}}- 显示帮助:
nix flake --helpnix registry · 管理 Nix 软件源注册表
另请参阅:
nix flake。 更多信息:https://nix.dev/manual/nix/stable/command-ref/new-cli/nix3-registry.html。
- 将
nixpkgs固定到上游仓库的当前版本:
nix registry pin {{nixpkgs}}- 将一个条目固定至分支的最新版本,或 GitHub 仓库的特定修订版本:
nix registry pin {{entry}} {{github:owner/repo/branch_or_revision}}- 添加一个新的条目,使其始终指向 GitHub 仓库的最新版本,并能自动更新:
nix registry add {{entry}} {{github:owner/repo}}- 移除一个注册表条目:
nix registry remove {{entry}}- 查看关于 Nix 软件源注册表的文档说明:
nix registry --helpnix-channel · 管理 nix 更新频道
- 列出当前频道:
nix-channel --list- 添加频道:
nix-channel --add {{https://nixos.org/channels/nixpkgs-unstable}}- 更新所有频道的软件包列表:
nix-channel --update继续阅读
- 文件与文本处理完整实践与解答
- 文件与文本处理上手与对象
- 文件与文本处理任务与配置