1{ stdenv, fetchurl, fetchpatch
2, bzip2
3, expat
4, libffi
5, gdbm
6, lzma
7, ncurses
8, openssl
9, readline
10, sqlite
11, tcl ? null, tk ? null, tix ? null, libX11 ? null, xorgproto ? null, x11Support ? false
12, zlib
13, self
14, configd
15, python-setup-hook
16, nukeReferences
17# For the Python package set
18, packageOverrides ? (self: super: {})
19, buildPackages
20, pythonForBuild ? buildPackages.${"python${sourceVersion.major}${sourceVersion.minor}"}
21, sourceVersion
22, sha256
23, passthruFun
24, bash
25, stripConfig ? false
26, stripIdlelib ? false
27, stripTests ? false
28, stripTkinter ? false
29, rebuildBytecode ? true
30, stripBytecode ? false
31}:
32
33assert x11Support -> tcl != null
34 && tk != null
35 && xorgproto != null
36 && libX11 != null;
37with stdenv.lib;
38
39let
40
41 passthru = passthruFun rec {
42 inherit self sourceVersion packageOverrides;
43 implementation = "cpython";
44 libPrefix = "python${pythonVersion}";
45 executable = libPrefix;
46 pythonVersion = with sourceVersion; "${major}.${minor}";
47 sitePackages = "lib/${libPrefix}/site-packages";
48 inherit hasDistutilsCxxPatch pythonForBuild;
49 };
50
51 version = with sourceVersion; "${major}.${minor}.${patch}${suffix}";
52
53 nativeBuildInputs = [
54 nukeReferences
55 ] ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
56 buildPackages.stdenv.cc
57 pythonForBuild
58 ];
59
60 buildInputs = filter (p: p != null) ([
61 zlib bzip2 expat lzma libffi gdbm sqlite readline ncurses openssl ]
62 ++ optionals x11Support [ tcl tk libX11 xorgproto ]
63 ++ optionals stdenv.isDarwin [ configd ]);
64
65 hasDistutilsCxxPatch = !(stdenv.cc.isGNU or false);
66
67 inherit pythonForBuild;
68
69 pythonForBuildInterpreter = if stdenv.hostPlatform == stdenv.buildPlatform then
70 "$out/bin/python"
71 else pythonForBuild.interpreter;
72
73in with passthru; stdenv.mkDerivation {
74 pname = "python3";
75 inherit version;
76
77 inherit buildInputs nativeBuildInputs;
78
79 src = fetchurl {
80 url = with sourceVersion; "https://www.python.org/ftp/python/${major}.${minor}.${patch}/Python-${version}.tar.xz";
81 inherit sha256;
82 };
83
84 prePatch = optionalString stdenv.isDarwin ''
85 substituteInPlace configure --replace '`/usr/bin/arch`' '"i386"'
86 substituteInPlace configure --replace '-Wl,-stack_size,1000000' ' '
87 '' + optionalString (stdenv.isDarwin && x11Support) ''
88 substituteInPlace setup.py --replace /Library/Frameworks /no-such-path
89 '';
90
91 patches = [
92 # Disable the use of ldconfig in ctypes.util.find_library (since
93 # ldconfig doesn't work on NixOS), and don't use
94 # ctypes.util.find_library during the loading of the uuid module
95 # (since it will do a futile invocation of gcc (!) to find
96 # libuuid, slowing down program startup a lot).
97 (./. + "/${sourceVersion.major}.${sourceVersion.minor}/no-ldconfig.patch")
98 ] ++ optionals (isPy35 || isPy36) [
99 # Determinism: Write null timestamps when compiling python files.
100 ./3.5/force_bytecode_determinism.patch
101 ] ++ optionals isPy35 [
102 # Backports support for LD_LIBRARY_PATH from 3.6
103 ./3.5/ld_library_path.patch
104 ] ++ optionals (isPy37 || isPy38) [
105 # Fix darwin build https://bugs.python.org/issue34027
106 ./3.7/darwin-libutil.patch
107 ] ++ optionals (isPy3k && hasDistutilsCxxPatch) [
108 # Fix for http://bugs.python.org/issue1222585
109 # Upstream distutils is calling C compiler to compile C++ code, which
110 # only works for GCC and Apple Clang. This makes distutils to call C++
111 # compiler when needed.
112 (
113 if isPy35 then
114 ./3.5/python-3.x-distutils-C++.patch
115 else if isPy37 || isPy38 then
116 ./3.7/python-3.x-distutils-C++.patch
117 else
118 fetchpatch {
119 url = "https://bugs.python.org/file48016/python-3.x-distutils-C++.patch";
120 sha256 = "1h18lnpx539h5lfxyk379dxwr8m2raigcjixkf133l4xy3f4bzi2";
121 }
122 )
123 ];
124
125 postPatch = ''
126 '' + optionalString (x11Support && (tix != null)) ''
127 substituteInPlace "Lib/tkinter/tix.py" --replace "os.environ.get('TIX_LIBRARY')" "os.environ.get('TIX_LIBRARY') or '${tix}/lib'"
128 '';
129
130 CPPFLAGS = concatStringsSep " " (map (p: "-I${getDev p}/include") buildInputs);
131 LDFLAGS = concatStringsSep " " (map (p: "-L${getLib p}/lib") buildInputs);
132 LIBS = "${optionalString (!stdenv.isDarwin) "-lcrypt"} ${optionalString (ncurses != null) "-lncurses"}";
133 NIX_LDFLAGS = optionalString stdenv.isLinux "-lgcc_s";
134 # Determinism: We fix the hashes of str, bytes and datetime objects.
135 PYTHONHASHSEED=0;
136
137 configureFlags = [
138 "--enable-shared"
139 "--with-threads"
140 "--without-ensurepip"
141 "--with-system-expat"
142 "--with-system-ffi"
143 ] ++ optionals (sqlite != null && isPy3k) [
144 "--enable-loadable-sqlite-extensions"
145 ] ++ optionals (openssl != null) [
146 "--with-openssl=${openssl.dev}"
147 ] ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
148 "ac_cv_buggy_getaddrinfo=no"
149 # Assume little-endian IEEE 754 floating point when cross compiling
150 "ac_cv_little_endian_double=yes"
151 "ac_cv_big_endian_double=no"
152 "ac_cv_mixed_endian_double=no"
153 "ac_cv_x87_double_rounding=yes"
154 "ac_cv_tanh_preserves_zero_sign=yes"
155 # Generally assume that things are present and work
156 "ac_cv_posix_semaphores_enabled=yes"
157 "ac_cv_broken_sem_getvalue=no"
158 "ac_cv_wchar_t_signed=yes"
159 "ac_cv_rshift_extends_sign=yes"
160 "ac_cv_broken_nice=no"
161 "ac_cv_broken_poll=no"
162 "ac_cv_working_tzset=yes"
163 "ac_cv_have_long_long_format=yes"
164 "ac_cv_have_size_t_format=yes"
165 "ac_cv_computed_gotos=yes"
166 "ac_cv_file__dev_ptmx=yes"
167 "ac_cv_file__dev_ptc=yes"
168 ] ++ optionals stdenv.hostPlatform.isLinux [
169 # Never even try to use lchmod on linux,
170 # don't rely on detecting glibc-isms.
171 "ac_cv_func_lchmod=no"
172 ];
173
174 preConfigure = ''
175 for i in /usr /sw /opt /pkg; do # improve purity
176 substituteInPlace ./setup.py --replace $i /no-such-path
177 done
178 '' + optionalString stdenv.isDarwin ''
179 export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -msse2"
180 export MACOSX_DEPLOYMENT_TARGET=10.6
181 '' + optionalString (isPy3k && pythonOlder "3.7") ''
182 # Determinism: The interpreter is patched to write null timestamps when compiling Python files
183 # so Python doesn't try to update the bytecode when seeing frozen timestamps in Nix's store.
184 export DETERMINISTIC_BUILD=1;
185 '' + optionalString stdenv.hostPlatform.isMusl ''
186 export NIX_CFLAGS_COMPILE+=" -DTHREAD_STACK_SIZE=0x100000"
187 '';
188
189 setupHook = python-setup-hook sitePackages;
190
191 postInstall = ''
192 # needed for some packages, especially packages that backport functionality
193 # to 2.x from 3.x
194 for item in $out/lib/${libPrefix}/test/*; do
195 if [[ "$item" != */test_support.py*
196 && "$item" != */test/support
197 && "$item" != */test/libregrtest
198 && "$item" != */test/regrtest.py* ]]; then
199 rm -rf "$item"
200 else
201 echo $item
202 fi
203 done
204 touch $out/lib/${libPrefix}/test/__init__.py
205
206 ln -s "$out/include/${executable}m" "$out/include/${executable}"
207
208 # Determinism: Windows installers were not deterministic.
209 # We're also not interested in building Windows installers.
210 find "$out" -name 'wininst*.exe' | xargs -r rm -f
211
212 # Use Python3 as default python
213 ln -s "$out/bin/idle3" "$out/bin/idle"
214 ln -s "$out/bin/pydoc3" "$out/bin/pydoc"
215 ln -s "$out/bin/python3" "$out/bin/python"
216 ln -s "$out/bin/python3-config" "$out/bin/python-config"
217 ln -s "$out/lib/pkgconfig/python3.pc" "$out/lib/pkgconfig/python.pc"
218
219 # Get rid of retained dependencies on -dev packages, and remove
220 # some $TMPDIR references to improve binary reproducibility.
221 # Note that the .pyc file of _sysconfigdata.py should be regenerated!
222 for i in $out/lib/${libPrefix}/_sysconfigdata*.py $out/lib/${libPrefix}/config-${sourceVersion.major}${sourceVersion.minor}*/Makefile; do
223 sed -i $i -e "s|$TMPDIR|/no-such-path|g"
224 done
225
226 # Further get rid of references. https://github.com/NixOS/nixpkgs/issues/51668
227 find $out/lib/python*/config-* -type f -print -exec nuke-refs -e $out '{}' +
228 find $out/lib -name '_sysconfigdata*.py*' -print -exec nuke-refs -e $out '{}' +
229
230 '' + optionalString stripConfig ''
231 rm -R $out/bin/python*-config $out/lib/python*/config-*
232 '' + optionalString stripIdlelib ''
233 # Strip IDLE (and turtledemo, which uses it)
234 rm -R $out/bin/idle* $out/lib/python*/{idlelib,turtledemo}
235 '' + optionalString stripTkinter ''
236 rm -R $out/lib/python*/tkinter
237 '' + optionalString stripTests ''
238 # Strip tests
239 rm -R $out/lib/python*/test $out/lib/python*/**/test{,s}
240 '' + ''
241 # Include a sitecustomize.py file
242 cp ${../sitecustomize.py} $out/${sitePackages}/sitecustomize.py
243 '' + optionalString rebuildBytecode ''
244
245 # Determinism: rebuild all bytecode
246 # We exclude lib2to3 because that's Python 2 code which fails
247 # We rebuild three times, once for each optimization level
248 # Python 3.7 implements PEP 552, introducing support for deterministic bytecode.
249 # This is automatically used when `SOURCE_DATE_EPOCH` is set.
250 find $out -name "*.py" | ${pythonForBuildInterpreter} -m compileall -q -f -x "lib2to3" -i -
251 find $out -name "*.py" | ${pythonForBuildInterpreter} -O -m compileall -q -f -x "lib2to3" -i -
252 find $out -name "*.py" | ${pythonForBuildInterpreter} -OO -m compileall -q -f -x "lib2to3" -i -
253 '' + optionalString stripBytecode ''
254 find $out -type d -name __pycache__ -print0 | xargs -0 -I {} rm -rf "{}"
255 '';
256
257 preFixup = stdenv.lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
258 # Ensure patch-shebangs uses shebangs of host interpreter.
259 export PATH=${stdenv.lib.makeBinPath [ "$out" bash ]}:$PATH
260 '';
261
262 # Enforce that we don't have references to the OpenSSL -dev package, which we
263 # explicitly specify in our configure flags above.
264 disallowedReferences =
265 stdenv.lib.optionals (openssl != null) [ openssl.dev ]
266 ++ stdenv.lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
267 # Ensure we don't have references to build-time packages.
268 # These typically end up in shebangs.
269 pythonForBuild buildPackages.bash
270 ];
271
272 inherit passthru;
273
274 enableParallelBuilding = true;
275
276 meta = {
277 homepage = http://python.org;
278 description = "A high-level dynamically-typed programming language";
279 longDescription = ''
280 Python is a remarkably powerful dynamic programming language that
281 is used in a wide variety of application domains. Some of its key
282 distinguishing features include: clear, readable syntax; strong
283 introspection capabilities; intuitive object orientation; natural
284 expression of procedural code; full modularity, supporting
285 hierarchical packages; exception-based error handling; and very
286 high level dynamic data types.
287 '';
288 license = licenses.psfl;
289 platforms = with platforms; linux ++ darwin;
290 maintainers = with maintainers; [ fridh ];
291 };
292}