nixos-config/lib/deploy.nix

108 lines
3.7 KiB
Nix
Raw Normal View History

2021-12-15 06:25:58 +00:00
{ self
2021-12-27 18:21:16 +00:00
, system ? "x86_64-linux"
, modules ? [ ]
2021-09-10 03:46:13 +00:00
}:
2021-12-06 23:42:54 +00:00
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
inherit (nixpkgs.lib) concatMapStrings;
2021-12-27 18:21:16 +00:00
inherit (nixpkgs.legacyPackages.${system}) pkgs;
2021-12-06 23:42:54 +00:00
sshKnownHostsTxt = pkgs.writeText "known_hosts" (concatMapStrings
(hostName:
2021-12-27 18:21:16 +00:00
let m = nixosConfigurations.${hostName}.config.sconfig;
2021-12-06 23:42:54 +00:00
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}
2021-12-27 18:21:16 +00:00
HostName ${nixosConfigurations.${hostName}.config.sconfig.deployment.targetHost}
2021-12-21 05:39:24 +00:00
'')
(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}
2021-12-27 18:21:16 +00:00
ip="$(nix eval --raw ".#nixosConfigurations.\"$1\".config.sconfig.deployment.targetHost")"
2021-12-21 06:41:01 +00:00
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 14:58:43 +00:00
livecd-deploy = pkgs.writeShellScript "livecd-deploy" ''
set -eux
config=".#nixosConfigurations.\"$1\".config"
2021-12-27 18:21:16 +00:00
ip="$(nix eval --raw "$config.sconfig.deployment.targetHost")"
2021-12-21 14:58:43 +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"
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-27 18:21:16 +00:00
check-updates = pkgs.writeShellScript "check-updates" ''
set -eu
export SSH_CONFIG_FILE=${sshConfig}
c="${pkgs.colmena}/bin/colmena"
2022-01-02 22:34:13 +00:00
j="$($c eval -E '{nodes,...}: builtins.mapAttrs (n: v: v.config.system.build.toplevel) nodes')"
$c exec -- '[ "$(echo '"'$j'"' | jq -r .$(hostname))" = "$(readlink /run/current-system)" ]'
2021-12-27 18:21:16 +00:00
'';
2022-01-02 22:40:07 +00:00
check-reboots = pkgs.writeShellScript "check-reboots" ''
set -eu
export SSH_CONFIG_FILE=${sshConfig}
c="${pkgs.colmena}/bin/colmena"
$c exec -- '[ "$(readlink /run/booted-system/kernel)" = "$(readlink /run/current-system/kernel)" ]'
'';
2021-12-06 23:42:54 +00:00
in
2022-01-02 22:40:07 +00:00
{ inherit check-updates check-reboots 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 {
2021-12-27 18:21:16 +00:00
buildInputs = [ pkgs.colmena ];
2021-12-06 23:42:54 +00:00
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}
2022-01-02 22:40:07 +00:00
alias check-reboots=${check-reboots}
2021-12-21 14:58:43 +00:00
alias livecd-deploy=${livecd-deploy}
2021-12-27 18:21:16 +00:00
alias c=colmena
2021-12-06 23:42:54 +00:00
'';
};
2021-12-27 18:21:16 +00:00
colmena =
{ meta.nixpkgs = nixpkgs.legacyPackages.${system}; } //
builtins.mapAttrs
(name: value: {
2022-01-02 21:37:05 +00:00
nixpkgs.system = value.config.nixpkgs.system; # needed for multi-arch deployments
2021-12-27 18:21:16 +00:00
imports = value.extraArgs.modules ++ [
({ config, ... }: { inherit (config.sconfig) deployment; })
2021-09-10 03:46:13 +00:00
];
2021-12-27 18:21:16 +00:00
})
(nixosConfigurations);
2021-09-10 03:46:13 +00:00
}