在 nix-darwin 上使用 Determinate Nix 配置 TUNA 镜像作为二进制缓存

我在 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-substitutersextra-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 模块只往里面写了一部分设置(coressandbox),似乎忽略了 substituter 相关的设置。

通过 environment.etc 直接写入 nix.custom.confextra-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.confsubstituters

为了让 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 模块生成的 coressandbox 设置,如果模块以后修改了它写入 nix.custom.conf 的内容,就会产生冲突。

想请教一下:Determinate 的 nix-darwin 模块是否有官方支持的添加自定义 substituter 的方式?

另外:有些包似乎不在 TUNA 镜像中——比如 dotnet-sdk_11dotnet-sdk_10dotnet-sdk_9 都会回退到 cache.nixos.org。TUNA 镜像的覆盖率大概是多少?这种回退是因为我使用的 nixpkgs-unstable 比较旧(155 天前更新)导致的吗?

你网址打错了

用途 官方 URL 镜像源 URL
Nix 通道 https://channels.nixos.org https://mirrors.tuna.tsinghua.edu.cn/nix-channels
Nix 二进制缓存 https://cache.nixos.org https://mirrors.tuna.tsinghua.edu.cn/nix-channels/store
Nixpkgs 源码 https://github.com/NixOS/nixpkgs.git https://mirror.nju.edu.cn/git/nixpkgs.git
Nix 安装脚本 https://nixos.org/nix/install https://mirrors.tuna.tsinghua.edu.cn/nix/latest/install

从nix安装脚本的镜像拉二进制缓存当然什么都找不到。应该用https://mirrors.tuna.tsinghua.edu.cn/nix-channels/store

nix-darwin和Determinate Nix没用过,这我不知道,但是缓存配置你肯定是弄错了。

EDIT: 覆盖优先级在url后面加上?priority=10,越小越优先,官方源默认是40。

1 Like

nix.enable = false 意味着nix-darwin的相关设置被关闭了,不仅是安装nix。如果你的nix不是由nixos或者nix-darwin管理的,而是直接安装的,你应该直接编辑nix.conf。