Nix 2.15 版本发布指南:核心 CLI 变更与 Store 交互新特性详解
2026/9/21 19:29:02 网站建设 项目流程
  • 开发工具
  • CLI

【免费下载链接】nix

Nix, the purely functional package manager

项目地址:https://gitcode.com/gh_mirrors/ni/nix
点击查看免费下载

本文围绕 Nix 2.15(2023-04-11 发布)的官方发布说明展开,逐条解读该版本在 installables 输入方式、哈希格式、store path 语义、store 信任机制与 derivation 子命令等方面的核心变更,并结合当前仓库源码(src/nixsrc/libcmdsrc/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::SRIHashFormat::Base64HashFormat::Nix32HashFormat::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-foo

SRI 格式形如sha256-<base64>,即算法名加短横线加 Base64 编码,便于在 Nix 表达式(如fetchurlhash属性)中直接使用。

三、.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
  • 特殊 URLauto会自动选择 store:优先本地/nix/store;若/nix/var/nix不可写则尝试连接 daemon socket;Linux 下还会考虑 local chroot store~/.local/share/nix/root

实战命令

$ nix help-stores

该命令会输出所有 store 类型及其支持的设置说明,是排查 store 配置问题时的第一手资料。

五、nix-storenix-env操作文档独立成页

变更内容nix-storenix-env各操作(operation)的文档现在在手册中有独立页面,并包含所有可指定的通用选项(common options)以及影响这些命令的通用环境变量。

离线查看方式

$ man nix-store-<operation> $ man nix-env-<operation>

或通过--help指定操作:

$ nix-store --help --<operation> $ nix-env --help --<operation>

例如man nix-store-querynix-store --help --query等。

六、客户端感知 store 是否信任自身

变更内容:Nix 作为客户端时,现在会检查 store(服务器)是否信任该客户端。(store 一直需要检查是否信任客户端,但现在客户端也会被告知 store 的判定结果。)这对与非 legacy-ssh 的远程 Nix store 进行脚本化交互很有用。

nix store pingnix 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 addnix derivation show更名

变更内容

  1. 新增nix derivation add:允许不经过 Nix 语言直接向 store 添加 derivation。它作为基础工具/管道(plumbing)命令,降低了对 Nix store 的替代前端(alternative front-ends)进行实验的门槛。它使用与nix derivation show相同的 JSON 布局,是show的逆操作。
  2. nix show-derivation更名为nix derivation show:与nix derivation add保持一致,避免膨胀顶层命名空间;旧名称作为兼容别名保留。
  3. 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 根对象包含versionderivations两个字段,其中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 add

nix 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 infonix 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

项目地址:https://gitcode.com/gh_mirrors/ni/nix
点击查看免费下载

相关推荐

上一篇:3步开启你的AI虚拟主播直播:PersonaLive终极入门指南
下一篇:VR内容制作终极指南:使用BackgroundRemover处理360度视频的完整教程

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询