Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions nix/docs/nixos-tests-on-macos.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,31 @@ 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:
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.

```bash
nix run github:supabase/postgres#setup-darwin-linux-builder
```

This command will:
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

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` 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 command. The bootstrap is skipped once a builder exists.

## Verify the setup

The setup script runs verification automatically after configuration.
Expand Down
27 changes: 23 additions & 4 deletions nix/hosts/darwin-nixostest/darwin-configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,43 @@ in
nix.linux-builder = {
enable = true;
ephemeral = true;
maxJobs = 4;
maxJobs = 2;
supportedFeatures = [
"kvm"
"benchmark"
"big-parallel"
"nixos-test"
"uid-range"
];
config = {
virtualisation = {
darwin-builder = {
diskSize = 40 * 1024;
memorySize = 8 * 1024;
diskSize = 100 * 1024;
memorySize = 24 * 1024;
};
cores = 6;
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";

nix.distributedBuilds = true;

system.stateVersion = 6;
Expand Down
66 changes: 61 additions & 5 deletions nix/packages/setup-darwin-linux-builder.nix
Original file line number Diff line number Diff line change
@@ -1,17 +1,73 @@
{
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 = ''
set -euo pipefail

if [[ $EUID -ne 0 ]]; then
echo "Run with sudo: sudo nix run .#setup-darwin-linux-builder" >&2
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 &
BOOTSTRAP_PID=$!
set +m

hostkey=""
for _ in $(seq 1 300); do
hostkey=$(ssh-keyscan -t ed25519 -p 31022 127.0.0.1 2>/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 ""

Expand All @@ -20,7 +76,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 ""
Expand All @@ -29,21 +85,21 @@ 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
}
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)"
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!"
Expand Down
Loading