Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 47 additions & 18 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -719,27 +719,55 @@ let
v2 =
{ loc, defs }:
let
evals = filter (x: x.optionalValue ? value) (
concatLists (
imap1 (
n: def:
imap1 (
m: def':
(mergeDefinitions (loc ++ [ "[definition ${toString n}-entry ${toString m}]" ]) elemType [
{
inherit (def) file;
value = def';
}
])
) def.value
) defs
)
);
evals =
foldl'
(acc: def: {
# Problem: definitions lists dont have equal lengths -> maintain a total counter, to avoid re-iterating
# Can we do this better? Previously we used concatLists which is optimized
# Repeated list concatentation might be slow
res =
acc.res
++ imap1 (
m: def':
(mergeDefinitions (loc ++ [ "[definition ${toString acc.i}-entry ${toString m}]" ]) elemType (
[
{
inherit (def) file;
value = def';
}
]
++ lib.optionals (elemType.name or null == "submodule") [
{
file = "<internal>";
value = {
_module.args.index = acc.total + m - 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with imap0

Suggested change
_module.args.index = acc.total + m - 1;
_module.args.index = acc.total + m;

};
}
]
))
) def.value;
total = acc.total + length def.value;
i = acc.i + 1;
})
{
res = [ ];
total = 0;
i = 0;
}
defs;
in
{
headError = checkDefsForError check loc defs;
value = map (x: x.optionalValue.value or x.mergedValue) evals;
valueMeta.list = map (v: v.checkedAndMerged.valueMeta) evals;
# Filtering doesn't affect submodules.
# Because the root of a submodule cannot be used with mkIf.
# If this worked, filtering would introduces paradoxons, for example:
# [ (mkIf (index == 0) { }) { } ]
value = map (x: x.optionalValue.value or x.mergedValue) (
filter (x: x.optionalValue ? value) evals.res
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC assuming m is sensitive to the index, a definition like [ m (lib.mkIf false n) m ] would produce indexes 0 and 2 instead of 0 and 1, because of this logic here.
Ideally that could just be fixed, but it might be ok to just fail, i.e. return [ r1 r2 r3 ] where r2 throws an error. That way at least the numbering is predictable in all cases where it produces a valid config.

);
valueMeta.list = map (v: v.checkedAndMerged.valueMeta) (
filter (x: x.optionalValue ? value) evals.res
);
};
};
emptyValue = {
Expand Down Expand Up @@ -1227,6 +1255,7 @@ let
# would be used, and use of `<` and `>` would break the XML document.
# It shouldn't cause an issue since this is cosmetic for the manual.
_module.args.name = lib.mkOptionDefault "‹name›";
_module.args.index = lib.mkOptionDefault "‹index›";
}
]
++ modules;
Expand Down
Loading