Caching nix

or how to avoid saturating the home bandwidth when home-labbing with nixos

N nixos machines, N downloads

The problem is basically the same for all linux distributions where a fleet of servers need to be updated: by default, N machines will download from upstream servers N times the packages. It's not efficient of course, but it's even worse if each of those downloads is capable of saturating the download bandwidth of your poor home internet connection...

It's the same with nixos, and the need is even greater when you start building container images with nix in CI jobs, from within docker containers with an empty nix store.

The answer is not distributed build

Somehow I thought (even after reading about it) setting up a distributed build, where one server would build for all the others, would be the solution. Well not only it's complicated to set up, but no, this won't make the build server download all packages or make use of its own nix store as cache.

What works (great) for me: ncps.

ncps (Nix Cache Proxy Server) just does what it says: it's a proxy server that downloads packages and caches them. In practice, I have it set up in a server in my home network, and in any place where I need to use nix build or nixos-rebuild, I simply define my ncps proxy as a nix substituter.

The result: say you update your flake inputs to a newer revision of nixpkgs. The first rebuild will take as long as usual. Subsequent builds (from the same machine, or from any other nixos system pinned on the same revision of nixpkgs) will download packages at the speed of your LAN, reducing by a ton the build time.

Setup

Just follow the examples on https://docs.ncps.dev. On the machine's configuration where you want to run the proxy, I added something like:

services.ncps = {
  enable = true;
  cache.hostName = "https://nix-cache.internal.ybycode.com";
  server.addr = "127.0.0.1:9432"; # 127.0.0.1 because behind a reverse proxy
  cache.upstream = {
    urls = [
      "https://cache.nixos.org"
      "https://nix-community.cachix.org"
    ];
    publicKeys = [
      "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
      "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
    ];
  };
};

I already have caddy as a reverse proxy on this server so it got https for free. Now to be used as a substituter, you first need to find its public key:

$ curl https://nix-cache.internal.ybycode.com/pubkey
myserver:bb21bb988b300f52cb56d47c3721fceb406c368289a=

This public key is required when defining this proxy as a nix substituter. ncps signs all the NAR files it serves, and the public key allows the client machines to trust what they received as being unaltered.

Now, to configure all my machines to use this proxy as a substituter, I defined this module that they all import:

{ config, lib, ... }:

let
  cfg = config.nix-cache-proxy-client;
  proxyUrl = "http://nix-cache.internal.ybycode.com";
  proxyPublicKey = "myserver:bb21bb988b300f52cb56d47c3721fceb406c368289a=";
in
{
  options.nix-cache-proxy-client = {
    enable = lib.mkEnableOption "set ${proxyUrl} (ncps) as a first substituter" // {
      default = true;
    };
  };

  config = {
    nix.settings = {
      substituters = [
        proxyUrl
        "https://cache.nixos.org" # official cache as fallback
      ];

      trusted-public-keys = [
        proxyPublicKey
        "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
      ];
    };
  };
}

And that's it!

It works!

▶ nix-shell -p hello
this path will be fetched (73.6 KiB download, 273.1 KiB unpacked):
  /nix/store/fkv51s1m2gsy6pz71nnyqyprbvfg7sd7-hello-2.12.3
copying path '/nix/store/fkv51s1m2gsy6pz71nnyqyprbvfg7sd7-hello-2.12.3' from 'http://nix-cache.internal.ybycode.com'...

\o/

The case of container building

As mentioned before, I build docker containers with nix, from CI jobs running themselves in containers. In them, the nix store is barely populated, so ncps has a huge impact.

In this kind of environment or just for a one-off tryout of the setup, substituters can be defined via the NIX_CONFIG environment variable:

env:
  NIX_CONFIG: |
    substituters = https://nix-cache.internal.ybycode.com https://cache.nixos.org
    trusted-public-keys = myserver:bb21bb988b300f52cb56d47c3721fceb406c368289a= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
jobs:
  make-build-image:
    runs-on: docker
    container:
      image: code.ybycode.com/me/docker-images/build-image:20261115-7ab5d53
    steps:
      - name: build image
        run: nix-build ./minimal/minimal.nix