From 907cb3d253e2b35e23f97be5ef8a208dc41f9b8d Mon Sep 17 00:00:00 2001 From: K900 Date: Tue, 3 Dec 2024 09:06:24 +0300 Subject: [PATCH] Revert "lib/types: init {types.attrsWith}" --- lib/modules.nix | 40 ++------- lib/tests/modules.sh | 9 -- lib/tests/modules/lazy-attrsWith.nix | 57 ------------- lib/types.nix | 118 ++++++++++----------------- 4 files changed, 47 insertions(+), 177 deletions(-) delete mode 100644 lib/tests/modules/lazy-attrsWith.nix diff --git a/lib/modules.nix b/lib/modules.nix index f5b8af0e9858b..855ffaf25ed81 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -751,47 +751,17 @@ let t' = opt.options.type; mergedType = t.typeMerge t'.functor; typesMergeable = mergedType != null; - - # TODO: Remove this when all downstream reliances of internals: 'functor.wrapped' are sufficiently migrated. - # A function that adds the deprecated wrapped message to a type. - addDeprecatedWrapped = t: - t // { - functor = t.functor // { - wrapped = t.functor.wrappedDeprecationMessage { - inherit loc; - }; - }; - }; - - typeSet = - if opt.options ? type then - if res ? type then - if typesMergeable then - { - type = - if mergedType ? functor.wrappedDeprecationMessage then - addDeprecatedWrapped mergedType - else - mergedType; - } - else - # Keep in sync with the same error below! - throw "The option `${showOption loc}' in `${opt._file}' is already declared in ${showFiles res.declarations}." - else if opt.options.type ? functor.wrappedDeprecationMessage then - { type = addDeprecatedWrapped opt.options.type; } - else - {} - else - {}; - + typeSet = if (bothHave "type") && typesMergeable + then { type = mergedType; } + else {}; bothHave = k: opt.options ? ${k} && res ? ${k}; in if bothHave "default" || bothHave "example" || bothHave "description" || - bothHave "apply" + bothHave "apply" || + (bothHave "type" && (! typesMergeable)) then - # Keep in sync with the same error above! throw "The option `${showOption loc}' in `${opt._file}' is already declared in ${showFiles res.declarations}." else let diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index 6085d4b7984ab..2884c00d0768c 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -386,14 +386,6 @@ checkConfigOutput '^true$' config.conditionalWorks ./declare-attrsOf.nix ./attrs checkConfigOutput '^false$' config.conditionalWorks ./declare-lazyAttrsOf.nix ./attrsOf-conditional-check.nix checkConfigOutput '^"empty"$' config.value.foo ./declare-lazyAttrsOf.nix ./attrsOf-conditional-check.nix -# Check attrsWith type merging -checkConfigError 'The option `mergedLazyNonLazy'\'' in `.*'\'' is already declared in `.*'\''\.' options.mergedLazyNonLazy ./lazy-attrsWith.nix -checkConfigOutput '^11$' config.lazyResult ./lazy-attrsWith.nix -checkConfigError 'infinite recursion encountered' config.nonLazyResult ./lazy-attrsWith.nix - -# Test the attrsOf functor.wrapped warning -# shellcheck disable=2016 -NIX_ABORT_ON_WARN=1 checkConfigError 'The deprecated `type.functor.wrapped` attribute of the option `mergedLazyLazy` is accessed, use `nestedTypes.elemType` instead.' options.mergedLazyLazy.type.functor.wrapped ./lazy-attrsWith.nix # Even with multiple assignments, a type error should be thrown if any of them aren't valid checkConfigError 'A definition for option .* is not of type .*' \ @@ -583,7 +575,6 @@ checkConfigOutput '^38|27$' options.submoduleLine38.declarationPositions.1.line # nested options work checkConfigOutput '^34$' options.nested.nestedLine34.declarationPositions.0.line ./declaration-positions.nix - cat <"]); + getSubModules = elemType.getSubModules; + substSubModules = m: attrsOf (elemType.substSubModules m); + functor = (defaultFunctor name) // { wrapped = elemType; }; + nestedTypes.elemType = 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: attrsWith { inherit elemType; lazy = true; }; - - # base type for lazyAttrsOf and attrsOf - attrsWith = - let - # Push down position info. - pushPositions = map (def: mapAttrs (n: v: { inherit (def) file; value = v; }) def.value); - binOp = lhs: rhs: - let - elemType = lhs.elemType.typeMerge rhs.elemType.functor; - lazy = - if lhs.lazy == rhs.lazy then - lhs.lazy - else - null; - in - if elemType == null || lazy == null then - null - else - { - inherit elemType lazy; - }; - in - { - elemType, - lazy ? false, - }: - mkOptionType { - 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}"; + lazyAttrsOf = elemType: mkOptionType rec { + name = "lazyAttrsOf"; + description = "lazy attribute set of ${optionDescriptionPhrase (class: class == "noun" || class == "composite") elemType}"; descriptionClass = "composite"; check = isAttrs; - 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))) - ); + merge = 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); emptyValue = { value = {}; }; getSubOptions = prefix: elemType.getSubOptions (prefix ++ [""]); getSubModules = elemType.getSubModules; - substSubModules = m: attrsWith { elemType = elemType.substSubModules m; inherit lazy; }; - functor = defaultFunctor "attrsWith" // { - wrappedDeprecationMessage = { loc }: lib.warn '' - The deprecated `type.functor.wrapped` attribute of the option `${showOption loc}` is accessed, use `type.nestedTypes.elemType` instead. - '' elemType; - payload = { - # Important!: Add new function attributes here in case of future changes - inherit elemType lazy; - }; - inherit binOp; - }; + substSubModules = m: lazyAttrsOf (elemType.substSubModules m); + functor = (defaultFunctor name) // { wrapped = elemType; }; nestedTypes.elemType = elemType; };