config/modules/services/btrfssnapshot/default.nix

49 lines
1.4 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 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 05:36:17 +03:00
attrsOf (submodule {
options = {
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 15:39:54 +03:00
services = lib.mapAttrs' (
subvolume: params:
lib.nameValuePair "btrfs-snapshot-${lib.strings.sanitizeDerivationName subvolume}" {
2024-07-29 16:11:01 +03:00
description = "${params.label} btrfs snapshot for ${subvolume} (keep ${builtins.toString params.keep})";
serviceConfig.ExecStart = "${pkgs.btrfs-auto-snapshot}/bin/btrfs-auto-snapshot --verbose --label=${params.label} --keep=${builtins.toString params.keep} ${subvolume}";
2024-07-29 15:39:54 +03:00
}
2024-07-29 16:11:01 +03:00
) cfg.subvolumes;
2024-07-29 05:36:17 +03:00
2024-07-29 15:39:54 +03:00
timers = lib.mapAttrs' (
subvolume: params:
lib.nameValuePair "btrfs-snapshot-${lib.strings.sanitizeDerivationName subvolume}" {
description = "${params.label} btrfs snapshot for ${subvolume}";
wantedBy = [ "timers.target" ];
timerConfig.OnCalendar = params.refreshInterval;
}
2024-07-29 16:11:01 +03:00
) cfg.subvolumes;
2024-07-29 05:36:17 +03:00
};
};
}