core/pkgs/by-name/py/python/python-packages-base.nix

112 lines
3.9 KiB
Nix
Raw Normal View History

2024-05-13 21:24:10 +00:00
{ pkgs, stdenv, lib, python }:
2024-05-02 00:46:19 +00:00
self:
let
inherit (self) callPackage;
namePrefix = python.libPrefix + "-";
# Derivations built with `buildPythonPackage` can already be overridden with `override`, `overrideAttrs`, and `overrideDerivation`.
# This function introduces `overridePythonAttrs` and it overrides the call to `buildPythonPackage`.
2024-05-13 21:24:10 +00:00
makeOverridablePythonPackage = f:
lib.mirrorFunctionArgs f (origArgs:
let
args = lib.fix (lib.extends (_: previousAttrs: {
2024-05-02 00:46:19 +00:00
passthru = (previousAttrs.passthru or { }) // {
2024-05-13 21:24:10 +00:00
overridePythonAttrs = newArgs:
makeOverridablePythonPackage f (overrideWith newArgs);
2024-05-02 00:46:19 +00:00
};
2024-05-13 21:24:10 +00:00
}) (_: origArgs));
result = f args;
overrideWith = newArgs:
args
// (if pkgs.lib.isFunction newArgs then newArgs args else newArgs);
in if builtins.isAttrs result then
result
2024-05-02 00:46:19 +00:00
else if builtins.isFunction result then {
2024-05-13 21:24:10 +00:00
overridePythonAttrs = newArgs:
makeOverridablePythonPackage f (overrideWith newArgs);
2024-05-02 00:46:19 +00:00
__functor = self: result;
2024-05-13 21:24:10 +00:00
} else
result);
2024-05-02 00:46:19 +00:00
mkPythonDerivation = if python.isPy3k then
./mk-python-derivation.nix
else
./python2/mk-python-derivation.nix;
2024-05-13 21:24:10 +00:00
buildPythonPackage = makeOverridablePythonPackage (lib.makeOverridable
(callPackage mkPythonDerivation {
inherit
namePrefix; # We want Python libraries to be named like e.g. "python3.6-${name}"
inherit toPythonModule; # Libraries provide modules
}));
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
buildPythonApplication = makeOverridablePythonPackage (lib.makeOverridable
(callPackage mkPythonDerivation {
namePrefix = ""; # Python applications should not have any prefix
toPythonModule = x: x; # Application does not provide modules.
}));
2024-05-02 00:46:19 +00:00
# Check whether a derivation provides a Python module.
2024-05-13 21:24:10 +00:00
hasPythonModule = drv: drv ? pythonModule && drv.pythonModule == python;
2024-05-02 00:46:19 +00:00
# Get list of required Python modules given a list of derivations.
2024-05-13 21:24:10 +00:00
requiredPythonModules = drvs:
let modules = lib.filter hasPythonModule drvs;
in lib.unique ([ python ] ++ modules
++ lib.concatLists (lib.catAttrs "requiredPythonModules" modules));
2024-05-02 00:46:19 +00:00
# Create a PYTHONPATH from a list of derivations. This function recurses into the items to find derivations
# providing Python modules.
2024-05-13 21:24:10 +00:00
makePythonPath = drvs:
lib.makeSearchPath python.sitePackages (requiredPythonModules drvs);
2024-05-02 00:46:19 +00:00
removePythonPrefix = lib.removePrefix namePrefix;
# Convert derivation to a Python module.
toPythonModule = drv:
2024-05-13 21:24:10 +00:00
drv.overrideAttrs (oldAttrs: {
2024-05-02 00:46:19 +00:00
# Use passthru in order to prevent rebuilds when possible.
2024-05-13 21:24:10 +00:00
passthru = (oldAttrs.passthru or { }) // {
2024-05-02 00:46:19 +00:00
pythonModule = python;
pythonPath = [ ]; # Deprecated, for compatibility.
requiredPythonModules = requiredPythonModules drv.propagatedBuildInputs;
};
});
# Convert a Python library to an application.
toPythonApplication = drv:
2024-05-13 21:24:10 +00:00
drv.overrideAttrs (oldAttrs: {
passthru = (oldAttrs.passthru or { }) // {
2024-05-02 00:46:19 +00:00
# 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;
};
});
2024-05-13 21:24:10 +00:00
disabled = drv:
throw "${
removePythonPrefix (drv.pname or drv.name)
} not supported for interpreter ${python.executable}";
2024-05-02 00:46:19 +00:00
disabledIf = x: drv: if x then disabled drv else drv;
in {
inherit lib pkgs stdenv;
2024-05-13 21:24:10 +00:00
inherit (python.passthru)
isPy27 isPy37 isPy38 isPy39 isPy310 isPy311 isPy3k isPyPy pythonAtLeast
pythonOlder;
2024-05-02 00:46:19 +00:00
inherit buildPythonPackage buildPythonApplication;
2024-05-13 21:24:10 +00:00
inherit hasPythonModule requiredPythonModules makePythonPath disabled
disabledIf;
2024-05-02 00:46:19 +00:00
inherit toPythonModule toPythonApplication;
python = toPythonModule python;
# Dont take pythonPackages from "global" pkgs scope to avoid mixing python versions
pythonPackages = self;
}