Files
nixpkgs/doc/packages/darwin-builder.section.md
2026-07-21 16:10:37 +02:00

8.6 KiB

darwin.linux-builder

:::{.warning} By default, darwin.linux-builder uses a publicly-known private SSH host key (this is different from the SSH key used by the user that connects to the builder).

Given the intended use case for it (a Linux builder that runs on the same machine), this shouldn't be an issue. However, if you plan to deviate from this use case in any way (e.g. by exposing this builder to remote machines), you should understand the security implications of doing so and take any appropriate measures. :::

darwin.linux-builder provides a way to bootstrap a Linux remote builder on a macOS machine.

This requires macOS version 12.4 or later.

The remote builder runs on host port 31022 by default. You can change it by overriding virtualisation.darwin-builder.hostPort. See the example.

You will also need to be a trusted user for your Nix installation. In other words, your /etc/nix/nix.conf should have something like:

extra-trusted-users = <your username goes here>

To launch the remote builder, run the following flake:

$ nix run nixpkgs#darwin.linux-builder

That will prompt you to enter your sudo password:

+ sudo --reset-timestamp /nix/store/…-install-credentials.sh ./keys
Password:

… so that it can install a private key used to ssh into the build server. After that the script will launch the virtual machine and automatically log you in as the builder user:

<<< Welcome to NixOS 22.11.20220901.1bd8d11 (aarch64) - ttyAMA0 >>>

Run 'nixos-help' for the NixOS manual.

nixos login: builder (automatic login)


[builder@nixos:~]$

Note: When you need to stop the VM, run shutdown now as the builder user.

To delegate builds to the remote builder, add the following options to your nix.conf file:

# - Replace ${ARCH} with either aarch64 or x86_64 to match your host machine
# - Replace ${MAX_JOBS} with the maximum number of builds (pick 4 if you're not sure)
builders = ssh-ng://builder@linux-builder ${ARCH}-linux /etc/nix/builder_ed25519 ${MAX_JOBS} - - - c3NoLWVkMjU1MTkgQUFBQUMzTnphQzFsWkRJMU5URTVBQUFBSUpCV2N4Yi9CbGFxdDFhdU90RStGOFFVV3JVb3RpQzVxQkorVXVFV2RWQ2Igcm9vdEBuaXhvcwo=

# Not strictly necessary, but this will reduce your disk utilization
builders-use-substitutes = true

To allow Nix to connect to the default remote builder, which does not run on port 22, you will also need to create a new file at /etc/ssh/ssh_config.d/100-linux-builder.conf:

Host linux-builder
  Hostname localhost
  HostKeyAlias linux-builder
  Port 31022
  User builder
  IdentityFile /etc/nix/builder_ed25519

… and then restart your Nix daemon to apply the change:

$ sudo launchctl kickstart -k system/org.nixos.nix-daemon

Note that if the builder is running and you have created the above ssh conf file, you can ssh into the builder with sudo ssh builder@linux-builder.

Using the Virtualization.framework backend

darwin.linux-builder-vz is a variant of darwin.linux-builder that runs the same NixOS guest on Apple's Virtualization.framework (via pkgs.vzvm) instead of QEMU. Instead of emulating x86_64, it exposes Rosetta to the guest, so x86_64-linux builds are translated rather than emulated, which is substantially faster. It requires an Apple silicon host (aarch64-darwin) running macOS 13 or newer, with Rosetta installed:

$ softwareupdate --install-rosetta --agree-to-license

The builder refuses to start when Rosetta is missing, rather than silently dropping x86_64-linux support; set virtualisation.vz.rosetta.enable = false to run without it.

It is a drop-in replacement: it listens on the same host port (31022) and presents the same host key as the QEMU builder, so the nix.conf and SSH configuration described above apply unchanged; only the transport behind the port changes, from TCP forwarding to vsock. Since a single builder VM handles both architectures through Rosetta, list both systems in your builders entry (aarch64-linux,x86_64-linux), or with nix-darwin:

{
  nix.linux-builder = {
    enable = true;
    package = pkgs.darwin.linux-builder-vz;
    systems = [
      "aarch64-linux"
      "x86_64-linux"
    ];
  };
}

