lib/introspect: seperate headType from nullable

This commit is contained in:
Johannes Kirschbauer
2025-10-01 09:51:34 +02:00
parent 873382eaa9
commit a8156d2fa6
2 changed files with 94 additions and 125 deletions

View File

@@ -45,17 +45,28 @@ let
lib.mapAttrs ( lib.mapAttrs (
_: opt: _: opt:
let let
headType =
let
typeName = opt.type.name or null;
in
if typeName == "nullOr" then opt.type.nestedTypes.name or null else typeName;
nullable = opt.type.name or null == "nullOr";
definitionInfo = { definitionInfo = {
__this = { __this = {
prio = opt.highestPrio or null; prio = opt.highestPrio or null;
files = opt.files or [ ]; files = opt.files or [ ];
type = opt.type.name or null; inherit headType nullable;
total = opt.type.name or null == "submodule"; total = opt.type.name or null == "submodule";
}; };
}; };
# TODO: respect freeformType # TODO: respect freeformType
submodulePrios = getPrios { options = filterOptions opt.valueMeta.configuration.options; }; submodulePrios = getPrios {
options =
filterOptions
opt.valueMeta.configuration.options or (throw "Please use a newer nixpkgs version >=25.11");
};
/** /**
Maps attrsOf and lazyAttrsOf Maps attrsOf and lazyAttrsOf

View File

@@ -13,10 +13,36 @@ let
}; };
in in
evaledConfig; evaledConfig;
# Return only used attributes, for test stability
stableView =
set:
let
mapProps =
attrs:
lib.intersectAttrs {
files = null;
prio = null;
total = null;
} attrs;
in
lib.mapAttrs (
name: value:
if name == "__this" then
mapProps value
else if name == "__list" then
[ ]
else if lib.isAttrs value then
stableView value
else
value
) set;
in in
{ {
test_default = { test_default = {
expr = slib.getPrios { expr = stableView (
slib.getPrios {
options = options =
(eval [ (eval [
{ {
@@ -27,7 +53,8 @@ in
}; };
} }
]).options; ]).options;
}; }
);
expected = { expected = {
foo = { foo = {
bar = { bar = {
@@ -35,14 +62,14 @@ in
files = [ "<unknown-file>" ]; files = [ "<unknown-file>" ];
prio = 1500; prio = 1500;
total = false; total = false;
type = "bool";
}; };
}; };
}; };
}; };
}; };
test_no_default = { test_no_default = {
expr = slib.getPrios { expr = stableView (
slib.getPrios {
options = options =
(eval [ (eval [
{ {
@@ -51,7 +78,8 @@ in
}; };
} }
]).options; ]).options;
}; }
);
expected = { expected = {
foo = { foo = {
bar = { bar = {
@@ -59,7 +87,6 @@ in
files = [ ]; files = [ ];
prio = 9999; prio = 9999;
total = false; total = false;
type = "bool";
}; };
}; };
}; };
@@ -67,7 +94,8 @@ in
}; };
test_submodule = { test_submodule = {
expr = slib.getPrios { expr = stableView (
slib.getPrios {
options = options =
(eval [ (eval [
{ {
@@ -82,21 +110,20 @@ in
}; };
} }
]).options; ]).options;
}; }
);
expected = { expected = {
foo = { foo = {
__this = { __this = {
files = [ ]; files = [ ];
prio = 9999; prio = 9999;
total = true; total = true;
type = "submodule";
}; };
bar = { bar = {
__this = { __this = {
files = [ ]; files = [ ];
prio = 9999; prio = 9999;
total = false; total = false;
type = "bool";
}; };
}; };
}; };
@@ -142,9 +169,11 @@ in
in in
{ {
inherit evaluated; inherit evaluated;
expr = slib.getPrios { expr = stableView (
slib.getPrios {
options = evaluated.options; options = evaluated.options;
}; }
);
expected = { expected = {
foo = { foo = {
__this = { __this = {
@@ -154,14 +183,12 @@ in
]; ];
prio = 100; prio = 100;
total = true; total = true;
type = "submodule";
}; };
default = { default = {
__this = { __this = {
files = [ "default" ]; files = [ "default" ];
prio = 1000; prio = 1000;
total = false; total = false;
type = "bool";
}; };
}; };
normal = { normal = {
@@ -169,7 +196,6 @@ in
files = [ "normal" ]; files = [ "normal" ];
prio = 100; prio = 100;
total = false; total = false;
type = "bool";
}; };
}; };
optionDefault = { optionDefault = {
@@ -177,7 +203,6 @@ in
files = [ "option" ]; files = [ "option" ];
prio = 1500; prio = 1500;
total = false; total = false;
type = "bool";
}; };
}; };
unset = { unset = {
@@ -185,7 +210,6 @@ in
files = [ ]; files = [ ];
prio = 9999; prio = 9999;
total = false; total = false;
type = "bool";
}; };
}; };
}; };
@@ -217,23 +241,23 @@ in
in in
{ {
inherit evaluated; inherit evaluated;
expr = slib.getPrios { expr = stableView (
slib.getPrios {
options = evaluated.options; options = evaluated.options;
}; }
);
expected = { expected = {
foo = { foo = {
__this = { __this = {
files = [ "<unknown-file>" ]; files = [ "<unknown-file>" ];
prio = 100; prio = 100;
total = true; total = true;
type = "submodule";
}; };
bar = { bar = {
__this = { __this = {
files = [ "<unknown-file>" ]; files = [ "<unknown-file>" ];
prio = 100; prio = 100;
total = false; total = false;
type = "bool";
}; };
}; };
}; };
@@ -302,14 +326,13 @@ in
]; ];
in in
{ {
expr = slib.getPrios { options = evaluated.options; }; expr = stableView (slib.getPrios { options = evaluated.options; });
expected = { expected = {
foo = { foo = {
__this = { __this = {
files = [ "<unknown-file>" ]; files = [ "<unknown-file>" ];
prio = 100; prio = 100;
total = false; total = false;
type = "attrsOf";
}; };
nested = { nested = {
bar = { bar = {
@@ -317,7 +340,6 @@ in
files = [ "<unknown-file>" ]; files = [ "<unknown-file>" ];
prio = 100; prio = 100;
total = false; total = false;
type = "int";
}; };
}; };
}; };
@@ -327,7 +349,6 @@ in
files = [ "<unknown-file>" ]; files = [ "<unknown-file>" ];
prio = 50; prio = 50;
total = false; total = false;
type = "int";
}; };
}; };
}; };
@@ -371,14 +392,13 @@ in
in in
{ {
inherit evaluated; inherit evaluated;
expr = slib.getPrios { options = evaluated.options; }; expr = stableView (slib.getPrios { options = evaluated.options; });
expected = { expected = {
foo = { foo = {
__this = { __this = {
files = [ "<unknown-file>" ]; files = [ "<unknown-file>" ];
prio = 100; prio = 100;
total = false; total = false;
type = "attrsOf";
}; };
a = { a = {
b = { b = {
@@ -387,7 +407,6 @@ in
files = [ "<unknown-file>" ]; files = [ "<unknown-file>" ];
prio = 100; prio = 100;
total = false; total = false;
type = "int";
}; };
}; };
}; };
@@ -397,7 +416,6 @@ in
files = [ "<unknown-file>" ]; files = [ "<unknown-file>" ];
prio = 100; prio = 100;
total = false; total = false;
type = "int";
}; };
}; };
}; };
@@ -409,7 +427,6 @@ in
files = [ "<unknown-file>" ]; files = [ "<unknown-file>" ];
prio = 100; prio = 100;
total = false; total = false;
type = "int";
}; };
}; };
}; };
@@ -419,7 +436,6 @@ in
files = [ "<unknown-file>" ]; files = [ "<unknown-file>" ];
prio = 100; prio = 100;
total = false; total = false;
type = "int";
}; };
}; };
}; };
@@ -468,7 +484,7 @@ in
in in
{ {
inherit evaluated; inherit evaluated;
expr = slib.getPrios { options = evaluated.options; }; expr = stableView (slib.getPrios { options = evaluated.options; });
expected = { expected = {
machines = { machines = {
__this = { __this = {
@@ -478,7 +494,6 @@ in
]; ];
prio = 100; prio = 100;
total = false; total = false;
type = "attrsOf";
}; };
jon = { jon = {
fludl = { fludl = {
@@ -486,7 +501,6 @@ in
files = [ "<unknown-file>" ]; files = [ "<unknown-file>" ];
prio = 1500; prio = 1500;
total = true; total = true;
type = "submodule";
}; };
}; };
prim = { prim = {
@@ -494,7 +508,6 @@ in
files = [ "inventory.json" ]; files = [ "inventory.json" ];
prio = 100; prio = 100;
total = false; total = false;
type = "int";
}; };
}; };
settings = { settings = {
@@ -502,7 +515,6 @@ in
files = [ "<unknown-file>" ]; files = [ "<unknown-file>" ];
prio = 1500; prio = 1500;
total = true; total = true;
type = "submodule";
}; };
}; };
}; };
@@ -554,63 +566,10 @@ in
in in
{ {
inherit evaluated; inherit evaluated;
expr = slib.getPrios { options = evaluated.options; }; expr = stableView (slib.getPrios { options = evaluated.options; });
expected = { expected = {
machines = { machines = {
__list = [ __list = [ ];
{
fludl = {
__this = {
files = [ "<unknown-file>" ];
prio = 1500;
total = true;
type = "submodule";
};
};
prim = {
__this = {
files = [ "clan.nix" ];
prio = 100;
total = false;
type = "int";
};
};
settings = {
__this = {
files = [ "<unknown-file>" ];
prio = 1500;
total = true;
type = "submodule";
};
};
}
{
fludl = {
__this = {
files = [ "<unknown-file>" ];
prio = 1500;
total = true;
type = "submodule";
};
};
prim = {
__this = {
files = [ "inventory.json" ];
prio = 100;
total = false;
type = "int";
};
};
settings = {
__this = {
files = [ "<unknown-file>" ];
prio = 1500;
total = true;
type = "submodule";
};
};
}
];
__this = { __this = {
files = [ files = [
"clan.nix" "clan.nix"
@@ -618,7 +577,6 @@ in
]; ];
prio = 100; prio = 100;
total = false; total = false;
type = "listOf";
}; };
}; };
}; };