{ 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 -c -v ${snapshotMount "%i"}"; }; }; }; }