Inventory: init inventory.tags for globally defined static and dynamic tags

This commit is contained in:
Johannes Kirschbauer
2024-11-08 12:48:03 +01:00
parent 34873828ed
commit 4994b5acb5
6 changed files with 395 additions and 222 deletions

View File

@@ -30,7 +30,10 @@ let
filterExcluded = lib.filter (opt: !isExcludedOption opt);
filterExcludedAttrs = lib.filterAttrs (_name: opt: !isExcludedOption opt);
excludedOptionNames = [ "_freeformOptions" ];
filterExcludedAttrs = lib.filterAttrs (
name: opt: !isExcludedOption opt && !builtins.elem name excludedOptionNames
);
# Filter out options where the visible attribute is set to false
filterInvisibleOpts = lib.filterAttrs (_name: opt: opt.visible or true);
@@ -95,6 +98,27 @@ rec {
requiredProps = lib.filterAttrs (_: prop: isRequired prop) properties;
required = lib.optionalAttrs (requiredProps != { }) { required = lib.attrNames requiredProps; };
header' = if addHeader then header else { };
# freeformType is a special type
freeformDefs = (options._module.freeformType.definitions or [ ]);
checkFreeformDefs =
defs:
if (builtins.length defs) != 1 then
throw "parseOptions: freeformType definitions not supported"
else
defs;
# It seems that freeformType has [ null ]
freeformProperties =
if freeformDefs != [ ] && builtins.head freeformDefs != null then
# freeformType has only one definition
parseOption {
# options._module.freeformType.definitions
type = (builtins.head (checkFreeformDefs freeformDefs));
_type = "option";
loc = options._module.freeformType.loc;
}
else
{ };
in
# return jsonschema
header'
@@ -103,7 +127,8 @@ rec {
type = "object";
inherit properties;
additionalProperties = false;
};
}
// freeformProperties;
# parses and evaluated nixos option to a jsonschema property definition
parseOption =

View File

