NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.08k stars 14.14k forks source link

Python: `buildPythonPackage` and `overridePythonAttrs` should support `final: prev:` style overrides #258246

Open RuRo opened 1 year ago

RuRo commented 1 year ago

After #119942, mkDerivation supports recursive definition of package attributes with

foo = mkDerivation (final: { });
foo_v2 = foo.overrideAttrs (final: prev: { });

# instead of
foo = mkDerivation rec { };
foo_v2 = foo.overrideAttrs (prev: { });

The buildPythonPackage.overridePythonAttrs functionality originally introduced in #26155 should also support this style of overrides (to avoid the rec antipattern and make the overrides more composable).

pyfoo = buildPythonPackage (final: { });
pyfoo_v2 = pyfoo.overridePythonAttrs (final: prev: { });

# instead of
pyfoo = buildPythonPackage rec { };
pyfoo_v2 = pyfoo.overridePythonAttrs (prev: { });

Also, this probably belongs in #1819.

ShamrockLee commented 12 months ago

234651 addresses this issue with new functions lib.extendMkDerivation and lib.extendMkDerivationModified to defined build helpers with fixed-point arguments based on existing ones. Please take a look!

uninsane commented 4 months ago

for simple packages, a stopgap fix is to use toPythonModule (stdenv.mkDerivation (finalAttrs: { ... })). you'll have to add the appropriate setup hooks to native{Build,Check}Inputs which buildPythonPackage would ordinarily takes care of though.

not the best ergonomics, but if you need it, at least it is possible.