2024-05-02 00:46:19 +00:00
|
|
|
# Generic builder.
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
{ lib, config, python, wrapPython, unzip, ensureNewerSourcesForZipFilesHook
|
2024-05-02 00:46:19 +00:00
|
|
|
# Whether the derivation provides a Python module or not.
|
2024-05-13 21:24:10 +00:00
|
|
|
, toPythonModule, namePrefix, update-python-libraries, setuptools, pypaBuildHook
|
|
|
|
, pypaInstallHook, pythonCatchConflictsHook, pythonImportsCheckHook
|
|
|
|
, pythonNamespacesHook, pythonOutputDistHook, pythonRemoveBinBytecodeHook
|
|
|
|
, pythonRemoveTestsDirHook, pythonRuntimeDepsCheckHook, setuptoolsBuildHook
|
|
|
|
, setuptoolsCheckHook, wheelUnpackHook, eggUnpackHook, eggBuildHook
|
|
|
|
, eggInstallHook }:
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
inherit (builtins) unsafeGetAttrPos;
|
|
|
|
inherit (lib)
|
2024-05-13 21:24:10 +00:00
|
|
|
elem optionalString max stringLength fixedWidthString getName optional
|
|
|
|
optionals optionalAttrs hasSuffix escapeShellArgs extendDerivation head
|
|
|
|
splitString isBool;
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
leftPadName = name: against:
|
|
|
|
let len = max (stringLength name) (stringLength against);
|
2024-05-02 00:46:19 +00:00
|
|
|
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;
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
withDistOutput' = lib.flip elem [ "pyproject" "setuptools" "wheel" ];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
isBootstrapInstallPackage' = lib.flip elem [ "flit-core" "installer" ];
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
isBootstrapPackage' = lib.flip elem
|
|
|
|
([ "build" "packaging" "pyproject-hooks" "wheel" ]
|
|
|
|
++ optionals (python.pythonOlder "3.11") [ "tomli" ]);
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
isSetuptoolsDependency' = lib.flip elem [ "setuptools" "wheel" ];
|
|
|
|
|
|
|
|
cleanAttrs = lib.flip removeAttrs [
|
2024-05-13 21:24:10 +00:00
|
|
|
"disabled"
|
|
|
|
"checkPhase"
|
|
|
|
"checkInputs"
|
|
|
|
"nativeCheckInputs"
|
|
|
|
"doCheck"
|
|
|
|
"doInstallCheck"
|
|
|
|
"dontWrapPythonPrograms"
|
|
|
|
"catchConflicts"
|
|
|
|
"pyproject"
|
|
|
|
"format"
|
|
|
|
"disabledTestPaths"
|
|
|
|
"outputs"
|
|
|
|
"stdenv"
|
|
|
|
"dependencies"
|
|
|
|
"optional-dependencies"
|
|
|
|
"build-system"
|
2024-05-02 00:46:19 +00:00
|
|
|
];
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
in { name ? "${attrs.pname}-${attrs.version}"
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
# Build-time dependencies for the package
|
|
|
|
, nativeBuildInputs ? [ ]
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
# Run-time dependencies for the package
|
|
|
|
, buildInputs ? [ ]
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
# Dependencies needed for running the checkPhase.
|
|
|
|
# These are added to buildInputs when doCheck = true.
|
|
|
|
, checkInputs ? [ ], nativeCheckInputs ? [ ]
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
# propagate build dependencies so in case we have A -> B -> C,
|
|
|
|
# C can import package A propagated by B
|
|
|
|
, propagatedBuildInputs ? [ ]
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
# Python module dependencies.
|
|
|
|
# These are named after PEP-621.
|
|
|
|
, dependencies ? [ ], optional-dependencies ? { }
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
# Python PEP-517 build systems.
|
|
|
|
, build-system ? [ ]
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
# DEPRECATED: use propagatedBuildInputs
|
|
|
|
, pythonPath ? [ ]
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
# Enabled to detect some (native)BuildInputs mistakes
|
2024-05-02 00:46:19 +00:00
|
|
|
, strictDeps ? true
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
, outputs ? [
|
|
|
|
"out"
|
|
|
|
]
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
# used to disable derivation, useful for specific python versions
|
|
|
|
, disabled ? false
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
# Raise an error if two packages are installed with the same name
|
|
|
|
# TODO: For cross we probably need a different PYTHONPATH, or not
|
|
|
|
# add the runtime deps until after buildPhase.
|
2024-05-02 00:46:19 +00:00
|
|
|
, catchConflicts ? (python.stdenv.hostPlatform == python.stdenv.buildPlatform)
|
|
|
|
|
|
|
|
# Additional arguments to pass to the makeWrapper function, which wraps
|
|
|
|
# generated binaries.
|
2024-05-13 21:24:10 +00:00
|
|
|
, makeWrapperArgs ? [ ]
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
# Skip wrapping of python programs altogether
|
2024-05-02 00:46:19 +00:00
|
|
|
, dontWrapPythonPrograms ? false
|
|
|
|
|
2024-05-13 21:24:10 +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.
|
2024-05-02 00:46:19 +00:00
|
|
|
, dontUsePipInstall ? false
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
# Skip setting the PYTHONNOUSERSITE environment variable in wrapped programs
|
2024-05-02 00:46:19 +00:00
|
|
|
, permitUserSite ? false
|
|
|
|
|
2024-05-13 21:24:10 +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.
|
2024-05-02 00:46:19 +00:00
|
|
|
, removeBinBytecode ? true
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
# pyproject = true <-> format = "pyproject"
|
|
|
|
# pyproject = false <-> format = "other"
|
|
|
|
# https://github.com/NixOS/nixpkgs/issues/253154
|
2024-05-02 00:46:19 +00:00
|
|
|
, pyproject ? null
|
|
|
|
|
2024-05-13 21:24:10 +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.
|
|
|
|
# "pyproject": Install a package using a ``pyproject.toml`` file (PEP517). This builds a wheel.
|
|
|
|
# "egg": Install a package from an egg.
|
|
|
|
# "other" : Provide your own buildPhase and installPhase.
|
2024-05-02 00:46:19 +00:00
|
|
|
, format ? null
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
, meta ? { }
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
, doCheck ? config.doCheckByDefault or false
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
, disabledTestPaths ? [ ]
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
# Allow passing in a custom stdenv to buildPython*
|
2024-05-02 00:46:19 +00:00
|
|
|
, stdenv ? python.stdenv
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
, ... }@attrs:
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
assert (pyproject != null) -> (format == null);
|
|
|
|
|
|
|
|
let
|
2024-05-13 21:24:10 +00:00
|
|
|
format' = if pyproject != null then
|
|
|
|
if pyproject then "pyproject" else "other"
|
|
|
|
else if format != null then
|
|
|
|
format
|
|
|
|
else
|
|
|
|
"setuptools";
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
withDistOutput = withDistOutput' format';
|
|
|
|
|
|
|
|
validatePythonMatches = let
|
2024-05-13 21:24:10 +00:00
|
|
|
throwMismatch = attrName: drv:
|
|
|
|
let
|
|
|
|
myName = "'${namePrefix}${name}'";
|
|
|
|
theirName = "'${drv.name}'";
|
|
|
|
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}";
|
|
|
|
in throw ''
|
|
|
|
Python version mismatch in ${myName}:
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
The Python derivation ${myName} depends on a Python derivation
|
|
|
|
named ${theirName}, but the two derivations use different versions
|
|
|
|
of Python:
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
${leftPadName myName theirName} uses ${python}
|
|
|
|
${leftPadName theirName myName} uses ${toString drv.pythonModule}
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
Possible solutions:
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
* 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
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
* If ${theirName} is used as a tool during the build, move the reference to
|
|
|
|
${theirName} in ${myName} from ${attrName} to nativeBuildInputs
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
* If ${theirName} provides executables that are called at run time, pass its
|
|
|
|
bin path to makeWrapperArgs:
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
makeWrapperArgs = [ "--prefix PATH : ''${lib.makeBinPath [ ${
|
|
|
|
getName drv
|
|
|
|
} ] }" ];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
${optionalLocation}
|
|
|
|
'';
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
checkDrv = attrName: drv:
|
2024-05-13 21:24:10 +00:00
|
|
|
if (isPythonModule drv) && (isMismatchedPython drv) then
|
|
|
|
throwMismatch attrName drv
|
|
|
|
else
|
|
|
|
drv;
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
in attrName: inputs: map (checkDrv attrName) inputs;
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
isBootstrapInstallPackage = isBootstrapInstallPackage' (attrs.pname or null);
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
isBootstrapPackage = isBootstrapInstallPackage
|
|
|
|
|| isBootstrapPackage' (attrs.pname or null);
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
isSetuptoolsDependency = isSetuptoolsDependency' (attrs.pname or null);
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
passthru = attrs.passthru or { } // {
|
|
|
|
updateScript = let filename = head (splitString ":" self.meta.position);
|
|
|
|
in attrs.passthru.updateScript or [ update-python-libraries filename ];
|
|
|
|
} // optionalAttrs (dependencies != [ ]) { inherit dependencies; }
|
|
|
|
// optionalAttrs (optional-dependencies != { }) {
|
2024-05-02 00:46:19 +00:00
|
|
|
inherit optional-dependencies;
|
2024-05-13 21:24:10 +00:00
|
|
|
} // optionalAttrs (build-system != [ ]) { inherit build-system; };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
# Keep extra attributes from `attrs`, e.g., `patchPhase', etc.
|
|
|
|
self = toPythonModule (stdenv.mkDerivation ((cleanAttrs attrs) // {
|
|
|
|
|
|
|
|
name = namePrefix + name;
|
|
|
|
|
|
|
|
nativeBuildInputs = [
|
|
|
|
python
|
|
|
|
wrapPython
|
2024-05-13 21:24:10 +00:00
|
|
|
ensureNewerSourcesForZipFilesHook # move to wheel installer (pip) or builder (setuptools, flit, ...)?
|
2024-05-02 00:46:19 +00:00
|
|
|
pythonRemoveTestsDirHook
|
2024-05-13 21:24:10 +00:00
|
|
|
] ++ optionals
|
|
|
|
(catchConflicts && !isBootstrapPackage && !isSetuptoolsDependency) [
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
pythonCatchConflictsHook
|
|
|
|
] ++ optionals removeBinBytecode [ pythonRemoveBinBytecodeHook ]
|
|
|
|
++ optionals (hasSuffix "zip" (attrs.src.name or "")) [ unzip ]
|
|
|
|
++ optionals (format' == "setuptools") [ setuptoolsBuildHook ]
|
|
|
|
++ optionals (format' == "pyproject") [
|
|
|
|
(if isBootstrapPackage then
|
|
|
|
pypaBuildHook.override {
|
|
|
|
inherit (python.pythonOnBuildForHost.pkgs.bootstrap) build;
|
|
|
|
wheel = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pypaBuildHook)
|
|
|
|
(if isBootstrapPackage then
|
|
|
|
pythonRuntimeDepsCheckHook.override {
|
|
|
|
inherit (python.pythonOnBuildForHost.pkgs.bootstrap) packaging;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pythonRuntimeDepsCheckHook)
|
|
|
|
] ++ optionals (format' == "wheel") [ wheelUnpackHook ]
|
|
|
|
++ optionals (format' == "egg") [
|
|
|
|
eggUnpackHook
|
|
|
|
eggBuildHook
|
|
|
|
eggInstallHook
|
|
|
|
] ++ optionals (format' != "other") [
|
|
|
|
(if isBootstrapInstallPackage then
|
|
|
|
pypaInstallHook.override {
|
|
|
|
inherit (python.pythonOnBuildForHost.pkgs.bootstrap) installer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pypaInstallHook)
|
|
|
|
] ++ optionals (stdenv.buildPlatform == stdenv.hostPlatform) [
|
|
|
|
# This is a test, however, it should be ran independent of the checkPhase and checkInputs
|
|
|
|
pythonImportsCheckHook
|
|
|
|
] ++ optionals (python.pythonAtLeast "3.3") [
|
|
|
|
# Optionally enforce PEP420 for python3
|
|
|
|
pythonNamespacesHook
|
|
|
|
] ++ optionals withDistOutput [ pythonOutputDistHook ]
|
|
|
|
++ nativeBuildInputs ++ build-system;
|
|
|
|
|
|
|
|
buildInputs =
|
|
|
|
validatePythonMatches "buildInputs" (buildInputs ++ pythonPath);
|
|
|
|
|
|
|
|
propagatedBuildInputs = validatePythonMatches "propagatedBuildInputs"
|
|
|
|
(propagatedBuildInputs ++ dependencies ++ [
|
|
|
|
# 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
|
|
|
|
]);
|
2024-05-02 00:46:19 +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-05-13 21:24:10 +00:00
|
|
|
nativeInstallCheckInputs = [ ] ++ optionals (format' == "setuptools") [
|
2024-05-02 00:46:19 +00:00
|
|
|
# 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
|
|
|
|
] ++ nativeCheckInputs;
|
|
|
|
installCheckInputs = checkInputs;
|
|
|
|
|
|
|
|
postFixup = optionalString (!dontWrapPythonPrograms) ''
|
|
|
|
wrapPythonPrograms
|
|
|
|
'' + attrs.postFixup or "";
|
|
|
|
|
|
|
|
# Python packages built through cross-compilation are always for the host platform.
|
2024-05-13 21:24:10 +00:00
|
|
|
disallowedReferences =
|
|
|
|
optionals (python.stdenv.hostPlatform != python.stdenv.buildPlatform)
|
|
|
|
[ python.pythonOnBuildForHost ];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
outputs = outputs ++ optional withDistOutput "dist";
|
|
|
|
|
|
|
|
inherit passthru;
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
# default to python's platforms
|
|
|
|
platforms = python.meta.platforms;
|
|
|
|
isBuildPythonPackage = python.meta.platforms;
|
|
|
|
} // meta;
|
2024-05-13 21:24:10 +00:00
|
|
|
} // optionalAttrs (attrs ? checkPhase) {
|
2024-05-02 00:46:19 +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-05-13 21:24:10 +00:00
|
|
|
} // optionalAttrs (disabledTestPaths != [ ]) {
|
|
|
|
disabledTestPaths = escapeShellArgs disabledTestPaths;
|
2024-05-02 00:46:19 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
in extendDerivation
|
2024-05-13 21:24:10 +00:00
|
|
|
(disabled -> throw "${name} not supported for interpreter ${python.executable}")
|
|
|
|
passthru self
|