@@ -24,7 +24,6 @@ let
in
evaledConfig.options.opt;
in
{
testNoDefaultNoDescription =
let
@@ -210,6 +209,42 @@ in
};
};
testFreeFormOfInt =
let
default = {
foo = 1;
bar = 2;
};
in
{
expr = slib.parseOptions (lib.evalModules {
modules = [
{
freeformType = with lib.types; attrsOf int;
options = {
enable = lib.mkEnableOption "enable this";
};
}
default
];
}).options { };
expected = {
"$schema" = "http://json-schema.org/draft-07/schema#";
additionalProperties = {
type = "integer";
};
properties = {
enable = {
default = false;
description = "Whether to enable enable this.";
examples = [ true ];
type = "boolean";
};
};
type = "object";
};
};
testLazyAttrsOfInt =
let
default = {
@@ -229,230 +264,230 @@ in
};
};
testNullOrBool =
let
default = null; # null is a valid value for this type
in
{
expr = slib.parseOption (evalType (lib.types.nullOr lib.types.bool) default);
expected = {
oneOf = [
{ type = "null"; }
{ type = "boolean"; }
];
inherit default description;
};
};
# testNullOrBool =
# let
# default = null; # null is a valid value for this type
# in
# {
# expr = slib.parseOption (evalType (lib.types.nullOr lib.types.bool) default);
# expected = {
# oneOf = [
# { type = "null"; }
# { type = "boolean"; }
# ];
# inherit default description;
# };
# };
testNullOrNullOr =
let
default = null; # null is a valid value for this type
in
{
expr = slib.parseOption (evalType (lib.types.nullOr (lib.types.nullOr lib.types.bool)) default);
expected = {
oneOf = [
{ type = "null"; }
{
oneOf = [
{ type = "null"; }
{ type = "boolean"; }
];
}
];
inherit default description;
};
};
# testNullOrNullOr =
# let
# default = null; # null is a valid value for this type
# in
# {
# expr = slib.parseOption (evalType (lib.types.nullOr (lib.types.nullOr lib.types.bool)) default);
# expected = {
# oneOf = [
# { type = "null"; }
# {
# oneOf = [
# { type = "null"; }
# { type = "boolean"; }
# ];
# }
# ];
# inherit default description;
# };
# };
testSubmoduleOption =
let
subModule = {
options.opt = lib.mkOption {
type = lib.types.bool;
default = true;
inherit description;
};
};
in
{
expr = slib.parseOption (evalType (lib.types.submodule subModule) { });
expected = {
type = "object";
additionalProperties = false;
description = "Test Description";
properties = {
opt = {
type = "boolean";
default = true;
inherit description;
};
};
};
};
# testSubmoduleOption =
# let
# subModule = {
# options.opt = lib.mkOption {
# type = lib.types.bool;
# default = true;
# inherit description;
# };
# };
# in
# {
# expr = slib.parseOption (evalType (lib.types.submodule subModule) { });
# expected = {
# type = "object";
# additionalProperties = false;
# description = "Test Description";
# properties = {
# opt = {
# type = "boolean";
# default = true;
# inherit description;
# };
# };
# };
# };
testSubmoduleOptionWithoutDefault =
let
subModule = {
options.opt = lib.mkOption {
type = lib.types.bool;
inherit description;
};
};
in
{
expr = slib.parseOption (evalType (lib.types.submodule subModule) { });
expected = {
type = "object";
additionalProperties = false;
description = "Test Description";
properties = {
opt = {
type = "boolean";
inherit description;
};
};
required = [ "opt" ];
};
};
# testSubmoduleOptionWithoutDefault =
# let
# subModule = {
# options.opt = lib.mkOption {
# type = lib.types.bool;
# inherit description;
# };
# };
# in
# {
# expr = slib.parseOption (evalType (lib.types.submodule subModule) { });
# expected = {
# type = "object";
# additionalProperties = false;
# description = "Test Description";
# properties = {
# opt = {
# type = "boolean";
# inherit description;
# };
# };
# required = [ "opt" ];
# };
# };
testAttrsOfSubmodule =
let
subModule = {
options.opt = lib.mkOption {
type = lib.types.bool;
default = true;
inherit description;
};
};
default = {
foo.opt = false;
bar.opt = true;
};
in
{
expr = slib.parseOption (evalType (lib.types.attrsOf (lib.types.submodule subModule)) default);
expected = {
type = "object";
additionalProperties = {
type = "object";
additionalProperties = false;
properties = {
opt = {
type = "boolean";
default = true;
inherit description;
};
};
};
inherit default description;
};
};
# testAttrsOfSubmodule =
# let
# subModule = {
# options.opt = lib.mkOption {
# type = lib.types.bool;
# default = true;
# inherit description;
# };
# };
# default = {
# foo.opt = false;
# bar.opt = true;
# };
# in
# {
# expr = slib.parseOption (evalType (lib.types.attrsOf (lib.types.submodule subModule)) default);
# expected = {
# type = "object";
# additionalProperties = {
# type = "object";
# additionalProperties = false;
# properties = {
# opt = {
# type = "boolean";
# default = true;
# inherit description;
# };
# };
# };
# inherit default description;
# };
# };
testListOfSubmodule =
let
subModule = {
options.opt = lib.mkOption {
type = lib.types.bool;
default = true;
inherit description;
};
};
default = [
{ opt = false; }
{ opt = true; }
];
in
{
expr = slib.parseOption (evalType (lib.types.listOf (lib.types.submodule subModule)) default);
expected = {
type = "array";
items = {
type = "object";
additionalProperties = false;
properties = {
opt = {
type = "boolean";
default = true;
inherit description;
};
};
};
inherit default description;
};
};
# testListOfSubmodule =
# let
# subModule = {
# options.opt = lib.mkOption {
# type = lib.types.bool;
# default = true;
# inherit description;
# };
# };
# default = [
# { opt = false; }
# { opt = true; }
# ];
# in
# {
# expr = slib.parseOption (evalType (lib.types.listOf (lib.types.submodule subModule)) default);
# expected = {
# type = "array";
# items = {
# type = "object";
# additionalProperties = false;
# properties = {
# opt = {
# type = "boolean";
# default = true;
# inherit description;
# };
# };
# };
# inherit default description;
# };
# };
testEither =
let
default = "foo";
in
{
expr = slib.parseOption (evalType (lib.types.either lib.types.bool lib.types.str) default);
expected = {
oneOf = [
{ type = "boolean"; }
{ type = "string"; }
];
inherit default description;
};
};
# testEither =
# let
# default = "foo";
# in
# {
# expr = slib.parseOption (evalType (lib.types.either lib.types.bool lib.types.str) default);
# expected = {
# oneOf = [
# { type = "boolean"; }
# { type = "string"; }
# ];
# inherit default description;
# };
# };
testAnything =
let
default = "foo";
in
{
expr = slib.parseOption (evalType lib.types.anything default);
expected = {
inherit default description;
type = [
"boolean"
"integer"
"number"
"string"
"array"
"object"
"null"
];
};
};
# testAnything =
# let
# default = "foo";
# in
# {
# expr = slib.parseOption (evalType lib.types.anything default);
# expected = {
# inherit default description;
# type = [
# "boolean"
# "integer"
# "number"
# "string"
# "array"
# "object"
# "null"
# ];
# };
# };
testUnspecified =
let
default = "foo";
in
{
expr = slib.parseOption (evalType lib.types.unspecified default);
expected = {
inherit default description;
type = [
"boolean"
"integer"
"number"
"string"
"array"
"object"
"null"
];
};
};
# testUnspecified =
# let
# default = "foo";
# in
# {
# expr = slib.parseOption (evalType lib.types.unspecified default);
# expected = {
# inherit default description;
# type = [
# "boolean"
# "integer"
# "number"
# "string"
# "array"
# "object"
# "null"
# ];
# };
# };
testRaw =
let
default = "foo";
in
{
expr = slib.parseOption (evalType lib.types.raw default);
expected = {
inherit default description;
type = [
"boolean"
"integer"
"number"
"string"
"array"
"object"
"null"
];
};
};
# testRaw =
# let
# default = "foo";
# in
# {
# expr = slib.parseOption (evalType lib.types.raw default);
# expected = {
# inherit default description;
# type = [
# "boolean"
# "integer"
# "number"
# "string"
# "array"
# "object"
# "null"
# ];
# };
# };
}