2026-08-17 17:00:44 +02:00
|
|
|
{
|
|
|
|
|
lib,
|
|
|
|
|
pkgs,
|
2026-08-18 15:52:18 +02:00
|
|
|
config,
|
2026-08-17 17:00:44 +02:00
|
|
|
...
|
|
|
|
|
}:
|
2026-08-12 17:45:17 +02:00
|
|
|
{
|
2026-08-17 17:00:44 +02:00
|
|
|
# Configuration
|
2026-08-18 15:52:18 +02:00
|
|
|
options.services.backup = lib.mkOption {
|
|
|
|
|
type = lib.types.attrsOf (
|
|
|
|
|
lib.types.submodule (
|
|
|
|
|
{ ... }:
|
|
|
|
|
{
|
|
|
|
|
options = {
|
|
|
|
|
enable = lib.mkEnableOption "Whether to enable backups for this path or not.";
|
2026-08-12 17:45:17 +02:00
|
|
|
|
2026-08-18 15:52:18 +02:00
|
|
|
dest = lib.mkOption {
|
|
|
|
|
type = lib.types.either lib.types.path lib.types.str;
|
|
|
|
|
default = null;
|
|
|
|
|
description = "A destination for the backup.";
|
|
|
|
|
};
|
2026-08-12 17:45:17 +02:00
|
|
|
|
2026-08-18 15:52:18 +02:00
|
|
|
compression = {
|
|
|
|
|
command = lib.mkOption {
|
|
|
|
|
type = lib.types.nullOr lib.types.str;
|
|
|
|
|
default = "tar -caf \"$DST\" \"$SRC\"";
|
|
|
|
|
description = "A bash command to compress backups. Defaults to an uncompressed tar archive.";
|
|
|
|
|
};
|
|
|
|
|
};
|
2026-08-12 17:45:17 +02:00
|
|
|
|
2026-08-18 15:52:18 +02:00
|
|
|
encryption = {
|
|
|
|
|
command = lib.mkOption {
|
|
|
|
|
type = lib.types.str;
|
|
|
|
|
description = "A bash command to encrypt backups. Defaults to a clear archive (simple cat).";
|
|
|
|
|
default = "cat \"$SRC\" > \"$DST\"";
|
|
|
|
|
};
|
|
|
|
|
key = lib.mkOption {
|
|
|
|
|
type = lib.types.nullOr lib.types.path;
|
|
|
|
|
description = "A key to encrypt backups.";
|
|
|
|
|
default = null;
|
|
|
|
|
};
|
|
|
|
|
};
|
2026-08-17 17:00:44 +02:00
|
|
|
|
2026-08-18 15:52:18 +02:00
|
|
|
salvation = {
|
|
|
|
|
command = lib.mkOption {
|
|
|
|
|
type = lib.types.str;
|
|
|
|
|
description = "A bash command to save the archive. Defaults to a copy from source to dest.";
|
|
|
|
|
default = "cp \"$SRC\" \"$DST\"";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
2026-08-17 17:00:44 +02:00
|
|
|
|
2026-08-18 15:52:18 +02:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
2026-08-12 17:45:17 +02:00
|
|
|
};
|
|
|
|
|
|
2026-08-17 17:00:44 +02:00
|
|
|
config =
|
|
|
|
|
let
|
2026-08-18 15:52:18 +02:00
|
|
|
systemd_backups = map (
|
|
|
|
|
attrset:
|
|
|
|
|
let
|
|
|
|
|
script = pkgs.writeShellApplication {
|
|
|
|
|
name = "backup-${baseNameOf attrset.name}.sh";
|
|
|
|
|
runtimeInputs = [
|
|
|
|
|
pkgs.gnutar
|
|
|
|
|
pkgs.gzip
|
|
|
|
|
];
|
|
|
|
|
text = ''
|
|
|
|
|
#!/bin/bash
|
2026-08-17 17:00:44 +02:00
|
|
|
|
2026-08-18 15:52:18 +02:00
|
|
|
# Archive creation and compression
|
|
|
|
|
SRC=${attrset.name}
|
|
|
|
|
DST=$(mktemp --suffix=.tar)
|
|
|
|
|
echo "Archiving and compressing $SRC to $DST."
|
|
|
|
|
${attrset.value.compression.command}
|
2026-08-17 17:00:44 +02:00
|
|
|
|
2026-08-18 15:52:18 +02:00
|
|
|
# Archive encryption (optionnal)
|
|
|
|
|
SRC=$DST
|
|
|
|
|
DST=$(mktemp --suffix=.tar.encrypted)
|
|
|
|
|
echo "Encyphering $SRC to $DST."
|
|
|
|
|
${attrset.value.encryption.command}
|
2026-08-17 17:00:44 +02:00
|
|
|
|
2026-08-18 15:52:18 +02:00
|
|
|
# Archive salvation
|
|
|
|
|
SRC=$DST
|
|
|
|
|
DST="${attrset.value.dest}/${baseNameOf attrset.name}.tar.enc"
|
|
|
|
|
echo "Saving $SRC to $DST."
|
|
|
|
|
${attrset.value.salvation.command}
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
timers."backup-${baseNameOf attrset.name}" = {
|
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
|
timerConfig = {
|
|
|
|
|
OnBootSec = "5m";
|
|
|
|
|
OnCalendar = "* *-*-* 00:00:00";
|
|
|
|
|
Unit = "backup-${baseNameOf attrset.name}.service";
|
|
|
|
|
Persistent = true;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
services."backup-${baseNameOf attrset.name}" = {
|
|
|
|
|
enable = attrset.value.enable;
|
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
|
description = "Saves a directory and its content.";
|
|
|
|
|
serviceConfig = {
|
|
|
|
|
Type = "simple";
|
|
|
|
|
ExecStart = "${script}/bin/backup-${baseNameOf attrset.name}.sh";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
) (lib.attrsets.attrsToList config.services.backup);
|
2026-08-17 17:00:44 +02:00
|
|
|
in
|
2026-08-18 15:52:18 +02:00
|
|
|
{
|
|
|
|
|
systemd = lib.attrsets.mergeAttrsList systemd_backups;
|
2026-08-12 17:45:17 +02:00
|
|
|
};
|
2026-08-18 15:52:18 +02:00
|
|
|
|
2026-08-12 17:45:17 +02:00
|
|
|
}
|