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: packages = forAllSystems (system:
let let
pkgs = self.legacyPackages.${system}; 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 { in {
inherit (pkgs) bmxd openwebrx; inherit (pkgs) bmxd openwebrx;
@ -132,25 +104,41 @@
} // } //
builtins.foldl' (result: host: result // { 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.attrNames (nixpkgs.lib.filterAttrs (_: { wol ? false, ... }: wol) hostRegistry.hosts)) //
builtins.foldl' (result: name: result // { builtins.foldl' (result: name:
"${name}-nixos-rebuild" = mkDeploy ({ let
inherit name;
host = getHostAddr name; host = getHostAddr name;
}); target = ''root@"${host}"'';
rebuildArg = "--flake ${self}#${name}";
"${name}-nixos-rebuild-local" = profile = self.nixosConfigurations.${name}.config.system.build.toplevel;
let in result // {
host = getHostAddr name; # Generate a small script for copying this flake to the
target = ''root@"${host}"''; # remote machine and bulding and switching there.
profile = self.nixosConfigurations.${name}.config.system.build.toplevel; # Can be run with `nix run c3d2#…-nixos-rebuild switch`
in pkgs.writeScriptBin "${name}-nixos-rebuild" '' "${name}-nixos-rebuild" = pkgs.writeScriptBin "${name}-nixos-rebuild" ''
#!${pkgs.runtimeShell} -e #!${pkgs.runtimeShell} -e
nix-copy-closure --to ${target} ${profile} nix-copy-closure --to ${target} ${secrets}
exec ssh -t ${target} "${profile}/bin/switch-to-configuration $@" 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.attrNames flakifiedHosts) //
builtins.foldl' (result: host: result // { builtins.foldl' (result: host: result // {