From 2bbb8b50108cd1d65b6467cf18fe5100001530ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Mon, 29 Jul 2024 05:36:17 +0300 Subject: [PATCH] btrfssnapshot --- modules/base/btrfssnapshot/default.nix | 53 ++++++++++++++++++++++++++ modules/base/default.nix | 1 + 2 files changed, 54 insertions(+) create mode 100644 modules/base/btrfssnapshot/default.nix diff --git a/modules/base/btrfssnapshot/default.nix b/modules/base/btrfssnapshot/default.nix new file mode 100644 index 0000000..141fe9d --- /dev/null +++ b/modules/base/btrfssnapshot/default.nix @@ -0,0 +1,53 @@ +{ + config, + lib, + pkgs, + ... +}: let + cfg = config.mj.base.btrfssnapshot; +in { + options.mj.base.btrfssnapshot = { + enable = lib.mkEnableOption "Enable btrfs snapshots"; + + subvolumes = lib.mkOption { + default = {}; + type = with lib.types; + attrsOf (submodule { + options = { + label = lib.mkOption {type = str;}; + keep = lib.mkOption {type = int;}; + refreshInterval = lib.mkOption {type = str;}; + }; + }); + }; + }; + + config = lib.mkIf cfg.enable { + systemd = { + services = + lib.mapAttrs' + ( + subvolume: params: + lib.nameValuePair + "btrfs-snapshot-${lib.strings.sanitizeDerivationName subvolume}" + { + description = "${params.label} btrfs snapshot for ${subvolume} (keep ${params.keep}"; + serviceConfig.ExecStart = "${pkgs.btrfs-auto-snapshot} --verbose --label=${params.label} --keep=${params.keep} ${subvolume}"; + } + ); + + 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; + } + ); + }; + }; +} diff --git a/modules/base/default.nix b/modules/base/default.nix index 7c4544d..172d8b7 100644 --- a/modules/base/default.nix +++ b/modules/base/default.nix @@ -9,6 +9,7 @@ in { imports = [ ./boot + ./btrfssnapshot ./fileSystems ./snapshot ./sshd