使用指南
跨平台配置
在 macOS 与 NixOS / Linux 之间共享同一份 pins-config.nix 与 pins.json,避免求值报错和双端乒乓重算。
当同一套 Nix 配置同时管理 macOS(aarch64-darwin)与 Linux / NixOS 虚拟机(aarch64-linux、x86_64-linux)时,可以组合使用以下能力,让多端共享一份 pins-config.nix 与 pins.json。
1. 使用 systems 声明平台范围
在任意 Pin 构造器上添加 systems 列表,限定该 Pin 仅在匹配平台求值与更新:
{ pin }: {
patchelf = pin.github {
target = "NixOS/patchelf";
tags = [ "core" ];
};
alpine-vm = pin.docker {
target = "alpine";
tag = "3.20";
platform = "linux/arm64";
os = "linux";
arch = "arm64";
systems = [ "aarch64-linux" "x86_64-linux" ];
tags = [ "vm" ];
};
}
- 在
aarch64-darwin上执行nix-pins update时,会自动跳过alpine-vm的平台相关求值,并在pins.json中保留其已有锁定结果。 - 如需切换目标平台,可传
--system aarch64-linux(或设置NIX_PINS_SYSTEM);如需忽略平台过滤,可传--all-systems。
2. 使用 pin.forSystems 管理多架构预编译资产
组合 pin.forSystems 与 pin.fetcher.githubRelease,在同一 Pin 下按平台锁定各架构资产:
{ pin }: {
tool = pin.mk {
checker = pin.checker.github { target = "acme/tool"; };
sources = pin.forSystems {
aarch64-darwin = pin.fetcher.githubRelease {
target = "acme/tool";
asset = v: "tool-${v}-aarch64-apple-darwin.tar.gz";
unpack = true;
};
aarch64-linux = pin.fetcher.githubRelease {
target = "acme/tool";
asset = v: "tool-${v}-aarch64-unknown-linux-gnu.tar.gz";
unpack = true;
};
};
};
}
消费侧可通过 flake.nix 导出的 nix-pins.lib.sourceFor 自动按当前系统取源:
let
pins = nix-pins.lib.loadPins {
inherit pkgs;
file = ./pins.json;
};
toolSource = nix-pins.lib.sourceFor pkgs pins.tool;
in
toolSource.src
sourceFor 优先读取 pin.sources.${pkgs.stdenv.hostPlatform.system},不存在时自动回退到 pin.sources.default。
3. 消除双端乒乓重算与合并 CI 矩阵产物
- 当 macOS 与 Linux 对同一 Pin 求得不同
drvPath但真实哈希一致时,Source.fingerprints会自动记录多平台指纹,后续在任意一端执行update都直接复用哈希,不再乒乓重算。 - 如果在 CI 矩阵中分别在 macOS 与 Linux runner 并行更新,可用
merge将多份pins.json合并为一份,并用diff复核变更:
nix-pins merge pins-linux.json --json
nix-pins diff HEAD --format markdown