mergeDefinitions
lib.modules.mergeDefinitions
Docs pulled from | This Revision | about 1 hour ago
Merge definitions of a value of a given type.
Noogle detected
Implementation
The following is the current implementation of this function.
mergeDefinitions = loc: type: defs: rec {
defsFinal' =
let
# Process mkMerge and mkIf properties.
defs' = concatMap (
m:
map (
value:
if value._type or null == "definition" then
value
else
{
inherit (m) file;
inherit value;
}
) (addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))
) defs;
# Process mkOverride properties.
defs'' = filterOverrides' defs';
# Sort mkOrder properties.
defs''' =
# Avoid sorting if we don't have to.
if any (def: def.value._type or "" == "order") defs''.values then
sortProperties defs''.values
else
defs''.values;
in
{
values = defs''';
inherit (defs'') highestPrio;
};
defsFinal = defsFinal'.values;
# Type-check the remaining definitions, and merge them. Or throw if no definitions.
mergedValue =
if isDefined then
if type.merge ? v2 then
# check and merge share the same closure
# .headError is either not-present, null, or a string describing the error
if checkedAndMerged.headError or null != null then
throw "A definition for option `${showOption loc}' is not of type `${type.description}'. TypeError: ${checkedAndMerged.headError.message}"
else
checkedAndMerged.value
else if all (def: type.check def.value) defsFinal then
type.merge loc defsFinal
else
let
allInvalid = filter (def: !type.check def.value) defsFinal;
in
throw "A definition for option `${showOption loc}' is not of type `${type.description}'. Definition values:${showDefs allInvalid}"
else
# (nixos-option detects this specific error message and gives it special
# handling. If changed here, please change it there too.)
throw
"The option `${showOption loc}' was accessed but has no value defined. Try setting the option.";
checkedAndMerged =
(
# This function (which is immediately applied) checks that type.merge
# returns the proper attrset.
# Once use of the merge.v2 feature has propagated, consider removing this
# for an estimated one thousandth performance improvement (NixOS by nr.thunks).
{
headError,
value,
valueMeta,
}@args:
args
)
(
if type.merge ? v2 then
let
r = type.merge.v2 {
inherit loc;
defs = defsFinal;
};
in
r
// {
valueMeta = r.valueMeta // {
_internal = {
inherit type;
};
};
}
else
{
headError = null;
value = mergedValue;
valueMeta = { };
}
);
isDefined = defsFinal != [ ];
optionalValue = if isDefined then { value = mergedValue; } else { };
};