Enable stdenv customization through (buildPython*.override { inherit stdenv; }) (#271762)

This commit is contained in:
Yueh-Shun Li
2025-10-24 15:11:13 +00:00
committed by GitHub
7 changed files with 60 additions and 16 deletions

View File

@@ -557,6 +557,19 @@ are used in [`buildPythonPackage`](#buildpythonpackage-function).
with the `pipInstallHook`. with the `pipInstallHook`.
- `unittestCheckHook` will run tests with `python -m unittest discover`. See [example usage](#using-unittestcheckhook). - `unittestCheckHook` will run tests with `python -m unittest discover`. See [example usage](#using-unittestcheckhook).
#### Overriding build helpers {#overriding-python-build-helpers}
Like many of the build helpers provided by Nixpkgs, Python build helpers typically provide a `<function>.override` attribute.
It works like [`<pkg>.override`](#sec-pkg-override), and can be used to override the dependencies of each build helper.
This allows specifying the stdenv to be used by `buildPythonPackage` or `buildPythonApplication`. The default (`python.stdenv`) can be overridden as follows:
```nix
buildPythonPackage.override { stdenv = customStdenv; } {
# package attrs...
}
```
## User Guide {#user-guide} ## User Guide {#user-guide}
### Using Python {#using-python} ### Using Python {#using-python}

View File

@@ -3807,6 +3807,9 @@
"buildpythonpackage-parameters": [ "buildpythonpackage-parameters": [
"index.html#buildpythonpackage-parameters" "index.html#buildpythonpackage-parameters"
], ],
"overriding-python-build-helpers": [
"index.html#overriding-python-build-helpers"
],
"overriding-python-packages": [ "overriding-python-packages": [
"index.html#overriding-python-packages" "index.html#overriding-python-packages"
], ],

View File

@@ -332,6 +332,10 @@
- `emacs` now disables the GC mark trace buffer by default. This improves GC performance by 5%, but can make GC issues harder to debug. This is configurable with `withGcMarkTrace`. - `emacs` now disables the GC mark trace buffer by default. This improves GC performance by 5%, but can make GC issues harder to debug. This is configurable with `withGcMarkTrace`.
- Passing `stdenv` to `buildPythonPackage` or `buildPythonApplication` has been deprecated and will trigger an error in a future release.
Instead, you should _override_ the python build helper, e.g., `(buildPythonPackage.override { stdenv = customStdenv; })`.
See [](#overriding-python-build-helpers).
- `buildPythonPackage` and `buildPythonApplication` now default to `nix-update-script` as their default `updateScript`. This should improve automated updates, since nix-update is better maintained than the in-tree update script and has more robust fetcher support. - `buildPythonPackage` and `buildPythonApplication` now default to `nix-update-script` as their default `updateScript`. This should improve automated updates, since nix-update is better maintained than the in-tree update script and has more robust fetcher support.
- `plasma6`: Fixed the `ksycoca` cache not being re-built when `$XDG_CACHE_HOME` is set to something that isn't `$HOME/.cache`. - `plasma6`: Fixed the `ksycoca` cache not being re-built when `$XDG_CACHE_HOME` is set to something that isn't `$HOME/.cache`.

View File

@@ -4,6 +4,8 @@
lib, lib,
config, config,
python, python,
# Allow passing in a custom stdenv to buildPython*.override
stdenv,
wrapPython, wrapPython,
unzip, unzip,
ensureNewerSourcesForZipFilesHook, ensureNewerSourcesForZipFilesHook,
@@ -192,9 +194,6 @@ in
doCheck ? true, doCheck ? true,
# Allow passing in a custom stdenv to buildPython*
stdenv ? python.stdenv,
... ...
}@attrs: }@attrs:

View File

@@ -45,21 +45,44 @@ let
override = lib.mirrorFunctionArgs f.override (fdrv: makeOverridablePythonPackage (f.override fdrv)); override = lib.mirrorFunctionArgs f.override (fdrv: makeOverridablePythonPackage (f.override fdrv));
}; };
overrideStdenvCompat =
f:
lib.setFunctionArgs (
args:
if !(lib.isFunction args) && (args ? stdenv) then
lib.warnIf (lib.oldestSupportedReleaseIsAtLeast 2511) ''
Passing `stdenv` directly to `buildPythonPackage` or `buildPythonApplication` is deprecated. You should use their `.override` function instead, e.g:
buildPythonPackage.override { stdenv = customStdenv; } { }
'' (f.override { stdenv = args.stdenv; } args)
else
f args
) (removeAttrs (lib.functionArgs f) [ "stdenv" ])
// {
# Intentionally drop the effect of overrideStdenvCompat when calling `buildPython*.override`.
inherit (f) override;
};
mkPythonDerivation = mkPythonDerivation =
if python.isPy3k then ./mk-python-derivation.nix else ./python2/mk-python-derivation.nix; if python.isPy3k then ./mk-python-derivation.nix else ./python2/mk-python-derivation.nix;
buildPythonPackage = makeOverridablePythonPackage ( buildPythonPackage = makeOverridablePythonPackage (
overrideStdenvCompat (
callPackage mkPythonDerivation { callPackage mkPythonDerivation {
inherit namePrefix; # We want Python libraries to be named like e.g. "python3.6-${name}" inherit namePrefix; # We want Python libraries to be named like e.g. "python3.6-${name}"
inherit toPythonModule; # Libraries provide modules inherit toPythonModule; # Libraries provide modules
inherit (python) stdenv;
} }
)
); );
buildPythonApplication = makeOverridablePythonPackage ( buildPythonApplication = makeOverridablePythonPackage (
overrideStdenvCompat (
callPackage mkPythonDerivation { callPackage mkPythonDerivation {
namePrefix = ""; # Python applications should not have any prefix namePrefix = ""; # Python applications should not have any prefix
toPythonModule = x: x; # Application does not provide modules. toPythonModule = x: x; # Application does not provide modules.
inherit (python) stdenv;
} }
)
); );
# Check whether a derivation provides a Python module. # Check whether a derivation provides a Python module.

View File

@@ -4,6 +4,8 @@
lib, lib,
config, config,
python, python,
# Allow passing in a custom stdenv to buildPython*.override
stdenv,
wrapPython, wrapPython,
unzip, unzip,
ensureNewerSourcesForZipFilesHook, ensureNewerSourcesForZipFilesHook,
@@ -101,8 +103,6 @@
}@attrs: }@attrs:
let let
inherit (python) stdenv;
withDistOutput = lib.elem format [ withDistOutput = lib.elem format [
"pyproject" "pyproject"
"setuptools" "setuptools"

View File

@@ -275,14 +275,16 @@ let
stdenv' = if cudaSupport then cudaPackages.backendStdenv else stdenv; stdenv' = if cudaSupport then cudaPackages.backendStdenv else stdenv;
in in
buildPythonPackage rec { let
# From here on, `stdenv` shall be `stdenv'`.
stdenv = stdenv';
in
buildPythonPackage.override { inherit stdenv; } rec {
pname = "torch"; pname = "torch";
# Don't forget to update torch-bin to the same version. # Don't forget to update torch-bin to the same version.
version = "2.8.0"; version = "2.8.0";
pyproject = true; pyproject = true;
stdenv = stdenv';
outputs = [ outputs = [
"out" # output standard python package "out" # output standard python package
"dev" # output libtorch headers "dev" # output libtorch headers