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)
|
|
|
|
);
|
|
|
|
|
|
|
|
sshConfig = pkgs.writeText "ssh_config" ''
|
|
|
|
Host *
|
|
|
|
User root
|
|
|
|
StrictHostKeyChecking yes
|
|
|
|
GlobalKnownHostsFile ${sshKnownHostsTxt}
|
|
|
|
'';
|
|
|
|
|
|
|
|
sh = scriptBody: pkgs.writeShellScriptBin "run" ''
|
|
|
|
set -eu
|
|
|
|
export SSH_CONFIG_FILE=${sshConfig}
|
|
|
|
${scriptBody}
|
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
|
|
|
{ inherit pkgs sh sshConfig; };
|
|
|
|
|
|
|
|
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 IN_NIX_SHELL=impure
|
|
|
|
export SSH_CONFIG_FILE=${sshConfig}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-12-06 23:42:54 +00:00
|
|
|
packages = system: with helpers system;
|
|
|
|
{
|
|
|
|
check-updates = sh ''
|
|
|
|
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-09-10 03:46:13 +00:00
|
|
|
'';
|
2021-10-29 18:01:14 +00:00
|
|
|
livecd-deploy = sh ''
|
|
|
|
config=".#nixosConfigurations.\"$1\".config"
|
|
|
|
ip="$(nix eval --raw "$config.sconfig.morph.deployment.targetHost")"
|
2021-12-15 19:44:03 +00:00
|
|
|
ssh-copy-id root@$ip
|
|
|
|
sys="$(nix eval --raw "$config.system.build.toplevel")"
|
|
|
|
nix build "$config.system.build.toplevel" --out-link "$(mktemp -d)/result"
|
2021-10-29 18:01:14 +00:00
|
|
|
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-15 06:25:58 +00:00
|
|
|
jump = sh ''
|
|
|
|
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}" "$ip" -t "cd ${self}; nix develop"
|
|
|
|
'';
|
2021-09-10 03:46:13 +00:00
|
|
|
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" "$@"
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
}
|