nixos/services.snapraid: remove with lib;

This commit is contained in:
Felix Buehler 2024-08-28 21:18:54 +02:00 committed by Jörg Thalheim
parent 4682ba9d88
commit 48d46271cb

View File

@ -1,18 +1,15 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib;
let cfg = config.services.snapraid; let cfg = config.services.snapraid;
in in
{ {
imports = [ imports = [
# Should have never been on the top-level. # Should have never been on the top-level.
(mkRenamedOptionModule [ "snapraid" ] [ "services" "snapraid" ]) (lib.mkRenamedOptionModule [ "snapraid" ] [ "services" "snapraid" ])
]; ];
options.services.snapraid = with types; { options.services.snapraid = with lib.types; {
enable = mkEnableOption "SnapRAID"; enable = lib.mkEnableOption "SnapRAID";
dataDisks = mkOption { dataDisks = lib.mkOption {
default = { }; default = { };
example = { example = {
d1 = "/mnt/disk1/"; d1 = "/mnt/disk1/";
@ -22,7 +19,7 @@ in
description = "SnapRAID data disks."; description = "SnapRAID data disks.";
type = attrsOf str; type = attrsOf str;
}; };
parityFiles = mkOption { parityFiles = lib.mkOption {
default = [ ]; default = [ ];
example = [ example = [
"/mnt/diskp/snapraid.parity" "/mnt/diskp/snapraid.parity"
@ -35,7 +32,7 @@ in
description = "SnapRAID parity files."; description = "SnapRAID parity files.";
type = listOf str; type = listOf str;
}; };
contentFiles = mkOption { contentFiles = lib.mkOption {
default = [ ]; default = [ ];
example = [ example = [
"/var/snapraid.content" "/var/snapraid.content"
@ -45,40 +42,40 @@ in
description = "SnapRAID content list files."; description = "SnapRAID content list files.";
type = listOf str; type = listOf str;
}; };
exclude = mkOption { exclude = lib.mkOption {
default = [ ]; default = [ ];
example = [ "*.unrecoverable" "/tmp/" "/lost+found/" ]; example = [ "*.unrecoverable" "/tmp/" "/lost+found/" ];
description = "SnapRAID exclude directives."; description = "SnapRAID exclude directives.";
type = listOf str; type = listOf str;
}; };
touchBeforeSync = mkOption { touchBeforeSync = lib.mkOption {
default = true; default = true;
example = false; example = false;
description = description =
"Whether {command}`snapraid touch` should be run before {command}`snapraid sync`."; "Whether {command}`snapraid touch` should be run before {command}`snapraid sync`.";
type = bool; type = bool;
}; };
sync.interval = mkOption { sync.interval = lib.mkOption {
default = "01:00"; default = "01:00";
example = "daily"; example = "daily";
description = "How often to run {command}`snapraid sync`."; description = "How often to run {command}`snapraid sync`.";
type = str; type = str;
}; };
scrub = { scrub = {
interval = mkOption { interval = lib.mkOption {
default = "Mon *-*-* 02:00:00"; default = "Mon *-*-* 02:00:00";
example = "weekly"; example = "weekly";
description = "How often to run {command}`snapraid scrub`."; description = "How often to run {command}`snapraid scrub`.";
type = str; type = str;
}; };
plan = mkOption { plan = lib.mkOption {
default = 8; default = 8;
example = 5; example = 5;
description = description =
"Percent of the array that should be checked by {command}`snapraid scrub`."; "Percent of the array that should be checked by {command}`snapraid scrub`.";
type = int; type = int;
}; };
olderThan = mkOption { olderThan = lib.mkOption {
default = 10; default = 10;
example = 20; example = 20;
description = description =
@ -86,7 +83,7 @@ in
type = int; type = int;
}; };
}; };
extraConfig = mkOption { extraConfig = lib.mkOption {
default = ""; default = "";
example = '' example = ''
nohidden nohidden
@ -105,7 +102,7 @@ in
nParity = builtins.length cfg.parityFiles; nParity = builtins.length cfg.parityFiles;
mkPrepend = pre: s: pre + s; mkPrepend = pre: s: pre + s;
in in
mkIf cfg.enable { lib.mkIf cfg.enable {
assertions = [ assertions = [
{ {
assertion = nParity <= 6; assertion = nParity <= 6;
@ -128,9 +125,9 @@ in
prependContent = mkPrepend "content "; prependContent = mkPrepend "content ";
prependExclude = mkPrepend "exclude "; prependExclude = mkPrepend "exclude ";
in in
concatStringsSep "\n" lib.concatStringsSep "\n"
(map prependData (map prependData
((mapAttrsToList (name: value: name + " " + value)) dataDisks) ((lib.mapAttrsToList (name: value: name + " " + value)) dataDisks)
++ zipListsWith (a: b: a + b) ++ zipListsWith (a: b: a + b)
([ "parity " ] ++ map (i: toString i + "-parity ") (range 2 6)) ([ "parity " ] ++ map (i: toString i + "-parity ") (range 2 6))
parityFiles ++ map prependContent contentFiles parityFiles ++ map prependContent contentFiles
@ -179,8 +176,8 @@ in
let let
contentDirs = map dirOf contentFiles; contentDirs = map dirOf contentFiles;
in in
unique ( lib.unique (
attrValues dataDisks ++ contentDirs lib.attrValues dataDisks ++ contentDirs
); );
}; };
unitConfig.After = "snapraid-sync.service"; unitConfig.After = "snapraid-sync.service";
@ -227,10 +224,10 @@ in
# https://www.snapraid.it/manual#7.1 # https://www.snapraid.it/manual#7.1
splitParityFiles = map (s: splitString "," s) parityFiles; splitParityFiles = map (s: splitString "," s) parityFiles;
in in
unique ( lib.unique (
attrValues dataDisks ++ splitParityFiles ++ contentDirs lib.attrValues dataDisks ++ splitParityFiles ++ contentDirs
); );
} // optionalAttrs touchBeforeSync { } // lib.optionalAttrs touchBeforeSync {
ExecStartPre = "${pkgs.snapraid}/bin/snapraid touch"; ExecStartPre = "${pkgs.snapraid}/bin/snapraid touch";
}; };
}; };