restic: Rename option resticBackups to restic.backups

This commit is contained in:
SebastianStork 2025-10-23 20:21:37 +02:00
parent 9ec0c676be
commit ba78828f4f
Signed by: SebastianStork
SSH key fingerprint: SHA256:tRrGdjYOwgHxpSc/wTOZQZEjxcb15P0tyXRsbAfd+2Q
10 changed files with 17 additions and 17 deletions

View file

@ -0,0 +1,87 @@
{ config, lib, ... }:
let
backups = config.custom.services.restic.backups |> lib.filterAttrs (_: value: value.enable);
in
{
options.custom.services.restic.backups = lib.mkOption {
type = lib.types.attrsOf (
lib.types.submodule {
options = {
enable = lib.mkEnableOption "" // {
default = true;
};
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 != { }) {
sops = {
secrets = {
"backblaze/key-id" = { };
"backblaze/application-key" = { };
"restic/password" = { };
};
templates."restic/environment".content = ''
AWS_ACCESS_KEY_ID=${config.sops.placeholder."backblaze/key-id"}
AWS_SECRET_ACCESS_KEY=${config.sops.placeholder."backblaze/application-key"}
'';
};
systemd.tmpfiles.rules =
backups |> lib.attrNames |> lib.map (name: "d /var/cache/restic-backups-${name} 700 - - -");
services.restic.backups =
backups
|> lib.mapAttrs (
name: value:
lib.mkMerge [
{
inherit (value) paths;
initialize = true;
repository = "s3:https://s3.eu-central-003.backblazeb2.com/stork-atlas/${name}";
environmentFile = config.sops.templates."restic/environment".path;
passwordFile = config.sops.secrets."restic/password".path;
pruneOpts = [
"--keep-daily 7"
"--keep-weekly 4"
"--keep-monthly 6"
];
timerConfig = {
OnCalendar = "03:00";
RandomizedDelaySec = "1h";
};
}
value.extraConfig
]
);
systemd.services =
backups
|> lib.mapAttrs' (
name: value:
lib.nameValuePair "restic-backups-${name}" (
lib.mkIf (value.conflictingService != null) {
unitConfig.Conflicts = [ value.conflictingService ];
after = [ value.conflictingService ];
onSuccess = [ value.conflictingService ];
onFailure = [ value.conflictingService ];
}
)
);
};
}

View file

@ -0,0 +1,60 @@
{
config,
pkgs,
lib,
...
}:
let
backupsWithHealthchecks =
config.custom.services.restic.backups
|> lib.filterAttrs (_: value: value.enable)
|> lib.filterAttrs (_: value: value.doHealthchecks);
in
{
options.custom.services.restic.backups = lib.mkOption {
type = lib.types.attrsOf (
lib.types.submodule {
options.doHealthchecks = lib.mkEnableOption "" // {
default = true;
};
}
);
};
config = lib.mkIf (backupsWithHealthchecks != { }) {
sops.secrets."healthchecks/ping-key" = { };
systemd.services = lib.mkMerge [
{
"healthcheck-ping@" = {
description = "Pings healthcheck (%i)";
serviceConfig.Type = "oneshot";
scriptArgs = "%i";
script = ''
ping_key="$(cat ${config.sops.secrets."healthchecks/ping-key".path})"
slug="$(echo "$1" | tr _ /)"
${lib.getExe pkgs.curl} \
--fail \
--silent \
--show-error \
--max-time 10 \
--retry 5 "https://hc-ping.com/$ping_key/$slug"
'';
};
}
(
backupsWithHealthchecks
|> lib.mapAttrs' (
name: _:
lib.nameValuePair "restic-backups-${name}" {
wants = [ "healthcheck-ping@${name}-backup_start.service" ];
onSuccess = [ "healthcheck-ping@${name}-backup.service" ];
onFailure = [ "healthcheck-ping@${name}-backup_fail.service" ];
}
)
)
];
};
}

View file

@ -0,0 +1,57 @@
{
config,
pkgs,
lib,
...
}:
let
backupsWithRestoreCommand =
config.custom.services.restic.backups
|> lib.filterAttrs (_: value: value.enable)
|> lib.filterAttrs (_: value: value.restoreCommand.enable);
in
{
options.custom.services.restic.backups = lib.mkOption {
type = lib.types.attrsOf (
lib.types.submodule {
options.restoreCommand = {
enable = lib.mkEnableOption "" // {
default = true;
};
preRestore = lib.mkOption {
type = lib.types.str;
default = "";
};
postRestore = lib.mkOption {
type = lib.types.str;
default = "";
};
};
}
);
};
config = {
environment.systemPackages =
backupsWithRestoreCommand
|> lib.mapAttrsToList (
name: value:
pkgs.writeShellApplication {
name = "restic-restore-${name}";
text =
let
inherit (value) conflictingService;
inherit (value.restoreCommand) preRestore postRestore;
hasconflictingService = conflictingService != null;
in
''
${lib.optionalString hasconflictingService "systemctl stop ${conflictingService}"}
${preRestore}
restic-${name} restore latest --target /
${postRestore}
${lib.optionalString hasconflictingService "systemctl start ${conflictingService}"}
'';
}
);
};
}