Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1# Generic builder.
2
3{ lib
4, config
5, python
6, wrapPython
7, setuptools
8, unzip
9, ensureNewerSourcesForZipFilesHook
10# Whether the derivation provides a Python module or not.
11, toPythonModule
12, namePrefix
13, writeScript
14, update-python-libraries
15}:
16
17{ name ? "${attrs.pname}-${attrs.version}"
18
19# Build-time dependencies for the package
20, nativeBuildInputs ? []
21
22# Run-time dependencies for the package
23, buildInputs ? []
24
25# Dependencies needed for running the checkPhase.
26# These are added to buildInputs when doCheck = true.
27, checkInputs ? []
28
29# propagate build dependencies so in case we have A -> B -> C,
30# C can import package A propagated by B
31, propagatedBuildInputs ? []
32
33# DEPRECATED: use propagatedBuildInputs
34, pythonPath ? []
35
36# used to disable derivation, useful for specific python versions
37, disabled ? false
38
39# Raise an error if two packages are installed with the same name
40, catchConflicts ? true
41
42# Additional arguments to pass to the makeWrapper function, which wraps
43# generated binaries.
44, makeWrapperArgs ? []
45
46# Skip wrapping of python programs altogether
47, dontWrapPythonPrograms ? false
48
49# Remove bytecode from bin folder.
50# When a Python script has the extension `.py`, bytecode is generated
51# Typically, executables in bin have no extension, so no bytecode is generated.
52# However, some packages do provide executables with extensions, and thus bytecode is generated.
53, removeBinBytecode ? true
54
55, meta ? {}
56
57, passthru ? {}
58
59, doCheck ? config.doCheckByDefault or false
60
61, ... } @ attrs:
62
63
64# Keep extra attributes from `attrs`, e.g., `patchPhase', etc.
65if disabled
66then throw "${name} not supported for interpreter ${python.executable}"
67else
68
69let self = toPythonModule (python.stdenv.mkDerivation (builtins.removeAttrs attrs [
70 "disabled" "checkInputs" "doCheck" "doInstallCheck" "dontWrapPythonPrograms" "catchConflicts"
71 ] // {
72
73 name = namePrefix + name;
74
75 nativeBuildInputs = [
76 python
77 wrapPython
78 ensureNewerSourcesForZipFilesHook
79 setuptools
80# ++ lib.optional catchConflicts setuptools # If we no longer propagate setuptools
81 ] ++ lib.optionals (lib.hasSuffix "zip" (attrs.src.name or "")) [
82 unzip
83 ] ++ nativeBuildInputs;
84
85 buildInputs = buildInputs ++ pythonPath;
86
87 # Propagate python and setuptools. We should stop propagating setuptools.
88 propagatedBuildInputs = propagatedBuildInputs ++ [ python setuptools ];
89
90 # Enabled to detect some (native)BuildInputs mistakes
91 strictDeps = true;
92
93 LANG = "${if python.stdenv.isDarwin then "en_US" else "C"}.UTF-8";
94
95 # Python packages don't have a checkPhase, only an installCheckPhase
96 doCheck = false;
97 doInstallCheck = doCheck;
98 installCheckInputs = checkInputs;
99
100 postFixup = lib.optionalString (!dontWrapPythonPrograms) ''
101 wrapPythonPrograms
102 '' + lib.optionalString removeBinBytecode ''
103 if [ -d "$out/bin" ]; then
104 rm -rf "$out/bin/__pycache__" # Python 3
105 find "$out/bin" -type f -name "*.pyc" -delete # Python 2
106 fi
107 '' + lib.optionalString catchConflicts ''
108 # Check if we have two packages with the same name in the closure and fail.
109 # If this happens, something went wrong with the dependencies specs.
110 # Intentionally kept in a subdirectory, see catch_conflicts/README.md.
111 ${python.pythonForBuild.interpreter} ${./catch_conflicts}/catch_conflicts.py
112 '' + attrs.postFixup or '''';
113
114 # Python packages built through cross-compilation are always for the host platform.
115 disallowedReferences = lib.optionals (python.stdenv.hostPlatform != python.stdenv.buildPlatform) [ python.pythonForBuild ];
116
117 meta = {
118 # default to python's platforms
119 platforms = python.meta.platforms;
120 isBuildPythonPackage = python.meta.platforms;
121 } // meta;
122}));
123
124passthru.updateScript = let
125 filename = builtins.head (lib.splitString ":" self.meta.position);
126 in attrs.passthru.updateScript or [ update-python-libraries filename ];
127in lib.extendDerivation true passthru self