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

145 lines
4.1 KiB
Nix
Raw Normal View History

2024-06-30 08:16:52 +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-06-30 08:16:52 +00:00
makeOverridablePythonPackage =
f:
lib.mirrorFunctionArgs f (
origArgs:
let
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);
in
if builtins.isAttrs result then
result
else if builtins.isFunction result then
{
overridePythonAttrs = newArgs: makeOverridablePythonPackage f (overrideWith newArgs);
__functor = self: result;
}
else
result
);
mkPythonDerivation =
if python.isPy3k then ./mk-python-derivation.nix else ./python2/mk-python-derivation.nix;
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-06-30 08:16:52 +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-06-30 08:16:52 +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-06-30 08:16:52 +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.
makePythonPath = drvs: lib.makeSearchPath python.sitePackages (requiredPythonModules drvs);
removePythonPrefix = lib.removePrefix namePrefix;
# Convert derivation to a Python module.
2024-06-30 08:16:52 +00:00
toPythonModule =
drv:
drv.overrideAttrs (oldAttrs: {
2024-05-02 00:46:19 +00:00
# Use passthru in order to prevent rebuilds when possible.
2024-06-30 08:16:52 +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.
2024-06-30 08:16:52 +00:00
toPythonApplication =
drv:
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-06-30 08:16:52 +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;
2024-06-30 08:16:52 +00:00
in
{
2024-05-02 00:46:19 +00:00
inherit lib pkgs stdenv;
2024-06-30 08:16:52 +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-06-30 08:16:52 +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;
}