2017-11-11 15:24:38 +00:00
|
|
|
# Generic builder.
|
2016-08-17 12:23:47 +00:00
|
|
|
|
|
|
|
{ lib
|
2018-04-25 17:15:48 +00:00
|
|
|
, config
|
2016-08-17 12:23:47 +00:00
|
|
|
, python
|
|
|
|
, wrapPython
|
|
|
|
, unzip
|
2018-02-13 14:32:16 +00:00
|
|
|
, ensureNewerSourcesForZipFilesHook
|
2017-05-28 07:20:47 +00:00
|
|
|
# Whether the derivation provides a Python module or not.
|
2017-12-10 13:20:38 +00:00
|
|
|
, toPythonModule
|
2017-05-28 07:20:47 +00:00
|
|
|
, namePrefix
|
2018-11-24 11:56:24 +00:00
|
|
|
, update-python-libraries
|
2019-07-17 18:36:47 +00:00
|
|
|
, setuptools
|
|
|
|
, flitBuildHook
|
|
|
|
, pipBuildHook
|
|
|
|
, pipInstallHook
|
|
|
|
, pythonCatchConflictsHook
|
|
|
|
, pythonImportsCheckHook
|
2020-03-17 21:29:47 +00:00
|
|
|
, pythonNamespacesHook
|
2022-09-20 07:49:55 +00:00
|
|
|
, pythonOutputDistHook
|
2019-07-17 18:36:47 +00:00
|
|
|
, pythonRemoveBinBytecodeHook
|
2020-03-02 17:37:05 +00:00
|
|
|
, pythonRemoveTestsDirHook
|
2019-07-17 18:36:47 +00:00
|
|
|
, setuptoolsBuildHook
|
|
|
|
, setuptoolsCheckHook
|
|
|
|
, wheelUnpackHook
|
2020-01-08 13:29:03 +00:00
|
|
|
, eggUnpackHook
|
|
|
|
, eggBuildHook
|
|
|
|
, eggInstallHook
|
2016-08-17 12:23:47 +00:00
|
|
|
}:
|
|
|
|
|
2017-11-03 04:14:22 +00:00
|
|
|
{ name ? "${attrs.pname}-${attrs.version}"
|
2016-08-17 12:23:47 +00:00
|
|
|
|
2017-12-21 20:49:06 +00:00
|
|
|
# Build-time dependencies for the package
|
|
|
|
, nativeBuildInputs ? []
|
|
|
|
|
|
|
|
# Run-time dependencies for the package
|
2016-08-17 12:23:47 +00:00
|
|
|
, buildInputs ? []
|
|
|
|
|
2016-08-31 09:01:16 +00:00
|
|
|
# Dependencies needed for running the checkPhase.
|
|
|
|
# These are added to buildInputs when doCheck = true.
|
|
|
|
, checkInputs ? []
|
2022-12-18 12:00:00 +00:00
|
|
|
, nativeCheckInputs ? []
|
2016-08-31 09:01:16 +00:00
|
|
|
|
2016-08-17 12:23:47 +00:00
|
|
|
# propagate build dependencies so in case we have A -> B -> C,
|
|
|
|
# C can import package A propagated by B
|
|
|
|
, propagatedBuildInputs ? []
|
|
|
|
|
|
|
|
# DEPRECATED: use propagatedBuildInputs
|
|
|
|
, pythonPath ? []
|
|
|
|
|
2019-04-24 19:10:25 +00:00
|
|
|
# Enabled to detect some (native)BuildInputs mistakes
|
|
|
|
, strictDeps ? true
|
|
|
|
|
2022-09-20 07:49:55 +00:00
|
|
|
, outputs ? [ "out" ]
|
|
|
|
|
2016-08-17 12:23:47 +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-11-19 19:57:22 +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)
|
2016-08-17 12:23:47 +00:00
|
|
|
|
|
|
|
# Additional arguments to pass to the makeWrapper function, which wraps
|
|
|
|
# generated binaries.
|
|
|
|
, makeWrapperArgs ? []
|
|
|
|
|
2017-05-25 19:56:25 +00:00
|
|
|
# Skip wrapping of python programs altogether
|
|
|
|
, dontWrapPythonPrograms ? false
|
|
|
|
|
2019-07-17 18:36:47 +00:00
|
|
|
# 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
|
|
|
|
|
2018-11-22 06:20:38 +00:00
|
|
|
# Skip setting the PYTHONNOUSERSITE environment variable in wrapped programs
|
2018-12-06 23:18:59 +00:00
|
|
|
, permitUserSite ? false
|
2018-11-22 06:20:38 +00:00
|
|
|
|
2017-12-10 14:41:05 +00:00
|
|
|
# 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
|
|
|
|
|
2019-07-17 18:36:47 +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.
|
|
|
|
# "flit" : Install a flit package. This builds a wheel.
|
2020-09-16 15:22:27 +00:00
|
|
|
# "pyproject": Install a package using a ``pyproject.toml`` file (PEP517). This builds a wheel.
|
|
|
|
# "egg": Install a package from an egg.
|
2019-07-17 18:36:47 +00:00
|
|
|
# "other" : Provide your own buildPhase and installPhase.
|
|
|
|
, format ? "setuptools"
|
|
|
|
|
2016-08-17 12:23:47 +00:00
|
|
|
, meta ? {}
|
|
|
|
|
|
|
|
, passthru ? {}
|
|
|
|
|
2018-04-25 17:15:48 +00:00
|
|
|
, doCheck ? config.doCheckByDefault or false
|
2016-09-01 15:10:38 +00:00
|
|
|
|
2021-02-19 14:58:39 +00:00
|
|
|
, disabledTestPaths ? []
|
|
|
|
|
2016-08-17 12:23:47 +00:00
|
|
|
, ... } @ attrs:
|
|
|
|
|
2019-07-17 18:36:47 +00:00
|
|
|
let
|
|
|
|
inherit (python) stdenv;
|
|
|
|
|
2022-10-03 15:23:26 +00:00
|
|
|
withDistOutput = lib.elem format ["pyproject" "setuptools" "flit" "wheel"];
|
2022-09-20 07:49:55 +00:00
|
|
|
|
2021-03-25 08:21:05 +00:00
|
|
|
name_ = name;
|
|
|
|
|
2023-04-15 23:02:48 +00:00
|
|
|
validatePythonMatches = attrName: let
|
|
|
|
isPythonModule = drv:
|
|
|
|
# all pythonModules have the pythonModule attribute
|
|
|
|
(drv ? "pythonModule")
|
|
|
|
# Some pythonModules are turned in to a pythonApplication by setting the field to false
|
|
|
|
&& (!builtins.isBool drv.pythonModule);
|
|
|
|
isMismatchedPython = drv: drv.pythonModule != python;
|
|
|
|
|
|
|
|
optionalLocation = let
|
|
|
|
pos = builtins.unsafeGetAttrPos (if attrs ? "pname" then "pname" else "name") attrs;
|
2023-06-24 18:19:19 +00:00
|
|
|
in lib.optionalString (pos != null) " at ${pos.file}:${toString pos.line}:${toString pos.column}";
|
2023-04-15 23:02:48 +00:00
|
|
|
|
|
|
|
leftPadName = name: against: let
|
|
|
|
len = lib.max (lib.stringLength name) (lib.stringLength against);
|
|
|
|
in lib.strings.fixedWidthString len " " name;
|
|
|
|
|
|
|
|
throwMismatch = drv: let
|
|
|
|
myName = "'${namePrefix}${name}'";
|
|
|
|
theirName = "'${drv.name}'";
|
|
|
|
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:
|
|
|
|
|
|
|
|
makeWrapperArgs = [ "--prefix PATH : ''${lib.makeBinPath [ ${lib.getName drv } ] }" ];
|
|
|
|
|
|
|
|
${optionalLocation}
|
|
|
|
'';
|
|
|
|
|
|
|
|
checkDrv = drv:
|
|
|
|
if (isPythonModule drv) && (isMismatchedPython drv)
|
|
|
|
then throwMismatch drv
|
|
|
|
else drv;
|
|
|
|
|
|
|
|
in inputs: builtins.map (checkDrv) inputs;
|
|
|
|
|
2023-02-02 14:13:53 +00:00
|
|
|
# Keep extra attributes from `attrs`, e.g., `patchPhase', etc.
|
2019-07-17 18:36:47 +00:00
|
|
|
self = toPythonModule (stdenv.mkDerivation ((builtins.removeAttrs attrs [
|
2022-12-18 12:00:00 +00:00
|
|
|
"disabled" "checkPhase" "checkInputs" "nativeCheckInputs" "doCheck" "doInstallCheck" "dontWrapPythonPrograms" "catchConflicts" "format"
|
2022-09-20 07:49:55 +00:00
|
|
|
"disabledTestPaths" "outputs"
|
2019-07-17 18:36:47 +00:00
|
|
|
]) // {
|
2016-08-17 12:23:47 +00:00
|
|
|
|
2021-03-25 08:21:05 +00:00
|
|
|
name = namePrefix + name_;
|
2020-06-27 14:26:03 +00:00
|
|
|
|
2019-01-05 09:31:16 +00:00
|
|
|
nativeBuildInputs = [
|
|
|
|
python
|
|
|
|
wrapPython
|
2019-07-17 18:36:47 +00:00
|
|
|
ensureNewerSourcesForZipFilesHook # move to wheel installer (pip) or builder (setuptools, flit, ...)?
|
2020-03-02 17:37:05 +00:00
|
|
|
pythonRemoveTestsDirHook
|
2019-07-17 18:36:47 +00:00
|
|
|
] ++ lib.optionals catchConflicts [
|
2022-09-20 07:49:27 +00:00
|
|
|
pythonCatchConflictsHook
|
2019-07-17 18:36:47 +00:00
|
|
|
] ++ lib.optionals removeBinBytecode [
|
|
|
|
pythonRemoveBinBytecodeHook
|
2019-01-05 09:31:16 +00:00
|
|
|
] ++ lib.optionals (lib.hasSuffix "zip" (attrs.src.name or "")) [
|
|
|
|
unzip
|
2019-07-17 18:36:47 +00:00
|
|
|
] ++ lib.optionals (format == "setuptools") [
|
|
|
|
setuptoolsBuildHook
|
|
|
|
] ++ lib.optionals (format == "flit") [
|
|
|
|
flitBuildHook
|
|
|
|
] ++ lib.optionals (format == "pyproject") [
|
|
|
|
pipBuildHook
|
|
|
|
] ++ lib.optionals (format == "wheel") [
|
|
|
|
wheelUnpackHook
|
2020-01-08 13:29:03 +00:00
|
|
|
] ++ lib.optionals (format == "egg") [
|
|
|
|
eggUnpackHook eggBuildHook eggInstallHook
|
2019-07-17 18:36:47 +00:00
|
|
|
] ++ lib.optionals (!(format == "other") || dontUsePipInstall) [
|
|
|
|
pipInstallHook
|
|
|
|
] ++ lib.optionals (stdenv.buildPlatform == stdenv.hostPlatform) [
|
|
|
|
# This is a test, however, it should be ran independent of the checkPhase and checkInputs
|
|
|
|
pythonImportsCheckHook
|
2020-03-17 21:29:47 +00:00
|
|
|
] ++ lib.optionals (python.pythonAtLeast "3.3") [
|
|
|
|
# Optionally enforce PEP420 for python3
|
|
|
|
pythonNamespacesHook
|
2022-09-20 07:49:55 +00:00
|
|
|
] ++ lib.optionals withDistOutput [
|
|
|
|
pythonOutputDistHook
|
2019-01-05 09:31:16 +00:00
|
|
|
] ++ nativeBuildInputs;
|
2020-06-27 14:26:03 +00:00
|
|
|
|
2023-04-15 23:02:48 +00:00
|
|
|
buildInputs = validatePythonMatches "buildInputs" (buildInputs ++ pythonPath);
|
2020-06-27 14:26:03 +00:00
|
|
|
|
2023-04-15 23:02:48 +00:00
|
|
|
propagatedBuildInputs = validatePythonMatches "propagatedBuildInputs" (propagatedBuildInputs ++ [
|
2022-06-27 09:44:20 +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-15 23:02:48 +00:00
|
|
|
]);
|
2020-06-27 14:26:03 +00:00
|
|
|
|
2019-04-24 19:10:25 +00:00
|
|
|
inherit strictDeps;
|
2020-06-27 14:26:03 +00:00
|
|
|
|
2019-02-15 12:30:59 +00:00
|
|
|
LANG = "${if python.stdenv.isDarwin then "en_US" else "C"}.UTF-8";
|
2020-06-27 14:26:03 +00:00
|
|
|
|
2016-08-17 12:23:47 +00:00
|
|
|
# Python packages don't have a checkPhase, only an installCheckPhase
|
|
|
|
doCheck = false;
|
2019-07-17 18:36:47 +00:00
|
|
|
doInstallCheck = attrs.doCheck or true;
|
2022-12-18 12:00:00 +00:00
|
|
|
nativeInstallCheckInputs = [
|
2019-07-17 18:36:47 +00:00
|
|
|
] ++ lib.optionals (format == "setuptools") [
|
|
|
|
# Longer-term we should get rid of this and require
|
|
|
|
# users of this function to set the `installCheckPhase` or
|
|
|
|
# pass in a hook that sets it.
|
|
|
|
setuptoolsCheckHook
|
2022-12-18 12:00:00 +00:00
|
|
|
] ++ nativeCheckInputs;
|
|
|
|
installCheckInputs = checkInputs;
|
2020-06-27 14:26:03 +00:00
|
|
|
|
2017-05-25 19:56:25 +00:00
|
|
|
postFixup = lib.optionalString (!dontWrapPythonPrograms) ''
|
2016-08-17 12:23:47 +00:00
|
|
|
wrapPythonPrograms
|
2021-01-24 09:19:10 +00:00
|
|
|
'' + attrs.postFixup or "";
|
2020-06-27 14:26:03 +00:00
|
|
|
|
2019-01-02 19:09:44 +00:00
|
|
|
# Python packages built through cross-compilation are always for the host platform.
|
|
|
|
disallowedReferences = lib.optionals (python.stdenv.hostPlatform != python.stdenv.buildPlatform) [ python.pythonForBuild ];
|
2020-06-27 14:26:03 +00:00
|
|
|
|
2022-09-20 07:49:55 +00:00
|
|
|
outputs = outputs ++ lib.optional withDistOutput "dist";
|
|
|
|
|
2017-11-11 12:37:21 +00:00
|
|
|
meta = {
|
2016-08-17 12:23:47 +00:00
|
|
|
# default to python's platforms
|
|
|
|
platforms = python.meta.platforms;
|
|
|
|
isBuildPythonPackage = python.meta.platforms;
|
2017-11-11 12:37:21 +00:00
|
|
|
} // meta;
|
2019-07-17 18:36:47 +00:00
|
|
|
} // lib.optionalAttrs (attrs?checkPhase) {
|
|
|
|
# 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;
|
2021-02-19 14:58:39 +00:00
|
|
|
} // lib.optionalAttrs (disabledTestPaths != []) {
|
|
|
|
disabledTestPaths = lib.escapeShellArgs disabledTestPaths;
|
2018-11-24 11:56:24 +00:00
|
|
|
}));
|
2020-06-27 14:26:03 +00:00
|
|
|
|
2018-12-23 08:59:56 +00:00
|
|
|
passthru.updateScript = let
|
2018-11-24 11:56:24 +00:00
|
|
|
filename = builtins.head (lib.splitString ":" self.meta.position);
|
2018-12-23 08:59:56 +00:00
|
|
|
in attrs.passthru.updateScript or [ update-python-libraries filename ];
|
2023-02-02 14:13:53 +00:00
|
|
|
in lib.extendDerivation
|
|
|
|
(disabled -> throw "${name} not supported for interpreter ${python.executable}")
|
|
|
|
passthru
|
|
|
|
self
|