git.iko.soy/typeable/nixpkgs
on master help sign in
Python: add buildPythonPackage.overridePythonPackage method.

This allows one to always override the call to `buildPythonPackage`. In the following example we create an environment where we have the `blaze` package using an older version of `pandas`. We override first the Python interpreter and pass `packageOverrides` which contains the overrides for packages in the package set. ``` with import <nixpkgs> {}; (let python = let packageOverrides = self: super: { pandas = super.pandas.overridePythonPackage(old: rec { version = "0.19.1"; name = "pandas-${version}"; src = super.fetchPypi { pname = "pandas"; inherit version; sha256 = "08blshqj9zj1wyjhhw3kl2vas75vhhicvv72flvf1z3jvapgw295"; }; }); }; in pkgs.python3.override {inherit packageOverrides;}; in python.withPackages(ps: [ps.blaze])).env ```

commit345b35c48af0c6534fc649ef4b2050ebf0b91d50 authorFrederik Rietdijk <fridh@fridh.nl> dateTue 21 Mar 2017, 10:08 (10 years ago) parent90282d9
2 files changed+52−6
doc/languages-frameworks/python.md+34−5 pkgs/top-level/python-packages.nix+18−1
doc/languages-frameworks/python.md+34−5
@@ -545,6 +545,35 @@ All parameters from `mkDerivation` function are still supported.
545545* `catchConflicts` If `true`, abort package build if a package name appears more than once in dependency tree. Default is `true`.
546546* `checkInputs` Dependencies needed for running the `checkPhase`. These are added to `buildInputs` when `doCheck = true`.
547547
548+##### Overriding Python packages
549+
550+The `buildPythonPackage` function has a `overridePythonPackage` method that
551+can be used to override the package. In the following example we create an
552+environment where we have the `blaze` package using an older version of `pandas`.
553+We override first the Python interpreter and pass
554+`packageOverrides` which contains the overrides for packages in the package set.
555+
556+```nix
557+with import <nixpkgs> {};
558+
559+(let
560+ python = let
561+ packageOverrides = self: super: {
562+ pandas = super.pandas.overridePythonPackage(old: rec {
563+ version = "0.19.1";
564+ name = "pandas-${version}";
565+ src = super.fetchPypi {
566+ pname = "pandas";
567+ inherit version;
568+ sha256 = "08blshqj9zj1wyjhhw3kl2vas75vhhicvv72flvf1z3jvapgw295";
569+ };
570+ });
571+ };
572+ in pkgs.python3.override {inherit packageOverrides;};
573+
574+in python.withPackages(ps: [ps.blaze])).env
575+```
576+
548577#### `buildPythonApplication` function
549578
550579The `buildPythonApplication` function is practically the same as `buildPythonPackage`.
@@ -754,17 +783,17 @@ In the following example we rename the `pandas` package and build it.
754783```nix
755784with import <nixpkgs> {};
756785
757−let
786+(let
758787 python = let
759788 packageOverrides = self: super: {
760− pandas = super.pandas.override {name="foo";};
789+ pandas = super.pandas.overridePythonPackage(old: {name="foo";});
761790 };
762791 in pkgs.python35.override {inherit packageOverrides;};
763792
764−in python.pkgs.pandas
793+in python.withPackages(ps: [ps.pandas])).env
765794```
766−Using `nix-build` on this expression will build the package `pandas`
767−but with the new name `foo`.
795+Using `nix-build` on this expression will build an environment that contains the
796+package `pandas` but with the new name `foo`.
768797
769798All packages in the package set will use the renamed package.
770799A typical use case is to switch to another version of a certain package.
pkgs/top-level/python-packages.nix+18−1
@@ -35,7 +35,24 @@ let
3535
3636 mkPythonDerivation = makeOverridable( callPackage ../development/interpreters/python/mk-python-derivation.nix {
3737 });
38− buildPythonPackage = makeOverridable (callPackage ../development/interpreters/python/build-python-package.nix {
38+
39+ # Derivations built with `buildPythonPackage` can already be overriden with `override`, `overrideAttrs`, and `overrideDerivation`.
40+ # This function introduces `overridePythonPackage` and it overrides the call to `buildPythonPackage`.
41+ makeOverridablePythonPackage = f: origArgs:
42+ let
43+ ff = f origArgs;
44+ overrideWith = newArgs: origArgs // (if builtins.isFunction newArgs then newArgs origArgs else newArgs);
45+ in
46+ if builtins.isAttrs ff then (ff // {
47+ overridePythonPackage = newArgs: makeOverridable f (overrideWith newArgs);
48+ })
49+ else if builtins.isFunction ff then {
50+ overridePythonPackage = newArgs: makeOverridable f (overrideWith newArgs);
51+ __functor = self: ff;
52+ }
53+ else ff;
54+
55+ buildPythonPackage = makeOverridablePythonPackage (callPackage ../development/interpreters/python/build-python-package.nix {
3956 inherit mkPythonDerivation;
4057 inherit bootstrapped-pip;
4158 flit = self.flit;