2021-12-15 06:25:58 +00:00
|
|
|
{ self
|
2021-09-10 03:46:13 +00:00
|
|
|
, extraMorphModules ? [ ]
|
|
|
|
}:
|
|
|
|
|
2021-12-06 23:42:54 +00:00
|
|
|
# to use this library, add the following to "morph.nix" in your repo:
|
|
|
|
# (builtins.getFlake (toString ./.)).morph-entrypoint builtins.currentSystem
|
|
|
|
|
|
|
|
let
|
2021-12-15 06:25:58 +00:00
|
|
|
inherit (self.inputs) nixpkgs;
|
|
|
|
inherit (self) nixosConfigurations;
|
|
|
|
|
2021-12-06 23:42:54 +00:00
|
|
|
helpers = system:
|
|
|
|
let
|
|
|
|
pkgs = nixpkgs.legacyPackages.${system};
|
|
|
|
inherit (nixpkgs.lib) concatMapStrings;
|
|
|
|
|
|
|
|
sshKnownHostsTxt = pkgs.writeText "known_hosts" (concatMapStrings
|
|
|
|
(hostName:
|
|
|
|
let m = nixosConfigurations.${hostName}.config.sconfig.morph;
|
|
|
|
in concatMapStrings (key: "${m.deployment.targetHost} ${key}\n") m.sshPublicKeys
|
|
|
|
)
|
|
|
|
(builtins.attrNames nixosConfigurations)
|
|
|
|
);
|
|
|
|
|
2021-12-21 05:39:24 +00:00
|
|
|
hostSshConfigs = concatMapStrings
|
|
|
|
(hostName: ''
|
|
|
|
Host ${hostName}
|
|
|
|
HostName ${nixosConfigurations.${hostName}.config.sconfig.morph.deployment.targetHost}
|
|
|
|
'')
|
|
|
|
(builtins.attrNames nixosConfigurations);
|
|
|
|
|
2021-12-06 23:42:54 +00:00
|
|
|
sshConfig = pkgs.writeText "ssh_config" ''
|
|
|
|
StrictHostKeyChecking yes
|
|
|
|
GlobalKnownHostsFile ${sshKnownHostsTxt}
|
2021-12-21 05:39:24 +00:00
|
|
|
${hostSshConfigs}
|
2021-12-21 15:06:06 +00:00
|
|
|
Host *
|
|
|
|
User root
|
2021-12-06 23:42:54 +00:00
|
|
|
'';
|
|
|
|
|
2021-12-21 06:41:01 +00:00
|
|
|
jump = pkgs.writeShellScript "jump" ''
|
|
|
|
set -eu
|
|
|
|
echo ${self}
|
|
|
|
ip="$(nix eval --raw ".#nixosConfigurations.\"$1\".config.sconfig.morph.deployment.targetHost")"
|
|
|
|
NIX_SSHOPTS="-F${sshConfig}" nix copy --to ssh://root@$ip ${self}
|
|
|
|
exec ssh -oForwardAgent=yes -F"${sshConfig}" "root@$ip" -t "cd ${self}; nix develop"
|
|
|
|
'';
|
|
|
|
|
2021-12-21 06:47:07 +00:00
|
|
|
check-updates = pkgs.writeShellScript "check-updates" ''
|
|
|
|
set -eu
|
|
|
|
export SSH_CONFIG_FILE=${sshConfig}
|
|
|
|
res="$(morph build morph.nix)"
|
|
|
|
diff \
|
|
|
|
<(find $res -type l | xargs readlink | sort) \
|
|
|
|
<(morph exec morph.nix 'readlink /run/current-system' |& grep '^/nix/store/' | sort)
|
|
|
|
'';
|
|
|
|
|
2021-12-21 14:58:43 +00:00
|
|
|
livecd-deploy = pkgs.writeShellScript "livecd-deploy" ''
|
|
|
|
set -eux
|
|
|
|
config=".#nixosConfigurations.\"$1\".config"
|
|
|
|
ip="$(nix eval --raw "$config.sconfig.morph.deployment.targetHost")"
|
|
|
|
ssh-copy-id root@$ip
|
|
|
|
sys="$(nix eval --raw "$config.system.build.toplevel")"
|
|
|
|
nix build "$config.system.build.toplevel" --out-link "$(mktemp -d)/result"
|
|
|
|
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-12-06 23:42:54 +00:00
|
|
|
in
|
2021-12-21 14:58:43 +00:00
|
|
|
{ inherit check-updates jump livecd-deploy pkgs sshConfig; };
|
2021-12-06 23:42:54 +00:00
|
|
|
|
|
|
|
in
|
2021-09-10 03:46:13 +00:00
|
|
|
{
|
2021-12-06 23:42:54 +00:00
|
|
|
devShell = system: with helpers system;
|
|
|
|
pkgs.mkShell {
|
|
|
|
buildInputs = [ pkgs.morph ];
|
|
|
|
shellHook = ''
|
|
|
|
export SSH_CONFIG_FILE=${sshConfig}
|
2021-12-21 05:39:24 +00:00
|
|
|
alias ssh='ssh -F${sshConfig}'
|
2021-12-21 06:41:01 +00:00
|
|
|
alias jump=${jump}
|
2021-12-21 06:47:07 +00:00
|
|
|
alias check-updates=${check-updates}
|
2021-12-21 14:58:43 +00:00
|
|
|
alias livecd-deploy=${livecd-deploy}
|
2021-12-06 23:42:54 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-09-10 03:46:13 +00:00
|
|
|
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 [
|
2021-10-28 19:51:41 +00:00
|
|
|
{ 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;
|
|
|
|
}
|