Unverified Commit 92b47305 authored by Silvan Mosberger's avatar Silvan Mosberger Committed by GitHub
Browse files

lib/types: init defaultComposedFunctor for types.{attrsWith,listOf} (#366015)

parents ece718c4 5782ef8d
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -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"
  ],
+7 −0
Original line number Diff line number Diff line
@@ -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`
+26 −8
Original line number Diff line number Diff line
@@ -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;
+3 −0
Original line number Diff line number Diff line
@@ -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"
  ],