2017-07-05 15:19:32 +01:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.snapper;
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options.services.snapper = {
|
|
|
|
|
2021-09-06 19:53:06 +01:00
|
|
|
snapshotRootOnBoot = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to snapshot root on boot
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-07-05 15:19:32 +01:00
|
|
|
snapshotInterval = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "hourly";
|
|
|
|
description = ''
|
|
|
|
Snapshot interval.
|
|
|
|
|
|
|
|
The format is described in
|
|
|
|
<citerefentry><refentrytitle>systemd.time</refentrytitle>
|
|
|
|
<manvolnum>7</manvolnum></citerefentry>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
cleanupInterval = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "1d";
|
|
|
|
description = ''
|
|
|
|
Cleanup interval.
|
|
|
|
|
|
|
|
The format is described in
|
|
|
|
<citerefentry><refentrytitle>systemd.time</refentrytitle>
|
|
|
|
<manvolnum>7</manvolnum></citerefentry>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
filters = mkOption {
|
|
|
|
type = types.nullOr types.lines;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Global display difference filter. See man:snapper(8) for more details.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
configs = mkOption {
|
|
|
|
default = { };
|
|
|
|
example = literalExample {
|
2019-08-13 22:52:01 +01:00
|
|
|
home = {
|
2017-07-05 15:19:32 +01:00
|
|
|
subvolume = "/home";
|
|
|
|
extraConfig = ''
|
|
|
|
ALLOW_USERS="alice"
|
2021-01-17 08:13:34 +00:00
|
|
|
TIMELINE_CREATE=yes
|
|
|
|
TIMELINE_CLEANUP=yes
|
2017-07-05 15:19:32 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
description = ''
|
|
|
|
Subvolume configuration
|
|
|
|
'';
|
|
|
|
|
|
|
|
type = types.attrsOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
subvolume = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
description = ''
|
|
|
|
Path of the subvolume or mount point.
|
|
|
|
This path is a subvolume and has to contain a subvolume named
|
|
|
|
.snapshots.
|
|
|
|
See also man:snapper(8) section PERMISSIONS.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
fstype = mkOption {
|
|
|
|
type = types.enum [ "btrfs" ];
|
|
|
|
default = "btrfs";
|
|
|
|
description = ''
|
|
|
|
Filesystem type. Only btrfs is stable and tested.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Additional configuration next to SUBVOLUME and FSTYPE.
|
|
|
|
See man:snapper-configs(5).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf (cfg.configs != {}) (let
|
|
|
|
documentation = [ "man:snapper(8)" "man:snapper-configs(5)" ];
|
|
|
|
in {
|
|
|
|
|
|
|
|
environment = {
|
|
|
|
|
|
|
|
systemPackages = [ pkgs.snapper ];
|
|
|
|
|
|
|
|
# Note: snapper/config-templates/default is only needed for create-config
|
|
|
|
# which is not the NixOS way to configure.
|
|
|
|
etc = {
|
|
|
|
|
|
|
|
"sysconfig/snapper".text = ''
|
|
|
|
SNAPPER_CONFIGS="${lib.concatStringsSep " " (builtins.attrNames cfg.configs)}"
|
|
|
|
'';
|
|
|
|
|
|
|
|
}
|
|
|
|
// (mapAttrs' (name: subvolume: nameValuePair "snapper/configs/${name}" ({
|
|
|
|
text = ''
|
|
|
|
${subvolume.extraConfig}
|
|
|
|
FSTYPE="${subvolume.fstype}"
|
|
|
|
SUBVOLUME="${subvolume.subvolume}"
|
|
|
|
'';
|
|
|
|
})) cfg.configs)
|
|
|
|
// (lib.optionalAttrs (cfg.filters != null) {
|
|
|
|
"snapper/filters/default.txt".text = cfg.filters;
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
services.dbus.packages = [ pkgs.snapper ];
|
|
|
|
|
2020-10-08 22:17:44 +01:00
|
|
|
systemd.services.snapperd = {
|
|
|
|
description = "DBus interface for snapper";
|
|
|
|
inherit documentation;
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "dbus";
|
|
|
|
BusName = "org.opensuse.Snapper";
|
|
|
|
ExecStart = "${pkgs.snapper}/bin/snapperd";
|
2021-09-23 03:51:58 +01:00
|
|
|
CapabilityBoundingSet = "CAP_DAC_OVERRIDE CAP_FOWNER CAP_CHOWN CAP_FSETID CAP_SETFCAP CAP_SYS_ADMIN CAP_SYS_MODULE CAP_IPC_LOCK CAP_SYS_NICE";
|
|
|
|
LockPersonality = true;
|
|
|
|
NoNewPrivileges = false;
|
|
|
|
PrivateNetwork = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
RestrictAddressFamilies = "AF_UNIX";
|
|
|
|
RestrictRealtime = true;
|
2020-10-08 22:17:44 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-07-05 15:19:32 +01:00
|
|
|
systemd.services.snapper-timeline = {
|
|
|
|
description = "Timeline of Snapper Snapshots";
|
|
|
|
inherit documentation;
|
2021-09-06 19:53:06 +01:00
|
|
|
requires = [ "local-fs.target" ];
|
2017-07-05 15:19:32 +01:00
|
|
|
serviceConfig.ExecStart = "${pkgs.snapper}/lib/snapper/systemd-helper --timeline";
|
2021-09-06 19:53:06 +01:00
|
|
|
startAt = cfg.snapshotInterval;
|
2017-07-05 15:19:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.snapper-cleanup = {
|
|
|
|
description = "Cleanup of Snapper Snapshots";
|
|
|
|
inherit documentation;
|
|
|
|
serviceConfig.ExecStart = "${pkgs.snapper}/lib/snapper/systemd-helper --cleanup";
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.timers.snapper-cleanup = {
|
|
|
|
description = "Cleanup of Snapper Snapshots";
|
|
|
|
inherit documentation;
|
2021-09-06 19:53:06 +01:00
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
requires = [ "local-fs.target" ];
|
2017-07-05 15:19:32 +01:00
|
|
|
timerConfig.OnBootSec = "10m";
|
|
|
|
timerConfig.OnUnitActiveSec = cfg.cleanupInterval;
|
|
|
|
};
|
2021-09-06 19:53:06 +01:00
|
|
|
|
|
|
|
systemd.services.snapper-boot = lib.optionalAttrs cfg.snapshotRootOnBoot {
|
|
|
|
description = "Take snapper snapshot of root on boot";
|
|
|
|
inherit documentation;
|
|
|
|
serviceConfig.ExecStart = "${pkgs.snapper}/bin/snapper --config root create --cleanup-algorithm number --description boot";
|
|
|
|
serviceConfig.type = "oneshot";
|
|
|
|
requires = [ "local-fs.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
unitConfig.ConditionPathExists = "/etc/snapper/configs/root";
|
|
|
|
};
|
|
|
|
|
2017-07-05 15:19:32 +01:00
|
|
|
});
|
|
|
|
}
|