From 310a4ba73381daeacdc263c2a492bac104a2858f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Boros?= Date: Fri, 13 Nov 2026 23:57:10 +0200 Subject: [PATCH 1/5] fix(nix): add uid-range to darwin linux-builder Co-Authored-By: Claude Sonnet 5.5 --- nix/docs/nixos-tests-on-macos.md | 14 +++++++++++++- .../darwin-nixostest/darwin-configuration.nix | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/nix/docs/nixos-tests-on-macos.md b/nix/docs/nixos-tests-on-macos.md index 0b3e8abb1d..1bea3f8987 100644 --- a/nix/docs/nixos-tests-on-macos.md +++ b/nix/docs/nixos-tests-on-macos.md @@ -31,7 +31,9 @@ The linux-builder VM is configured with: - 8GB RAM - 40GB disk - Support for both x86_64-linux and aarch64-linux builds -- The `nixos-test` feature required for running NixOS tests +- The `nixos-test` and `uid-range` features required for running NixOS tests +- `auto-allocate-uids` and `cgroups` enabled inside the VM, so builds that need `uid-range` run +- `-cpu host` passed to QEMU, since the default `-cpu max` breaks PAC-built guest binaries on M4 After setup completes, restart your shell to access the helper commands. @@ -152,6 +154,16 @@ start-linux-builder The VM is configured as ephemeral, meaning it's recreated fresh on each start. This ensures a clean environment but requires re-downloading cached build artifacts. +### QEMU aborts on M4 with macOS 15.2 or later + +nixpkgs-unstable's QEMU 11.0.1 aborts during HVF vCPU init on M4. +If your own nix-darwin config manages the builder, point `virtualisation.qemu.package` at `qemu_kvm` from the `nixos-26.05` branch. + +### Using your own nix-darwin config + +The setup script applies the `darwin-nixostest` configuration. If you already manage the machine with nix-darwin, copy the `nix.linux-builder` block and the `QEMU_OPTS` line from `nix/hosts/darwin-nixostest/darwin-configuration.nix` instead. +Add `uid-range` to `nix.linux-builder.supportedFeatures` and to `nix.settings.system-features` inside the builder config. + ### Slow first build The first NixOS test run may download significant data. diff --git a/nix/hosts/darwin-nixostest/darwin-configuration.nix b/nix/hosts/darwin-nixostest/darwin-configuration.nix index 8f5ec36038..e55ee5ceae 100644 --- a/nix/hosts/darwin-nixostest/darwin-configuration.nix +++ b/nix/hosts/darwin-nixostest/darwin-configuration.nix @@ -113,6 +113,7 @@ in "benchmark" "big-parallel" "nixos-test" + "uid-range" ]; config = { virtualisation = { @@ -122,9 +123,25 @@ in }; cores = 6; }; + nix.settings = { + experimental-features = [ + "auto-allocate-uids" + "cgroups" + ]; + auto-allocate-uids = true; + system-features = [ + "kvm" + "benchmark" + "big-parallel" + "nixos-test" + "uid-range" + ]; + }; }; }; + launchd.daemons.linux-builder.environment.QEMU_OPTS = "-cpu host"; + nix.distributedBuilds = true; system.stateVersion = 6; From 09dbf1e9a0141373c0af96c2308aba7aa4a5de8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Boros?= Date: Sat, 14 Nov 2026 14:06:48 +0200 Subject: [PATCH 2/5] fix(nix): pin qemu 10.2 and size darwin builder for uid-range tests Co-Authored-By: Claude Sonnet 5.5 --- flake.lock | 17 +++++++ flake.nix | 2 + nix/docs/nixos-tests-on-macos.md | 40 +++++++++++---- nix/hosts.nix | 2 +- .../darwin-nixostest/darwin-configuration.nix | 50 +++++++++++-------- nix/packages/setup-darwin-linux-builder.nix | 13 +++-- 6 files changed, 86 insertions(+), 38 deletions(-) diff --git a/flake.lock b/flake.lock index e381b678b3..6af9a148e1 100644 --- a/flake.lock +++ b/flake.lock @@ -293,6 +293,22 @@ "type": "github" } }, + "nixpkgs-qemu10": { + "locked": { + "lastModified": 1794165970, + "narHash": "sha256-XuAV2cUwaod3Bt4XFVqiXz3v1w2ex16W6Sv4S7EWres=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "ac588ef1c914865dad517b337ea0eb9694a4021c", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-26.05", + "repo": "nixpkgs", + "type": "github" + } + }, "nixpkgs_2": { "locked": { "lastModified": 1767379071, @@ -320,6 +336,7 @@ "nixpkgs": "nixpkgs_2", "nixpkgs-2211": "nixpkgs-2211", "nixpkgs-oldstable": "nixpkgs-oldstable", + "nixpkgs-qemu10": "nixpkgs-qemu10", "rust-overlay": "rust-overlay", "treefmt-nix": "treefmt-nix" } diff --git a/flake.nix b/flake.nix index afbb66cd11..86d2d5f56e 100644 --- a/flake.nix +++ b/flake.nix @@ -31,6 +31,8 @@ nixpkgs-2211.url = "https://channels.nixos.org/nixos-22.11/nixexprs.tar.xz"; nixpkgs-2211.flake = false; nixpkgs.url = "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz"; + # unstable's QEMU 11.0.1 aborts in HVF vCPU init on M4 with macOS >= 15.2. 26.05 ships QEMU 10.2. + nixpkgs-qemu10.url = "github:NixOS/nixpkgs/nixos-26.05"; rust-overlay.inputs.nixpkgs.follows = "nixpkgs"; rust-overlay.url = "github:oxalica/rust-overlay"; treefmt-nix.inputs.nixpkgs.follows = "nixpkgs"; diff --git a/nix/docs/nixos-tests-on-macos.md b/nix/docs/nixos-tests-on-macos.md index 1bea3f8987..58ced59a9b 100644 --- a/nix/docs/nixos-tests-on-macos.md +++ b/nix/docs/nixos-tests-on-macos.md @@ -12,13 +12,13 @@ You need: Run the setup script to configure nix-darwin with the linux-builder: ```bash -nix run .#setup-darwin-linux-builder +sudo nix run .#setup-darwin-linux-builder ``` Note that you don't have to checkout the repository to run the setup, you can run it directly from GitHub: ```bash -nix run github:supabase/postgres#setup-darwin-linux-builder +sudo nix run github:supabase/postgres#setup-darwin-linux-builder ``` This command will: @@ -27,16 +27,39 @@ This command will: - Install helper scripts for managing the builder The linux-builder VM is configured with: -- 6 CPU cores -- 8GB RAM -- 40GB disk +- 8 CPU cores +- 24GB RAM +- 100GB disk - Support for both x86_64-linux and aarch64-linux builds -- The `nixos-test` and `uid-range` features required for running NixOS tests +- The `nixos-test` feature required for running NixOS tests +- The `uid-range` feature for builds that request it - `auto-allocate-uids` and `cgroups` enabled inside the VM, so builds that need `uid-range` run - `-cpu host` passed to QEMU, since the default `-cpu max` breaks PAC-built guest binaries on M4 After setup completes, restart your shell to access the helper commands. +To re-apply the configuration after changing it, run the same `sudo nix run .#setup-darwin-linux-builder` command. + +### First install + +The builder VM is a Linux system, so the first install needs a Linux builder to build it. +If setup fails with `platform mismatch, Required system: 'aarch64-linux'`, start the stock builder first: + +```bash +nix run github:NixOS/nixpkgs/nixos-26.05#darwin.linux-builder +``` + +Leave it running, then build the configuration through it from another terminal and rerun setup: + +```bash +sudo env NIX_SSHOPTS="-p 31022" nix build .#darwinConfigurations.darwin-nixostest.system --no-link \ + --builders "ssh-ng://builder@127.0.0.1 aarch64-linux $PWD/keys/builder_ed25519 4 1 kvm,benchmark,big-parallel,nixos-test - " +sudo nix run .#setup-darwin-linux-builder +``` + +Run the builder from the directory that holds its `keys/` folder. +The host key is `ssh-keyscan -t ed25519 -p 31022 127.0.0.1` with the comment line removed, fields 2 and 3, base64 encoded. + ## Verify the setup The setup script runs verification automatically after configuration. @@ -159,11 +182,6 @@ This ensures a clean environment but requires re-downloading cached build artifa nixpkgs-unstable's QEMU 11.0.1 aborts during HVF vCPU init on M4. If your own nix-darwin config manages the builder, point `virtualisation.qemu.package` at `qemu_kvm` from the `nixos-26.05` branch. -### Using your own nix-darwin config - -The setup script applies the `darwin-nixostest` configuration. If you already manage the machine with nix-darwin, copy the `nix.linux-builder` block and the `QEMU_OPTS` line from `nix/hosts/darwin-nixostest/darwin-configuration.nix` instead. -Add `uid-range` to `nix.linux-builder.supportedFeatures` and to `nix.settings.system-features` inside the builder config. - ### Slow first build The first NixOS test run may download significant data. diff --git a/nix/hosts.nix b/nix/hosts.nix index f8f7c394d2..9ace764347 100644 --- a/nix/hosts.nix +++ b/nix/hosts.nix @@ -3,7 +3,7 @@ flake = { darwinConfigurations = { darwin-nixostest = inputs.nix-darwin.lib.darwinSystem { - specialArgs = { inherit self; }; + specialArgs = { inherit self inputs; }; modules = [ ./hosts/darwin-nixostest/darwin-configuration.nix ]; }; }; diff --git a/nix/hosts/darwin-nixostest/darwin-configuration.nix b/nix/hosts/darwin-nixostest/darwin-configuration.nix index e55ee5ceae..900dad194f 100644 --- a/nix/hosts/darwin-nixostest/darwin-configuration.nix +++ b/nix/hosts/darwin-nixostest/darwin-configuration.nix @@ -1,4 +1,5 @@ { + inputs, lib, pkgs, self, @@ -107,7 +108,7 @@ in nix.linux-builder = { enable = true; ephemeral = true; - maxJobs = 4; + maxJobs = 2; supportedFeatures = [ "kvm" "benchmark" @@ -115,29 +116,34 @@ in "nixos-test" "uid-range" ]; - config = { - virtualisation = { - darwin-builder = { - diskSize = 40 * 1024; - memorySize = 8 * 1024; + config = + { lib, ... }: + { + virtualisation = { + qemu.package = lib.mkForce inputs.nixpkgs-qemu10.legacyPackages.aarch64-darwin.qemu_kvm; + darwin-builder = { + diskSize = 100 * 1024; + memorySize = 24 * 1024; + }; + cores = 8; + }; + nix.settings = { + experimental-features = [ + "auto-allocate-uids" + "cgroups" + ]; + auto-allocate-uids = true; + max-jobs = 2; + cores = 4; + system-features = [ + "kvm" + "benchmark" + "big-parallel" + "nixos-test" + "uid-range" + ]; }; - cores = 6; - }; - nix.settings = { - experimental-features = [ - "auto-allocate-uids" - "cgroups" - ]; - auto-allocate-uids = true; - system-features = [ - "kvm" - "benchmark" - "big-parallel" - "nixos-test" - "uid-range" - ]; }; - }; }; launchd.daemons.linux-builder.environment.QEMU_OPTS = "-cpu host"; diff --git a/nix/packages/setup-darwin-linux-builder.nix b/nix/packages/setup-darwin-linux-builder.nix index 58d1787fb0..b68bdc1d3f 100644 --- a/nix/packages/setup-darwin-linux-builder.nix +++ b/nix/packages/setup-darwin-linux-builder.nix @@ -12,6 +12,11 @@ writeShellApplication { text = '' set -euo pipefail + if [[ $EUID -ne 0 ]]; then + echo "Run with sudo: sudo nix run .#setup-darwin-linux-builder" >&2 + exit 1 + fi + echo "Configuring nix-darwin linux-builder..." echo "" @@ -20,7 +25,7 @@ writeShellApplication { for file in /etc/nix/nix.conf /etc/bashrc /etc/zshrc; do if [[ -f "$file" && ! -L "$file" ]]; then echo " Backing up $file" - sudo mv "$file" "$file.before-nix-darwin" + mv "$file" "$file.before-nix-darwin" fi done echo "" @@ -29,7 +34,7 @@ writeShellApplication { for file in /etc/nix/nix.conf /etc/bashrc /etc/zshrc; do if [[ ! -L "$file" && -f "$file.before-nix-darwin" ]]; then echo " Restoring original $file" - sudo mv "$file.before-nix-darwin" "$file" + mv "$file.before-nix-darwin" "$file" fi done } @@ -37,13 +42,13 @@ writeShellApplication { echo "This will configure your system with:" echo " - NixOS linux-builder VM (ephemeral)" - echo " - 6 cores, 8GB RAM, 40GB disk" + echo " - 8 cores, 24GB RAM, 100GB disk" echo " - Support for x86_64-linux and aarch64-linux builds" echo "" echo "Running darwin-rebuild switch..." echo "" - sudo darwin-rebuild switch --refresh --flake ${self}#darwin-nixostest + darwin-rebuild switch --refresh --flake ${self}#darwin-nixostest echo "" echo "Configuration complete!" From 4451ee798e0e21e0a9b10f1961da9d4e1841b088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Boros?= Date: Sat, 14 Nov 2026 23:25:02 +0200 Subject: [PATCH 3/5] docs(nix): require official builder bootstrap for darwin setup Co-Authored-By: Claude Sonnet 5.5 --- nix/docs/nixos-tests-on-macos.md | 47 ++++++++++++++------------------ 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/nix/docs/nixos-tests-on-macos.md b/nix/docs/nixos-tests-on-macos.md index 58ced59a9b..9e1bf49492 100644 --- a/nix/docs/nixos-tests-on-macos.md +++ b/nix/docs/nixos-tests-on-macos.md @@ -9,16 +9,31 @@ You need: ## Setup -Run the setup script to configure nix-darwin with the linux-builder: +### 1. Bootstrap with the official builder + +The only way to install is to bootstrap with the official builder first. +The builder VM is a Linux system, and building it needs a Linux builder that does not exist yet. +Running setup without this step fails with `platform mismatch, Required system: 'aarch64-linux'`. + +Start the official builder from the directory where you want its `keys/` folder, and leave it running: ```bash -sudo nix run .#setup-darwin-linux-builder +nix run github:NixOS/nixpkgs/nixos-26.05#darwin.linux-builder +``` + +From another terminal in the repository, build the configuration through it: + +```bash +sudo env NIX_SSHOPTS="-p 31022" nix build .#darwinConfigurations.darwin-nixostest.system --no-link \ + --builders "ssh-ng://builder@127.0.0.1 aarch64-linux /builder_ed25519 4 1 kvm,benchmark,big-parallel,nixos-test - " ``` -Note that you don't have to checkout the repository to run the setup, you can run it directly from GitHub: +The host key is the output of `ssh-keyscan -t ed25519 -p 31022 127.0.0.1` without the comment line, fields 2 and 3, base64 encoded. + +### 2. Run setup ```bash -sudo nix run github:supabase/postgres#setup-darwin-linux-builder +sudo nix run .#setup-darwin-linux-builder ``` This command will: @@ -36,29 +51,9 @@ The linux-builder VM is configured with: - `auto-allocate-uids` and `cgroups` enabled inside the VM, so builds that need `uid-range` run - `-cpu host` passed to QEMU, since the default `-cpu max` breaks PAC-built guest binaries on M4 -After setup completes, restart your shell to access the helper commands. - -To re-apply the configuration after changing it, run the same `sudo nix run .#setup-darwin-linux-builder` command. - -### First install - -The builder VM is a Linux system, so the first install needs a Linux builder to build it. -If setup fails with `platform mismatch, Required system: 'aarch64-linux'`, start the stock builder first: - -```bash -nix run github:NixOS/nixpkgs/nixos-26.05#darwin.linux-builder -``` - -Leave it running, then build the configuration through it from another terminal and rerun setup: - -```bash -sudo env NIX_SSHOPTS="-p 31022" nix build .#darwinConfigurations.darwin-nixostest.system --no-link \ - --builders "ssh-ng://builder@127.0.0.1 aarch64-linux $PWD/keys/builder_ed25519 4 1 kvm,benchmark,big-parallel,nixos-test - " -sudo nix run .#setup-darwin-linux-builder -``` +After setup completes, stop the official builder with Ctrl-C and restart your shell to access the helper commands. -Run the builder from the directory that holds its `keys/` folder. -The host key is `ssh-keyscan -t ed25519 -p 31022 127.0.0.1` with the comment line removed, fields 2 and 3, base64 encoded. +To re-apply the configuration after changing it, run step 2 again. Step 1 is only needed on the first install. ## Verify the setup From 4a7e5c5d986074066cf856a445c7c2847e37fa1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Boros?= Date: Mon, 16 Nov 2026 16:40:55 +0200 Subject: [PATCH 4/5] fix(nix): drop qemu pin from darwin builder Co-Authored-By: Claude Sonnet 5.5 --- flake.lock | 17 ------- flake.nix | 2 - nix/docs/nixos-tests-on-macos.md | 5 -- nix/hosts.nix | 2 +- .../darwin-nixostest/darwin-configuration.nix | 50 +++++++++---------- 5 files changed, 24 insertions(+), 52 deletions(-) diff --git a/flake.lock b/flake.lock index 6af9a148e1..e381b678b3 100644 --- a/flake.lock +++ b/flake.lock @@ -293,22 +293,6 @@ "type": "github" } }, - "nixpkgs-qemu10": { - "locked": { - "lastModified": 1794165970, - "narHash": "sha256-XuAV2cUwaod3Bt4XFVqiXz3v1w2ex16W6Sv4S7EWres=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "ac588ef1c914865dad517b337ea0eb9694a4021c", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixos-26.05", - "repo": "nixpkgs", - "type": "github" - } - }, "nixpkgs_2": { "locked": { "lastModified": 1767379071, @@ -336,7 +320,6 @@ "nixpkgs": "nixpkgs_2", "nixpkgs-2211": "nixpkgs-2211", "nixpkgs-oldstable": "nixpkgs-oldstable", - "nixpkgs-qemu10": "nixpkgs-qemu10", "rust-overlay": "rust-overlay", "treefmt-nix": "treefmt-nix" } diff --git a/flake.nix b/flake.nix index 86d2d5f56e..afbb66cd11 100644 --- a/flake.nix +++ b/flake.nix @@ -31,8 +31,6 @@ nixpkgs-2211.url = "https://channels.nixos.org/nixos-22.11/nixexprs.tar.xz"; nixpkgs-2211.flake = false; nixpkgs.url = "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz"; - # unstable's QEMU 11.0.1 aborts in HVF vCPU init on M4 with macOS >= 15.2. 26.05 ships QEMU 10.2. - nixpkgs-qemu10.url = "github:NixOS/nixpkgs/nixos-26.05"; rust-overlay.inputs.nixpkgs.follows = "nixpkgs"; rust-overlay.url = "github:oxalica/rust-overlay"; treefmt-nix.inputs.nixpkgs.follows = "nixpkgs"; diff --git a/nix/docs/nixos-tests-on-macos.md b/nix/docs/nixos-tests-on-macos.md index 9e1bf49492..86dc71d203 100644 --- a/nix/docs/nixos-tests-on-macos.md +++ b/nix/docs/nixos-tests-on-macos.md @@ -172,11 +172,6 @@ start-linux-builder The VM is configured as ephemeral, meaning it's recreated fresh on each start. This ensures a clean environment but requires re-downloading cached build artifacts. -### QEMU aborts on M4 with macOS 15.2 or later - -nixpkgs-unstable's QEMU 11.0.1 aborts during HVF vCPU init on M4. -If your own nix-darwin config manages the builder, point `virtualisation.qemu.package` at `qemu_kvm` from the `nixos-26.05` branch. - ### Slow first build The first NixOS test run may download significant data. diff --git a/nix/hosts.nix b/nix/hosts.nix index 9ace764347..f8f7c394d2 100644 --- a/nix/hosts.nix +++ b/nix/hosts.nix @@ -3,7 +3,7 @@ flake = { darwinConfigurations = { darwin-nixostest = inputs.nix-darwin.lib.darwinSystem { - specialArgs = { inherit self inputs; }; + specialArgs = { inherit self; }; modules = [ ./hosts/darwin-nixostest/darwin-configuration.nix ]; }; }; diff --git a/nix/hosts/darwin-nixostest/darwin-configuration.nix b/nix/hosts/darwin-nixostest/darwin-configuration.nix index 900dad194f..0793655567 100644 --- a/nix/hosts/darwin-nixostest/darwin-configuration.nix +++ b/nix/hosts/darwin-nixostest/darwin-configuration.nix @@ -1,5 +1,4 @@ { - inputs, lib, pkgs, self, @@ -116,34 +115,31 @@ in "nixos-test" "uid-range" ]; - config = - { lib, ... }: - { - virtualisation = { - qemu.package = lib.mkForce inputs.nixpkgs-qemu10.legacyPackages.aarch64-darwin.qemu_kvm; - darwin-builder = { - diskSize = 100 * 1024; - memorySize = 24 * 1024; - }; - cores = 8; - }; - nix.settings = { - experimental-features = [ - "auto-allocate-uids" - "cgroups" - ]; - auto-allocate-uids = true; - max-jobs = 2; - cores = 4; - system-features = [ - "kvm" - "benchmark" - "big-parallel" - "nixos-test" - "uid-range" - ]; + config = { + virtualisation = { + darwin-builder = { + diskSize = 100 * 1024; + memorySize = 24 * 1024; }; + cores = 8; + }; + nix.settings = { + experimental-features = [ + "auto-allocate-uids" + "cgroups" + ]; + auto-allocate-uids = true; + max-jobs = 2; + cores = 4; + system-features = [ + "kvm" + "benchmark" + "big-parallel" + "nixos-test" + "uid-range" + ]; }; + }; }; launchd.daemons.linux-builder.environment.QEMU_OPTS = "-cpu host"; From 31752413378b789d320483fdaf6621cf4f754a36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Boros?= Date: Mon, 16 Nov 2026 18:21:40 +0200 Subject: [PATCH 5/5] feat(nix): bootstrap official linux-builder in darwin setup Co-Authored-By: Claude Sonnet 5.5 --- nix/docs/nixos-tests-on-macos.md | 32 +++---------- nix/packages/setup-darwin-linux-builder.nix | 53 ++++++++++++++++++++- 2 files changed, 59 insertions(+), 26 deletions(-) diff --git a/nix/docs/nixos-tests-on-macos.md b/nix/docs/nixos-tests-on-macos.md index 86dc71d203..a00bd5051b 100644 --- a/nix/docs/nixos-tests-on-macos.md +++ b/nix/docs/nixos-tests-on-macos.md @@ -9,34 +9,16 @@ You need: ## Setup -### 1. Bootstrap with the official builder - -The only way to install is to bootstrap with the official builder first. -The builder VM is a Linux system, and building it needs a Linux builder that does not exist yet. -Running setup without this step fails with `platform mismatch, Required system: 'aarch64-linux'`. - -Start the official builder from the directory where you want its `keys/` folder, and leave it running: - -```bash -nix run github:NixOS/nixpkgs/nixos-26.05#darwin.linux-builder -``` - -From another terminal in the repository, build the configuration through it: - -```bash -sudo env NIX_SSHOPTS="-p 31022" nix build .#darwinConfigurations.darwin-nixostest.system --no-link \ - --builders "ssh-ng://builder@127.0.0.1 aarch64-linux /builder_ed25519 4 1 kvm,benchmark,big-parallel,nixos-test - " -``` - -The host key is the output of `ssh-keyscan -t ed25519 -p 31022 127.0.0.1` without the comment line, fields 2 and 3, base64 encoded. - -### 2. Run setup +Run the setup script to configure nix-darwin with the linux-builder: ```bash sudo nix run .#setup-darwin-linux-builder ``` -This command will: +The builder VM is a Linux system, and building it needs a Linux builder. +If none is configured, the script first bootstraps with the official `nixos-26.05#darwin.linux-builder`, builds the configuration through it, and stops it before switching. + +The script will: - Back up existing system files (`/etc/nix/nix.conf`, `/etc/bashrc`, `/etc/zshrc`) - Configure nix-darwin with the linux-builder VM - Install helper scripts for managing the builder @@ -51,9 +33,9 @@ The linux-builder VM is configured with: - `auto-allocate-uids` and `cgroups` enabled inside the VM, so builds that need `uid-range` run - `-cpu host` passed to QEMU, since the default `-cpu max` breaks PAC-built guest binaries on M4 -After setup completes, stop the official builder with Ctrl-C and restart your shell to access the helper commands. +After setup completes, restart your shell to access the helper commands. -To re-apply the configuration after changing it, run step 2 again. Step 1 is only needed on the first install. +To re-apply the configuration after changing it, run the same command. The bootstrap is skipped once a builder exists. ## Verify the setup diff --git a/nix/packages/setup-darwin-linux-builder.nix b/nix/packages/setup-darwin-linux-builder.nix index b68bdc1d3f..a4548a4b60 100644 --- a/nix/packages/setup-darwin-linux-builder.nix +++ b/nix/packages/setup-darwin-linux-builder.nix @@ -1,12 +1,20 @@ { inputs, self, + coreutils, + gawk, + gnugrep, + openssh, stdenv, writeShellApplication, }: writeShellApplication { name = "setup-darwin-linux-builder"; runtimeInputs = [ + coreutils + gawk + gnugrep + openssh inputs.nix-darwin.packages.${stdenv.hostPlatform.system}.darwin-rebuild ]; text = '' @@ -17,6 +25,49 @@ writeShellApplication { exit 1 fi + BOOTSTRAP_PID="" + stop_bootstrap() { + if [[ -n "$BOOTSTRAP_PID" ]]; then + kill -- "-$BOOTSTRAP_PID" 2>/dev/null || true + wait "$BOOTSTRAP_PID" 2>/dev/null || true + BOOTSTRAP_PID="" + fi + } + trap stop_bootstrap EXIT + + if ! grep -qs aarch64-linux /etc/nix/machines; then + echo "No Linux builder found. Bootstrapping with the official builder..." + echo "" + workdir=$(mktemp -d) + cd "$workdir" + set -m + nix run github:NixOS/nixpkgs/nixos-26.05#darwin.linux-builder \ + >"$workdir/builder.log" 2>&1 /dev/null | + awk '!/^#/ {printf "%s %s", $2, $3}' | base64 | tr -d '\n') || true + [[ -n "$hostkey" && -f "$workdir/keys/builder_ed25519" ]] && break + hostkey="" + sleep 1 + done + if [[ -z "$hostkey" ]]; then + echo "Official builder did not start. Log: $workdir/builder.log" >&2 + exit 1 + fi + + echo "Building the configuration on the official builder..." + NIX_SSHOPTS="-p 31022" nix build \ + "${self}#darwinConfigurations.darwin-nixostest.system" --no-link \ + --builders "ssh-ng://builder@127.0.0.1 aarch64-linux $workdir/keys/builder_ed25519 4 1 kvm,benchmark,big-parallel,nixos-test - $hostkey" + stop_bootstrap + cd / + echo "" + fi + echo "Configuring nix-darwin linux-builder..." echo "" @@ -38,7 +89,7 @@ writeShellApplication { fi done } - trap revert ERR SIGINT SIGTERM + trap 'stop_bootstrap; revert' ERR SIGINT SIGTERM echo "This will configure your system with:" echo " - NixOS linux-builder VM (ephemeral)"