When switching an existing QEMU builder over, delete its data disk first (e.g. sudo rm /var/lib/linux-builder/nixos.qcow2): the vz builder reuses the file name but writes a raw image, and refuses to misread a genuine qcow2 left behind.

The guest console goes to the macOS unified log by default; read it with:

$ /usr/bin/log show --last 5m --predicate 'subsystem == "systems.applicative.vzvm"'

The virtualisation.vz.* NixOS options configure the backend further, e.g. virtualisation.vz.nestedVirtualization gives the guest a working /dev/kvm for running NixOS integration tests on the builder (macOS 15+, M3 or newer).

Example flake usage

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-22.11-darwin";
    darwin.url = "github:nix-darwin/nix-darwin/master";
    darwin.inputs.nixpkgs.follows = "nixpkgs";
  };

  outputs =
    {
      self,
      darwin,
      nixpkgs,
      ...
    }@inputs:
    let

      inherit (darwin.lib) darwinSystem;
      system = "aarch64-darwin";
      pkgs = nixpkgs.legacyPackages."${system}";
      linuxSystem = builtins.replaceStrings [ "darwin" ] [ "linux" ] system;

      darwin-builder = nixpkgs.lib.nixosSystem {
        system = linuxSystem;
        modules = [
          "${nixpkgs}/nixos/modules/profiles/nix-builder-vm.nix"
          {
            virtualisation = {
              host.pkgs = pkgs;
              darwin-builder.workingDirectory = "/var/lib/darwin-builder";
              darwin-builder.hostPort = 22;
            };
          }
        ];
      };
    in
    {

      darwinConfigurations = {
        machine1 = darwinSystem {
          inherit system;
          modules = [
            {
              nix.distributedBuilds = true;
              nix.buildMachines = [
                {
                  hostName = "localhost";
                  sshUser = "builder";
                  sshKey = "/etc/nix/builder_ed25519";
                  system = linuxSystem;
                  maxJobs = 4;
                  supportedFeatures = [
                    "kvm"
                    "benchmark"
                    "big-parallel"
                  ];
                }
              ];

              launchd.daemons.darwin-builder = {
                command = "${darwin-builder.config.system.build.macos-builder-installer}/bin/create-builder";
                serviceConfig = {
                  KeepAlive = true;
                  RunAtLoad = true;
                  StandardOutPath = "/var/log/darwin-builder.log";
                  StandardErrorPath = "/var/log/darwin-builder.log";
                };
              };
            }
          ];
        };
      };

    };
}

Reconfiguring the remote builder

Initially you should not change the remote builder configuration else you will not be able to use the binary cache. However, after you have the remote builder running locally you may use it to build a modified remote builder with additional storage or memory.

To do this, you just need to set the virtualisation.darwin-builder.* parameters as in the example below and rebuild.

{
  darwin-builder = nixpkgs.lib.nixosSystem {
    system = linuxSystem;
    modules = [
      "${nixpkgs}/nixos/modules/profiles/nix-builder-vm.nix"
      {
        virtualisation.host.pkgs = pkgs;
        virtualisation.darwin-builder.diskSize = 5120;
        virtualisation.darwin-builder.memorySize = 1024;
        virtualisation.darwin-builder.hostPort = 33022;
        virtualisation.darwin-builder.workingDirectory = "/var/lib/darwin-builder";
      }
    ];
  };
}

You may make any other changes to your VM in this attribute set. For example, you could enable Docker or X11 forwarding to your Darwin host.

Troubleshooting the generated configuration

The linux-builder package exposes the attributes nixosConfig and nixosOptions that allow you to inspect the generated NixOS configuration in the nix repl. For example:

$ nix repl --file ~/src/nixpkgs --argstr system aarch64-darwin

nix-repl> darwin.linux-builder.nixosConfig.nix.package
«derivation /nix/store/...-nix-2.17.0.drv»

nix-repl> :p darwin.linux-builder.nixosOptions.virtualisation.memorySize.definitionsWithLocations
[ { file = "/home/user/src/nixpkgs/nixos/modules/profiles/nix-builder-vm.nix"; value = 3072; } ]