query
On this page

mergeDefinitions

lib.modules.mergeDefinitions

Docs pulled from | This Revision | 10 minutes ago


Merge definitions of a value of a given type.


Noogle detected

Aliases

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 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.";

    isDefined = defsFinal != [ ];

    optionalValue = if isDefined then { value = mergedValue; } else { };
  };