1{ pkgs
2, stdenv
3, lib
4, python
5}:
6
7self:
8
9let
10 inherit (self) callPackage;
11 inherit (python.passthru) isPy27 isPy35 isPy36 isPy37 isPy38 isPy39 isPy310 isPy311 isPy3k isPyPy pythonAtLeast pythonOlder;
12
13 namePrefix = python.libPrefix + "-";
14
15 # Derivations built with `buildPythonPackage` can already be overriden with `override`, `overrideAttrs`, and `overrideDerivation`.
16 # This function introduces `overridePythonAttrs` and it overrides the call to `buildPythonPackage`.
17 makeOverridablePythonPackage = f: origArgs:
18 let
19 ff = f origArgs;
20 overrideWith = newArgs: origArgs // (if pkgs.lib.isFunction newArgs then newArgs origArgs else newArgs);
21 in
22 if builtins.isAttrs ff then (ff // {
23 overridePythonAttrs = newArgs: makeOverridablePythonPackage f (overrideWith newArgs);
24 })
25 else if builtins.isFunction ff then {
26 overridePythonAttrs = newArgs: makeOverridablePythonPackage f (overrideWith newArgs);
27 __functor = self: ff;
28 }
29 else ff;
30
31 buildPythonPackage = makeOverridablePythonPackage (lib.makeOverridable (callPackage ./mk-python-derivation.nix {
32 inherit namePrefix; # We want Python libraries to be named like e.g. "python3.6-${name}"
33 inherit toPythonModule; # Libraries provide modules
34 }));
35
36 buildPythonApplication = makeOverridablePythonPackage (lib.makeOverridable (callPackage ./mk-python-derivation.nix {
37 namePrefix = ""; # Python applications should not have any prefix
38 toPythonModule = x: x; # Application does not provide modules.
39 }));
40
41 # See build-setupcfg/default.nix for documentation.
42 buildSetupcfg = import ../../../build-support/build-setupcfg self;
43
44 fetchPypi = callPackage ./fetchpypi.nix { };
45
46 # Check whether a derivation provides a Python module.
47 hasPythonModule = drv: drv?pythonModule && drv.pythonModule == python;
48
49 # Get list of required Python modules given a list of derivations.
50 requiredPythonModules = drvs: let
51 modules = lib.filter hasPythonModule drvs;
52 in lib.unique ([python] ++ modules ++ lib.concatLists (lib.catAttrs "requiredPythonModules" modules));
53
54 # Create a PYTHONPATH from a list of derivations. This function recurses into the items to find derivations
55 # providing Python modules.
56 makePythonPath = drvs: lib.makeSearchPath python.sitePackages (requiredPythonModules drvs);
57
58 removePythonPrefix = lib.removePrefix namePrefix;
59
60 # Convert derivation to a Python module.
61 toPythonModule = drv:
62 drv.overrideAttrs( oldAttrs: {
63 # Use passthru in order to prevent rebuilds when possible.
64 passthru = (oldAttrs.passthru or {})// {
65 pythonModule = python;
66 pythonPath = [ ]; # Deprecated, for compatibility.
67 requiredPythonModules = requiredPythonModules drv.propagatedBuildInputs;
68 };
69 });
70
71 # Convert a Python library to an application.
72 toPythonApplication = drv:
73 drv.overrideAttrs( oldAttrs: {
74 passthru = (oldAttrs.passthru or {}) // {
75 # Remove Python prefix from name so we have a "normal" name.
76 # While the prefix shows up in the store path, it won't be
77 # used by `nix-env`.
78 name = removePythonPrefix oldAttrs.name;
79 pythonModule = false;
80 };
81 });
82
83 disabled = drv: throw "${removePythonPrefix (drv.pname or drv.name)} not supported for interpreter ${python.executable}";
84
85 disabledIf = x: drv: if x then disabled drv else drv;
86
87in {
88
89 inherit lib pkgs stdenv;
90 inherit (python.passthru) isPy27 isPy35 isPy36 isPy37 isPy38 isPy39 isPy310 isPy311 isPy3k isPyPy pythonAtLeast pythonOlder;
91 inherit buildPythonPackage buildPythonApplication;
92 inherit fetchPypi;
93 inherit hasPythonModule requiredPythonModules makePythonPath disabled disabledIf;
94 inherit toPythonModule toPythonApplication;
95 inherit buildSetupcfg;
96
97 python = toPythonModule python;
98 # Dont take pythonPackages from "global" pkgs scope to avoid mixing python versions
99 pythonPackages = self;
100
101 # Remove?
102 recursivePthLoader = toPythonModule (callPackage ../../../development/python-modules/recursive-pth-loader { });
103
104}