From b14a520b064565b7d913da02734058183789a8ec Mon Sep 17 00:00:00 2001 From: DavHau Date: Mon, 26 Aug 2024 15:23:59 +0200 Subject: [PATCH] remove clanDir default The default value for clanDir did not make sense, as it pointed to the directory of the module definition Also, we should not have a default at this level as all. --- checks/backups/flake-module.nix | 1 + checks/postgresql/default.nix | 1 + clanModules/borgbackup-static/default.nix | 184 +++++++++++----------- nixosModules/clanCore/metadata.nix | 1 - 4 files changed, 95 insertions(+), 92 deletions(-) diff --git a/checks/backups/flake-module.nix b/checks/backups/flake-module.nix index b06f15f83..d237cb138 100644 --- a/checks/backups/flake-module.nix +++ b/checks/backups/flake-module.nix @@ -133,6 +133,7 @@ self.nixosModules.test-backup ]; virtualisation.emptyDiskImages = [ 256 ]; + clan.core.clanDir = ./.; }; testScript = '' diff --git a/checks/postgresql/default.nix b/checks/postgresql/default.nix index eb1c7e4c5..595e00203 100644 --- a/checks/postgresql/default.nix +++ b/checks/postgresql/default.nix @@ -13,6 +13,7 @@ clan.postgresql.databases.test.create.options.OWNER = "test"; clan.postgresql.databases.test.restore.stopOnRestore = [ "sample-service" ]; clan.localbackup.targets.hdd.directory = "/mnt/external-disk"; + clan.core.clanDir = ./.; systemd.services.sample-service = { wantedBy = [ "multi-user.target" ]; diff --git a/clanModules/borgbackup-static/default.nix b/clanModules/borgbackup-static/default.nix index 931ea15ff..9c76e486e 100644 --- a/clanModules/borgbackup-static/default.nix +++ b/clanModules/borgbackup-static/default.nix @@ -3,99 +3,101 @@ let clanDir = config.clan.core.clanDir; machineDir = clanDir + "/machines/"; in -lib.warn "This module is deprecated use the service via the inventory interface instead." { - imports = [ ../borgbackup ]; +lib.warn + "The borgbackup-static module is deprecated use the service via the inventory interface instead." + { + imports = [ ../borgbackup ]; - options.clan.borgbackup-static = { - excludeMachines = lib.mkOption { - type = lib.types.listOf lib.types.str; - example = [ config.clan.core.machineName ]; - default = [ ]; - description = '' - Machines that should not be backuped. - Mutually exclusive with includeMachines. - If this is not empty, every other machine except the targets in the clan will be backuped by this module. - If includeMachines is set, only the included machines will be backuped. - ''; + options.clan.borgbackup-static = { + excludeMachines = lib.mkOption { + type = lib.types.listOf lib.types.str; + example = [ config.clan.core.machineName ]; + default = [ ]; + description = '' + Machines that should not be backuped. + Mutually exclusive with includeMachines. + If this is not empty, every other machine except the targets in the clan will be backuped by this module. + If includeMachines is set, only the included machines will be backuped. + ''; + }; + includeMachines = lib.mkOption { + type = lib.types.listOf lib.types.str; + example = [ config.clan.core.machineName ]; + default = [ ]; + description = '' + Machines that should be backuped. + Mutually exclusive with excludeMachines. + ''; + }; + targets = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = [ ]; + description = '' + Machines that should act as target machines for backups. + ''; + }; }; - includeMachines = lib.mkOption { - type = lib.types.listOf lib.types.str; - example = [ config.clan.core.machineName ]; - default = [ ]; - description = '' - Machines that should be backuped. - Mutually exclusive with excludeMachines. - ''; - }; - targets = lib.mkOption { - type = lib.types.listOf lib.types.str; - default = [ ]; - description = '' - Machines that should act as target machines for backups. - ''; - }; - }; - config.services.borgbackup.repos = - let - machines = builtins.readDir machineDir; - borgbackupIpMachinePath = machines: machineDir + machines + "/facts/borgbackup.ssh.pub"; - filteredMachines = - if ((builtins.length config.clan.borgbackup-static.includeMachines) != 0) then - lib.filterAttrs (name: _: (lib.elem name config.clan.borgbackup-static.includeMachines)) machines - else - lib.filterAttrs (name: _: !(lib.elem name config.clan.borgbackup-static.excludeMachines)) machines; - machinesMaybeKey = lib.mapAttrsToList ( - machine: _: - let - fullPath = borgbackupIpMachinePath machine; - in - if builtins.pathExists fullPath then machine else null - ) filteredMachines; - machinesWithKey = lib.filter (x: x != null) machinesMaybeKey; - hosts = builtins.map (machine: { - name = machine; - value = { - path = "/var/lib/borgbackup/${machine}"; - authorizedKeys = [ (builtins.readFile (borgbackupIpMachinePath machine)) ]; - }; - }) machinesWithKey; - in - lib.mkIf - (builtins.any ( + config.services.borgbackup.repos = + let + machines = builtins.readDir machineDir; + borgbackupIpMachinePath = machines: machineDir + machines + "/facts/borgbackup.ssh.pub"; + filteredMachines = + if ((builtins.length config.clan.borgbackup-static.includeMachines) != 0) then + lib.filterAttrs (name: _: (lib.elem name config.clan.borgbackup-static.includeMachines)) machines + else + lib.filterAttrs (name: _: !(lib.elem name config.clan.borgbackup-static.excludeMachines)) machines; + machinesMaybeKey = lib.mapAttrsToList ( + machine: _: + let + fullPath = borgbackupIpMachinePath machine; + in + if builtins.pathExists fullPath then machine else null + ) filteredMachines; + machinesWithKey = lib.filter (x: x != null) machinesMaybeKey; + hosts = builtins.map (machine: { + name = machine; + value = { + path = "/var/lib/borgbackup/${machine}"; + authorizedKeys = [ (builtins.readFile (borgbackupIpMachinePath machine)) ]; + }; + }) machinesWithKey; + in + lib.mkIf + (builtins.any ( + target: target == config.clan.core.machineName + ) config.clan.borgbackup-static.targets) + (if (builtins.listToAttrs hosts) != null then builtins.listToAttrs hosts else { }); + + config.clan.borgbackup.destinations = + let + destinations = builtins.map (d: { + name = d; + value = { + repo = "borg@${d}:/var/lib/borgbackup/${config.clan.core.machineName}"; + }; + }) config.clan.borgbackup-static.targets; + in + lib.mkIf (builtins.any ( target: target == config.clan.core.machineName - ) config.clan.borgbackup-static.targets) - (if (builtins.listToAttrs hosts) != null then builtins.listToAttrs hosts else { }); + ) config.clan.borgbackup-static.includeMachines) (builtins.listToAttrs destinations); - config.clan.borgbackup.destinations = - let - destinations = builtins.map (d: { - name = d; - value = { - repo = "borg@${d}:/var/lib/borgbackup/${config.clan.core.machineName}"; - }; - }) config.clan.borgbackup-static.targets; - in - lib.mkIf (builtins.any ( - target: target == config.clan.core.machineName - ) config.clan.borgbackup-static.includeMachines) (builtins.listToAttrs destinations); - - config.assertions = [ - { - assertion = - !( - ((builtins.length config.clan.borgbackup-static.excludeMachines) != 0) - && ((builtins.length config.clan.borgbackup-static.includeMachines) != 0) - ); - message = '' - The options: - config.clan.borgbackup-static.excludeMachines = [${builtins.toString config.clan.borgbackup-static.excludeMachines}] - and - config.clan.borgbackup-static.includeMachines = [${builtins.toString config.clan.borgbackup-static.includeMachines}] - are mutually exclusive. - Use excludeMachines to exclude certain machines and backup the other clan machines. - Use include machines to only backup certain machines. - ''; - } - ]; -} + config.assertions = [ + { + assertion = + !( + ((builtins.length config.clan.borgbackup-static.excludeMachines) != 0) + && ((builtins.length config.clan.borgbackup-static.includeMachines) != 0) + ); + message = '' + The options: + config.clan.borgbackup-static.excludeMachines = [${builtins.toString config.clan.borgbackup-static.excludeMachines}] + and + config.clan.borgbackup-static.includeMachines = [${builtins.toString config.clan.borgbackup-static.includeMachines}] + are mutually exclusive. + Use excludeMachines to exclude certain machines and backup the other clan machines. + Use include machines to only backup certain machines. + ''; + } + ]; + } diff --git a/nixosModules/clanCore/metadata.nix b/nixosModules/clanCore/metadata.nix index 6f037a8a0..50dd2fb56 100644 --- a/nixosModules/clanCore/metadata.nix +++ b/nixosModules/clanCore/metadata.nix @@ -45,7 +45,6 @@ }; clanDir = lib.mkOption { type = lib.types.path; - default = ./.; description = '' the location of the flake repo, used to calculate the location of facts and secrets '';