2021-07-04 02:40:35 +00:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
let
|
2022-06-16 17:23:12 +00:00
|
|
|
inherit (lib)
|
2022-10-30 15:09:59 +00:00
|
|
|
concatLists
|
|
|
|
concatMap
|
2022-06-16 17:23:12 +00:00
|
|
|
concatMapStringsSep
|
|
|
|
concatStringsSep
|
|
|
|
filterAttrs
|
|
|
|
isAttrs
|
|
|
|
literalExpression
|
|
|
|
mapAttrs'
|
|
|
|
mapAttrsToList
|
|
|
|
mkIf
|
|
|
|
mkOption
|
|
|
|
optionalString
|
2022-10-30 15:09:59 +00:00
|
|
|
sort
|
2022-06-16 17:23:12 +00:00
|
|
|
types
|
|
|
|
;
|
|
|
|
|
2022-10-30 15:09:59 +00:00
|
|
|
# The priority of an option or section.
|
|
|
|
# The configurations format are order-sensitive. Pairs are added as children of
|
|
|
|
# the last sections if possible, otherwise, they start a new section.
|
|
|
|
# We sort them in topological order:
|
|
|
|
# 1. Leaf pairs.
|
|
|
|
# 2. Sections that may contain (1).
|
|
|
|
# 3. Sections that may contain (1) or (2).
|
|
|
|
# 4. Etc.
|
|
|
|
prioOf = { name, value }:
|
|
|
|
if !isAttrs value then 0 # Leaf options.
|
|
|
|
else {
|
|
|
|
target = 1; # Contains: options.
|
|
|
|
subvolume = 2; # Contains: options, target.
|
|
|
|
volume = 3; # Contains: options, target, subvolume.
|
|
|
|
}.${name} or (throw "Unknow section '${name}'");
|
|
|
|
|
|
|
|
genConfig' = set: concatStringsSep "\n" (genConfig set);
|
|
|
|
genConfig = set:
|
2021-07-04 02:40:35 +00:00
|
|
|
let
|
2022-10-30 15:09:59 +00:00
|
|
|
pairs = mapAttrsToList (name: value: { inherit name value; }) set;
|
|
|
|
sortedPairs = sort (a: b: prioOf a < prioOf b) pairs;
|
2021-07-04 02:40:35 +00:00
|
|
|
in
|
2022-10-30 15:09:59 +00:00
|
|
|
concatMap genPair sortedPairs;
|
|
|
|
genSection = sec: secName: value:
|
|
|
|
[ "${sec} ${secName}" ] ++ map (x: " " + x) (genConfig value);
|
|
|
|
genPair = { name, value }:
|
|
|
|
if !isAttrs value
|
|
|
|
then [ "${name} ${value}" ]
|
|
|
|
else concatLists (mapAttrsToList (genSection name) value);
|
|
|
|
|
2023-03-15 16:39:30 +00:00
|
|
|
sudo_doas =
|
|
|
|
if config.security.sudo.enable then "sudo"
|
|
|
|
else if config.security.doas.enable then "doas"
|
|
|
|
else throw "The btrbk nixos module needs either sudo or doas enabled in the configuration";
|
|
|
|
|
|
|
|
addDefaults = settings: { backend = "btrfs-progs-${sudo_doas}"; } // settings;
|
2022-10-30 15:09:59 +00:00
|
|
|
|
|
|
|
mkConfigFile = name: settings: pkgs.writeTextFile {
|
|
|
|
name = "btrbk-${name}.conf";
|
|
|
|
text = genConfig' (addDefaults settings);
|
|
|
|
checkPhase = ''
|
|
|
|
set +e
|
|
|
|
${pkgs.btrbk}/bin/btrbk -c $out dryrun
|
|
|
|
# According to btrbk(1), exit status 2 means parse error
|
|
|
|
# for CLI options or the config file.
|
|
|
|
if [[ $? == 2 ]]; then
|
|
|
|
echo "Btrbk configuration is invalid:"
|
|
|
|
cat $out
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
set -e
|
2021-07-04 02:40:35 +00:00
|
|
|
'';
|
2022-10-30 15:09:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
cfg = config.services.btrbk;
|
|
|
|
sshEnabled = cfg.sshAccess != [ ];
|
|
|
|
serviceEnabled = cfg.instances != { };
|
2021-07-04 02:40:35 +00:00
|
|
|
in
|
|
|
|
{
|
2022-06-16 17:23:12 +00:00
|
|
|
meta.maintainers = with lib.maintainers; [ oxalica ];
|
|
|
|
|
2021-07-04 02:40:35 +00:00
|
|
|
options = {
|
|
|
|
services.btrbk = {
|
2022-06-16 17:23:12 +00:00
|
|
|
extraPackages = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Extra packages for btrbk, like compression utilities for `stream_compress`";
|
2022-06-16 17:23:12 +00:00
|
|
|
type = types.listOf types.package;
|
2021-07-04 02:40:35 +00:00
|
|
|
default = [ ];
|
2022-06-16 17:23:12 +00:00
|
|
|
example = literalExpression "[ pkgs.xz ]";
|
2021-07-04 02:40:35 +00:00
|
|
|
};
|
2022-06-16 17:23:12 +00:00
|
|
|
niceness = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Niceness for local instances of btrbk. Also applies to remote ones connecting via ssh when positive.";
|
2022-06-16 17:23:12 +00:00
|
|
|
type = types.ints.between (-20) 19;
|
2021-07-04 02:40:35 +00:00
|
|
|
default = 10;
|
|
|
|
};
|
2022-06-16 17:23:12 +00:00
|
|
|
ioSchedulingClass = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "IO scheduling class for btrbk (see ionice(1) for a quick description). Applies to local instances, and remote ones connecting by ssh if set to idle.";
|
2022-06-16 17:23:12 +00:00
|
|
|
type = types.enum [ "idle" "best-effort" "realtime" ];
|
2021-07-04 02:40:35 +00:00
|
|
|
default = "best-effort";
|
|
|
|
};
|
2022-06-16 17:23:12 +00:00
|
|
|
instances = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Set of btrbk instances. The instance named `btrbk` is the default one.";
|
2022-06-16 17:23:12 +00:00
|
|
|
type = with types;
|
2021-07-04 02:40:35 +00:00
|
|
|
attrsOf (
|
|
|
|
submodule {
|
|
|
|
options = {
|
2022-06-16 17:23:12 +00:00
|
|
|
onCalendar = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
2021-07-04 02:40:35 +00:00
|
|
|
default = "daily";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-06-16 17:23:12 +00:00
|
|
|
How often this btrbk instance is started. See systemd.time(7) for more information about the format.
|
|
|
|
Setting it to null disables the timer, thus this instance can only be started manually.
|
|
|
|
'';
|
2021-07-04 02:40:35 +00:00
|
|
|
};
|
2022-06-16 17:23:12 +00:00
|
|
|
settings = mkOption {
|
|
|
|
type = let t = types.attrsOf (types.either types.str (t // { description = "instances of this type recursively"; })); in t;
|
2021-07-04 02:40:35 +00:00
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
snapshot_preserve_min = "2d";
|
|
|
|
snapshot_preserve = "14d";
|
|
|
|
volume = {
|
|
|
|
"/mnt/btr_pool" = {
|
|
|
|
target = "/mnt/btr_backup/mylaptop";
|
|
|
|
subvolume = {
|
|
|
|
"rootfs" = { };
|
|
|
|
"home" = { snapshot_create = "always"; };
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "configuration options for btrbk. Nested attrsets translate to subsections.";
|
2021-07-04 02:40:35 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
default = { };
|
|
|
|
};
|
2022-06-16 17:23:12 +00:00
|
|
|
sshAccess = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "SSH keys that should be able to make or push snapshots on this system remotely with btrbk";
|
2022-06-16 17:23:12 +00:00
|
|
|
type = with types; listOf (
|
2021-07-04 02:40:35 +00:00
|
|
|
submodule {
|
|
|
|
options = {
|
2022-06-16 17:23:12 +00:00
|
|
|
key = mkOption {
|
2021-07-04 02:40:35 +00:00
|
|
|
type = str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "SSH public key allowed to login as user `btrbk` to run remote backups.";
|
2021-07-04 02:40:35 +00:00
|
|
|
};
|
2022-06-16 17:23:12 +00:00
|
|
|
roles = mkOption {
|
2021-07-04 02:40:35 +00:00
|
|
|
type = listOf (enum [ "info" "source" "target" "delete" "snapshot" "send" "receive" ]);
|
|
|
|
example = [ "source" "info" "send" ];
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "What actions can be performed with this SSH key. See ssh_filter_btrbk(1) for details";
|
2021-07-04 02:40:35 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
default = [ ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
2022-06-16 17:23:12 +00:00
|
|
|
config = mkIf (sshEnabled || serviceEnabled) {
|
2021-07-04 02:40:35 +00:00
|
|
|
environment.systemPackages = [ pkgs.btrbk ] ++ cfg.extraPackages;
|
2023-03-15 16:39:30 +00:00
|
|
|
security.sudo = mkIf (sudo_doas == "sudo") {
|
|
|
|
extraRules = [
|
|
|
|
{
|
|
|
|
users = [ "btrbk" ];
|
|
|
|
commands = [
|
|
|
|
{ command = "${pkgs.btrfs-progs}/bin/btrfs"; options = [ "NOPASSWD" ]; }
|
|
|
|
{ command = "${pkgs.coreutils}/bin/mkdir"; options = [ "NOPASSWD" ]; }
|
|
|
|
{ command = "${pkgs.coreutils}/bin/readlink"; options = [ "NOPASSWD" ]; }
|
|
|
|
# for ssh, they are not the same than the one hard coded in ${pkgs.btrbk}
|
2023-10-09 19:29:22 +00:00
|
|
|
{ command = "/run/current-system/sw/bin/btrfs"; options = [ "NOPASSWD" ]; }
|
2023-03-15 16:39:30 +00:00
|
|
|
{ command = "/run/current-system/sw/bin/mkdir"; options = [ "NOPASSWD" ]; }
|
|
|
|
{ command = "/run/current-system/sw/bin/readlink"; options = [ "NOPASSWD" ]; }
|
|
|
|
];
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
security.doas = mkIf (sudo_doas == "doas") {
|
|
|
|
extraRules = let
|
|
|
|
doasCmdNoPass = cmd: { users = [ "btrbk" ]; cmd = cmd; noPass = true; };
|
|
|
|
in
|
|
|
|
[
|
|
|
|
(doasCmdNoPass "${pkgs.btrfs-progs}/bin/btrfs")
|
|
|
|
(doasCmdNoPass "${pkgs.coreutils}/bin/mkdir")
|
|
|
|
(doasCmdNoPass "${pkgs.coreutils}/bin/readlink")
|
|
|
|
# for ssh, they are not the same than the one hard coded in ${pkgs.btrbk}
|
2023-10-09 19:29:22 +00:00
|
|
|
(doasCmdNoPass "/run/current-system/sw/bin/btrfs")
|
2023-03-15 16:39:30 +00:00
|
|
|
(doasCmdNoPass "/run/current-system/sw/bin/mkdir")
|
|
|
|
(doasCmdNoPass "/run/current-system/sw/bin/readlink")
|
|
|
|
|
|
|
|
# doas matches command, not binary
|
|
|
|
(doasCmdNoPass "btrfs")
|
|
|
|
(doasCmdNoPass "mkdir")
|
|
|
|
(doasCmdNoPass "readlink")
|
2021-07-04 02:40:35 +00:00
|
|
|
];
|
2023-03-15 16:39:30 +00:00
|
|
|
};
|
2021-07-04 02:40:35 +00:00
|
|
|
users.users.btrbk = {
|
|
|
|
isSystemUser = true;
|
|
|
|
# ssh needs a home directory
|
|
|
|
home = "/var/lib/btrbk";
|
|
|
|
createHome = true;
|
|
|
|
shell = "${pkgs.bash}/bin/bash";
|
|
|
|
group = "btrbk";
|
|
|
|
openssh.authorizedKeys.keys = map
|
|
|
|
(
|
|
|
|
v:
|
|
|
|
let
|
2022-06-16 17:23:12 +00:00
|
|
|
options = concatMapStringsSep " " (x: "--" + x) v.roles;
|
2021-07-04 02:40:35 +00:00
|
|
|
ioniceClass = {
|
|
|
|
"idle" = 3;
|
|
|
|
"best-effort" = 2;
|
|
|
|
"realtime" = 1;
|
|
|
|
}.${cfg.ioSchedulingClass};
|
2023-03-15 16:39:30 +00:00
|
|
|
sudo_doas_flag = "--${sudo_doas}";
|
2021-07-04 02:40:35 +00:00
|
|
|
in
|
2023-03-15 16:39:30 +00:00
|
|
|
''command="${pkgs.util-linux}/bin/ionice -t -c ${toString ioniceClass} ${optionalString (cfg.niceness >= 1) "${pkgs.coreutils}/bin/nice -n ${toString cfg.niceness}"} ${pkgs.btrbk}/share/btrbk/scripts/ssh_filter_btrbk.sh ${sudo_doas_flag} ${options}" ${v.key}''
|
2021-07-04 02:40:35 +00:00
|
|
|
)
|
|
|
|
cfg.sshAccess;
|
|
|
|
};
|
|
|
|
users.groups.btrbk = { };
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d /var/lib/btrbk 0750 btrbk btrbk"
|
|
|
|
"d /var/lib/btrbk/.ssh 0700 btrbk btrbk"
|
|
|
|
"f /var/lib/btrbk/.ssh/config 0700 btrbk btrbk - StrictHostKeyChecking=accept-new"
|
|
|
|
];
|
2022-06-16 17:23:12 +00:00
|
|
|
environment.etc = mapAttrs'
|
2021-07-04 02:40:35 +00:00
|
|
|
(
|
|
|
|
name: instance: {
|
|
|
|
name = "btrbk/${name}.conf";
|
2022-10-30 15:09:59 +00:00
|
|
|
value.source = mkConfigFile name instance.settings;
|
2021-07-04 02:40:35 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
cfg.instances;
|
2022-06-16 17:23:12 +00:00
|
|
|
systemd.services = mapAttrs'
|
2021-07-04 02:40:35 +00:00
|
|
|
(
|
|
|
|
name: _: {
|
|
|
|
name = "btrbk-${name}";
|
|
|
|
value = {
|
|
|
|
description = "Takes BTRFS snapshots and maintains retention policies.";
|
|
|
|
unitConfig.Documentation = "man:btrbk(1)";
|
|
|
|
path = [ "/run/wrappers" ] ++ cfg.extraPackages;
|
|
|
|
serviceConfig = {
|
|
|
|
User = "btrbk";
|
|
|
|
Group = "btrbk";
|
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${pkgs.btrbk}/bin/btrbk -c /etc/btrbk/${name}.conf run";
|
|
|
|
Nice = cfg.niceness;
|
|
|
|
IOSchedulingClass = cfg.ioSchedulingClass;
|
|
|
|
StateDirectory = "btrbk";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
cfg.instances;
|
|
|
|
|
2022-06-16 17:23:12 +00:00
|
|
|
systemd.timers = mapAttrs'
|
2021-07-04 02:40:35 +00:00
|
|
|
(
|
|
|
|
name: instance: {
|
|
|
|
name = "btrbk-${name}";
|
|
|
|
value = {
|
|
|
|
description = "Timer to take BTRFS snapshots and maintain retention policies.";
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = instance.onCalendar;
|
|
|
|
AccuracySec = "10min";
|
|
|
|
Persistent = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
2022-06-16 17:23:12 +00:00
|
|
|
(filterAttrs (name: instance: instance.onCalendar != null)
|
|
|
|
cfg.instances);
|
2021-07-04 02:40:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|