diff options
Diffstat (limited to 'custom')
| -rw-r--r-- | custom/btrfs-snapshots.nix | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/custom/btrfs-snapshots.nix b/custom/btrfs-snapshots.nix new file mode 100644 index 00000000..7caf7a30 --- /dev/null +++ b/custom/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: "${cfg.mountPoint}/${cfg.mountPrefix}${str}" | ||
| 9 | in { | ||
| 10 | options = { | ||
| 11 | |||
| 12 | services.btrfs-snapshots = { | ||
| 13 | enable = mkEnableOption "btrfs snapshot unit"; | ||
| 14 | |||
| 15 | mountPoint = mkOption { | ||
| 16 | type = types.path; | ||
| 17 | default = "/mnt"; | ||
| 18 | }; | ||
| 19 | |||
| 20 | mountPrefix = mkOption { | ||
| 21 | type = types.str; | ||
| 22 | default = "snapshot-"; | ||
| 23 | }; | ||
| 24 | |||
| 25 | readOnly = mkOption { | ||
| 26 | type = types.bool; | ||
| 27 | default = true; | ||
| 28 | }; | ||
| 29 | }; | ||
| 30 | |||
| 31 | }; | ||
| 32 | |||
| 33 | |||
| 34 | config = mkIf cfg.enable { | ||
| 35 | |||
| 36 | systemd.services."btrfs-snapshot@" = { | ||
| 37 | enable = true; | ||
| 38 | path = with pkgs; [btrfs-progs]; | ||
| 39 | |||
| 40 | unitConfig = { | ||
| 41 | AssertPathIsDirectory = ${cfg.mountPoint}; | ||
| 42 | }; | ||
| 43 | |||
| 44 | serviceConfig = with pkgs; { | ||
| 45 | ExecStart = "${btrfs-progs}/bin/btrfs subvolume snapshot ${optionalString cfg.readOnly "-r"} %f ${snapshotMount "%i"}"; | ||
| 46 | RemainAfterExit = true; | ||
| 47 | ExecStop = "${btrfs-progs}/bin/btrfs subvolume delete ${snapshotMount "%i"}"; | ||
| 48 | }; | ||
| 49 | }; | ||
| 50 | |||
| 51 | }; | ||
| 52 | } | ||
