2022-10-03 12:25:02 +02:00
|
|
|
{ pkgs
|
|
|
|
, stdenv
|
|
|
|
, lib
|
|
|
|
, python
|
|
|
|
}:
|
|
|
|
|
|
|
|
self:
|
|
|
|
|
|
|
|
let
|
|
|
|
inherit (self) callPackage;
|
2022-11-28 02:13:21 +01:00
|
|
|
inherit (python.passthru) isPy27 isPy37 isPy38 isPy39 isPy310 isPy311 isPy3k isPyPy pythonAtLeast pythonOlder;
|
2022-10-03 12:25:02 +02:00
|
|
|
|
|
|
|
namePrefix = python.libPrefix + "-";
|
|
|
|
|
2022-12-18 01:39:44 +01:00
|
|
|
# Derivations built with `buildPythonPackage` can already be overridden with `override`, `overrideAttrs`, and `overrideDerivation`.
|
2022-10-03 12:25:02 +02:00
|
|
|
# This function introduces `overridePythonAttrs` and it overrides the call to `buildPythonPackage`.
|
|
|
|
makeOverridablePythonPackage = f: origArgs:
|
|
|
|
let
|
2022-11-20 15:19:15 +01:00
|
|
|
args = lib.fix (lib.extends
|
|
|
|
(_: previousAttrs: {
|
|
|
|
passthru = (previousAttrs.passthru or { }) // {
|
|
|
|
overridePythonAttrs = newArgs: makeOverridablePythonPackage f (overrideWith newArgs);
|
|
|
|
};
|
|
|
|
})
|
|
|
|
(_: origArgs));
|
|
|
|
result = f args;
|
|
|
|
overrideWith = newArgs: args // (if pkgs.lib.isFunction newArgs then newArgs args else newArgs);
|
2022-10-03 12:25:02 +02:00
|
|
|
in
|
2022-11-20 15:19:15 +01:00
|
|
|
if builtins.isAttrs result then result
|
|
|
|
else if builtins.isFunction result then {
|
2022-10-03 12:25:02 +02:00
|
|
|
overridePythonAttrs = newArgs: makeOverridablePythonPackage f (overrideWith newArgs);
|
2022-11-20 15:19:15 +01:00
|
|
|
__functor = self: result;
|
2022-10-03 12:25:02 +02:00
|
|
|
}
|
2022-11-20 15:19:15 +01:00
|
|
|
else result;
|
2022-10-03 12:25:02 +02:00
|
|
|
|
|
|
|
buildPythonPackage = makeOverridablePythonPackage (lib.makeOverridable (callPackage ./mk-python-derivation.nix {
|
|
|
|
inherit namePrefix; # We want Python libraries to be named like e.g. "python3.6-${name}"
|
|
|
|
inherit toPythonModule; # Libraries provide modules
|
|
|
|
}));
|
|
|
|
|
|
|
|
buildPythonApplication = makeOverridablePythonPackage (lib.makeOverridable (callPackage ./mk-python-derivation.nix {
|
|
|
|
namePrefix = ""; # Python applications should not have any prefix
|
|
|
|
toPythonModule = x: x; # Application does not provide modules.
|
|
|
|
}));
|
|
|
|
|
|
|
|
# See build-setupcfg/default.nix for documentation.
|
2023-02-14 19:11:59 +01:00
|
|
|
buildSetupcfg = import ../../../build-support/build-setupcfg lib self;
|
2022-10-03 12:25:02 +02:00
|
|
|
|
|
|
|
# Check whether a derivation provides a Python module.
|
|
|
|
hasPythonModule = drv: drv?pythonModule && drv.pythonModule == python;
|
|
|
|
|
|
|
|
# Get list of required Python modules given a list of derivations.
|
|
|
|
requiredPythonModules = drvs: let
|
|
|
|
modules = lib.filter hasPythonModule drvs;
|
|
|
|
in lib.unique ([python] ++ modules ++ lib.concatLists (lib.catAttrs "requiredPythonModules" modules));
|
|
|
|
|
|
|
|
# Create a PYTHONPATH from a list of derivations. This function recurses into the items to find derivations
|
|
|
|
# providing Python modules.
|
|
|
|
makePythonPath = drvs: lib.makeSearchPath python.sitePackages (requiredPythonModules drvs);
|
|
|
|
|
|
|
|
removePythonPrefix = lib.removePrefix namePrefix;
|
|
|
|
|
|
|
|
# Convert derivation to a Python module.
|
|
|
|
toPythonModule = drv:
|
|
|
|
drv.overrideAttrs( oldAttrs: {
|
|
|
|
# Use passthru in order to prevent rebuilds when possible.
|
|
|
|
passthru = (oldAttrs.passthru or {})// {
|
|
|
|
pythonModule = python;
|
|
|
|
pythonPath = [ ]; # Deprecated, for compatibility.
|
|
|
|
requiredPythonModules = requiredPythonModules drv.propagatedBuildInputs;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
# Convert a Python library to an application.
|
|
|
|
toPythonApplication = drv:
|
|
|
|
drv.overrideAttrs( oldAttrs: {
|
|
|
|
passthru = (oldAttrs.passthru or {}) // {
|
|
|
|
# Remove Python prefix from name so we have a "normal" name.
|
|
|
|
# While the prefix shows up in the store path, it won't be
|
|
|
|
# used by `nix-env`.
|
|
|
|
name = removePythonPrefix oldAttrs.name;
|
|
|
|
pythonModule = false;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
disabled = drv: throw "${removePythonPrefix (drv.pname or drv.name)} not supported for interpreter ${python.executable}";
|
|
|
|
|
|
|
|
disabledIf = x: drv: if x then disabled drv else drv;
|
|
|
|
|
|
|
|
in {
|
|
|
|
|
|
|
|
inherit lib pkgs stdenv;
|
2022-11-28 02:13:21 +01:00
|
|
|
inherit (python.passthru) isPy27 isPy37 isPy38 isPy39 isPy310 isPy311 isPy3k isPyPy pythonAtLeast pythonOlder;
|
2022-10-03 12:25:02 +02:00
|
|
|
inherit buildPythonPackage buildPythonApplication;
|
2023-01-17 17:46:03 +01:00
|
|
|
inherit (pkgs) fetchPypi;
|
2022-10-03 12:25:02 +02:00
|
|
|
inherit hasPythonModule requiredPythonModules makePythonPath disabled disabledIf;
|
|
|
|
inherit toPythonModule toPythonApplication;
|
|
|
|
inherit buildSetupcfg;
|
|
|
|
|
|
|
|
python = toPythonModule python;
|
|
|
|
# Dont take pythonPackages from "global" pkgs scope to avoid mixing python versions
|
|
|
|
pythonPackages = self;
|
|
|
|
|
|
|
|
# Remove?
|
|
|
|
recursivePthLoader = toPythonModule (callPackage ../../../development/python-modules/recursive-pth-loader { });
|
|
|
|
|
|
|
|
}
|