diff --git a/lib/build-clan/computed-tags.nix b/lib/build-clan/computed-tags.nix index 920e984c4..36eba2c5c 100644 --- a/lib/build-clan/computed-tags.nix +++ b/lib/build-clan/computed-tags.nix @@ -1,33 +1,23 @@ { - config, lib, ... }: { - config.inventory = { + # Add the computed tags to machine tags for displaying them + inventory = { tags = ( { machines, ... }: { # Only compute the default value # The option MUST be defined in ./build-inventory/interface.nix all = lib.mkDefault (builtins.attrNames machines); + nixos = lib.mkDefault ( + builtins.attrNames (lib.filterAttrs (_n: m: m.machineClass == "nixos") machines) + ); + darwin = lib.mkDefault ( + builtins.attrNames (lib.filterAttrs (_n: m: m.machineClass == "darwin") machines) + ); } ); }; - # Add the computed tags to machine tags for displaying them - options.inventory = { - machines = lib.mkOption { - type = lib.types.attrsOf ( - lib.types.submodule ( - # 'name' is the machines attribute-name - { name, ... }: - { - tags = builtins.attrNames ( - lib.filterAttrs (_t: tagMembers: builtins.elem name tagMembers) config.inventory.tags - ); - } - ) - ); - }; - }; } diff --git a/lib/inventory/build-inventory/interface.nix b/lib/inventory/build-inventory/interface.nix index 45564f6d9..318cb2e91 100644 --- a/lib/inventory/build-inventory/interface.nix +++ b/lib/inventory/build-inventory/interface.nix @@ -223,6 +223,32 @@ in ``` ''; }; + nixos = lib.mkOption { + type = with lib.types; listOf str; + defaultText = "[ ]"; + description = '' + !!! example "Predefined Tag" + + Will be added to all machines that set `machineClass = "darwin"` + + ```nix + inventory.machines.machineA.tags = [ "nixos" ]; + ``` + ''; + }; + darwin = lib.mkOption { + type = with lib.types; listOf str; + defaultText = "[ ]"; + description = '' + !!! example "Predefined Tag" + + Will be added to all machines that set `machineClass = "darwin"` + + ```nix + inventory.machines.machineA.tags = [ "darwin" ]; + ``` + ''; + }; }; } ]; @@ -236,58 +262,71 @@ in Each machine declared here can be referencd via its `attributeName` by the `inventory.service`s `roles`. ''; default = { }; - type = types.attrsOf ( - types.submodule ( - { name, ... }: - { - options = { - inherit (metaOptionsWith name) name description icon; + type = types.lazyAttrsOf ( + types.submoduleWith ({ + modules = [ + ( + { name, ... }: + { + tags = builtins.attrNames ( + # config.tags + lib.filterAttrs (_t: tagMembers: builtins.elem name tagMembers) config.tags + ); + } + ) + ( + { name, ... }: + { + options = { + inherit (metaOptionsWith name) name description icon; - machineClass = lib.mkOption { - default = "nixos"; - type = types.enum [ - "nixos" - "darwin" - ]; - description = '' - The module system that should be used to construct the machine + machineClass = lib.mkOption { + default = "nixos"; + type = types.enum [ + "nixos" + "darwin" + ]; + description = '' + The module system that should be used to construct the machine - Set this to `darwin` for macOS machines - ''; - }; + Set this to `darwin` for macOS machines + ''; + }; - tags = lib.mkOption { - description = '' - List of tags for the machine. + tags = lib.mkOption { + description = '' + List of tags for the machine. - The machine can be referenced by its tags in `inventory.services` + The machine can be referenced by its tags in `inventory.services` - ???+ Example - ```nix - inventory.machines.machineA.tags = [ "tag1" "tag2" ]; - ``` + ???+ Example + ```nix + inventory.machines.machineA.tags = [ "tag1" "tag2" ]; + ``` - ```nix - services.borgbackup."instance_1".roles.client.tags = [ "tag1" ]; - ``` + ```nix + services.borgbackup."instance_1".roles.client.tags = [ "tag1" ]; + ``` - !!! Note - Tags can be used to determine the membership of the machine in the services. - Without changing the service configuration, the machine can be added to a service by adding the correct tags to the machine. + !!! Note + Tags can be used to determine the membership of the machine in the services. + Without changing the service configuration, the machine can be added to a service by adding the correct tags to the machine. - ''; - default = [ ]; - apply = lib.unique; - type = types.listOf types.str; - }; - deploy.targetHost = lib.mkOption { - description = "Configuration for the deployment of the machine"; - default = null; - type = types.nullOr types.str; - }; - }; - } - ) + ''; + default = [ ]; + apply = lib.unique; + type = types.listOf types.str; + }; + deploy.targetHost = lib.mkOption { + description = "Configuration for the deployment of the machine"; + default = null; + type = types.nullOr types.str; + }; + }; + } + ) + ]; + }) ); }; diff --git a/lib/inventory/distributed-service/tests/default.nix b/lib/inventory/distributed-service/tests/default.nix index 8eecc4852..0267dd9c1 100644 --- a/lib/inventory/distributed-service/tests/default.nix +++ b/lib/inventory/distributed-service/tests/default.nix @@ -8,12 +8,18 @@ let evalModules ; + # TODO: Use makeTestClan evalInventory = m: (evalModules { # Static modules modules = [ clanLib.inventory.interface + { + tags.all = [ ]; + tags.nixos = [ ]; + tags.darwin = [ ]; + } { modules.test = { }; }