From 10537afaa7749e38fdb6a5e67be91afaabb47dc8 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 17 Dec 2017 00:23:35 +0100 Subject: btrfs-snapshots on hel --- custom/btrfs-snapshots.nix | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 custom/btrfs-snapshots.nix (limited to 'custom') 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 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.services.btrfs-snapshots; + + snapshotMount = str: "${cfg.mountPoint}/${cfg.mountPrefix}${str}" +in { + options = { + + services.btrfs-snapshots = { + enable = mkEnableOption "btrfs snapshot unit"; + + mountPoint = mkOption { + type = types.path; + default = "/mnt"; + }; + + mountPrefix = mkOption { + type = types.str; + default = "snapshot-"; + }; + + readOnly = mkOption { + type = types.bool; + default = true; + }; + }; + + }; + + + config = mkIf cfg.enable { + + systemd.services."btrfs-snapshot@" = { + enable = true; + path = with pkgs; [btrfs-progs]; + + unitConfig = { + AssertPathIsDirectory = ${cfg.mountPoint}; + }; + + serviceConfig = with pkgs; { + ExecStart = "${btrfs-progs}/bin/btrfs subvolume snapshot ${optionalString cfg.readOnly "-r"} %f ${snapshotMount "%i"}"; + RemainAfterExit = true; + ExecStop = "${btrfs-progs}/bin/btrfs subvolume delete ${snapshotMount "%i"}"; + }; + }; + + }; +} -- cgit v1.2.3