Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions doc/languages-frameworks/python.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,19 @@ are used in [`buildPythonPackage`](#buildpythonpackage-function).
with the `pipInstallHook`.
- `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}

### Using Python {#using-python}
Expand Down
3 changes: 3 additions & 0 deletions doc/redirects.json
Original file line number Diff line number Diff line change
Expand Up @@ -3807,6 +3807,9 @@
"buildpythonpackage-parameters": [
"index.html#buildpythonpackage-parameters"
],
"overriding-python-build-helpers": [
"index.html#overriding-python-build-helpers"
],
"overriding-python-packages": [
"index.html#overriding-python-packages"
],
Expand Down
4 changes: 4 additions & 0 deletions doc/release-notes/rl-2511.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,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`.

- 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.

- `plasma6`: Fixed the `ksycoca` cache not being re-built when `$XDG_CACHE_HOME` is set to something that isn't `$HOME/.cache`.
Expand Down
5 changes: 2 additions & 3 deletions pkgs/development/interpreters/python/mk-python-derivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
lib,
config,
python,
# Allow passing in a custom stdenv to buildPython*.override
stdenv,
wrapPython,
unzip,
ensureNewerSourcesForZipFilesHook,
Expand Down Expand Up @@ -192,9 +194,6 @@ in

doCheck ? true,

# Allow passing in a custom stdenv to buildPython*
stdenv ? python.stdenv,

...
}@attrs:

Expand Down
39 changes: 31 additions & 8 deletions pkgs/development/interpreters/python/python-packages-base.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,44 @@ let
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 =
if python.isPy3k then ./mk-python-derivation.nix else ./python2/mk-python-derivation.nix;

buildPythonPackage = makeOverridablePythonPackage (
callPackage mkPythonDerivation {
inherit namePrefix; # We want Python libraries to be named like e.g. "python3.6-${name}"
inherit toPythonModule; # Libraries provide modules
}
overrideStdenvCompat (
callPackage mkPythonDerivation {
inherit namePrefix; # We want Python libraries to be named like e.g. "python3.6-${name}"
inherit toPythonModule; # Libraries provide modules
inherit (python) stdenv;
}
)
);

buildPythonApplication = makeOverridablePythonPackage (
callPackage mkPythonDerivation {
namePrefix = ""; # Python applications should not have any prefix
toPythonModule = x: x; # Application does not provide modules.
}
overrideStdenvCompat (
callPackage mkPythonDerivation {
namePrefix = ""; # Python applications should not have any prefix
toPythonModule = x: x; # Application does not provide modules.
inherit (python) stdenv;
}
)
);

# Check whether a derivation provides a Python module.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
lib,
config,
python,
# Allow passing in a custom stdenv to buildPython*.override
stdenv,
wrapPython,
unzip,
ensureNewerSourcesForZipFilesHook,
Expand Down Expand Up @@ -101,8 +103,6 @@
}@attrs:

let
inherit (python) stdenv;

withDistOutput = lib.elem format [
"pyproject"
"setuptools"
Expand Down
8 changes: 5 additions & 3 deletions pkgs/development/python-modules/torch/source/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,16 @@ let

stdenv' = if cudaSupport then cudaPackages.backendStdenv else stdenv;
in
buildPythonPackage rec {
let
# From here on, `stdenv` shall be `stdenv'`.
stdenv = stdenv';
in
buildPythonPackage.override { inherit stdenv; } rec {
pname = "torch";
# Don't forget to update torch-bin to the same version.
version = "2.8.0";
pyproject = true;

stdenv = stdenv';

outputs = [
"out" # output standard python package
"dev" # output libtorch headers
Expand Down
Loading