nixos-config/lib/morph.nix

126 lines
3.9 KiB
Nix
Raw Normal View History

2021-09-10 03:46:13 +00:00
{ nixpkgs
, nixosConfigurations
, path
, flake
, extraMorphModules ? [ ]
}:
{
morph-entrypoint = system:
let
globalHealthChecks.cmd = [
{
cmd = [ "nixos-check-reboot" ];
description = "Check for pending reboot";
}
{
cmd = [ "systemctl is-system-running" ];
description = "Check services are running";
}
];
getConfig = name: value: { ... }: {
imports = extraMorphModules ++ nixosConfigurations.${name}.extraArgs.modules;
config = nixpkgs.lib.mkMerge [
{ inherit (value.config.sconfig.morph) deployment; }
2021-09-10 03:46:13 +00:00
{ deployment.healthChecks = globalHealthChecks; }
];
};
in
{ network.pkgs = nixpkgs.legacyPackages.${system}; } //
builtins.mapAttrs getConfig nixosConfigurations;
packages = system:
let
pkgs = nixpkgs.legacyPackages.${system};
inherit (nixpkgs.lib) concatMapStrings;
sh = (scriptBody: pkgs.writeShellScriptBin "run" ''
set -e
die() { echo "$*" 1>&2 ; exit 1; }
${scriptBody}
'');
sshKnownHostsTxt = pkgs.writeText "known_hosts" (concatMapStrings
(hostName:
let m = nixosConfigurations.${hostName}.config.sconfig.morph;
2021-09-10 03:46:13 +00:00
in concatMapStrings (key: "${m.deployment.targetHost} ${key}\n") m.sshPublicKeys
)
(builtins.attrNames nixosConfigurations)
);
sshConfig = pkgs.writeText "ssh_config" ''
2021-10-19 18:17:58 +00:00
Host *
User root
2021-09-10 03:46:13 +00:00
StrictHostKeyChecking yes
GlobalKnownHostsFile ${sshKnownHostsTxt}
'';
in
rec {
morph-config = nixpkgs.legacyPackages.${system}.writeText "morph.nix" ''
let flake = builtins.getFlake "${flake}";
in flake.morph-entrypoint builtins.currentSystem
'';
morph = pkgs.runCommand "morph" { } ''
mkdir -p $out/bin
. ${pkgs.makeWrapper}/nix-support/setup-hook
makeWrapper ${pkgs.morph}/bin/morph $out/bin/morph \
--set SSH_CONFIG_FILE ${sshConfig}
'';
deploy = sh ''
exec "${morph}/bin/morph" deploy ${morph-config} "$@"
'';
2021-10-29 18:01:14 +00:00
livecd-deploy = sh ''
config=".#nixosConfigurations.\"$1\".config"
nix build "$config.system.build.toplevel" --out-link "$(mktemp -d)/result"
sys="$(nix eval --raw "$config.system.build.toplevel")"
ip="$(nix eval --raw "$config.sconfig.morph.deployment.targetHost")"
nix copy --to ssh://root@$ip?remote-store=local?root=/mnt "$sys"
ssh root@$ip nix-env --store /mnt -p /mnt/nix/var/nix/profiles/system --set "$sys"
ssh root@$ip mkdir /mnt/etc
ssh root@$ip touch /mnt/etc/NIXOS
ssh root@$ip ln -sfn /proc/mounts /mnt/etc/mtab
ssh root@$ip NIXOS_INSTALL_BOOTLOADER=1 nixos-enter --root /mnt -- /run/current-system/bin/switch-to-configuration boot
'';
2021-10-29 23:00:41 +00:00
proxy = sh ''
export SSH_CONFIG_FILE="${sshConfig}"
ip="$(nix eval --raw ".#nixosConfigurations.\"$1\".config.sconfig.morph.deployment.targetHost")"
shift
set -- nix run "$@"
nix copy --to ssh://$ip "${flake}"
exec ssh -t -oForwardAgent=yes "$ip" "cd ${flake}; $@"
'';
2021-09-10 03:46:13 +00:00
push = sh ''
exec "${morph}/bin/morph" push ${morph-config} "$@"
'';
ssh = sh ''
2021-11-15 17:04:48 +00:00
ip="$(nix eval --raw ".#nixosConfigurations.\"$1\".config.sconfig.morph.deployment.targetHost")"
2021-09-10 03:46:13 +00:00
shift
exec ssh -F"${sshConfig}" "$ip" "$@"
'';
check = sh ''
res="$( ${morph}/bin/morph build ${morph-config} )"
${morph}/bin/morph check-health ${morph-config}
echo -e "\nUpdate checks:"
for hostname in $( ls "$res" ); do
echo " $hostname"
diff \
<('${ssh}/bin/run' "$hostname" readlink /run/current-system) \
<(readlink "$res/$hostname") || true
done
'';
};
}