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.
This commit is contained in:
DavHau
2024-08-26 15:23:59 +02:00
parent f9c8dd1518
commit 61e1fd7f85
4 changed files with 95 additions and 92 deletions

View File

@@ -133,6 +133,7 @@
self.nixosModules.test-backup self.nixosModules.test-backup
]; ];
virtualisation.emptyDiskImages = [ 256 ]; virtualisation.emptyDiskImages = [ 256 ];
clan.core.clanDir = ./.;
}; };
testScript = '' testScript = ''

View File

@@ -13,6 +13,7 @@
clan.postgresql.databases.test.create.options.OWNER = "test"; clan.postgresql.databases.test.create.options.OWNER = "test";
clan.postgresql.databases.test.restore.stopOnRestore = [ "sample-service" ]; clan.postgresql.databases.test.restore.stopOnRestore = [ "sample-service" ];
clan.localbackup.targets.hdd.directory = "/mnt/external-disk"; clan.localbackup.targets.hdd.directory = "/mnt/external-disk";
clan.core.clanDir = ./.;
systemd.services.sample-service = { systemd.services.sample-service = {
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];

View File

@@ -3,99 +3,101 @@ let
clanDir = config.clan.core.clanDir; clanDir = config.clan.core.clanDir;
machineDir = clanDir + "/machines/"; machineDir = clanDir + "/machines/";
in in
lib.warn "This module is deprecated use the service via the inventory interface instead." { lib.warn
imports = [ ../borgbackup ]; "The borgbackup-static module is deprecated use the service via the inventory interface instead."
{
imports = [ ../borgbackup ];
options.clan.borgbackup-static = { options.clan.borgbackup-static = {
excludeMachines = lib.mkOption { excludeMachines = lib.mkOption {
type = lib.types.listOf lib.types.str; type = lib.types.listOf lib.types.str;
example = [ config.clan.core.machineName ]; example = [ config.clan.core.machineName ];
default = [ ]; default = [ ];
description = '' description = ''
Machines that should not be backuped. Machines that should not be backuped.
Mutually exclusive with includeMachines. 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 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. 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 = config.services.borgbackup.repos =
let let
machines = builtins.readDir machineDir; machines = builtins.readDir machineDir;
borgbackupIpMachinePath = machines: machineDir + machines + "/facts/borgbackup.ssh.pub"; borgbackupIpMachinePath = machines: machineDir + machines + "/facts/borgbackup.ssh.pub";
filteredMachines = filteredMachines =
if ((builtins.length config.clan.borgbackup-static.includeMachines) != 0) then if ((builtins.length config.clan.borgbackup-static.includeMachines) != 0) then
lib.filterAttrs (name: _: (lib.elem name config.clan.borgbackup-static.includeMachines)) machines lib.filterAttrs (name: _: (lib.elem name config.clan.borgbackup-static.includeMachines)) machines
else else
lib.filterAttrs (name: _: !(lib.elem name config.clan.borgbackup-static.excludeMachines)) machines; lib.filterAttrs (name: _: !(lib.elem name config.clan.borgbackup-static.excludeMachines)) machines;
machinesMaybeKey = lib.mapAttrsToList ( machinesMaybeKey = lib.mapAttrsToList (
machine: _: machine: _:
let let
fullPath = borgbackupIpMachinePath machine; fullPath = borgbackupIpMachinePath machine;
in in
if builtins.pathExists fullPath then machine else null if builtins.pathExists fullPath then machine else null
) filteredMachines; ) filteredMachines;
machinesWithKey = lib.filter (x: x != null) machinesMaybeKey; machinesWithKey = lib.filter (x: x != null) machinesMaybeKey;
hosts = builtins.map (machine: { hosts = builtins.map (machine: {
name = machine; name = machine;
value = { value = {
path = "/var/lib/borgbackup/${machine}"; path = "/var/lib/borgbackup/${machine}";
authorizedKeys = [ (builtins.readFile (borgbackupIpMachinePath machine)) ]; authorizedKeys = [ (builtins.readFile (borgbackupIpMachinePath machine)) ];
}; };
}) machinesWithKey; }) machinesWithKey;
in in
lib.mkIf lib.mkIf
(builtins.any ( (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 target: target == config.clan.core.machineName
) config.clan.borgbackup-static.targets) ) config.clan.borgbackup-static.includeMachines) (builtins.listToAttrs destinations);
(if (builtins.listToAttrs hosts) != null then builtins.listToAttrs hosts else { });
config.clan.borgbackup.destinations = config.assertions = [
let {
destinations = builtins.map (d: { assertion =
name = d; !(
value = { ((builtins.length config.clan.borgbackup-static.excludeMachines) != 0)
repo = "borg@${d}:/var/lib/borgbackup/${config.clan.core.machineName}"; && ((builtins.length config.clan.borgbackup-static.includeMachines) != 0)
}; );
}) config.clan.borgbackup-static.targets; message = ''
in The options:
lib.mkIf (builtins.any ( config.clan.borgbackup-static.excludeMachines = [${builtins.toString config.clan.borgbackup-static.excludeMachines}]
target: target == config.clan.core.machineName and
) config.clan.borgbackup-static.includeMachines) (builtins.listToAttrs destinations); config.clan.borgbackup-static.includeMachines = [${builtins.toString config.clan.borgbackup-static.includeMachines}]
are mutually exclusive.
config.assertions = [ Use excludeMachines to exclude certain machines and backup the other clan machines.
{ Use include machines to only backup certain machines.
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.
'';
}
];
}

View File

@@ -45,7 +45,6 @@
}; };
clanDir = lib.mkOption { clanDir = lib.mkOption {
type = lib.types.path; type = lib.types.path;
default = ./.;
description = '' description = ''
the location of the flake repo, used to calculate the location of facts and secrets the location of the flake repo, used to calculate the location of facts and secrets
''; '';