summaryrefslogtreecommitdiff
path: root/custom/btrfs-snapshots.nix
diff options
context:
space:
mode:
Diffstat (limited to 'custom/btrfs-snapshots.nix')
-rw-r--r--custom/btrfs-snapshots.nix23
1 files changed, 6 insertions, 17 deletions
diff --git a/custom/btrfs-snapshots.nix b/custom/btrfs-snapshots.nix
index 39e0bf0c..96d2b2ba 100644
--- a/custom/btrfs-snapshots.nix
+++ b/custom/btrfs-snapshots.nix
@@ -5,21 +5,16 @@ with lib;
5let 5let
6 cfg = config.services.btrfs-snapshots; 6 cfg = config.services.btrfs-snapshots;
7 7
8 snapshotMount = str: "${cfg.mountPoint}/${cfg.mountPrefix}${str}"; 8 snapshotMount = str: "${str}${cfg.mountSuffix}";
9in { 9in {
10 options = { 10 options = {
11 11
12 services.btrfs-snapshots = { 12 services.btrfs-snapshots = {
13 enable = mkEnableOption "a systemd unit for btrfs snapshots"; 13 enable = mkEnableOption "a systemd unit for btrfs snapshots";
14 14
15 mountPoint = mkOption { 15 mountSuffix = mkOption {
16 type = types.path;
17 default = "/mnt";
18 };
19
20 mountPrefix = mkOption {
21 type = types.str; 16 type = types.str;
22 default = "snapshot-"; 17 default = ".snapshot";
23 }; 18 };
24 19
25 readOnly = mkOption { 20 readOnly = mkOption {
@@ -37,25 +32,19 @@ in {
37 32
38 33
39 config = mkIf cfg.enable { 34 config = mkIf cfg.enable {
40
41 system.activationScripts."btrfs-snapshots" = ''
42 mkdir -p ${cfg.mountPoint}
43 '';
44
45 systemd.services."btrfs-snapshot@" = { 35 systemd.services."btrfs-snapshot@" = {
46 enable = true; 36 enable = true;
47 37
48 unitConfig = { 38 unitConfig = {
49 AssertPathIsDirectory = cfg.mountPoint;
50 StopWhenUnneeded = !cfg.persist; 39 StopWhenUnneeded = !cfg.persist;
51 }; 40 };
52 41
53 serviceConfig = with pkgs; { 42 serviceConfig = with pkgs; {
54 Type = "oneshot"; 43 Type = "oneshot";
55 ExecStartPre = "-${btrfs-progs}/bin/btrfs subvolume delete -c ${snapshotMount "%i"}"; 44 ExecStartPre = "-${btrfs-progs}/bin/btrfs subvolume delete -c ${snapshotMount "%f"}";
56 ExecStart = "${btrfs-progs}/bin/btrfs subvolume snapshot ${optionalString cfg.readOnly "-r"} %f ${snapshotMount "%i"}"; 45 ExecStart = "${btrfs-progs}/bin/btrfs subvolume snapshot ${optionalString cfg.readOnly "-r"} %f ${snapshotMount "%f"}";
57 RemainAfterExit = true; 46 RemainAfterExit = true;
58 ExecStop = "${btrfs-progs}/bin/btrfs subvolume delete -c ${snapshotMount "%i"}"; 47 ExecStop = "${btrfs-progs}/bin/btrfs subvolume delete -c ${snapshotMount "%f"}";
59 }; 48 };
60 }; 49 };
61 50