What is the best practice to use binary cache for this situation?

I have 1 laptop (My-laptop) and 1 PC (My-PC), both of them are using NixOS, and both connected to the same network.

I set up binary cache (nix-serve) on PC as described in nixos wiki for local use, but without the step 3. (nginx).

I use binary cache also described in nixos wiki on My-laptop to use the nix-serve already set up on My-PC. This is my setup on My-laptop:

{
  lib,
  config,
  ...
}:
{
  nix = {
    settings = {
      substituters = [
        "http://192.168.100.101:5000"
        "https://nix-community.cachix.org"
        "https://cache.nixos.org/"
      ];

      trusted-public-keys = [
        "My-PC-1:eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee="
        "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
        "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
      ];
    };
  };
}

All are working fine when both machines are running on the same network.

However, when I brought my laptop outside, or when My-PC was shutdown, and do a

nh os switch

on My-laptop, nix complained that it cannot find My-PC and it just stop the process.

The workaround for this situation is explicitly specify the substituters and the trusted-public-keys in the command (to eliminate My-PC substituters):

nh os switch -- --option substituters "https://nix-community.cachix.org https://cache.nixos.org" --option extra-trusted-public-keys "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="

I do not know why it is unable to automaticcally fallback to use "https://nix-community.cachix.org" and "https://cache.nixos.org/", is it a bug?

If auto fallback is not possible, why did it let me provide a list of substituters?

I think I have the same use case and solve this with

nix.settings = {
  connect-timeout = 3; # optional
  fallback = true;
}

See the nix.conf - Nix 2.29.1 Reference Manual:

Maybe give it a try …
You also can try this beforehand at/with your nh os switch command. – I don’t know the details and only will mention, that you do not have to add the substituter https://cache.nixos.org because this is the default and you only append the other subsituters (to this default) … #AFAIK

And also FYI: this is how I add my personal substituter + cache (powered by ncps ATM):

nix.settings = {
    #substituters = lib.mkBefore [ "http://$MYNCACHE:8501" ];
    substituters = [ "http://$MYNCACHE:8501?priority=1" ];
    trusted-public-keys = [ "$MYNCACHE:T[…]=" ];
    trusted-substituters = [ "http://$MYNCACHE:8501" ];
  };

:slight_smile: KR