backup: hoist variable definitions, add source to target's knownHosts

This commit is contained in:
Katherina Walshe-Grey 2025-03-04 18:12:44 +00:00
parent fefc7bd20d
commit 373bd88e1b

View file

@ -1,6 +1,7 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
let let
cfg = config.fountain.backup; cfg = config.fountain.backup;
keys = import ../keys.nix;
syncOptions = { syncOptions = {
dataset = lib.mkOption { dataset = lib.mkOption {
@ -91,36 +92,47 @@ in
# TODO: add some assertions to verify the options # TODO: add some assertions to verify the options
config.flake.colmena = lib.mkMerge (lib.mapAttrsToList config.flake.colmena = lib.mkMerge (lib.mapAttrsToList
(name: sync: { (name: sync:
${sync.sourceHost} = { pkgs, ... }: { let
randomcat.services.zfs.datasets."${sync.source}/${sync.dataset}".zfsPermissions.users.backup = [ "hold" "send" ]; inherit (sync) dataset sourceHost targetHost source target;
users.users.backup = { # TODO: don't want to have to dig into the node config for the fqdn
group = "backup"; sourceFqdn = config.flake.nixosConfigurations.${sourceHost}.config.networking.fqdn;
isSystemUser = true; in
useDefaultShell = true; {
openssh.authorizedKeys.keys = cfg.keys.${sync.targetHost}; ${sourceHost} = { pkgs, ... }: {
packages = with pkgs; [ mbuffer lzop ]; # syncoid uses these if available but doesn't pull them in automatically randomcat.services.zfs.datasets."${source}/${dataset}".zfsPermissions.users.backup = [ "hold" "send" ];
};
users.groups.backup = { };
};
${sync.targetHost} = { users.users.backup = {
randomcat.services.zfs.datasets."${sync.target}".zfsPermissions.users.syncoid = [ "mount" "create" "receive" "recordsize" ]; group = "backup";
services.syncoid = { isSystemUser = true;
enable = true; useDefaultShell = true;
interval = "*-*-* *:15:00"; openssh.authorizedKeys.keys = cfg.keys.${targetHost};
commonArgs = [ "--no-sync-snap" ]; packages = with pkgs; [ mbuffer lzop ]; # syncoid uses these if available but doesn't pull them in automatically
commands = { };
${name} = { users.groups.backup = { };
source = "backup@${config.flake.nixosConfigurations.${sync.sourceHost}.config.networking.fqdn}:${sync.source}/${sync.dataset}"; };
target = "${sync.target}/${sync.dataset}";
recursive = true; ${targetHost} = {
recvOptions = "ux recordsize o compression=lz4"; randomcat.services.zfs.datasets.${target}.zfsPermissions.users.syncoid = [ "mount" "create" "receive" "recordsize" ];
services.syncoid = {
enable = true;
interval = "*-*-* *:15:00";
commonArgs = [ "--no-sync-snap" ];
commands = {
${name} = {
source = "backup@${sourceFqdn}:${source}/${dataset}";
target = "${target}/${dataset}";
recursive = true;
recvOptions = "ux recordsize o compression=lz4";
};
}; };
}; };
# TODO: this should be handled by a networking module
programs.ssh.knownHosts.${sourceFqdn}.publicKey = keys.machines.${sourceHost};
}; };
}; })
})
cfg.sync cfg.sync
); );
} }