我在 nix-darwin 上使用 Determinate Nix,想添加 TUNA 镜像(https://mirrors.tuna.tsinghua.edu.cn/nix)作为二进制缓存,以加速国内的下载速度。
我的尝试
nix.settings.{substituters, trusted-public-keys}
我的 nix-darwin 配置中设置了 nix.enable = false,让 Determinate Nix 来管理 Nix,但 nix.settings.experimental-features 是生效的,所以我尝试了:
nix.settings.substituters = [
"https://mirrors.tuna.tsinghua.edu.cn/nix"
];
nix.settings.trusted-public-keys = [
"cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
];
执行 darwin-rebuild switch 后,nix config show substituters 仍然只显示默认值,没有效果。
extra-substituters 和 extra-trusted-public-keys
由于 /etc/nix/nix.conf 中 Determinate 自己的缓存也使用了 extra-substituters,我改用了带 extra- 前缀的版本:
nix.settings.extra-substituters = [
"https://mirrors.tuna.tsinghua.edu.cn/nix"
];
nix.settings.extra-trusted-public-keys = [
"cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
];
仍然没有效果。我查看了 /etc/nix/nix.custom.conf,发现 Determinate 的 nix-darwin 模块只往里面写了一部分设置(cores、sandbox),似乎忽略了 substituter 相关的设置。
通过 environment.etc 直接写入 nix.custom.conf(extra-substituters)
Determinate Nix 的 /etc/nix/nix.conf 头部注释写着:
# user modification can go in nix.custom.conf
并通过 !include nix.custom.conf 引用它。既然 Determinate 模块不会传递 substituter 配置,我直接写了进去:
environment.etc."nix/nix.custom.conf".text = ''
cores = 0
sandbox = false
extra-substituters = https://mirrors.tuna.tsinghua.edu.cn/nix
extra-trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
'';
这样镜像出现在了 nix config show substituters 中,但是在列表的末尾——所以 Nix 仍然会优先尝试 cache.nixos.org,没有达到目的。extra- 前缀是追加而不是设置优先级。
通过 environment.etc 直接写入 nix.custom.conf(substituters)
为了让 TUNA 成为首选缓存,我使用了 substituters(而不是 extra-substituters)来完全控制顺序,同时使用 extra-trusted-public-keys 以免覆盖 Determinate 的密钥:
environment.etc."nix/nix.custom.conf".text = ''
cores = 0
sandbox = false
substituters = https://mirrors.tuna.tsinghua.edu.cn/nix https://cache.nixos.org/ https://install.determinate.systems
extra-trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
'';
这样可以工作。TUNA 会被优先尝试,而且 Determinate 在主 nix.conf 中的 extra-trusted-public-keys 仍然会在 include 之后被追加进来。但这仍然感觉很脆弱——我重复了 Determinate 模块生成的 cores 和 sandbox 设置,如果模块以后修改了它写入 nix.custom.conf 的内容,就会产生冲突。
想请教一下:Determinate 的 nix-darwin 模块是否有官方支持的添加自定义 substituter 的方式?
另外:有些包似乎不在 TUNA 镜像中——比如 dotnet-sdk_11、dotnet-sdk_10、dotnet-sdk_9 都会回退到 cache.nixos.org。TUNA 镜像的覆盖率大概是多少?这种回退是因为我使用的 nixpkgs-unstable 比较旧(155 天前更新)导致的吗?