TL;DR
Connect both NixOS systems to the same network.
On the host system, run: nix run github:edolstra/nix-serve
Use SSH port forwarding: ssh -L 5000:127.0.0.1:5000 <user>@<host-ip> -N
Build with: sudo nix build .#nixosConfigurations.<your config name>.config.system.build.toplevel –extra-substituters ‘http://localhost:5000’ –no-require-sigs
Switch configuration: sudo nixos-rebuild switch –flake .#<your config name>
Introduction
In NixOS, rebuilding and redownloading packages can be time-consuming. To streamline this process, you can use another NixOS system on your home network as a binary cache. This guide will walk you through setting up a binary cache on a host machine and configuring another system to use this cache, reducing rebuild and download times for Nix flakes configurations.
Step-by-Step Instructions
Step 1: Connect Both Systems to the Same Network
Ensure both the host and target NixOS systems are connected to the same LAN or Wi-Fi network. This setup is essential for seamless communication between the systems.
Step 2: Run nix-serve on the Host Machine
On the host machine, open a terminal and run the following command:
This command launches the nix-serve utility, turning your host system into a binary cache server.
Step 3: Set Up SSH Port Forwarding
To avoid signature verification issues, use SSH port forwarding. Open a terminal on the target system and run:
Replace <user> with your username and <host-ip> with the IP address of the host machine. This command forwards port 5000 on the target system to port 5000 on the host system.
Step 4: Build the System Using nix build
Since nixos-rebuild doesn’t support specifying substituters directly, use nix build:
Replace <your config name> with the name of your NixOS configuration. This command builds the system using the binary cache on the host machine.
Step 5: Switch to the New Configuration
After building the system, switch to the new configuration using nixos-rebuild:
Replace <your config name> with the appropriate configuration name.
Conclusion
By following these steps, you can effectively use another NixOS system as a binary cache on your home network, saving time on package rebuilds and downloads. This setup is particularly useful for users working with Nix flakes, enabling a more efficient and streamlined NixOS experience.