diff --git a/doc/redirects.json b/doc/redirects.json index 330c25effc146..d5360283e4613 100644 --- a/doc/redirects.json +++ b/doc/redirects.json @@ -71,6 +71,9 @@ "sec-nixpkgs-release-25.05-lib-breaking": [ "release-notes.html#sec-nixpkgs-release-25.05-lib-breaking" ], + "sec-nixpkgs-release-25.05-lib-deprecations": [ + "release-notes.html#sec-nixpkgs-release-25.05-lib-deprecations" + ], "sec-overlays-install": [ "index.html#sec-overlays-install" ], diff --git a/doc/release-notes/rl-2505.section.md b/doc/release-notes/rl-2505.section.md index 9fa72cc1f484f..901124b91c7ce 100644 --- a/doc/release-notes/rl-2505.section.md +++ b/doc/release-notes/rl-2505.section.md @@ -17,3 +17,10 @@ - Structure of the `functor` of some types has changed. `functor` is an implementation detail and should not be relied upon. If you did rely on it let us know in this [PR](https://github.com/NixOS/nixpkgs/pull/363565). - [`lib.types.enum`](https://nixos.org/manual/nixos/unstable/#sec-option-types-basic): Previously the `functor.payload` was the list of enum values directly. Now it is an attribute set containing the values in the `values` attribute. - [`lib.types.separatedString`](https://nixos.org/manual/nixos/unstable/#sec-option-types-string): Previously the `functor.payload` was the seperator directly. Now it is an attribute set containing the seperator in the `sep` attribute. + +### Deprecations {#sec-nixpkgs-release-25.05-lib-deprecations} + +`functor` is an implementation detail and should not be relied upon, but since its status wasn't clear and it has had some use cases without alternatives, changes are being handled as gracefully as possible. Deprecations within functor: +- `functor.wrapped` is now deprecated for some types and using it will give a warning with migration instructions. It is deprecated for these types: + - `lib.types.attrsWith` + - `lib.types.listOf` diff --git a/lib/types.nix b/lib/types.nix index 43ace4a3f6a50..e74775d0a27ad 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -71,6 +71,26 @@ let let pos = builtins.unsafeGetAttrPos name v; in if pos == null then "" else " at ${pos.file}:${toString pos.line}:${toString pos.column}"; + # Internal functor to help for migrating functor.wrapped to functor.payload.elemType + # Note that individual attributes can be overriden if needed. + elemTypeFunctor = name: { elemType, ... }@payload: { + inherit name payload; + type = outer_types.types.${name}; + binOp = a: b: + let + merged = a.elemType.typeMerge b.elemType.functor; + in + if merged == null + then + null + else + { elemType = merged; }; + wrappedDeprecationMessage = { loc }: lib.warn '' + The deprecated `type.functor.wrapped` attribute of the option `${showOption loc}` is accessed, use `type.nestedTypes.elemType` instead. + '' payload.elemType; + }; + + outer_types = rec { isType = type: x: (x._type or "") == type; @@ -580,7 +600,9 @@ rec { getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["*"]); getSubModules = elemType.getSubModules; substSubModules = m: listOf (elemType.substSubModules m); - functor = (defaultFunctor name) // { wrapped = elemType; }; + functor = (elemTypeFunctor name { inherit elemType; }) // { + type = payload: types.listOf payload.elemType; + }; nestedTypes.elemType = elemType; }; @@ -664,14 +686,10 @@ rec { getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<${placeholder}>"]); getSubModules = elemType.getSubModules; substSubModules = m: attrsWith { elemType = elemType.substSubModules m; inherit lazy placeholder; }; - 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 + functor = (elemTypeFunctor "attrsWith" { inherit elemType lazy placeholder; - }; + }) // { + # Custom type merging required because of the "placeholder" attribute inherit binOp; }; nestedTypes.elemType = elemType; diff --git a/nixos/doc/manual/redirects.json b/nixos/doc/manual/redirects.json index 106148d1dd041..b404fa35052fc 100644 --- a/nixos/doc/manual/redirects.json +++ b/nixos/doc/manual/redirects.json @@ -1904,6 +1904,9 @@ "sec-nixpkgs-release-25.05-lib-breaking": [ "release-notes.html#sec-nixpkgs-release-25.05-lib-breaking" ], + "sec-nixpkgs-release-25.05-lib-deprecations": [ + "release-notes.html#sec-nixpkgs-release-25.05-lib-deprecations" + ], "sec-release-24.11": [ "release-notes.html#sec-release-24.11" ],