Skip to content
Open
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
91 changes: 69 additions & 22 deletions doc/languages-frameworks/python.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,61 @@ following are specific to `buildPythonPackage`:
* `setupPyGlobalFlags ? []`: List of flags passed to `setup.py` command.
* `setupPyBuildFlags ? []`: List of flags passed to `setup.py build_ext` command.

##### Using fixed-point arguments {#buildpythonpackage-fixed-point-arguments}

Both `buildPythonPackage` and `buildPythonApplication` support [fixed-point arguments](#chap-build-helpers-finalAttrs), similar to `stdenv.mkDerivation`.
This allows you to reference the final attributes of the derivation.

Instead of using `rec`:

```nix
buildPythonPackage rec {
pname = "pyspread";
version = "2.4";
src = fetchPypi {
inherit pname version;
hash = "sha256-...";
};
}
```

You can use the `finalAttrs` pattern:

```nix
buildPythonPackage (finalAttrs: {
pname = "pyspread";
version = "2.4";
src = fetchPypi {
pname = "pyspread";
inherit (finalAttrs) version;
Comment on lines +235 to +236
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect it to inherit pname as well, this is one of the few times I'd think using pname would be fine (as opposed to e.g: fetchFromGitHub's repo argument).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't really. We normalize pname (e.g. apply lowercasing), but the pypi name may not reflect that, so it is often untied.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is often untied

So this means while this documentation suggest untying it, tying pname is also accepted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it might be stable, if the library name already is normalized, but uppercase, dashes, etc. will cause flapping every once in a while.

Copy link
Contributor

@MattSturgeon MattSturgeon Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if you do reuse the name for pname and src.pname, it's best to use a locally defined variable rather than finalAttrs.pname. Values in finalAttrs represent the final state, including any user defined overrides.

pname is an example of something an end-user may wish to override, e.g. pname = prevAttrs.pname + "-with-my-tweaks". Such an override shouldn't propagate to the src's project.

hash = "sha256-...";
};
})
```

See the [general documentation on fixed-point arguments](#chap-build-helpers-finalAttrs) for more details on the benefits of this pattern.

::: {.note}

Some `buildPythonPackage`/`buildPythonApplication` arguments are passed down indirectly to `stdenv.mkDerivation` via `passthru`.
Therefore the final state of these attributes can be accessed via `finalAttrs.passthru.${name}`.
[`<pkg>.overrideAttrs`](#sec-pkg-overrideAttrs) can override them using the `passthru = prevAttrs.passthru // { foo = "bar"; }` pattern.
Such arguments include:

- `disabled`
- `pyproject`
- `format`
- `build-system`, `dependencies` and `optional-dependencies`

<!--
TODO(@doronbehar): When [#258246][1] will be resolved, or when
`.overridePythonAttrs` will be removed, the above text might need to be revised.

- [1]: https://github.com/NixOS/nixpkgs/issues/258246

-->
:::

The [`stdenv.mkDerivation`](#sec-using-stdenv) function accepts various parameters for describing
build inputs (see "Specifying dependencies"). The following are of special
interest for Python packages, either because these are primarily used, or
Expand Down Expand Up @@ -237,29 +292,21 @@ the overrides for packages in the package set.
```nix
with import <nixpkgs> { };

(
let
python =
let
packageOverrides = self: super: {
pandas = super.pandas.overridePythonAttrs (old: rec {
version = "0.19.1";
src = fetchPypi {
pname = "pandas";
inherit version;
hash = "sha256-JQn+rtpy/OA2deLszSKEuxyttqBzcAil50H+JDHUdCE=";
};
});
let
python = pkgs.python3.override {
packageOverrides = self: super: {
pandas = super.pandas.overridePythonAttrs (old: {
version = "0.19.1";
src = fetchPypi {
pname = "pandas";
inherit version;
hash = "sha256-JQn+rtpy/OA2deLszSKEuxyttqBzcAil50H+JDHUdCE=";
};
in
pkgs.python3.override {
inherit packageOverrides;
self = python;
};

in
python.withPackages (ps: [ ps.blaze ])
).env
});
};
};
in
(python.withPackages (ps: [ ps.blaze ])).env
```

The next example shows a non trivial overriding of the `blas` implementation to
Expand Down
3 changes: 3 additions & 0 deletions doc/redirects.json
Original file line number Diff line number Diff line change
Expand Up @@ -3845,6 +3845,9 @@
"buildpythonpackage-parameters": [
"index.html#buildpythonpackage-parameters"
],
"buildpythonpackage-fixed-point-arguments": [
"index.html#buildpythonpackage-fixed-point-arguments"
],
"overriding-python-build-helpers": [
"index.html#overriding-python-build-helpers"
],
Expand Down
Loading