diff options
| author | Max Audron <audron@cocaine.farm> | 2023-08-11 16:51:35 +0200 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2023-08-11 16:51:35 +0200 |
| commit | 8f7c59e10a48c24120dd580b196acd419e5875d0 (patch) | |
| tree | 57a470c9e726b0e09aede6c8470467f790d77dc8 /nixinate | |
| parent | refactor k8s module (diff) | |
add custom nixinate impl
Diffstat (limited to 'nixinate')
| -rw-r--r-- | nixinate/default.nix | 18 | ||||
| -rw-r--r-- | nixinate/generate-apps.nix | 32 | ||||
| -rw-r--r-- | nixinate/make-deploy-script.nix | 45 |
3 files changed, 95 insertions, 0 deletions
diff --git a/nixinate/default.nix b/nixinate/default.nix new file mode 100644 index 0000000..26cd4d4 --- /dev/null +++ b/nixinate/default.nix @@ -0,0 +1,18 @@ +# The importApply argument. Use this to reference things defined locally, +# as opposed to the flake where this is imported. +localFlake: + +# Regular module arguments; self, inputs, etc all reference the final user flake, +# where this module was imported. +{ lib, config, self, inputs, ... }: +let + lib = inputs.nixpkgs.lib; + generateApps = import ./generate-apps.nix inputs.nixpkgs; +in +{ + flake = { + }; + perSystem = { system, pkgs, ... }: { + apps = generateApps pkgs self; + }; +} diff --git a/nixinate/generate-apps.nix b/nixinate/generate-apps.nix new file mode 100644 index 0000000..6871d04 --- /dev/null +++ b/nixinate/generate-apps.nix @@ -0,0 +1,32 @@ +nixpkgs: pkgs: flake: + +let + machines = builtins.attrNames flake.nixosConfigurations; + validMachines = nixpkgs.lib.remove "" + (nixpkgs.lib.forEach machines + (x: nixpkgs.lib.optionalString + (flake.nixosConfigurations."${x}"._module.args ? nixinate) "${x}")); + mkDeployScript = import ./make-deploy-script.nix { inherit nixpkgs pkgs flake; }; +in +nixpkgs.lib.genAttrs + validMachines + (x: + { + type = "app"; + program = toString (mkDeployScript { + machine = x; + dryRun = false; + }); + } + ) + // nixpkgs.lib.genAttrs + (map (a: a + "-dry-run") validMachines) + (x: + { + type = "app"; + program = toString (mkDeployScript { + machine = nixpkgs.lib.removeSuffix "-dry-run" x; + dryRun = true; + }); + } + ) diff --git a/nixinate/make-deploy-script.nix b/nixinate/make-deploy-script.nix new file mode 100644 index 0000000..ab128b3 --- /dev/null +++ b/nixinate/make-deploy-script.nix @@ -0,0 +1,45 @@ +{ nixpkgs, pkgs, flake, ... }: +{ machine, dryRun }: +let + inherit (builtins) abort; + inherit (pkgs.lib) getExe optionalString concatStringsSep; + + nix = "${getExe pkgs.nix}"; + nixos-rebuild = "${getExe pkgs.nixos-rebuild}"; + openssh = "${getExe pkgs.openssh}"; + flock = "${getExe pkgs.flock}"; + + n = flake.nixosConfigurations.${machine}._module.args.nixinate; + hermetic = n.hermetic or true; + user = n.sshUser or "root"; + host = n.host; + where = n.buildOn or "remote"; + remote = if where == "remote" then true else if where == "local" then false else abort "_module.args.nixinate.buildOn is not set to a valid value of 'local' or 'remote'"; + substituteOnTarget = n.substituteOnTarget or false; + switch = if dryRun then "dry-activate" else "switch"; + nixOptions = concatStringsSep " " (n.nixOptions or [ ]); + + script = + '' + set -e + echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}" + echo "👤 SSH User: ${user}" + echo "🌐 SSH Host: ${host}" + '' + (if remote then '' + echo "🚀 Sending flake to ${machine} via nix copy:" + ( set -x; ${nix} ${nixOptions} copy ${flake} --to ssh://${user}@${host} ) + '' + (if hermetic then '' + echo "🤞 Activating configuration hermetically on ${machine} via ssh:" + ( set -x; ${nix} ${nixOptions} copy --derivation ${nixos-rebuild} ${flock} --to ssh://${user}@${host} ) + ( set -x; ${openssh} -t ${user}@${host} "sudo nix-store --realise ${nixos-rebuild} ${flock} && sudo ${flock} -w 60 /dev/shm/nixinate-${machine} ${nixos-rebuild} ${nixOptions} ${switch} --flake ${flake}#${machine}" ) + '' else '' + echo "🤞 Activating configuration non-hermetically on ${machine} via ssh:" + ( set -x; ${openssh} -t ${user}@${host} "sudo flock -w 60 /dev/shm/nixinate-${machine} nixos-rebuild ${switch} --flake ${flake}#${machine}" ) + '') + else '' + echo "🔨 Building system closure locally, copying it to remote store and activating it:" + ( set -x; NIX_SSHOPTS="-t" ${flock} -w 60 /dev/shm/nixinate-${machine} ${nixos-rebuild} ${nixOptions} ${switch} --flake ${flake}#${machine} --target-host ${user}@${host} --use-remote-sudo ${optionalString substituteOnTarget "-s"} ) + + ''); +in +pkgs.writeScript "deploy-${machine}.sh" script |
