query
On this page

attrsWith

lib.types.attrsWith

Docs pulled from | This Revision | 14 minutes ago


Nixpkgs manual

types.attrsWith { elemType, lazy ? false, placeholder ? "name" }

An attribute set of where all the values are of elemType type.

Parameters

elemType (Required)
Specifies the type of the values contained in the attribute set.
lazy
Determines whether the attribute set is lazily evaluated. See: types.lazyAttrsOf
placeholder (String, default: name )
Placeholder string in documentation for the attribute names. The default value name results in the placeholder <name>

Behavior

Noogle detected

Implementation

The following is the current implementation of this function.

{
      elemType,
      lazy ? false,
      placeholder ? "name",
    }:
    mkOptionType rec {
      name = if lazy then "lazyAttrsOf" else "attrsOf";
      description =
        (if lazy then "lazy attribute set" else "attribute set")
        + " of ${optionDescriptionPhrase (class: class == "noun" || class == "composite") elemType}";
      descriptionClass = "composite";
      check = {
        __functor = _self: isAttrs;
        isV2MergeCoherent = true;
      };
      merge = {
        __functor =
          self: loc: defs:
          (self.v2 { inherit loc defs; }).value;
        v2 =
          { loc, defs }:
          let
            evals =
              if lazy then
                zipAttrsWith (name: mergeDefinitions (loc ++ [ name ]) elemType) (pushPositions defs)
              else
                # Filtering makes the merge function more strict
                # Meaning it is less lazy
                filterAttrs (n: v: v.optionalValue ? value) (
                  zipAttrsWith (name: mergeDefinitions (loc ++ [ name ]) elemType) (pushPositions defs)
                );
          in
          {
            headError = checkDefsForError check loc defs;
            value = mapAttrs (
              if lazy then
                n: v: v.optionalValue.value or elemType.emptyValue.value or v.mergedValue
              else
                n: v: v.optionalValue.value
            ) evals;
            valueMeta.attrs = mapAttrs (n: v: v.checkedAndMerged.valueMeta) evals;
          };
      };

      emptyValue = {
        value = { };
      };
      getSubOptions = prefix: elemType.getSubOptions (prefix ++ [ "<${placeholder}>" ]);
      getSubModules = elemType.getSubModules;
      substSubModules =
        m:
        attrsWith {
          elemType = elemType.substSubModules m;
          inherit lazy placeholder;
        };
      functor =
        (elemTypeFunctor "attrsWith" {
          inherit elemType lazy placeholder;
        })
        // {
          # Custom type merging required because of the "placeholder" attribute
          inherit binOp;
        };
      nestedTypes.elemType = elemType;
    }