config/modules/services/btrfssnapshot/default.nix

67 lines
1.8 KiB
Nix
Raw Normal View History

2024-07-29 05:36:17 +03:00
{
config,
lib,
pkgs,
...
2024-07-29 15:39:54 +03:00
}:
let
2024-07-29 16:11:01 +03:00
cfg = config.mj.services.btrfssnapshot;
2024-07-29 16:31:32 +03:00
svcName =
subvol: label:
"btrfs-snapshot-${lib.strings.sanitizeDerivationName subvol}-${lib.strings.sanitizeDerivationName label}";
2024-07-29 15:39:54 +03:00
in
{
2024-07-29 16:11:01 +03:00
options.mj.services.btrfssnapshot = {
2024-07-29 05:36:17 +03:00
enable = lib.mkEnableOption "Enable btrfs snapshots";
subvolumes = lib.mkOption {
2024-07-29 15:39:54 +03:00
default = { };
type =
with lib.types;
2024-07-29 16:31:32 +03:00
listOf (submodule {
2024-07-29 05:36:17 +03:00
options = {
2024-07-29 16:31:32 +03:00
subvolume = lib.mkOption { type = str; };
2024-07-29 15:39:54 +03:00
label = lib.mkOption { type = str; };
keep = lib.mkOption { type = int; };
refreshInterval = lib.mkOption { type = str; };
2024-07-29 05:36:17 +03:00
};
});
};
};
config = lib.mkIf cfg.enable {
systemd = {
2024-07-29 16:31:32 +03:00
timers = lib.listToAttrs (
map (
params:
lib.nameValuePair (svcName params.subvolume params.label) {
description = "${params.label} btrfs snapshot for ${params.subvolume}";
wantedBy = [ "timers.target" ];
timerConfig.OnCalendar = params.refreshInterval;
}
) cfg.subvolumes
);
services = lib.listToAttrs (
map (
params:
lib.nameValuePair (svcName params.subvolume params.label) {
description = "${params.label} btrfs snapshot for ${params.subvolume} (keep ${builtins.toString params.keep})";
serviceConfig = {
Type = "oneshot";
ExecStart = ''
${pkgs.btrfs-auto-snapshot}/bin/btrfs-auto-snapshot \
--verbose \
--label=${params.label} \
--keep=${builtins.toString params.keep} \
${params.subvolume}'';
};
}
) cfg.subvolumes
);
2024-07-29 05:36:17 +03:00
};
};
}