···11-# darwin.builder {#sec-darwin-builder}
11+# darwin.linux-builder {#sec-darwin-builder}
2233-`darwin.builder` provides a way to bootstrap a Linux builder on a macOS machine.
33+`darwin.linux-builder` provides a way to bootstrap a Linux builder on a macOS machine.
4455This requires macOS version 12.4 or later.
6677-This also requires that port 22 on your machine is free (since Nix does not
88-permit specifying a non-default SSH port for builders).
77+The builder runs on host port 31022 by default.
88+You can change it by overriding `virtualisation.darwin-builder.hostPort`.
99+See the [example](#sec-darwin-builder-example-flake).
9101011You will also need to be a trusted user for your Nix installation. In other
1112words, your `/etc/nix/nix.conf` should have something like:
···1718To launch the builder, run the following flake:
18191920```ShellSession
2020-$ nix run nixpkgs#darwin.builder
2121+$ nix run nixpkgs#darwin.linux-builder
2122```
22232324That will prompt you to enter your `sudo` password:
···5051```
5152# - Replace ${ARCH} with either aarch64 or x86_64 to match your host machine
5253# - Replace ${MAX_JOBS} with the maximum number of builds (pick 4 if you're not sure)
5353-builders = ssh-ng://builder@localhost ${ARCH}-linux /etc/nix/builder_ed25519 ${MAX_JOBS} - - - c3NoLWVkMjU1MTkgQUFBQUMzTnphQzFsWkRJMU5URTVBQUFBSUpCV2N4Yi9CbGFxdDFhdU90RStGOFFVV3JVb3RpQzVxQkorVXVFV2RWQ2Igcm9vdEBuaXhvcwo=
5454+builders = ssh-ng://builder@linux-builder ${ARCH}-linux /etc/nix/builder_ed25519 ${MAX_JOBS} - - - c3NoLWVkMjU1MTkgQUFBQUMzTnphQzFsWkRJMU5URTVBQUFBSUpCV2N4Yi9CbGFxdDFhdU90RStGOFFVV3JVb3RpQzVxQkorVXVFV2RWQ2Igcm9vdEBuaXhvcwo=
54555556# Not strictly necessary, but this will reduce your disk utilization
5657builders-use-substitutes = true
5858+```
5959+6060+To allow Nix to connect to a builder not running on port 22, you will also need to create a new file at `/etc/ssh/ssh_config.d/100-linux-builder.conf`:
6161+6262+```
6363+Host linux-builder
6464+ Hostname localhost
6565+ HostKeyAlias linux-builder
6666+ Port 31022
5767```
58685969… and then restart your Nix daemon to apply the change:
+11-7
nixos/modules/profiles/macos-builder.nix
···11-{ config, lib, pkgs, ... }:
11+{ config, lib, ... }:
2233let
44 keysDirectory = "/var/keys";
···6767 '';
6868 };
6969 hostPort = mkOption {
7070- default = 22;
7070+ default = 31022;
7171 type = types.int;
7272- example = 31022;
7272+ example = 22;
7373 description = ''
7474 The localhost host port to forward TCP to the guest port.
7575 '';
···139139140140 hostPkgs = config.virtualisation.host.pkgs;
141141142142- script = hostPkgs.writeShellScriptBin "create-builder" (
142142+ script = hostPkgs.writeShellScriptBin "create-builder" (
143143 # When running as non-interactively as part of a DarwinConfiguration the working directory
144144 # must be set to a writeable directory.
145145 (if cfg.workingDirectory != "." then ''
146146 ${hostPkgs.coreutils}/bin/mkdir --parent "${cfg.workingDirectory}"
147147 cd "${cfg.workingDirectory}"
148148- '' else "") + ''
148148+ '' else "") + ''
149149 KEYS="''${KEYS:-./keys}"
150150 ${hostPkgs.coreutils}/bin/mkdir --parent "''${KEYS}"
151151 PRIVATE_KEY="''${KEYS}/${user}_${keyType}"
···157157 if ! ${hostPkgs.diffutils}/bin/cmp "''${PUBLIC_KEY}" ${publicKey}; then
158158 (set -x; sudo --reset-timestamp ${installCredentials} "''${KEYS}")
159159 fi
160160- KEYS="$(${hostPkgs.nix}/bin/nix-store --add "$KEYS")" ${config.system.build.vm}/bin/run-nixos-vm
160160+ KEYS="$(${hostPkgs.nix}/bin/nix-store --add "$KEYS")" ${lib.getExe config.system.build.vm}
161161 '');
162162163163 in
···177177 Please inspect the trace of the following command to figure out which module
178178 has a dependency on stateVersion.
179179180180- nix-instantiate --attr darwin.builder --show-trace
180180+ nix-instantiate --attr darwin.linux-builder --show-trace
181181 '');
182182 };
183183···234234 # This ensures that anything built on the guest isn't lost when the guest is
235235 # restarted.
236236 writableStoreUseTmpfs = false;
237237+238238+ # Pass certificates from host to the guest otherwise when custom CA certificates
239239+ # are required we can't use the cached builder.
240240+ useHostCerts = true;
237241 };
238242 };
239243}
···166166 # Create a directory for exchanging data with the VM.
167167 mkdir -p "$TMPDIR/xchg"
168168169169+ ${lib.optionalString cfg.useHostCerts
170170+ ''
171171+ mkdir -p "$TMPDIR/certs"
172172+ if [ -e "$NIX_SSL_CERT_FILE" ]; then
173173+ cp -L "$NIX_SSL_CERT_FILE" "$TMPDIR"/certs/ca-certificates.crt
174174+ else
175175+ echo \$NIX_SSL_CERT_FILE should point to a valid file if virtualisation.useHostCerts is enabled.
176176+ fi
177177+ ''}
178178+169179 ${lib.optionalString cfg.useEFIBoot
170180 ''
171181 # Expose EFI variables, it's useful even when we are not using a bootloader (!).
···877887 '';
878888 };
879889880880-881890 virtualisation.bios =
882891 mkOption {
883892 type = types.nullOr types.package;
···887896 An alternate BIOS (such as `qboot`) with which to start the VM.
888897 Should contain a file named `bios.bin`.
889898 If `null`, QEMU's builtin SeaBIOS will be used.
899899+ '';
900900+ };
901901+902902+ virtualisation.useHostCerts =
903903+ mkOption {
904904+ type = types.bool;
905905+ default = false;
906906+ description =
907907+ lib.mdDoc ''
908908+ If enabled, when `NIX_SSL_CERT_FILE` is set on the host,
909909+ pass the CA certificates from the host to the VM.
890910 '';
891911 };
892912···10241044 source = ''"''${SHARED_DIR:-$TMPDIR/xchg}"'';
10251045 target = "/tmp/shared";
10261046 };
10471047+ certs = mkIf cfg.useHostCerts {
10481048+ source = ''"$TMPDIR"/certs'';
10491049+ target = "/etc/ssl/certs";
10501050+ };
10271051 };
10521052+10531053+ security.pki.installCACerts = mkIf cfg.useHostCerts false;
1028105410291055 virtualisation.qemu.networkingOptions =
10301056 let
+7-3
pkgs/top-level/darwin-packages.nix
···33, generateSplicesForMkScope, makeScopeWithSplicing
44, stdenv
55, preLibcCrossHeaders
66+, config
67}:
7889let
···229230 discrete-scroll = callPackage ../os-specific/darwin/discrete-scroll { };
230231231232 # See doc/builders/special/darwin-builder.section.md
232232- builder =
233233+ linux-builder = lib.makeOverridable ({ modules }:
233234 let
234235 toGuest = builtins.replaceStrings [ "darwin" ] [ "linux" ];
235236···237238 configuration = {
238239 imports = [
239240 ../../nixos/modules/profiles/macos-builder.nix
240240- ];
241241+ ] ++ modules;
241242242243 virtualisation.host = { inherit pkgs; };
243244 };
···246247 };
247248248249 in
249249- nixos.config.system.build.macos-builder-installer;
250250+ nixos.config.system.build.macos-builder-installer) { modules = [ ]; };
251251+252252+} // lib.optionalAttrs config.allowAliases {
253253+ builder = throw "'darwin.builder' has been changed and renamed to 'darwin.linux-builder'. The default ssh port is now 31022. Please update your configuration or override the port back to 22. See https://nixos.org/manual/nixpkgs/unstable/#sec-darwin-builder"; # added 2023-07-06
250254})