diff --git a/pkgs/development/interpreters/python/mk-python-derivation.nix b/pkgs/development/interpreters/python/mk-python-derivation.nix index f3e5249c189e..0fb0a1326025 100644 --- a/pkgs/development/interpreters/python/mk-python-derivation.nix +++ b/pkgs/development/interpreters/python/mk-python-derivation.nix @@ -418,6 +418,7 @@ lib.extendMkDerivation { optional-dependencies ; updateScript = nix-update-script { }; + ${if attrs ? stdenv then "__stdenvPythonCompat" else null} = attrs.stdenv; } // attrs.passthru or { }; diff --git a/pkgs/development/interpreters/python/python-packages-base.nix b/pkgs/development/interpreters/python/python-packages-base.nix index 91d733a33c3e..77ac26630161 100644 --- a/pkgs/development/interpreters/python/python-packages-base.nix +++ b/pkgs/development/interpreters/python/python-packages-base.nix @@ -53,15 +53,28 @@ let f': lib.mirrorFunctionArgs f ( args: - if !(lib.isFunction args) && (args ? stdenv) then - lib.warnIf (lib.oldestSupportedReleaseIsAtLeast 2511) '' - ${ - args.name or args.pname or "" - }: Passing `stdenv` directly to `buildPythonPackage` or `buildPythonApplication` is deprecated. You should use their `.override` function instead, e.g: - buildPythonPackage.override { stdenv = customStdenv; } { } - '' (f'.override { inherit (args) stdenv; } (removeAttrs args [ "stdenv" ])) + let + result = f args; + getName = x: x.pname or (lib.getName (x.name or "")); + applyMsgStdenvArg = + name: + lib.warnIf (lib.oldestSupportedReleaseIsAtLeast 2511) '' + ${name}: Passing `stdenv` directly to `buildPythonPackage` or `buildPythonApplication` is deprecated. You should use their `.override` function instead, e.g: + buildPythonPackage.override { stdenv = customStdenv; } { } + ''; + in + if lib.isFunction args && result ? __stdenvPythonCompat then + # Less reliable, as constructing with the wrong `stdenv` might lead to evaluation errors in the package definition. + f'.override { stdenv = applyMsgStdenvArg (getName result) result.__stdenvPythonCompat; } ( + finalAttrs: removeAttrs (args finalAttrs) [ "stdenv" ] + ) + else if (!lib.isFunction args) && (args ? stdenv) then + # More reliable, but only works when args is not `(finalAttrs: { })` + f'.override { stdenv = applyMsgStdenvArg (getName args) args.stdenv; } ( + removeAttrs args [ "stdenv" ] + ) else - f args + result ) // { # Preserve the effect of overrideStdenvCompat when calling `buildPython*.override`.