From 3c9f71da0e6008cf1d59c2b8668b63074e35c63b Mon Sep 17 00:00:00 2001 From: ibizaman Date: Fri, 8 Dec 2023 10:44:47 -0800 Subject: [PATCH] allow to stagger backup jobs --- docs/default.nix | 30 +++++++++++++++++++++++----- modules/blocks/backup.nix | 42 ++++++++++++++++++++++++++++----------- 2 files changed, 55 insertions(+), 17 deletions(-) diff --git a/docs/default.nix b/docs/default.nix index 619aec1..615d1d3 100644 --- a/docs/default.nix +++ b/docs/default.nix @@ -25,12 +25,32 @@ let ghRoot = (gitHubDeclaration "ibizaman" "selfhostblocks" "").url; buildOptionsDocs = args@{ modules, includeModuleSystemOptions ? true, ... }: - let options = (lib.evalModules { inherit modules; }).options; + let + config = { + _module.check = false; + _module.args = {}; + system.stateVersion = "22.11"; + }; + + utils = import "${pkgs.path}/nixos/lib/utils.nix" { + inherit config lib; + pkgs = null; + }; + + eval = lib.evalModules { + inherit modules; + + specialArgs = { + inherit utils; + }; + }; + + options = if includeModuleSystemOptions + then eval.options + else builtins.removeAttrs eval.options [ "_module" ]; in buildPackages.nixosOptionsDoc ({ - options = if includeModuleSystemOptions then - options - else - builtins.removeAttrs options [ "_module" ]; + inherit options; + transformOptions = opt: opt // { # Clean up declaration sites to not refer to the Home Manager diff --git a/modules/blocks/backup.nix b/modules/blocks/backup.nix index 387f645..3e57d58 100644 --- a/modules/blocks/backup.nix +++ b/modules/blocks/backup.nix @@ -1,4 +1,4 @@ -{ config, pkgs, lib, ... }: +{ config, pkgs, lib, utils, ... }: let cfg = config.shb.backup; @@ -37,7 +37,28 @@ let repositories = lib.mkOption { description = "Repositories to back this instance to."; - type = lib.types.nonEmptyListOf lib.types.str; + type = lib.types.nonEmptyListOf (lib.types.submodule { + options = { + path = lib.mkOption { + type = lib.types.str; + description = "Repository location"; + }; + + timerConfig = lib.mkOption { + type = lib.types.attrsOf utils.systemdUtils.unitOptions.unitOption; + default = { + OnCalendar = "daily"; + Persistent = true; + }; + description = ''When to run the backup. See {manpage}`systemd.timer(5)` for details.''; + example = { + OnCalendar = "00:05"; + RandomizedDelaySec = "5h"; + Persistent = true; + }; + }; + }; + }); }; retention = lib.mkOption { @@ -191,15 +212,15 @@ in group = cfg.group; }; } - ] ++ lib.optional ((lib.filter (lib.strings.hasPrefix "s3") instance.repositories) != []) { + ] ++ lib.optional ((lib.filter ({path, ...}: lib.strings.hasPrefix "s3" path) instance.repositories) != []) { "${instance.backend}/environmentfiles/${if isNull instance.secretName then name else instance.secretName}" = { sopsFile = instance.keySopsFile; mode = "0440"; owner = cfg.user; group = cfg.group; }; - } ++ lib.optionals (instance.backend == "borgmatic") (lib.flatten (map (repository: { - "${instance.backend}/keys/${repoSlugName repository}" = { + } ++ lib.optionals (instance.backend == "borgmatic") (lib.flatten (map ({path, ...}: { + "${instance.backend}/keys/${repoSlugName path}" = { key = "${instance.backend}/keys/${if isNull instance.secretName then name else instance.secretName}"; sopsFile = instance.keySopsFile; mode = "0440"; @@ -244,7 +265,7 @@ in location = { source_directories = instance.sourceDirectories; - repositories = instance.repositories; + repositories = map ({path, ...}: path) instance.repositories; } // (lib.attrsets.optionalAttrs (builtins.length instance.excludePatterns > 0) { excludePatterns = instance.excludePatterns; @@ -284,9 +305,9 @@ in services.restic.backups = let mkRepositorySettings = name: instance: repository: { - "${name}_${repoSlugName repository}" = { + "${name}_${repoSlugName repository.path}" = { inherit (cfg) user; - inherit repository; + repository = repository.path; paths = instance.sourceDirectories; @@ -294,10 +315,7 @@ in initialize = true; - timerConfig = { - OnCalendar = "00,12:00:00"; - RandomizedDelaySec = "5m"; - }; + inherit (repository) timerConfig; pruneOpts = lib.mapAttrsToList (name: value: "--${builtins.replaceStrings ["_"] ["-"] name} ${builtins.toString value}"