Skip to content
Open
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
10 changes: 5 additions & 5 deletions .github/workflows/nix-eval.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ jobs:
steps:
- name: Checkout Repo
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
- name: Mount Nix cache disk
uses: useblacksmith/stickydisk@a652394bf1bf95399f406e648482b41fbd25c51f # v1
with:
key: ${{ github.repository }}-nix-cache-eval-${{ runner.os }}
path: /nix
- name: Install nix
uses: ./.github/actions/nix-install-ephemeral
with:
Expand All @@ -34,11 +39,6 @@ jobs:
- name: Restart Nix Daemon
run: |
sudo mv /nix/var/nix/daemon-socket/socket /tmp
- name: Mount Nix cache disk
uses: useblacksmith/stickydisk@a652394bf1bf95399f406e648482b41fbd25c51f # v1
with:
key: ${{ github.repository }}-nix-cache-eval-${{ runner.os }}
path: /nix
- name: Restart Nix Daemon
run: |
sudo systemctl restart nix-daemon.service nix-daemon.socket
Expand Down
53 changes: 44 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
flake-utils.url = "github:numtide/flake-utils";
git-hooks.inputs.nixpkgs.follows = "nixpkgs";
git-hooks.url = "github:cachix/git-hooks.nix";
nix-darwin.url = "github:nix-darwin/nix-darwin";
nix-editor.inputs.nixpkgs.follows = "nixpkgs";
nix-editor.inputs.utils.follows = "flake-utils";
nix-editor.url = "github:snowfallorg/nix-editor";
Expand Down Expand Up @@ -45,6 +46,7 @@
nix/devShells.nix
nix/fmt.nix
nix/hooks.nix
nix/hosts.nix
nix/nixpkgs.nix
nix/packages
nix/overlays
Expand Down
10 changes: 10 additions & 0 deletions nix/hosts.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{ inputs, ... }:
{
flake = {
darwinConfigurations = {
darwin-nixostest = inputs.nix-darwin.lib.darwinSystem {
modules = [ ./hosts/darwin-nixostest/darwin-configuration.nix ];
};
};
};
}
128 changes: 128 additions & 0 deletions nix/hosts/darwin-nixostest/darwin-configuration.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{ lib, pkgs, ... }:
let
start-linux-builder = pkgs.writeShellApplication {
name = "start-linux-builder";
text = ''
echo "Starting linux-builder..."

if sudo launchctl list | grep -q org.nixos.linux-builder; then
echo "linux-builder is already running"
exit 0
fi

# Use load instead of start to re-enable the service
if sudo launchctl load -w /Library/LaunchDaemons/org.nixos.linux-builder.plist 2>/dev/null; then
echo "linux-builder started successfully"
else
echo "Error: Could not start linux-builder"
echo "Make sure nix-darwin is configured with linux-builder enabled"
exit 1
fi

# Check if it's running
sleep 2
if sudo launchctl list | grep -q org.nixos.linux-builder; then
echo "linux-builder is now running"
else
echo "Warning: linux-builder may not have started properly"
fi
'';
};
stop-linux-builder = pkgs.writeShellApplication {
name = "stop-linux-builder";
text = ''
echo "Stopping linux-builder..."

# Use unload instead of stop because KeepAlive=true will restart it
if sudo launchctl unload -w /Library/LaunchDaemons/org.nixos.linux-builder.plist 2>/dev/null; then
echo "linux-builder stopped successfully"
else
echo "Warning: Could not stop linux-builder (it may not be running)"
fi

# Check if it's still running
sleep 1
if sudo launchctl list | grep -q org.nixos.linux-builder; then
echo "Warning: linux-builder is still running"
STATUS=$(sudo launchctl list | grep org.nixos.linux-builder || true)
echo "Current status: $STATUS"
else
echo "linux-builder is not running"
fi
'';
};
in
{
nixpkgs.hostPlatform = "aarch64-darwin";

# Install builder control scripts
environment.systemPackages = [
start-linux-builder
stop-linux-builder
];

nix.settings = {
experimental-features = [
"nix-command"
"flakes"
];
always-allow-substitutes = true;
max-jobs = "auto";
trusted-users = [ "@admin" ];
extra-substituters = [ "https://nix-postgres-artifacts.s3.amazonaws.com" ];
extra-trusted-substituters = [ "https://nix-postgres-artifacts.s3.amazonaws.com" ];
extra-trusted-public-keys = [
"nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI="
];
};

nix.extraOptions = ''
!include nix.custom.conf
'';

# accept existing nix.custom.conf
system.activationScripts.checks.text = lib.mkForce "";
system.activationScripts.nix-daemon.text = lib.mkForce ''
if ! diff /etc/nix/nix.conf /run/current-system/etc/nix/nix.conf &> /dev/null || ! diff /etc/nix/machines /run/current-system/etc/nix/machines &> /dev/null; then
echo "reloading nix-daemon..." >&2
launchctl kill HUP system/org.nixos.nix-daemon
fi
max_wait=30
waited=0
while ! nix-store --store daemon -q --hash ${pkgs.stdenv.shell} &>/dev/null; do
if [ $waited -ge $max_wait ]; then
echo "ERROR: nix-daemon failed to start after $max_wait seconds" >&2
exit 1
fi
echo "waiting for nix-daemon" >&2
launchctl kickstart system/org.nixos.nix-daemon
sleep 1
waited=$((waited + 1))
done
'';

nix.linux-builder = {
enable = true;
ephemeral = true;
maxJobs = 4;
supportedFeatures = [
"kvm"
"benchmark"
"big-parallel"
"nixos-test"
];
config = {
virtualisation = {
darwin-builder = {
diskSize = 40 * 1024;
memorySize = 8 * 1024;
};
cores = 6;
};
};
};

nix.distributedBuilds = true;

system.stateVersion = 6;
}
3 changes: 3 additions & 0 deletions nix/packages/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
cargo-pgrx_0_14_3
;
}
// lib.optionalAttrs pkgs.stdenv.isDarwin {
setup-darwin-linux-builder = pkgs.callPackage ./setup-darwin-linux-builder.nix { inherit inputs; };
}
// lib.filterAttrs (n: _v: n != "override" && n != "overrideAttrs" && n != "overrideDerivation") (
pkgs.callPackage ../postgresql/default.nix {
inherit self';
Expand Down
60 changes: 60 additions & 0 deletions nix/packages/setup-darwin-linux-builder.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
inputs,
stdenv,
writeShellApplication,
}:
writeShellApplication {
name = "setup-darwin-linux-builder";
runtimeInputs = [
inputs.nix-darwin.packages.${stdenv.hostPlatform.system}.darwin-rebuild
];
text = ''
set -euo pipefail

echo "Configuring nix-darwin linux-builder..."
echo ""

# Backup files that nix-darwin will manage
echo "Preparing for nix-darwin..."
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"
fi
done
echo ""

revert() {
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"
fi
done
Comment on lines +27 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Rollback won’t restore backups after nix-darwin symlinks files.

revert skips restoration when the target is a symlink, but darwin-rebuild typically creates symlinks in /etc. On failure, the originals remain stranded and the symlinked config persists.

🔧 Proposed fix
-        if [[ ! -L "$file" && -f "$file.before-nix-darwin" ]]; then
-          echo "  Restoring original $file"
-          sudo mv "$file.before-nix-darwin" "$file" 
+        if [[ -f "$file.before-nix-darwin" ]]; then
+          echo "  Restoring original $file"
+          sudo rm -f "$file"
+          sudo mv "$file.before-nix-darwin" "$file"
         fi
🤖 Prompt for AI Agents
In `@nix/packages/setup-darwin-linux-builder.nix` around lines 27 - 33, The
revert() function currently skips restoration when the target file is a symlink;
update revert() so it restores backups even if /etc files are symlinks: for each
file in the loop, check if the backup "$file.before-nix-darwin" exists ( -f )
and if the target is a symlink ( -L ), remove the symlink (sudo rm "$file")
before moving the backup into place (sudo mv "$file.before-nix-darwin" "$file");
keep the existing branch for non-symlink files but ensure both cases handle
presence of the backup and use sudo for removals/moves to avoid permission
issues in the revert() function.

}
trap 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 " - Support for x86_64-linux and aarch64-linux builds"
echo ""
echo "Running darwin-rebuild switch..."
echo ""

sudo darwin-rebuild switch --refresh --flake github:supabase/postgres/darwin-linux-builder#darwin-nixostest

echo ""
echo "Configuration complete!"
echo ""
echo "The linux-builder is now configured. You can test it by:"
echo " nix build --system x86_64-linux nixpkgs#hello"
echo " nix build --system aarch64-linux nixpkgs#hello"
echo ""
echo "To control the linux builder vm, you can use:"
echo " stop-linux-builder # to stop the linux builder vm"
echo " start-linux-builder # to start the linux builder vm"
echo ""
echo "If this is the first install, you may need to restart your shell to use these scripts."
'';
}
Loading