diff options
Diffstat (limited to 'modules/borgbackup/btrfs-snapshots.nix')
| -rw-r--r-- | modules/borgbackup/btrfs-snapshots.nix | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/modules/borgbackup/btrfs-snapshots.nix b/modules/borgbackup/btrfs-snapshots.nix new file mode 100644 index 00000000..96d2b2ba --- /dev/null +++ b/modules/borgbackup/btrfs-snapshots.nix | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | { config, lib, pkgs, ... }: | ||
| 2 | |||
| 3 | with lib; | ||
| 4 | |||
| 5 | let | ||
| 6 | cfg = config.services.btrfs-snapshots; | ||
| 7 | |||
| 8 | snapshotMount = str: "${str}${cfg.mountSuffix}"; | ||
| 9 | in { | ||
| 10 | options = { | ||
| 11 | |||
| 12 | services.btrfs-snapshots = { | ||
| 13 | enable = mkEnableOption "a systemd unit for btrfs snapshots"; | ||
| 14 | |||
| 15 | mountSuffix = mkOption { | ||
| 16 | type = types.str; | ||
| 17 | default = ".snapshot"; | ||
| 18 | }; | ||
| 19 | |||
| 20 | readOnly = mkOption { | ||
| 21 | type = types.bool; | ||
| 22 | default = true; | ||
| 23 | }; | ||
| 24 | |||
| 25 | persist = mkOption { | ||
| 26 | type = types.bool; | ||
| 27 | default = false; | ||
| 28 | }; | ||
| 29 | }; | ||
| 30 | |||
| 31 | }; | ||
| 32 | |||
| 33 | |||
| 34 | config = mkIf cfg.enable { | ||
| 35 | systemd.services."btrfs-snapshot@" = { | ||
| 36 | enable = true; | ||
| 37 | |||
| 38 | unitConfig = { | ||
| 39 | StopWhenUnneeded = !cfg.persist; | ||
| 40 | }; | ||
| 41 | |||
| 42 | serviceConfig = with pkgs; { | ||
| 43 | Type = "oneshot"; | ||
| 44 | ExecStartPre = "-${btrfs-progs}/bin/btrfs subvolume delete -c ${snapshotMount "%f"}"; | ||
| 45 | ExecStart = "${btrfs-progs}/bin/btrfs subvolume snapshot ${optionalString cfg.readOnly "-r"} %f ${snapshotMount "%f"}"; | ||
| 46 | RemainAfterExit = true; | ||
| 47 | ExecStop = "${btrfs-progs}/bin/btrfs subvolume delete -c ${snapshotMount "%f"}"; | ||
| 48 | }; | ||
| 49 | }; | ||
| 50 | |||
| 51 | }; | ||
| 52 | } | ||
