flake.nix: inline deployment scripts

This commit is contained in:
Astro 2021-09-29 23:26:05 +02:00
parent af83724eb3
commit a5dd695b83
1 changed files with 30 additions and 42 deletions

View File

@ -68,34 +68,6 @@
packages = forAllSystems (system:
let
pkgs = self.legacyPackages.${system};
mkDeploy =
# Generate a small script for copying this flake to the
# remote machine and bulding and switching there.
# Can be run with nix run c3d2#deploy-…
{ name
, host
}:
let target = "root@${host}";
rebuildArg =
"--flake ${self}#${name}";
in pkgs.writeScriptBin "${name}-nixos-rebuild" ''
#!${pkgs.runtimeShell} -e
nix-copy-closure --to ${target} ${secrets}
nix-copy-closure --to ${target} ${self}
if [ "$1" = "--flakify" ]; then
shift
exec ssh -t ${target} "nix-shell -p nixFlakes -p git --command '_NIXOS_REBUILD_REEXEC=1 nixos-rebuild ${rebuildArg} '$@"
else
exec ssh -t ${target} nixos-rebuild ${rebuildArg} $@
fi
'';
mkWake = name:
pkgs.writeScriptBin "${name}-wake" ''
#!${pkgs.runtimeShell}
exec ${pkgs.wol}/bin/wol ${hostRegistry.hosts.${name}.ether}
'';
# TODO: check if the ethernet address is reachable and if not,
# execute wol on a machine in HQ.
in {
inherit (pkgs) bmxd openwebrx;
@ -132,25 +104,41 @@
} //
builtins.foldl' (result: host: result // {
"${host}-wake" = mkWake host;
# TODO: check if the ethernet address is reachable and if not,
# execute wol on a machine in HQ.
"${host}-wake" = pkgs.writeScriptBin "${host}-wake" ''
#!${pkgs.runtimeShell}
exec ${pkgs.wol}/bin/wol ${hostRegistry.hosts.${host}.ether}
'';
}) {} (builtins.attrNames (nixpkgs.lib.filterAttrs (_: { wol ? false, ... }: wol) hostRegistry.hosts)) //
builtins.foldl' (result: name: result // {
"${name}-nixos-rebuild" = mkDeploy ({
inherit name;
builtins.foldl' (result: name:
let
host = getHostAddr name;
});
"${name}-nixos-rebuild-local" =
let
host = getHostAddr name;
target = ''root@"${host}"'';
profile = self.nixosConfigurations.${name}.config.system.build.toplevel;
in pkgs.writeScriptBin "${name}-nixos-rebuild" ''
target = ''root@"${host}"'';
rebuildArg = "--flake ${self}#${name}";
profile = self.nixosConfigurations.${name}.config.system.build.toplevel;
in result // {
# Generate a small script for copying this flake to the
# remote machine and bulding and switching there.
# Can be run with `nix run c3d2#…-nixos-rebuild switch`
"${name}-nixos-rebuild" = pkgs.writeScriptBin "${name}-nixos-rebuild" ''
#!${pkgs.runtimeShell} -e
nix-copy-closure --to ${target} ${profile}
exec ssh -t ${target} "${profile}/bin/switch-to-configuration $@"
nix-copy-closure --to ${target} ${secrets}
nix-copy-closure --to ${target} ${self}
exec ssh -t ${target} nixos-rebuild ${rebuildArg} $@
'';
"${name}-nixos-rebuild-local" = pkgs.writeScriptBin "${name}-nixos-rebuild" ''
#!${pkgs.runtimeShell} -e
nix-copy-closure --to ${target} --gzip ${profile}
exec ssh -t ${target} "${profile}/bin/switch-to-configuration $@"
'';
"${name}-cleanup" = pkgs.writeScriptBin "${name}-cleanup" ''
#!${pkgs.runtimeShell} -e
exec ssh -t ${target} "nix-collect-garbage -d && nix-store --optimise"
'';
}) {} (builtins.attrNames flakifiedHosts) //
builtins.foldl' (result: host: result // {