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