2019-10-27 04:37:30 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.sanoid;
|
|
|
|
|
|
|
|
datasetSettingsType = with types;
|
|
|
|
(attrsOf (nullOr (oneOf [ str int bool (listOf str) ]))) // {
|
|
|
|
description = "dataset/template options";
|
|
|
|
};
|
|
|
|
|
|
|
|
commonOptions = {
|
|
|
|
hourly = mkOption {
|
|
|
|
description = "Number of hourly snapshots.";
|
2020-03-31 18:09:59 +02:00
|
|
|
type = with types; nullOr ints.unsigned;
|
|
|
|
default = null;
|
2019-10-27 04:37:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
daily = mkOption {
|
|
|
|
description = "Number of daily snapshots.";
|
2020-03-31 18:09:59 +02:00
|
|
|
type = with types; nullOr ints.unsigned;
|
|
|
|
default = null;
|
2019-10-27 04:37:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
monthly = mkOption {
|
|
|
|
description = "Number of monthly snapshots.";
|
2020-03-31 18:09:59 +02:00
|
|
|
type = with types; nullOr ints.unsigned;
|
|
|
|
default = null;
|
2019-10-27 04:37:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
yearly = mkOption {
|
|
|
|
description = "Number of yearly snapshots.";
|
2020-03-31 18:09:59 +02:00
|
|
|
type = with types; nullOr ints.unsigned;
|
|
|
|
default = null;
|
2019-10-27 04:37:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
autoprune = mkOption {
|
|
|
|
description = "Whether to automatically prune old snapshots.";
|
2020-03-31 18:09:59 +02:00
|
|
|
type = with types; nullOr bool;
|
|
|
|
default = null;
|
2019-10-27 04:37:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
autosnap = mkOption {
|
|
|
|
description = "Whether to automatically take snapshots.";
|
2020-03-31 18:09:59 +02:00
|
|
|
type = with types; nullOr bool;
|
|
|
|
default = null;
|
2019-10-27 04:37:30 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-03-31 18:09:59 +02:00
|
|
|
datasetOptions = rec {
|
|
|
|
use_template = mkOption {
|
2019-10-27 04:37:30 +01:00
|
|
|
description = "Names of the templates to use for this dataset.";
|
2021-11-19 00:26:27 +01:00
|
|
|
type = types.listOf (types.str // {
|
|
|
|
check = (types.enum (attrNames cfg.templates)).check;
|
|
|
|
description = "configured template name";
|
|
|
|
});
|
2021-07-25 18:31:42 +02:00
|
|
|
default = [ ];
|
2019-10-27 04:37:30 +01:00
|
|
|
};
|
2020-03-31 18:09:59 +02:00
|
|
|
useTemplate = use_template;
|
2019-10-27 04:37:30 +01:00
|
|
|
|
|
|
|
recursive = mkOption {
|
2021-09-02 13:35:30 +02:00
|
|
|
description = ''
|
|
|
|
Whether to recursively snapshot dataset children.
|
|
|
|
You can also set this to <literal>"zfs"</literal> to handle datasets
|
|
|
|
recursively in an atomic way without the possibility to
|
|
|
|
override settings for child datasets.
|
|
|
|
'';
|
|
|
|
type = with types; oneOf [ bool (enum [ "zfs" ]) ];
|
2019-10-27 04:37:30 +01:00
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
2020-03-31 18:09:59 +02:00
|
|
|
process_children_only = mkOption {
|
2019-10-27 04:37:30 +01:00
|
|
|
description = "Whether to only snapshot child datasets if recursing.";
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
2020-03-31 18:09:59 +02:00
|
|
|
processChildrenOnly = process_children_only;
|
2019-10-27 04:37:30 +01:00
|
|
|
};
|
|
|
|
|
2021-07-25 10:00:37 +02:00
|
|
|
# Extract unique dataset names
|
|
|
|
datasets = unique (attrNames cfg.datasets);
|
2019-10-27 04:37:30 +01:00
|
|
|
|
2021-07-25 18:27:36 +02:00
|
|
|
# Function to build "zfs allow" and "zfs unallow" commands for the
|
|
|
|
# filesystems we've delegated permissions to.
|
|
|
|
buildAllowCommand = zfsAction: permissions: dataset: lib.escapeShellArgs [
|
|
|
|
# Here we explicitly use the booted system to guarantee the stable API needed by ZFS
|
|
|
|
"-+/run/booted-system/sw/bin/zfs"
|
|
|
|
zfsAction
|
|
|
|
"sanoid"
|
|
|
|
(concatStringsSep "," permissions)
|
|
|
|
dataset
|
|
|
|
];
|
|
|
|
|
2021-07-25 18:31:42 +02:00
|
|
|
configFile =
|
|
|
|
let
|
|
|
|
mkValueString = v:
|
|
|
|
if builtins.isList v then concatStringsSep "," v
|
|
|
|
else generators.mkValueStringDefault { } v;
|
|
|
|
|
|
|
|
mkKeyValue = k: v:
|
|
|
|
if v == null then ""
|
|
|
|
else if k == "processChildrenOnly" then ""
|
|
|
|
else if k == "useTemplate" then ""
|
|
|
|
else generators.mkKeyValueDefault { inherit mkValueString; } "=" k v;
|
|
|
|
in
|
|
|
|
generators.toINI { inherit mkKeyValue; } cfg.settings;
|
|
|
|
|
|
|
|
in
|
|
|
|
{
|
|
|
|
|
|
|
|
# Interface
|
|
|
|
|
|
|
|
options.services.sanoid = {
|
|
|
|
enable = mkEnableOption "Sanoid ZFS snapshotting service";
|
|
|
|
|
|
|
|
interval = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "hourly";
|
|
|
|
example = "daily";
|
|
|
|
description = ''
|
|
|
|
Run sanoid at this interval. The default is to run hourly.
|
|
|
|
|
|
|
|
The format is described in
|
|
|
|
<citerefentry><refentrytitle>systemd.time</refentrytitle>
|
|
|
|
<manvolnum>7</manvolnum></citerefentry>.
|
|
|
|
'';
|
|
|
|
};
|
2019-10-27 04:37:30 +01:00
|
|
|
|
2021-07-25 18:31:42 +02:00
|
|
|
datasets = mkOption {
|
|
|
|
type = types.attrsOf (types.submodule ({ config, options, ... }: {
|
|
|
|
freeformType = datasetSettingsType;
|
|
|
|
options = commonOptions // datasetOptions;
|
|
|
|
config.use_template = mkAliasDefinitions (mkDefault options.useTemplate or { });
|
|
|
|
config.process_children_only = mkAliasDefinitions (mkDefault options.processChildrenOnly or { });
|
|
|
|
}));
|
|
|
|
default = { };
|
|
|
|
description = "Datasets to snapshot.";
|
|
|
|
};
|
2019-10-27 04:37:30 +01:00
|
|
|
|
2021-07-25 18:31:42 +02:00
|
|
|
templates = mkOption {
|
|
|
|
type = types.attrsOf (types.submodule {
|
|
|
|
freeformType = datasetSettingsType;
|
|
|
|
options = commonOptions;
|
|
|
|
});
|
|
|
|
default = { };
|
|
|
|
description = "Templates for datasets.";
|
|
|
|
};
|
2019-10-27 04:37:30 +01:00
|
|
|
|
2021-07-25 18:31:42 +02:00
|
|
|
settings = mkOption {
|
|
|
|
type = types.attrsOf datasetSettingsType;
|
|
|
|
description = ''
|
|
|
|
Free-form settings written directly to the config file. See
|
|
|
|
<link xlink:href="https://github.com/jimsalterjrs/sanoid/blob/master/sanoid.defaults.conf"/>
|
|
|
|
for allowed values.
|
|
|
|
'';
|
|
|
|
};
|
2019-10-27 04:37:30 +01:00
|
|
|
|
2021-07-25 18:31:42 +02:00
|
|
|
extraArgs = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [ ];
|
|
|
|
example = [ "--verbose" "--readonly" "--debug" ];
|
|
|
|
description = ''
|
|
|
|
Extra arguments to pass to sanoid. See
|
|
|
|
<link xlink:href="https://github.com/jimsalterjrs/sanoid/#sanoid-command-line-options"/>
|
|
|
|
for allowed options.
|
|
|
|
'';
|
2019-10-27 04:37:30 +01:00
|
|
|
};
|
2021-07-25 18:31:42 +02:00
|
|
|
};
|
2019-10-27 04:37:30 +01:00
|
|
|
|
2021-07-25 18:31:42 +02:00
|
|
|
# Implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
services.sanoid.settings = mkMerge [
|
|
|
|
(mapAttrs' (d: v: nameValuePair ("template_" + d) v) cfg.templates)
|
|
|
|
(mapAttrs (d: v: v) cfg.datasets)
|
|
|
|
];
|
|
|
|
|
|
|
|
systemd.services.sanoid = {
|
|
|
|
description = "Sanoid snapshot service";
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStartPre = (map (buildAllowCommand "allow" [ "snapshot" "mount" "destroy" ]) datasets);
|
|
|
|
ExecStopPost = (map (buildAllowCommand "unallow" [ "snapshot" "mount" "destroy" ]) datasets);
|
|
|
|
ExecStart = lib.escapeShellArgs ([
|
|
|
|
"${pkgs.sanoid}/bin/sanoid"
|
|
|
|
"--cron"
|
|
|
|
"--configdir"
|
|
|
|
(pkgs.writeTextDir "sanoid.conf" configFile)
|
|
|
|
] ++ cfg.extraArgs);
|
|
|
|
User = "sanoid";
|
|
|
|
Group = "sanoid";
|
|
|
|
DynamicUser = true;
|
|
|
|
RuntimeDirectory = "sanoid";
|
|
|
|
CacheDirectory = "sanoid";
|
2019-10-27 04:37:30 +01:00
|
|
|
};
|
2021-07-25 18:31:42 +02:00
|
|
|
# Prevents missing snapshots during DST changes
|
|
|
|
environment.TZ = "UTC";
|
|
|
|
after = [ "zfs.target" ];
|
|
|
|
startAt = cfg.interval;
|
2019-10-27 04:37:30 +01:00
|
|
|
};
|
2021-07-25 18:31:42 +02:00
|
|
|
};
|
2019-10-27 04:37:30 +01:00
|
|
|
|
2021-07-25 18:31:42 +02:00
|
|
|
meta.maintainers = with maintainers; [ lopsided98 ];
|
|
|
|
}
|