- 开发工具
- CLI
【免费下载链接】nix
Nix, the purely functional package manager
本文围绕 Nix 2.15(2023-04-11 发布)的官方发布说明展开,逐条解读该版本在 installables 输入方式、哈希格式、store path 语义、store 信任机制与 derivation 子命令等方面的核心变更,并结合当前仓库源码(
src/nix、src/libcmd、src/libstore等)给出实现层面的佐证与实战示例,帮助你理解变更动机、新语法用法以及升级迁移时的注意点。
一、--stdin:installables 从标准输入读取
变更内容:所有接受 installables 作为命令行参数的 Nix 命令,现在都可以通过--stdin标志从标准输入读取 installables。这在拥有大量 store path(超过操作系统参数长度上限 ARG_MAX)时尤其有用。
从源码看,--stdin标志定义于 src/libcmd/installables.cc 的RawInstallablesCommand构造函数中,其处理逻辑为:
if (readFromStdIn && !isatty(STDIN_FILENO)) { std::string word; while (std::cin >> word) { rawInstallables.emplace_back(std::move(word)); } } else { applyDefaultInstallables(rawInstallables); }即当--stdin被传入且标准输入不是 TTY 时,按空白字符切分 stdin 内容作为 installables;否则回退到默认行为(默认 installable 为.)。
实战示例:假设你有一个包含大量 store path 的文件paths.txt,每行一个路径,则可以用管道方式调用:
$ cat paths.txt | nix path-info --stdin或者:
$ nix path-info --stdin < paths.txt这避免了把所有路径一次性拼在命令行上导致的 "Argument list too long" 错误。
二、nix-hash的 Base64 与 SRI 格式支持
变更内容:nix-hash命令现在支持 Base64 和 SRI(Subresource Integrity)两种输出格式:
- 使用
--base64或--sri指定输出哈希的格式; - 使用
--to-base64或--to-sri将哈希转换为 Base64 或 SRI 格式; - 由于哈希格式的选择不再是二元的(binary),还新增了
--base16标志用于显式指定 Base16 格式(Base16 仍是默认格式)。
从源码看,在 src/nix/hash.cc 的CmdHashBase构造函数中注册了--sri、--base64、--base32、--base16四个标志,分别对应HashFormat::SRI、HashFormat::Base64、HashFormat::Nix32、HashFormat::Base16:
addFlag({ .longName = "sri", .description = "Print the hash in SRI format.", .handler = {&hashFormat, HashFormat::SRI}, }); addFlag({ .longName = "base64", .description = "Print the hash in base-64 format.", .handler = {&hashFormat, HashFormat::Base64}, }); addFlag({ .longName = "base16", .description = "Print the hash in base-16 format.", .handler = {&hashFormat, HashFormat::Base16}, });输出的默认哈希格式在CmdHashBase中声明为HashFormat::SRI(见 src/nix/hash.cc),而旧的nix-hash兼容实现(compatNixHash,见 src/nix/hash.cc)则默认使用HashFormat::Base16,并且同样支持--base64、--sri、--to-base64、--to-sri、--truncate等参数。
实战示例:
$ nix-hash --base64 /nix/store/xxx-foo $ nix-hash --sri /nix/store/xxx-foo $ nix-hash --to-base64 <hash> $ nix-hash --to-sri <hash> $ nix-hash --base16 /nix/store/xxx-fooSRI 格式形如sha256-<base64>,即算法名加短横线加 Base64 编码,便于在 Nix 表达式(如fetchurl的hash属性)中直接使用。
三、.drv后缀特殊语义移除与^语法
变更内容:Nix 2.15 移除了对.drv后缀 installable 的特殊处理——之前.drv后缀会被解释为"该 store derivation 的所有输出路径",现在则被当作它字面表示的 store path 本身。
原因是 Nix 2.13 引入的^语法已经可以显式引用 derivation 的输出路径,这比依赖被移除的.drv特殊处理更清晰。
示例对比:
# 现在:给出 derivation 本身的信息 $ nix path-info /nix/store/fpq78s2h8ffh66v2iy0q1838mhff06y8-glibc-2.33-78.drv # 现在:给出该 derivation 每个输出的信息(使用 ^ 语法) $ nix path-info /nix/store/fpq78s2h8ffh66v2iy0q1838mhff06y8-glibc-2.33-78.drv^*其中^*表示该 derivation 的所有输出。这条变更意味着如果你之前依赖.drv后缀自动展开为所有输出路径的行为,升级到 2.15 后需要改用^语法(具体输出名用^out、^dev等,所有输出用^*)。
四、nix describe-stores移除,store 文档迁移至nix help-stores
变更内容:实验性命令nix describe-stores已被移除。Nix store 及其设置现在统一在nix help-stores中说明。
对应的文档源文件为 src/nix/help-stores.md,它涵盖了不同 store 类型(local store、daemon store、SSH store、HTTP binary cache 等)以及store URL 格式:
- Store 通过 URL 式语法指定,例如:
# nix path-info --store https://cache.nixos.org/ --json \ /nix/store/1542dip9i7k4f24y6hqgd04hmvid9hr5-coreutils-9.1- Store URL 可以用查询字符串指定 store 设置,例如:
--store ssh://machine.example.org?ssh-key=/path/to/my/key- 特殊 URL
auto会自动选择 store:优先本地/nix/store;若/nix/var/nix不可写则尝试连接 daemon socket;Linux 下还会考虑 local chroot store~/.local/share/nix/root。
实战命令:
$ nix help-stores该命令会输出所有 store 类型及其支持的设置说明,是排查 store 配置问题时的第一手资料。
五、nix-store与nix-env操作文档独立成页
变更内容:nix-store和nix-env各操作(operation)的文档现在在手册中有独立页面,并包含所有可指定的通用选项(common options)以及影响这些命令的通用环境变量。
离线查看方式:
$ man nix-store-<operation> $ man nix-env-<operation>或通过--help指定操作:
$ nix-store --help --<operation> $ nix-env --help --<operation>例如man nix-store-query、nix-store --help --query等。
六、客户端感知 store 是否信任自身
变更内容:Nix 作为客户端时,现在会检查 store(服务器)是否信任该客户端。(store 一直需要检查是否信任客户端,但现在客户端也会被告知 store 的判定结果。)这对与非 legacy-ssh 的远程 Nix store 进行脚本化交互很有用。
nix store ping和nix doctor现在会显示这一信息。
源码佐证:信任判定的序列化与传输位于 src/libstore/worker-protocol.cc,其中WorkerProto::Serialise<std::optional<TrustedFlag>>::read/write负责在 worker 协议中读写"远程是否信任我们"的字段;daemon 侧通过authPeer(src/nix/unix/daemon.cc)判断客户端是否在trusted-users列表中。
nix store info(即nix store ping的正式名称,见 src/nix/store.cc 中"ping"被标记为Deprecated并别名到"info")在 src/nix/store-info.cc 中输出信任信息:
if (auto trusted = store->isTrustedClient()) notice("Trusted: %s", *trusted);实战命令:
$ nix store ping $ nix doctor输出中会显示Trusted: true/false(或 JSON 输出中的"trusted": true/false),帮助你确认当前用户是否被远程 store/daemon 信任,例如是否具备trusted-users权限。
七、新命令nix derivation add与nix derivation show更名
变更内容:
- 新增
nix derivation add:允许不经过 Nix 语言直接向 store 添加 derivation。它作为基础工具/管道(plumbing)命令,降低了对 Nix store 的替代前端(alternative front-ends)进行实验的门槛。它使用与nix derivation show相同的 JSON 布局,是show的逆操作。 nix show-derivation更名为nix derivation show:与nix derivation add保持一致,避免膨胀顶层命名空间;旧名称作为兼容别名保留。- JSON 格式新增顶层
name字段:nix derivation {add,show}的 JSON 格式现在包含 derivation 名称作为顶层字段,这对add方向尤其必要(否则某些情况下需要带外传递名称)。
源码佐证:
nix derivation add的实现位于 src/nix/derivation-add.cc,它从标准输入读取 JSON(nlohmann::json::parse(drainFD(STDIN_FILENO))),调用derivation::parseJsonAndValidate解析并校验,然后store->writeDerivation(drv, NoRepair)写入 store(支持--dry-run和只读模式),最后输出生成的.drvstore path。nix derivation show的实现位于 src/nix/derivation-show.cc,它把 installables 转换为 derivations,可选--recursive/-r计算闭包,输出 JSON 根对象包含version和derivations两个字段,其中derivations以 store path 为 key、derivation 属性为 value。- 命令注册:
registerCommand2<CmdAddDerivation>({"derivation", "add"})(src/nix/derivation-add.cc)与registerCommand2<CmdShowDerivation>({"derivation", "show"})(src/nix/derivation-show.cc)。
实战示例:
# 查看某个 derivation 的 JSON 表示 $ nix derivation show /nix/store/fpq78s2h8ffh66v2iy0q1838mhff06y8-glibc-2.33-78.drv # 将其 JSON 重新写入 store(反向操作) $ nix derivation show /nix/store/...-foo.drv | nix derivation addnix derivation add的文档(src/nix/derivation-add.md)说明:该命令从标准输入读取 store derivation 的 JSON 表示;store derivation 是 Nix 内部使用的、扩展名为.drv的 store path,表示 Nix 表达式求值得到的构建时依赖图。详细的 JSON 格式文档见 doc/manual/source/protocols/json/derivation/index.md。
八、升级迁移要点小结
针对从 Nix 2.14 及更早版本升级到 2.15,建议关注以下几点:
| 变更 | 旧行为 | 新行为 | 迁移建议 |
|---|---|---|---|
.drv后缀 installable | 展开为该 derivation 的所有输出 | 视为该.drvstore path 本身 | 改用drvPath^*或drvPath^out等^语法 |
nix show-derivation | 顶层命令 | 更名为nix derivation show(旧名保留为别名) | 新脚本使用nix derivation show |
nix describe-stores | 实验性命令 | 已移除 | 改用nix help-stores查看 store 类型与设置 |
nix-hash格式 | 仅 Base16/Base32 | 新增 Base64、SRI;--base16显式指定 | 需要 Base64/SRI 输出时用--base64/--sri,转换用--to-base64/--to-sri |
| 大量 installables | 命令行直接传入 | 可用--stdin从标准输入读取 | 路径数量接近 ARG_MAX 时使用--stdin |
| store 信任信息 | 客户端无从得知 | nix store ping/nix store info与nix doctor显示 | 脚本化检查远程 store 信任状态 |
结语
Nix 2.15 的这批变更整体上属于"梳理 CLI 边界、夯实 plumbing 基础"的版本:--stdin与^语法补全了大规模 store path 处理与 derivation 输出引用的表达力;nix-hash的格式扩展对齐了现代哈希表达习惯(SRI);nix derivation add/show与 store 信任信息的引入则让围绕 Nix store 做脚本化、工具化集成的路径更加顺畅。结合 src/nix、src/libcmd、src/libstore 中的实现,可以在升级后快速定位到对应行为的具体代码,便于排查与二次开发。
- 开发工具
- CLI
【免费下载链接】nix
Nix, the purely functional package manager
相关推荐
NumPy 1.15.0 版本发布详解:核心新特性、弃用变更与 438 个合并 PR 全解析
NumPy 1.15.0 版本发布详解:核心新特性、弃用变更与 438 个合并 PR 全解析 导读 本文基于 NumPy 官方 1.15.0 版本发布说明(见仓
科学计算数据分析Balloon.css 终极更新指南:从1.0到1.2.0版本的核心特性变化详解
Balloon.css 终极更新指南:从1.0到1.2.0版本的核心特性变化详解 Balloon.css 是一个轻量级的纯CSS工具提示库,无需JavaScri
人工智能语音音频本地部署Nix 2.22 版本核心变更解读:repl-flake 实验特性移除与 `nix eval` 派生打印优化
Nix 2.22 版本核心变更解读:repl flake 实验特性移除与 nix eval 派生打印优化 本文基于 Nix 官方发布说明 Release 2.2
开发工具CLI
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考