Using Another NixOS System as a Binary Cache for Flake Configurations on Your Home Network

ehsan2003

ehsan2003

Posted on July 6, 2024

Using Another NixOS System as a Binary Cache for Flake Configurations on Your Home Network

TL;DR

  1. Connect both NixOS systems to the same network.
  2. On the host system, run: nix run github:edolstra/nix-serve
  3. Use SSH port forwarding: ssh -L 5000:127.0.0.1:5000 <user>@<host-ip> -N
  4. Build with: sudo nix build .#nixosConfigurations.<your config name>.config.system.build.toplevel --extra-substituters 'http://localhost:5000' --no-require-sigs
  5. 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:

nix run github:edolstra/nix-serve
Enter fullscreen mode Exit fullscreen mode

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:

ssh -L 5000:127.0.0.1:5000 <user>@<host-ip> -N
Enter fullscreen mode Exit fullscreen mode

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:

sudo nix build .#nixosConfigurations.<your config name>.config.system.build.toplevel --extra-substituters 'http://localhost:5000' --no-require-sigs
Enter fullscreen mode Exit fullscreen mode

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:

sudo nixos-rebuild switch --flake .#<your config name>
Enter fullscreen mode Exit fullscreen mode

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.

💖 💪 🙅 🚩
ehsan2003
ehsan2003

Posted on July 6, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related