Merge pull request 'Inventory/tags: init {nixos,darwin} tags' (#3370) from hsjobeki/clan-core:tags-2 into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3370
This commit is contained in:
hsjobeki
2025-04-21 15:27:13 +00:00
3 changed files with 97 additions and 62 deletions

View File

@@ -1,33 +1,23 @@
{ {
config,
lib, lib,
... ...
}: }:
{ {
config.inventory = { # Add the computed tags to machine tags for displaying them
inventory = {
tags = ( tags = (
{ machines, ... }: { machines, ... }:
{ {
# Only compute the default value # Only compute the default value
# The option MUST be defined in ./build-inventory/interface.nix # The option MUST be defined in ./build-inventory/interface.nix
all = lib.mkDefault (builtins.attrNames machines); 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
);
}
)
);
};
};
} }

View File

@@ -223,6 +223,32 @@ in
``` ```
''; '';
}; };
nixos = lib.mkOption {
type = with lib.types; listOf str;
defaultText = "[ <All NixOS Machines> ]";
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 = "[ <All Darwin Machines> ]";
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`. Each machine declared here can be referencd via its `attributeName` by the `inventory.service`s `roles`.
''; '';
default = { }; default = { };
type = types.attrsOf ( type = types.lazyAttrsOf (
types.submodule ( types.submoduleWith ({
{ name, ... }: modules = [
{ (
options = { { name, ... }:
inherit (metaOptionsWith name) name description icon; {
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 { machineClass = lib.mkOption {
default = "nixos"; default = "nixos";
type = types.enum [ type = types.enum [
"nixos" "nixos"
"darwin" "darwin"
]; ];
description = '' description = ''
The module system that should be used to construct the machine 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 { tags = lib.mkOption {
description = '' description = ''
List of tags for the machine. 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 ???+ Example
```nix ```nix
inventory.machines.machineA.tags = [ "tag1" "tag2" ]; inventory.machines.machineA.tags = [ "tag1" "tag2" ];
``` ```
```nix ```nix
services.borgbackup."instance_1".roles.client.tags = [ "tag1" ]; services.borgbackup."instance_1".roles.client.tags = [ "tag1" ];
``` ```
!!! Note !!! Note
Tags can be used to determine the membership of the machine in the services. 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. Without changing the service configuration, the machine can be added to a service by adding the correct tags to the machine.
''; '';
default = [ ]; default = [ ];
apply = lib.unique; apply = lib.unique;
type = types.listOf types.str; type = types.listOf types.str;
}; };
deploy.targetHost = lib.mkOption { deploy.targetHost = lib.mkOption {
description = "Configuration for the deployment of the machine"; description = "Configuration for the deployment of the machine";
default = null; default = null;
type = types.nullOr types.str; type = types.nullOr types.str;
}; };
}; };
} }
) )
];
})
); );
}; };

View File

@@ -8,12 +8,18 @@ let
evalModules evalModules
; ;
# TODO: Use makeTestClan
evalInventory = evalInventory =
m: m:
(evalModules { (evalModules {
# Static modules # Static modules
modules = [ modules = [
clanLib.inventory.interface clanLib.inventory.interface
{
tags.all = [ ];
tags.nixos = [ ];
tags.darwin = [ ];
}
{ {
modules.test = { }; modules.test = { };
} }