diff options
Diffstat (limited to 'custom')
| -rw-r--r-- | custom/borgbackup.nix | 19 | ||||
| -rw-r--r-- | custom/btrfs-snapshots.nix | 23 | ||||
| -rw-r--r-- | custom/lvm-snapshots.nix | 7 |
3 files changed, 21 insertions, 28 deletions
diff --git a/custom/borgbackup.nix b/custom/borgbackup.nix index 3871af0a..b292f6f2 100644 --- a/custom/borgbackup.nix +++ b/custom/borgbackup.nix | |||
| @@ -21,9 +21,9 @@ let | |||
| 21 | 21 | ||
| 22 | systemdPath = path: escapeSystemdPath (if cfg.snapshots == "lvm" then "${path.VG}-${path.LV}" else path); | 22 | systemdPath = path: escapeSystemdPath (if cfg.snapshots == "lvm" then "${path.VG}-${path.LV}" else path); |
| 23 | 23 | ||
| 24 | withPrefix = path: config.services.btrfs-snapshots.mountPrefix + path; | 24 | withSuffix = path: path + (if cfg.snapshots == "btrfs" then config.services.btrfs-snapshots.mountSuffix else config.services.lvm-snapshots.mountSuffix); |
| 25 | 25 | ||
| 26 | mountPoint = if cfg.snapshots == "lvm" then config.services.lvm-snapshots.mountPoint else config.services.btrfs-snapshots.mountPoint; | 26 | mountPoint = if cfg.snapshots == "lvm" then config.services.lvm-snapshots.mountPoint else ""; |
| 27 | 27 | ||
| 28 | targetOptions = { | 28 | targetOptions = { |
| 29 | options = { | 29 | options = { |
| @@ -86,18 +86,17 @@ in { | |||
| 86 | }; | 86 | }; |
| 87 | }; | 87 | }; |
| 88 | 88 | ||
| 89 | imports = [ | 89 | imports = |
| 90 | ./lvm-snapshots.nix | 90 | [ ./lvm-snapshots.nix |
| 91 | ./btrfs-snapshots.nix | 91 | ./btrfs-snapshots.nix |
| 92 | ]; | 92 | ]; |
| 93 | 93 | ||
| 94 | config = mkIf (any (t: t.paths != []) (attrValues cfg.targets)) { | 94 | config = mkIf (any (t: t.paths != []) (attrValues cfg.targets)) { |
| 95 | |||
| 96 | services.btrfs-snapshots.enable = mkIf (cfg.snapshots == "btrfs") true; | 95 | services.btrfs-snapshots.enable = mkIf (cfg.snapshots == "btrfs") true; |
| 97 | 96 | ||
| 98 | services.lvm-snapshots.snapshots = mkIf (cfg.snapshots == "lvm") (listToAttrs (map (path: nameValuePair (path.VG + "-" + path.LV) { | 97 | services.lvm-snapshots.snapshots = mkIf (cfg.snapshots == "lvm") (listToAttrs (map (path: nameValuePair (path.VG + "-" + path.LV) { |
| 99 | inherit (path) LV VG; | 98 | inherit (path) LV VG; |
| 100 | mountName = withPrefix (path.VG + "-" + path.LV); | 99 | mountName = withSuffix (path.VG + "-" + path.LV); |
| 101 | }) (unique (flatten (mapAttrsToList (target: tCfg: tCfg.paths) cfg.targets))))); | 100 | }) (unique (flatten (mapAttrsToList (target: tCfg: tCfg.paths) cfg.targets))))); |
| 102 | 101 | ||
| 103 | systemd.targets."timers-borg" = { | 102 | systemd.targets."timers-borg" = { |
| @@ -159,12 +158,12 @@ in { | |||
| 159 | unitConfig = { | 158 | unitConfig = { |
| 160 | AssertPathIsDirectory = mkIf (tCfg.lock != null) "/var/lock"; | 159 | AssertPathIsDirectory = mkIf (tCfg.lock != null) "/var/lock"; |
| 161 | DefaultDependencies = false; | 160 | DefaultDependencies = false; |
| 162 | RequiresMountsFor = mkIf (cfg.snapshots == "lvm") [ "${mountPoint}/${withPrefix "%I"}" ]; | 161 | RequiresMountsFor = mkIf (cfg.snapshots == "lvm") [ "${mountPoint}/${withSuffix "%I"}" ]; |
| 163 | }; | 162 | }; |
| 164 | 163 | ||
| 165 | serviceConfig = { | 164 | serviceConfig = { |
| 166 | Type = "oneshot"; | 165 | Type = "oneshot"; |
| 167 | WorkingDirectory = if (cfg.snapshots == null) then "%I" else (if (cfg.snapshots == "lvm") then "${mountPoint}/${withPrefix "%I"}" else "${mountPoint}/${withPrefix "%i"}"); | 166 | WorkingDirectory = if (cfg.snapshots == null) then "%I" else (if (cfg.snapshots == "lvm") then "${withSuffix "%I"}" else "${withSuffix "%f"}"); |
| 168 | Nice = 15; | 167 | Nice = 15; |
| 169 | IOSchedulingClass = 2; | 168 | IOSchedulingClass = 2; |
| 170 | IOSchedulingPriority = 7; | 169 | IOSchedulingPriority = 7; |
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; | |||
| 5 | let | 5 | let |
| 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}"; |
| 9 | in { | 9 | in { |
| 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 | ||
diff --git a/custom/lvm-snapshots.nix b/custom/lvm-snapshots.nix index 98c64330..13cbf421 100644 --- a/custom/lvm-snapshots.nix +++ b/custom/lvm-snapshots.nix | |||
| @@ -7,7 +7,7 @@ let | |||
| 7 | cfg = config.services.lvm-snapshots; | 7 | cfg = config.services.lvm-snapshots; |
| 8 | 8 | ||
| 9 | snapshotMount = name: "${cfg.mountPoint}/${if isNull cfg.snapshots."${name}".mountName then name else cfg.snapshots."${name}".mountName}"; | 9 | snapshotMount = name: "${cfg.mountPoint}/${if isNull cfg.snapshots."${name}".mountName then name else cfg.snapshots."${name}".mountName}"; |
| 10 | snapshotName = name: "${name}-snapshot"; | 10 | snapshotName = name: "${name}-${cfg.mountSuffix}"; |
| 11 | 11 | ||
| 12 | snapshotConfig = { | 12 | snapshotConfig = { |
| 13 | options = { | 13 | options = { |
| @@ -53,6 +53,11 @@ in { | |||
| 53 | type = types.path; | 53 | type = types.path; |
| 54 | default = "/mnt"; | 54 | default = "/mnt"; |
| 55 | }; | 55 | }; |
| 56 | |||
| 57 | mountSuffix = mkOption { | ||
| 58 | type = types.str; | ||
| 59 | default = "-snapshot"; | ||
| 60 | }; | ||
| 56 | }; | 61 | }; |
| 57 | }; | 62 | }; |
| 58 | 63 | ||
