尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

Nix 2.15 版本发布指南:核心 CLI 变更与 Store 交互新特性详解

Nix 2.15 版本发布指南:核心 CLI 变更与 Store 交互新特性详解 开发工具CLI【免费下载链接】nixNix, the purely functional package manager项目地址https://gitcode.com/gh_mirrors/ni/nix点击查看免费下载本文围绕 Nix 2.152023-04-11 发布的官方发布说明展开逐条解读该版本在 installables 输入方式、哈希格式、store path 语义、store 信任机制与 derivation 子命令等方面的核心变更并结合当前仓库源码src/nix、src/libcmd、src/libstore等给出实现层面的佐证与实战示例帮助你理解变更动机、新语法用法以及升级迁移时的注意点。一、--stdininstallables 从标准输入读取变更内容所有接受 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 和 SRISubresource 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::Base16addFlag({ .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.1Store URL 可以用查询字符串指定 store 设置例如--store ssh://machine.example.org?ssh-key/path/to/my/key特殊 URLauto会自动选择 store优先本地/nix/store若/nix/var/nix不可写则尝试连接 daemon socketLinux 下还会考虑 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::Serialisestd::optionalTrustedFlag::read/write负责在 worker 协议中读写远程是否信任我们的字段daemon 侧通过authPeersrc/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它从标准输入读取 JSONnlohmann::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。命令注册registerCommand2CmdAddDerivation({derivation, add})src/nix/derivation-add.cc与registerCommand2CmdShowDerivation({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 shownix 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 时使用--stdinstore 信任信息客户端无从得知nix store ping/nix store info与nix doctor显示脚本化检查远程 store 信任状态结语Nix 2.15 的这批变更整体上属于梳理 CLI 边界、夯实 plumbing 基础的版本--stdin与^语法补全了大规模 store path 处理与 derivation 输出引用的表达力nix-hash的格式扩展对齐了现代哈希表达习惯SRInix derivation add/show与 store 信任信息的引入则让围绕 Nix store 做脚本化、工具化集成的路径更加顺畅。结合 src/nix、src/libcmd、src/libstore 中的实现可以在升级后快速定位到对应行为的具体代码便于排查与二次开发。赞分享开发工具CLI【免费下载链接】nixNix, the purely functional package manager项目地址https://gitcode.com/gh_mirrors/ni/nix点击查看免费下载相关推荐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上一篇3步开启你的AI虚拟主播直播PersonaLive终极入门指南下一篇VR内容制作终极指南使用BackgroundRemover处理360度视频的完整教程创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表