restic: Refactor

This commit is contained in:
SebastianStork 2026-02-02 21:31:39 +01:00
parent e20ad1398b
commit a322ebb623
Signed by: SebastianStork
SSH key fingerprint: SHA256:tRrGdjYOwgHxpSc/wTOZQZEjxcb15P0tyXRsbAfd+2Q
3 changed files with 94 additions and 86 deletions

View file

@ -1,34 +1,42 @@
{ config, lib, ... }:
let
backups = config.custom.services.restic.backups |> lib.filterAttrs (_: backup: backup.enable);
backups =
config.custom.services.restic.backups |> lib.attrValues |> lib.filter (backup: backup.enable);
in
{
options.custom.services.restic.backups = lib.mkOption {
type = lib.types.attrsOf (
lib.types.submodule {
options = {
enable = lib.mkEnableOption "" // {
default = true;
lib.types.submodule (
{ name, ... }:
{
options = {
enable = lib.mkEnableOption "" // {
default = true;
};
name = lib.mkOption {
type = lib.types.nonEmptyStr;
default = name;
};
conflictingService = lib.mkOption {
type = lib.types.nullOr lib.types.nonEmptyStr;
default = null;
};
paths = lib.mkOption {
type = lib.types.listOf lib.types.path;
default = [ ];
};
extraConfig = lib.mkOption {
type = lib.types.attrsOf lib.types.anything;
default = { };
};
};
conflictingService = lib.mkOption {
type = lib.types.nullOr lib.types.nonEmptyStr;
default = null;
};
paths = lib.mkOption {
type = lib.types.listOf lib.types.path;
default = [ ];
};
extraConfig = lib.mkOption {
type = lib.types.attrsOf lib.types.anything;
default = { };
};
};
}
}
)
);
default = { };
};
config = lib.mkIf (backups != { }) {
config = lib.mkIf (backups != [ ]) {
sops = {
secrets = {
"backblaze/key-id" = { };
@ -43,17 +51,17 @@ in
};
systemd.tmpfiles.rules =
backups |> lib.attrNames |> lib.map (name: "d /var/cache/restic-backups-${name} 700 - - -");
backups |> lib.map (backup: "d /var/cache/restic-backups-${backup.name} 700 - - -");
services.restic.backups =
backups
|> lib.mapAttrs (
name: backup:
lib.mkMerge [
|> lib.map (backup: {
inherit (backup) name;
value = lib.mkMerge [
{
inherit (backup) paths;
initialize = true;
repository = "s3:https://s3.eu-central-003.backblazeb2.com/stork-atlas/${name}";
repository = "s3:https://s3.eu-central-003.backblazeb2.com/stork-atlas/${backup.name}";
environmentFile = config.sops.templates."restic/environment".path;
passwordFile = config.sops.secrets."restic/password".path;
pruneOpts = [
@ -67,21 +75,22 @@ in
};
}
backup.extraConfig
]
);
];
})
|> lib.listToAttrs;
systemd.services =
backups
|> lib.mapAttrs' (
name: backup:
lib.nameValuePair "restic-backups-${name}" (
lib.mkIf (backup.conflictingService != null) {
unitConfig.Conflicts = [ backup.conflictingService ];
after = [ backup.conflictingService ];
onSuccess = [ backup.conflictingService ];
onFailure = [ backup.conflictingService ];
}
)
);
|> lib.filter (backup: backup.conflictingService != null)
|> lib.map (backup: {
name = "restic-backups-${backup.name}";
value = {
unitConfig.Conflicts = [ backup.conflictingService ];
after = [ backup.conflictingService ];
onSuccess = [ backup.conflictingService ];
onFailure = [ backup.conflictingService ];
};
})
|> lib.listToAttrs;
};
}