From 9fa94af7140afb7c9fcced8aa1fb20abb81c5955 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sun, 2 Nov 2025 14:20:57 -0800 Subject: add helpers to build remotely with nixos --- flake/scripts/default.nix | 4 +- flake/scripts/remote.nix | 134 ++++++++++++++++++++++++++++++ machines/darwin/aarch64-darwin/mba-m2.nix | 2 +- profiles/defaults.nix | 1 + profiles/nix/remote-builder.nix | 32 ------- profiles/remote-builder.nix | 32 +++++++ 6 files changed, 171 insertions(+), 34 deletions(-) create mode 100644 flake/scripts/remote.nix delete mode 100644 profiles/nix/remote-builder.nix create mode 100644 profiles/remote-builder.nix diff --git a/flake/scripts/default.nix b/flake/scripts/default.nix index db6febe..6b16e75 100644 --- a/flake/scripts/default.nix +++ b/flake/scripts/default.nix @@ -6,10 +6,12 @@ let common = import ./common.nix { inherit pkgs; }; darwin = import ./darwin.nix { inherit pkgs system inputs; }; + remote = import ./remote.nix { inherit pkgs system inputs; }; in { common = common; + remote = remote; darwin = if pkgs.lib.hasSuffix "darwin" system then darwin else [ ]; - all = common ++ (if pkgs.lib.hasSuffix "darwin" system then darwin else [ ]); + all = common ++ remote ++ (if pkgs.lib.hasSuffix "darwin" system then darwin else [ ]); } diff --git a/flake/scripts/remote.nix b/flake/scripts/remote.nix new file mode 100644 index 0000000..c96aa81 --- /dev/null +++ b/flake/scripts/remote.nix @@ -0,0 +1,134 @@ +{ + pkgs, +}: +[ + (pkgs.writeScriptBin "rbuild" '' + #!${pkgs.bash}/bin/bash + set -e + + # Check if host argument is provided + if [ -z "$1" ]; then + echo "❌ Error: Please specify a host" + echo "Usage: rbuild " + echo "Example: rbuild rivendell" + exit 1 + fi + + HOST="$1" + echo "> Running nixos-rebuild build for $HOST..." + + ${pkgs.nixos-rebuild}/bin/nixos-rebuild build \ + --keep-going \ + --flake ".#$HOST" \ + --target-host "$HOST" \ + --fast \ + --use-remote-sudo \ + --use-substitutes + + echo "> nixos-rebuild build for $HOST was successful ✅" + '') + + (pkgs.writeScriptBin "rswitch" '' + #!${pkgs.bash}/bin/bash + set -e + + # Check if host argument is provided + if [ -z "$1" ]; then + echo "❌ Error: Please specify a host" + echo "Usage: rswitch " + echo "Example: rswitch rivendell" + exit 1 + fi + + HOST="$1" + echo "> Running nixos-rebuild switch for $HOST..." + + ${pkgs.nixos-rebuild}/bin/nixos-rebuild switch \ + --keep-going \ + --flake ".#$HOST" \ + --target-host "$HOST" \ + --fast \ + --use-remote-sudo \ + --use-substitutes + + echo "> NixOS config was successfully applied to $HOST 🚀" + '') + + (pkgs.writeScriptBin "rdeploy" '' + #!${pkgs.bash}/bin/bash + set -e + + # Check if host argument is provided + if [ -z "$1" ]; then + echo "❌ Error: Please specify a host" + echo "Usage: rdeploy " + echo "Example: rdeploy rivendell" + exit 1 + fi + + HOST="$1" + echo "> Deploying NixOS configuration to $HOST..." + echo "" + + # First build + echo "📦 Step 1/2: Building configuration..." + ${pkgs.nixos-rebuild}/bin/nixos-rebuild build \ + --keep-going \ + --flake ".#$HOST" \ + --target-host "$HOST" \ + --fast \ + --use-remote-sudo \ + --use-substitutes + + echo "" + echo "🔄 Step 2/2: Switching configuration..." + ${pkgs.nixos-rebuild}/bin/nixos-rebuild switch \ + --keep-going \ + --flake ".#$HOST" \ + --target-host "$HOST" \ + --fast \ + --use-remote-sudo \ + --use-substitutes + + echo "" + echo "> NixOS deployment to $HOST completed successfully! 🎉" + '') + + (pkgs.writeScriptBin "rhosts" '' + #!${pkgs.bash}/bin/bash + echo "> Available NixOS hosts in your flake:" + echo "" + + # This attempts to list nixosConfigurations from the flake + # You might need to adjust this based on your flake structure + nix flake show --json 2>/dev/null | \ + ${pkgs.jq}/bin/jq -r '.nixosConfigurations | keys[]' 2>/dev/null || \ + echo "Unable to list hosts automatically. Check your flake/hosts.nix" + '') + + (pkgs.writeScriptBin "rtest" '' + #!${pkgs.bash}/bin/bash + set -e + + # Check if host argument is provided + if [ -z "$1" ]; then + echo "❌ Error: Please specify a host" + echo "Usage: rtest " + echo "Example: rtest rivendell" + exit 1 + fi + + HOST="$1" + echo "> Running dry-run build for $HOST..." + + ${pkgs.nixos-rebuild}/bin/nixos-rebuild dry-build \ + --keep-going \ + --flake ".#$HOST" \ + --target-host "$HOST" \ + --fast \ + --use-remote-sudo \ + --use-substitutes + + echo "> Dry-run build for $HOST completed ✅" + '') +] diff --git a/machines/darwin/aarch64-darwin/mba-m2.nix b/machines/darwin/aarch64-darwin/mba-m2.nix index 943ccf0..d4c444a 100644 --- a/machines/darwin/aarch64-darwin/mba-m2.nix +++ b/machines/darwin/aarch64-darwin/mba-m2.nix @@ -11,7 +11,7 @@ imports = [ ../../../profiles/darwin.nix ../../../profiles/home-manager.nix - ../../../profiles/nix/remote-builder.nix + ../../../profiles/remote-builder.nix ]; # https://github.com/nix-darwin/nix-darwin/issues/1339 diff --git a/profiles/defaults.nix b/profiles/defaults.nix index 80292b6..573bf68 100644 --- a/profiles/defaults.nix +++ b/profiles/defaults.nix @@ -103,6 +103,7 @@ security.sudo.wheelNeedsPassword = false; environment.systemPackages = with pkgs; [ + bottom curl dysk fd diff --git a/profiles/nix/remote-builder.nix b/profiles/nix/remote-builder.nix deleted file mode 100644 index 50d3e84..0000000 --- a/profiles/nix/remote-builder.nix +++ /dev/null @@ -1,32 +0,0 @@ -{ config, ... }: -{ - nix.buildMachines = [ - { - hostName = "builder"; - sshUser = "builder"; - - protocol = "ssh"; - - sshKey = config.age.secrets.ssh-remote-builder.path; - - systems = [ - "x86_64-linux" - ]; - - maxJobs = 1; - - supportedFeatures = [ - "nixos-test" - ]; - } - ]; - - nix.distributedBuilds = true; - - programs.ssh.extraConfig = '' - Host builder - User builder - HostName vm-synology - IdentityFile ${config.age.secrets.ssh-remote-builder.path} - ''; -} diff --git a/profiles/remote-builder.nix b/profiles/remote-builder.nix new file mode 100644 index 0000000..3aa772f --- /dev/null +++ b/profiles/remote-builder.nix @@ -0,0 +1,32 @@ +{ config, ... }: +{ + nix.buildMachines = [ + { + hostName = "builder"; + sshUser = "builder"; + + protocol = "ssh"; + + sshKey = config.age.secrets.ssh-remote-builder.path; + + systems = [ + "x86_64-linux" + ]; + + maxJobs = 4; + + supportedFeatures = [ + "nixos-test" + ]; + } + ]; + + nix.distributedBuilds = true; + + programs.ssh.extraConfig = '' + Host builder + User builder + HostName rivendell + IdentityFile ${config.age.secrets.ssh-remote-builder.path} + ''; +} -- cgit v1.2.3