mergeDefaultOption
lib.mergeDefaultOption
Docs pulled from | This Revision | about 1 hour ago
A merge function that merges multiple definitions of an option into a single value
This function is used as the default merge operation in lib.types.mkOptionType
. In most cases, explicit usage of this function is unnecessary.
Inputs
loc
- location of the option in the configuration as a list of strings.
e.g.
["boot" "loader "grub" "enable"]
defs
- list of definition values and locations.
e.g.
[ { file = "/foo.nix"; value = 1; } { file = "/bar.nix"; value = 2 } ]
Example
lib.options.mergeDefaultOption
usage example
myType = mkOptionType {
name = "myType";
merge = mergeDefaultOption; # <- This line is redundant. It is the default aready.
};
Merge behavior
Merging requires all definition values to have the same type.
- If all definitions are booleans, the result of a
foldl'
with theor
operation is returned. - If all definitions are strings, they are concatenated. (
lib.concatStrings
) - If all definitions are integers and all are equal, the first one is returned.
- If all definitions are lists, they are concatenated. (
++
) - If all definitions are attribute sets, they are merged. (
lib.mergeAttrs
) - If all definitions are functions, the first function is applied to the result of the second function. (
f -> x: f x
) - Otherwise, an error is thrown.
Noogle detected
Implementation
The following is the current implementation of this function.
mergeDefaultOption = loc: defs:
let list = getValues defs; in
if length list == 1 then head list
else if all isFunction list then x: mergeDefaultOption loc (map (f: f x) list)
else if all isList list then concatLists list
else if all isAttrs list then foldl' lib.mergeAttrs {} list
else if all isBool list then foldl' lib.or false list
else if all isString list then lib.concatStrings list
else if all isInt list && all (x: x == head list) list then head list
else throw "Cannot merge definitions of `${showOption loc}'. Definition values:${showDefs defs}";