From dc45ca2e42862e9b77b543de8095699534c555fd Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Tue, 24 Sep 2024 15:25:03 +0200 Subject: [PATCH 1/8] lib.types: init attrsWith --- lib/options.nix | 29 +++++++++++++++--------- lib/types.nix | 60 ++++++++++++++++++++++++++----------------------- 2 files changed, 50 insertions(+), 39 deletions(-) diff --git a/lib/options.nix b/lib/options.nix index f4d0d9d36cfc930..e08955464f75698 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -427,21 +427,28 @@ rec { Placeholders will not be quoted as they are not actual values: (showOption ["foo" "*" "bar"]) == "foo.*.bar" (showOption ["foo" "" "bar"]) == "foo..bar" + (showOption ["foo" "" "bar"]) == "foo..bar" */ showOption = parts: let + # If the part is a named placeholder of the form "<...>" don't escape it. + # Required for compatibility with: namedAttrsOf + # Can lead to misleading escaping if somebody uses literally "<...>" in their option names. + # This is the trade-off to allow for named placeholders in option names. + isNamedPlaceholder = builtins.match "\<(.*)\>"; + # "" # functionTo + # "" # attrsOf submoule + # "" # attrsWith { name = "customName"; elemType = submoule; } + # We assume that these are "special values" and not real configuration data. + # If it is real configuration data, it is rendered incorrectly. + specialIdentifiers = [ + "*" # listOf (submodule {}) + ]; escapeOptionPart = part: - let - # We assume that these are "special values" and not real configuration data. - # If it is real configuration data, it is rendered incorrectly. - specialIdentifiers = [ - "" # attrsOf (submodule {}) - "*" # listOf (submodule {}) - "" # functionTo - ]; - in if builtins.elem part specialIdentifiers - then part - else lib.strings.escapeNixIdentifier part; + if builtins.elem part specialIdentifiers || isNamedPlaceholder part != null + then part + else lib.strings.escapeNixIdentifier part; in (concatStringsSep ".") (map escapeOptionPart parts); + showFiles = files: concatStringsSep " and " (map (f: "`${f}'") files); showDefs = defs: concatMapStrings (def: diff --git a/lib/types.nix b/lib/types.nix index 6c4a66c4e3c0b5f..6e5fc28b02e6c5a 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -568,48 +568,52 @@ rec { substSubModules = m: nonEmptyListOf (elemType.substSubModules m); }; - attrsOf = elemType: mkOptionType rec { - name = "attrsOf"; - description = "attribute set of ${optionDescriptionPhrase (class: class == "noun" || class == "composite") elemType}"; - descriptionClass = "composite"; - check = isAttrs; - merge = loc: defs: - mapAttrs (n: v: v.value) (filterAttrs (n: v: v ? value) (zipAttrsWith (name: defs: - (mergeDefinitions (loc ++ [name]) elemType defs).optionalValue - ) - # Push down position info. - (map (def: mapAttrs (n: v: { inherit (def) file; value = v; }) def.value) defs))); - emptyValue = { value = {}; }; - getSubOptions = prefix: elemType.getSubOptions (prefix ++ [""]); - getSubModules = elemType.getSubModules; - substSubModules = m: attrsOf (elemType.substSubModules m); - functor = (defaultFunctor name) // { wrapped = elemType; }; - nestedTypes.elemType = elemType; - }; + attrsOf = elemType: attrsWith { inherit elemType; }; # A version of attrsOf that's lazy in its values at the expense of # conditional definitions not working properly. E.g. defining a value with # `foo.attr = mkIf false 10`, then `foo ? attr == true`, whereas with # attrsOf it would correctly be `false`. Accessing `foo.attr` would throw an # error that it's not defined. Use only if conditional definitions don't make sense. - lazyAttrsOf = elemType: mkOptionType rec { - name = "lazyAttrsOf"; - description = "lazy attribute set of ${optionDescriptionPhrase (class: class == "noun" || class == "composite") elemType}"; - descriptionClass = "composite"; - check = isAttrs; - merge = loc: defs: + lazyAttrsOf = elemType: attrsWith { inherit elemType; lazy = true; }; + + # base type for lazyAttrsOf and attrsOf + attrsWith = { + elemType, + name ? "name", + lazy ? false, + }: + let + typeName = "attrsOf"; + lazyMergeFn = loc: defs: zipAttrsWith (name: defs: let merged = mergeDefinitions (loc ++ [name]) elemType defs; # mergedValue will trigger an appropriate error when accessed in merged.optionalValue.value or elemType.emptyValue.value or merged.mergedValue ) # Push down position info. - (map (def: mapAttrs (n: v: { inherit (def) file; value = v; }) def.value) defs); + (pushPositions defs); + + mergeFn = loc: defs: + mapAttrs (n: v: v.value) (filterAttrs (n: v: v ? value) (zipAttrsWith (name: defs: + (mergeDefinitions (loc ++ [name]) elemType defs).optionalValue + ) + # Push down position info. + (pushPositions defs))); + # Push down position info. + pushPositions = map (def: mapAttrs (n: v: { inherit (def) file; value = v; }) def.value); + in + mkOptionType { + name = typeName; + description = (if lazy then "lazy attribute set" else "attribute set") + " of ${optionDescriptionPhrase (class: class == "noun" || class == "composite") elemType}"; + descriptionClass = "composite"; + check = isAttrs; + merge = if lazy then lazyMergeFn else mergeFn; emptyValue = { value = {}; }; - getSubOptions = prefix: elemType.getSubOptions (prefix ++ [""]); + getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<${name}>"]); getSubModules = elemType.getSubModules; - substSubModules = m: lazyAttrsOf (elemType.substSubModules m); - functor = (defaultFunctor name) // { wrapped = elemType; }; + substSubModules = m: attrsWith { elemType = elemType.substSubModules m; inherit name lazy; }; + functor = (defaultFunctor typeName) // { wrapped = elemType; }; nestedTypes.elemType = elemType; }; From 0a2f174429f04a2132a7513f66a67de90911a264 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Fri, 27 Sep 2024 10:19:11 +0200 Subject: [PATCH 2/8] lib.attrsWith: init eval tests --- lib/tests/misc.nix | 38 ++++++++++++++++++++++++++++++++++++++ lib/types.nix | 6 +++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 116d86cdfb3fb5d..ca8de7c28c626b6 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -1851,6 +1851,44 @@ runTests { expected = [ [ "_module" "args" ] [ "foo" ] [ "foo" "" "bar" ] [ "foo" "bar" ] ]; }; + testAttrsWithName = { + expr = let + eval = evalModules { + modules = [ + { + options = { + foo = lib.mkOption { + type = lib.types.attrsWith { + name = "MyCustomPlaceholder"; + elemType = lib.types.submodule { + options.bar = lib.mkOption { + type = lib.types.int; + default = 42; + }; + }; + }; + }; + }; + } + ]; + }; + opt = eval.options.foo; + in + (opt.type.getSubOptions opt.loc).bar.loc; + expected = [ + "foo" + "" + "bar" + ]; + }; + + testShowOptionWithPlaceholder = { + # , *, should now be escaped. It is used as a placeholder by convention. + # Other symbols should be escaped. `{}` + expr = lib.showOption ["" "" "*" "{foo}"]; + expected = "..*.\"{foo}\""; + }; + testCartesianProductOfEmptySet = { expr = cartesianProduct {}; expected = [ {} ]; diff --git a/lib/types.nix b/lib/types.nix index 6e5fc28b02e6c5a..812d54efba7486a 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -584,7 +584,7 @@ rec { lazy ? false, }: let - typeName = "attrsOf"; + typeName = if lazy then "lazyAttrsOf" else "attrsOf"; lazyMergeFn = loc: defs: zipAttrsWith (name: defs: let merged = mergeDefinitions (loc ++ [name]) elemType defs; @@ -596,7 +596,7 @@ rec { mergeFn = loc: defs: mapAttrs (n: v: v.value) (filterAttrs (n: v: v ? value) (zipAttrsWith (name: defs: - (mergeDefinitions (loc ++ [name]) elemType defs).optionalValue + (mergeDefinitions (loc ++ [name]) elemType (defs)).optionalValue ) # Push down position info. (pushPositions defs))); @@ -613,7 +613,7 @@ rec { getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<${name}>"]); getSubModules = elemType.getSubModules; substSubModules = m: attrsWith { elemType = elemType.substSubModules m; inherit name lazy; }; - functor = (defaultFunctor typeName) // { wrapped = elemType; }; + functor = (defaultFunctor typeName) // { wrapped = elemType; type = t: attrsWith { elemType = t; inherit name lazy; }; }; nestedTypes.elemType = elemType; }; From 3bd01bca8fec2d208a168edb50c457fc36a6c96f Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Wed, 2 Oct 2024 09:48:54 +0200 Subject: [PATCH 3/8] lib/attrsWith: inline merge functions --- lib/types.nix | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/types.nix b/lib/types.nix index 812d54efba7486a..f08402533c9561a 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -585,21 +585,6 @@ rec { }: let typeName = if lazy then "lazyAttrsOf" else "attrsOf"; - lazyMergeFn = loc: defs: - zipAttrsWith (name: defs: - let merged = mergeDefinitions (loc ++ [name]) elemType defs; - # mergedValue will trigger an appropriate error when accessed - in merged.optionalValue.value or elemType.emptyValue.value or merged.mergedValue - ) - # Push down position info. - (pushPositions defs); - - mergeFn = loc: defs: - mapAttrs (n: v: v.value) (filterAttrs (n: v: v ? value) (zipAttrsWith (name: defs: - (mergeDefinitions (loc ++ [name]) elemType (defs)).optionalValue - ) - # Push down position info. - (pushPositions defs))); # Push down position info. pushPositions = map (def: mapAttrs (n: v: { inherit (def) file; value = v; }) def.value); in @@ -608,7 +593,25 @@ rec { description = (if lazy then "lazy attribute set" else "attribute set") + " of ${optionDescriptionPhrase (class: class == "noun" || class == "composite") elemType}"; descriptionClass = "composite"; check = isAttrs; - merge = if lazy then lazyMergeFn else mergeFn; + merge = if lazy then ( + # Lazy merge Function + loc: defs: + zipAttrsWith (name: defs: + let merged = mergeDefinitions (loc ++ [name]) elemType defs; + # mergedValue will trigger an appropriate error when accessed + in merged.optionalValue.value or elemType.emptyValue.value or merged.mergedValue + ) + # Push down position info. + (pushPositions defs) + ) else ( + # Non-lazy merge Function + loc: defs: + mapAttrs (n: v: v.value) (filterAttrs (n: v: v ? value) (zipAttrsWith (name: defs: + (mergeDefinitions (loc ++ [name]) elemType (defs)).optionalValue + ) + # Push down position info. + (pushPositions defs))) + ); emptyValue = { value = {}; }; getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<${name}>"]); getSubModules = elemType.getSubModules; From 11a6fd813a9f83b56d87546162e4e80f13ca7776 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Sat, 5 Oct 2024 03:04:56 +0200 Subject: [PATCH 4/8] lib.types.attrsWith: Improved type merging --- lib/types.nix | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/types.nix b/lib/types.nix index f08402533c9561a..66292739fd42977 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -616,7 +616,35 @@ rec { getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<${name}>"]); getSubModules = elemType.getSubModules; substSubModules = m: attrsWith { elemType = elemType.substSubModules m; inherit name lazy; }; - functor = (defaultFunctor typeName) // { wrapped = elemType; type = t: attrsWith { elemType = t; inherit name lazy; }; }; + functor = defaultFunctor "attrsWith" // { + payload = { + inherit elemType name lazy; + }; + binOp = lhs: rhs: + let + elemType = lhs.elemType.typeMerge rhs.elemType.functor; + name = + if lhs.name == rhs.name then + lhs.name + else if lhs.name == "name" then + rhs.name + else if rhs.name == "name" then + lhs.name + else + null; + lazy = + if lhs.lazy == rhs.lazy then + lhs.lazy + else + null; + in + if elemType == null || name == null || lazy == null then + null + else + { + inherit elemType name lazy; + }; + }; nestedTypes.elemType = elemType; }; From a21dc4a543ba6950ca26d7d2e16afc8b5dcd7d84 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Tue, 8 Oct 2024 21:26:10 +0200 Subject: [PATCH 5/8] lib.types.attrsWith: format whitespaces --- lib/types.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/types.nix b/lib/types.nix index 66292739fd42977..f6cb6bd34614cc8 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -623,7 +623,7 @@ rec { binOp = lhs: rhs: let elemType = lhs.elemType.typeMerge rhs.elemType.functor; - name = + name = if lhs.name == rhs.name then lhs.name else if lhs.name == "name" then @@ -632,7 +632,7 @@ rec { lhs.name else null; - lazy = + lazy = if lhs.lazy == rhs.lazy then lhs.lazy else From f9108287ac17b2463bda35aad8469961df8c933b Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Tue, 8 Oct 2024 21:38:44 +0200 Subject: [PATCH 6/8] lib.types.attrsWith: init documentation --- .../manual/development/option-types.section.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/nixos/doc/manual/development/option-types.section.md b/nixos/doc/manual/development/option-types.section.md index b44a84553b37bc0..3c6afe5ff6b7c07 100644 --- a/nixos/doc/manual/development/option-types.section.md +++ b/nixos/doc/manual/development/option-types.section.md @@ -399,6 +399,22 @@ Composed types are types that take a type as parameter. `listOf returned instead for the same `mkIf false` definition. ::: +`types.attrsWith` *`attrs`* + +: An attribute set of where all the values are of *`attrs.elemType`* type. + + `attrs.lazy` (`Bool`, default: `false` ) + : If set to `true` attributes will be evaluated lazily. See also: `types.lazyAttrsOf` + + `attrs.name` (`String`, default: `name` ) + : Placeholder string in documentation for the attribute names. + The default value `name` results in the placeholder `` + + ::: {.note} + This is the underlying implementation of `types.attrsOf` and `types.lazyAttrsOf` + ::: + + `types.uniq` *`t`* : Ensures that type *`t`* cannot be merged. It is used to ensure option From 249372691df7a92a60485c0d8b97bfd490912ac2 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Tue, 8 Oct 2024 22:03:54 +0200 Subject: [PATCH 7/8] lib.types.attrsWith: add required functor attributes --- lib/types.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/types.nix b/lib/types.nix index f6cb6bd34614cc8..a63608df6abca45 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -617,6 +617,8 @@ rec { getSubModules = elemType.getSubModules; substSubModules = m: attrsWith { elemType = elemType.substSubModules m; inherit name lazy; }; functor = defaultFunctor "attrsWith" // { + wrapped = elemType; + type = t: attrsWith { elemType = t; inherit name lazy; }; payload = { inherit elemType name lazy; }; From 95c2276e5978f404645a4013d8aa286efef23313 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Wed, 9 Oct 2024 09:16:59 +0200 Subject: [PATCH 8/8] lib.types.attrsWith: doc add missing listing --- nixos/doc/manual/development/option-types.section.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nixos/doc/manual/development/option-types.section.md b/nixos/doc/manual/development/option-types.section.md index 3c6afe5ff6b7c07..82eaae47504d088 100644 --- a/nixos/doc/manual/development/option-types.section.md +++ b/nixos/doc/manual/development/option-types.section.md @@ -403,6 +403,9 @@ Composed types are types that take a type as parameter. `listOf : An attribute set of where all the values are of *`attrs.elemType`* type. + `attrs.elemType` (`type`, required ) + : The expected type of all attribute values. + `attrs.lazy` (`Bool`, default: `false` ) : If set to `true` attributes will be evaluated lazily. See also: `types.lazyAttrsOf`