2020-04-24 23:36:52 +00:00
|
|
|
# Generic builder.
|
|
|
|
|
|
|
|
{ lib
|
|
|
|
, config
|
|
|
|
, python
|
|
|
|
, wrapPython
|
|
|
|
, unzip
|
|
|
|
, ensureNewerSourcesForZipFilesHook
|
|
|
|
# Whether the derivation provides a Python module or not.
|
|
|
|
, toPythonModule
|
|
|
|
, namePrefix
|
|
|
|
, update-python-libraries
|
|
|
|
, setuptools
|
2023-10-09 19:29:22 +00:00
|
|
|
, pypaBuildHook
|
|
|
|
, pypaInstallHook
|
2020-04-24 23:36:52 +00:00
|
|
|
, pythonCatchConflictsHook
|
|
|
|
, pythonImportsCheckHook
|
|
|
|
, pythonNamespacesHook
|
2022-09-30 11:47:45 +00:00
|
|
|
, pythonOutputDistHook
|
2024-07-27 06:49:29 +00:00
|
|
|
, pythonRelaxDepsHook
|
2020-04-24 23:36:52 +00:00
|
|
|
, pythonRemoveBinBytecodeHook
|
|
|
|
, pythonRemoveTestsDirHook
|
2024-01-13 08:15:51 +00:00
|
|
|
, pythonRuntimeDepsCheckHook
|
2020-04-24 23:36:52 +00:00
|
|
|
, setuptoolsBuildHook
|
|
|
|
, wheelUnpackHook
|
|
|
|
, eggUnpackHook
|
|
|
|
, eggBuildHook
|
|
|
|
, eggInstallHook
|
|
|
|
}:
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
let
|
|
|
|
inherit (builtins) unsafeGetAttrPos;
|
|
|
|
inherit (lib)
|
|
|
|
elem optionalString max stringLength fixedWidthString getName
|
|
|
|
optional optionals optionalAttrs hasSuffix escapeShellArgs
|
|
|
|
extendDerivation head splitString isBool;
|
|
|
|
|
|
|
|
leftPadName = name: against: let
|
|
|
|
len = max (stringLength name) (stringLength against);
|
|
|
|
in fixedWidthString len " " name;
|
|
|
|
|
|
|
|
isPythonModule = drv:
|
|
|
|
# all pythonModules have the pythonModule attribute
|
|
|
|
(drv ? "pythonModule")
|
|
|
|
# Some pythonModules are turned in to a pythonApplication by setting the field to false
|
|
|
|
&& (!isBool drv.pythonModule);
|
|
|
|
|
|
|
|
isMismatchedPython = drv: drv.pythonModule != python;
|
|
|
|
|
|
|
|
withDistOutput' = lib.flip elem ["pyproject" "setuptools" "wheel"];
|
|
|
|
|
|
|
|
isBootstrapInstallPackage' = lib.flip elem [ "flit-core" "installer" ];
|
|
|
|
|
|
|
|
isBootstrapPackage' = lib.flip elem ([
|
|
|
|
"build" "packaging" "pyproject-hooks" "wheel"
|
|
|
|
] ++ optionals (python.pythonOlder "3.11") [
|
|
|
|
"tomli"
|
|
|
|
]);
|
|
|
|
|
|
|
|
isSetuptoolsDependency' = lib.flip elem [ "setuptools" "wheel" ];
|
|
|
|
|
|
|
|
cleanAttrs = lib.flip removeAttrs [
|
|
|
|
"disabled" "checkPhase" "checkInputs" "nativeCheckInputs" "doCheck" "doInstallCheck" "dontWrapPythonPrograms" "catchConflicts" "pyproject" "format"
|
|
|
|
"disabledTestPaths" "outputs" "stdenv"
|
|
|
|
"dependencies" "optional-dependencies" "build-system"
|
|
|
|
];
|
|
|
|
|
|
|
|
in
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
{ name ? "${attrs.pname}-${attrs.version}"
|
|
|
|
|
|
|
|
# Build-time dependencies for the package
|
|
|
|
, nativeBuildInputs ? []
|
|
|
|
|
|
|
|
# Run-time dependencies for the package
|
|
|
|
, buildInputs ? []
|
|
|
|
|
|
|
|
# Dependencies needed for running the checkPhase.
|
|
|
|
# These are added to buildInputs when doCheck = true.
|
|
|
|
, checkInputs ? []
|
2023-02-02 18:25:31 +00:00
|
|
|
, nativeCheckInputs ? []
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# propagate build dependencies so in case we have A -> B -> C,
|
|
|
|
# C can import package A propagated by B
|
|
|
|
, propagatedBuildInputs ? []
|
|
|
|
|
2024-02-29 20:09:43 +00:00
|
|
|
# Python module dependencies.
|
|
|
|
# These are named after PEP-621.
|
|
|
|
, dependencies ? []
|
|
|
|
, optional-dependencies ? {}
|
|
|
|
|
|
|
|
# Python PEP-517 build systems.
|
|
|
|
, build-system ? []
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
# DEPRECATED: use propagatedBuildInputs
|
|
|
|
, pythonPath ? []
|
|
|
|
|
|
|
|
# Enabled to detect some (native)BuildInputs mistakes
|
|
|
|
, strictDeps ? true
|
|
|
|
|
2022-09-30 11:47:45 +00:00
|
|
|
, outputs ? [ "out" ]
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
# used to disable derivation, useful for specific python versions
|
|
|
|
, disabled ? false
|
|
|
|
|
|
|
|
# Raise an error if two packages are installed with the same name
|
2020-12-03 08:41:04 +00:00
|
|
|
# TODO: For cross we probably need a different PYTHONPATH, or not
|
|
|
|
# add the runtime deps until after buildPhase.
|
|
|
|
, catchConflicts ? (python.stdenv.hostPlatform == python.stdenv.buildPlatform)
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Additional arguments to pass to the makeWrapper function, which wraps
|
|
|
|
# generated binaries.
|
|
|
|
, makeWrapperArgs ? []
|
|
|
|
|
|
|
|
# Skip wrapping of python programs altogether
|
|
|
|
, dontWrapPythonPrograms ? false
|
|
|
|
|
|
|
|
# Don't use Pip to install a wheel
|
|
|
|
# Note this is actually a variable for the pipInstallPhase in pip's setupHook.
|
|
|
|
# It's included here to prevent an infinite recursion.
|
|
|
|
, dontUsePipInstall ? false
|
|
|
|
|
|
|
|
# Skip setting the PYTHONNOUSERSITE environment variable in wrapped programs
|
|
|
|
, permitUserSite ? false
|
|
|
|
|
|
|
|
# Remove bytecode from bin folder.
|
|
|
|
# When a Python script has the extension `.py`, bytecode is generated
|
|
|
|
# Typically, executables in bin have no extension, so no bytecode is generated.
|
|
|
|
# However, some packages do provide executables with extensions, and thus bytecode is generated.
|
|
|
|
, removeBinBytecode ? true
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
# pyproject = true <-> format = "pyproject"
|
|
|
|
# pyproject = false <-> format = "other"
|
|
|
|
# https://github.com/NixOS/nixpkgs/issues/253154
|
|
|
|
, pyproject ? null
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
# Several package formats are supported.
|
|
|
|
# "setuptools" : Install a common setuptools/distutils based package. This builds a wheel.
|
|
|
|
# "wheel" : Install from a pre-compiled wheel.
|
2020-09-25 04:45:31 +00:00
|
|
|
# "pyproject": Install a package using a ``pyproject.toml`` file (PEP517). This builds a wheel.
|
|
|
|
# "egg": Install a package from an egg.
|
2020-04-24 23:36:52 +00:00
|
|
|
# "other" : Provide your own buildPhase and installPhase.
|
2023-10-09 19:29:22 +00:00
|
|
|
, format ? null
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
, meta ? {}
|
|
|
|
|
2024-07-27 06:49:29 +00:00
|
|
|
, doCheck ? true
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-05-28 09:39:13 +00:00
|
|
|
, disabledTestPaths ? []
|
|
|
|
|
2023-11-16 04:20:00 +00:00
|
|
|
# Allow passing in a custom stdenv to buildPython*
|
|
|
|
, stdenv ? python.stdenv
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
, ... } @ attrs:
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
assert (pyproject != null) -> (format == null);
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
let
|
2023-10-09 19:29:22 +00:00
|
|
|
format' =
|
|
|
|
if pyproject != null then
|
|
|
|
if pyproject then
|
|
|
|
"pyproject"
|
|
|
|
else
|
|
|
|
"other"
|
|
|
|
else if format != null then
|
|
|
|
format
|
|
|
|
else
|
|
|
|
"setuptools";
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
withDistOutput = withDistOutput' format';
|
2021-04-26 19:14:03 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
validatePythonMatches = let
|
|
|
|
throwMismatch = attrName: drv: let
|
2023-04-29 16:46:19 +00:00
|
|
|
myName = "'${namePrefix}${name}'";
|
|
|
|
theirName = "'${drv.name}'";
|
2024-04-21 15:54:59 +00:00
|
|
|
optionalLocation = let
|
|
|
|
pos = unsafeGetAttrPos (if attrs ? "pname" then "pname" else "name") attrs;
|
|
|
|
in optionalString (pos != null) " at ${pos.file}:${toString pos.line}:${toString pos.column}";
|
2023-04-29 16:46:19 +00:00
|
|
|
in throw ''
|
|
|
|
Python version mismatch in ${myName}:
|
|
|
|
|
|
|
|
The Python derivation ${myName} depends on a Python derivation
|
|
|
|
named ${theirName}, but the two derivations use different versions
|
|
|
|
of Python:
|
|
|
|
|
|
|
|
${leftPadName myName theirName} uses ${python}
|
|
|
|
${leftPadName theirName myName} uses ${toString drv.pythonModule}
|
|
|
|
|
|
|
|
Possible solutions:
|
|
|
|
|
|
|
|
* If ${theirName} is a Python library, change the reference to ${theirName}
|
|
|
|
in the ${attrName} of ${myName} to use a ${theirName} built from the same
|
|
|
|
version of Python
|
|
|
|
|
|
|
|
* If ${theirName} is used as a tool during the build, move the reference to
|
|
|
|
${theirName} in ${myName} from ${attrName} to nativeBuildInputs
|
|
|
|
|
|
|
|
* If ${theirName} provides executables that are called at run time, pass its
|
|
|
|
bin path to makeWrapperArgs:
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
makeWrapperArgs = [ "--prefix PATH : ''${lib.makeBinPath [ ${getName drv } ] }" ];
|
2023-04-29 16:46:19 +00:00
|
|
|
|
|
|
|
${optionalLocation}
|
|
|
|
'';
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
checkDrv = attrName: drv:
|
|
|
|
if (isPythonModule drv) && (isMismatchedPython drv) then throwMismatch attrName drv
|
2023-04-29 16:46:19 +00:00
|
|
|
else drv;
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
in attrName: inputs: map (checkDrv attrName) inputs;
|
2023-04-29 16:46:19 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
isBootstrapInstallPackage = isBootstrapInstallPackage' (attrs.pname or null);
|
2023-10-09 19:29:22 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
isBootstrapPackage = isBootstrapInstallPackage || isBootstrapPackage' (attrs.pname or null);
|
2023-10-09 19:29:22 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
isSetuptoolsDependency = isSetuptoolsDependency' (attrs.pname or null);
|
2023-10-09 19:29:22 +00:00
|
|
|
|
2024-02-29 20:09:43 +00:00
|
|
|
passthru =
|
|
|
|
attrs.passthru or { }
|
|
|
|
// {
|
|
|
|
updateScript = let
|
2024-04-21 15:54:59 +00:00
|
|
|
filename = head (splitString ":" self.meta.position);
|
2024-02-29 20:09:43 +00:00
|
|
|
in attrs.passthru.updateScript or [ update-python-libraries filename ];
|
|
|
|
}
|
2024-04-21 15:54:59 +00:00
|
|
|
// optionalAttrs (dependencies != []) {
|
2024-02-29 20:09:43 +00:00
|
|
|
inherit dependencies;
|
|
|
|
}
|
2024-04-21 15:54:59 +00:00
|
|
|
// optionalAttrs (optional-dependencies != {}) {
|
2024-02-29 20:09:43 +00:00
|
|
|
inherit optional-dependencies;
|
|
|
|
}
|
2024-04-21 15:54:59 +00:00
|
|
|
// optionalAttrs (build-system != []) {
|
2024-02-29 20:09:43 +00:00
|
|
|
inherit build-system;
|
|
|
|
};
|
|
|
|
|
2023-02-02 18:25:31 +00:00
|
|
|
# Keep extra attributes from `attrs`, e.g., `patchPhase', etc.
|
2024-04-21 15:54:59 +00:00
|
|
|
self = toPythonModule (stdenv.mkDerivation ((cleanAttrs attrs) // {
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
name = namePrefix + name;
|
2020-09-25 04:45:31 +00:00
|
|
|
|
|
|
|
nativeBuildInputs = [
|
|
|
|
python
|
|
|
|
wrapPython
|
|
|
|
ensureNewerSourcesForZipFilesHook # move to wheel installer (pip) or builder (setuptools, flit, ...)?
|
|
|
|
pythonRemoveTestsDirHook
|
2024-04-21 15:54:59 +00:00
|
|
|
] ++ optionals (catchConflicts && !isBootstrapPackage && !isSetuptoolsDependency) [
|
2023-10-09 19:29:22 +00:00
|
|
|
#
|
|
|
|
# 1. When building a package that is also part of the bootstrap chain, we
|
|
|
|
# must ignore conflicts after installation, because there will be one with
|
|
|
|
# the package in the bootstrap.
|
|
|
|
#
|
|
|
|
# 2. When a package is a dependency of setuptools, we must ignore conflicts
|
|
|
|
# because the hook that checks for conflicts uses setuptools.
|
|
|
|
#
|
2022-09-30 11:47:45 +00:00
|
|
|
pythonCatchConflictsHook
|
2024-07-27 06:49:29 +00:00
|
|
|
] ++ optionals (attrs ? pythonRelaxDeps || attrs ? pythonRemoveDeps) [
|
|
|
|
pythonRelaxDepsHook
|
2024-04-21 15:54:59 +00:00
|
|
|
] ++ optionals removeBinBytecode [
|
2020-09-25 04:45:31 +00:00
|
|
|
pythonRemoveBinBytecodeHook
|
2024-04-21 15:54:59 +00:00
|
|
|
] ++ optionals (hasSuffix "zip" (attrs.src.name or "")) [
|
2020-09-25 04:45:31 +00:00
|
|
|
unzip
|
2024-04-21 15:54:59 +00:00
|
|
|
] ++ optionals (format' == "setuptools") [
|
2020-09-25 04:45:31 +00:00
|
|
|
setuptoolsBuildHook
|
2024-04-21 15:54:59 +00:00
|
|
|
] ++ optionals (format' == "pyproject") [(
|
2023-10-09 19:29:22 +00:00
|
|
|
if isBootstrapPackage then
|
|
|
|
pypaBuildHook.override {
|
2023-11-16 04:20:00 +00:00
|
|
|
inherit (python.pythonOnBuildForHost.pkgs.bootstrap) build;
|
2023-10-09 19:29:22 +00:00
|
|
|
wheel = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pypaBuildHook
|
2024-01-13 08:15:51 +00:00
|
|
|
) (
|
|
|
|
if isBootstrapPackage then
|
|
|
|
pythonRuntimeDepsCheckHook.override {
|
|
|
|
inherit (python.pythonOnBuildForHost.pkgs.bootstrap) packaging;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pythonRuntimeDepsCheckHook
|
2024-04-21 15:54:59 +00:00
|
|
|
)] ++ optionals (format' == "wheel") [
|
2020-09-25 04:45:31 +00:00
|
|
|
wheelUnpackHook
|
2024-04-21 15:54:59 +00:00
|
|
|
] ++ optionals (format' == "egg") [
|
2020-09-25 04:45:31 +00:00
|
|
|
eggUnpackHook eggBuildHook eggInstallHook
|
2024-04-21 15:54:59 +00:00
|
|
|
] ++ optionals (format' != "other") [(
|
2023-10-09 19:29:22 +00:00
|
|
|
if isBootstrapInstallPackage then
|
|
|
|
pypaInstallHook.override {
|
2023-11-16 04:20:00 +00:00
|
|
|
inherit (python.pythonOnBuildForHost.pkgs.bootstrap) installer;
|
2023-10-09 19:29:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
pypaInstallHook
|
2024-04-21 15:54:59 +00:00
|
|
|
)] ++ optionals (stdenv.buildPlatform == stdenv.hostPlatform) [
|
2020-09-25 04:45:31 +00:00
|
|
|
# This is a test, however, it should be ran independent of the checkPhase and checkInputs
|
|
|
|
pythonImportsCheckHook
|
2024-04-21 15:54:59 +00:00
|
|
|
] ++ optionals (python.pythonAtLeast "3.3") [
|
2020-09-25 04:45:31 +00:00
|
|
|
# Optionally enforce PEP420 for python3
|
|
|
|
pythonNamespacesHook
|
2024-04-21 15:54:59 +00:00
|
|
|
] ++ optionals withDistOutput [
|
2022-09-30 11:47:45 +00:00
|
|
|
pythonOutputDistHook
|
2024-02-29 20:09:43 +00:00
|
|
|
] ++ nativeBuildInputs ++ build-system;
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2023-04-29 16:46:19 +00:00
|
|
|
buildInputs = validatePythonMatches "buildInputs" (buildInputs ++ pythonPath);
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2024-02-29 20:09:43 +00:00
|
|
|
propagatedBuildInputs = validatePythonMatches "propagatedBuildInputs" (propagatedBuildInputs ++ dependencies ++ [
|
2022-07-14 12:49:19 +00:00
|
|
|
# we propagate python even for packages transformed with 'toPythonApplication'
|
|
|
|
# this pollutes the PATH but avoids rebuilds
|
|
|
|
# see https://github.com/NixOS/nixpkgs/issues/170887 for more context
|
|
|
|
python
|
2023-04-29 16:46:19 +00:00
|
|
|
]);
|
2020-09-25 04:45:31 +00:00
|
|
|
|
|
|
|
inherit strictDeps;
|
|
|
|
|
|
|
|
LANG = "${if python.stdenv.isDarwin then "en_US" else "C"}.UTF-8";
|
|
|
|
|
|
|
|
# Python packages don't have a checkPhase, only an installCheckPhase
|
|
|
|
doCheck = false;
|
|
|
|
doInstallCheck = attrs.doCheck or true;
|
2024-09-19 14:19:46 +00:00
|
|
|
nativeInstallCheckInputs = nativeCheckInputs;
|
2023-02-02 18:25:31 +00:00
|
|
|
installCheckInputs = checkInputs;
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
postFixup = optionalString (!dontWrapPythonPrograms) ''
|
2020-09-25 04:45:31 +00:00
|
|
|
wrapPythonPrograms
|
2021-02-05 17:12:51 +00:00
|
|
|
'' + attrs.postFixup or "";
|
2020-09-25 04:45:31 +00:00
|
|
|
|
|
|
|
# Python packages built through cross-compilation are always for the host platform.
|
2024-04-21 15:54:59 +00:00
|
|
|
disallowedReferences = optionals (python.stdenv.hostPlatform != python.stdenv.buildPlatform) [ python.pythonOnBuildForHost ];
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
outputs = outputs ++ optional withDistOutput "dist";
|
2022-09-30 11:47:45 +00:00
|
|
|
|
2024-02-29 20:09:43 +00:00
|
|
|
inherit passthru;
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
meta = {
|
|
|
|
# default to python's platforms
|
|
|
|
platforms = python.meta.platforms;
|
|
|
|
isBuildPythonPackage = python.meta.platforms;
|
|
|
|
} // meta;
|
2024-04-21 15:54:59 +00:00
|
|
|
} // optionalAttrs (attrs?checkPhase) {
|
2020-09-25 04:45:31 +00:00
|
|
|
# If given use the specified checkPhase, otherwise use the setup hook.
|
|
|
|
# Longer-term we should get rid of `checkPhase` and use `installCheckPhase`.
|
|
|
|
installCheckPhase = attrs.checkPhase;
|
2024-04-21 15:54:59 +00:00
|
|
|
} // optionalAttrs (disabledTestPaths != []) {
|
|
|
|
disabledTestPaths = escapeShellArgs disabledTestPaths;
|
2020-09-25 04:45:31 +00:00
|
|
|
}));
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
in extendDerivation
|
2023-02-02 18:25:31 +00:00
|
|
|
(disabled -> throw "${name} not supported for interpreter ${python.executable}")
|
|
|
|
passthru
|
|
|
|
self
|