1{
2 lib,
3 stdenv,
4 fetchFromGitHub,
5 fetchpatch,
6 bzip2,
7 expat,
8 libffi,
9 gdbm,
10 db,
11 ncurses,
12 openssl,
13 readline,
14 sqlite,
15 tcl ? null,
16 tk ? null,
17 tclPackages,
18 libX11 ? null,
19 x11Support ? false,
20 zlib,
21 self,
22 coreutils,
23 autoreconfHook,
24 python-setup-hook,
25 # Some proprietary libs assume UCS2 unicode, especially on darwin :(
26 ucsEncoding ? 4,
27 # For the Python package set
28 packageOverrides ? (self: super: { }),
29 pkgsBuildBuild,
30 pkgsBuildHost,
31 pkgsBuildTarget,
32 pkgsHostHost,
33 pkgsTargetTarget,
34 sourceVersion,
35 hash,
36 passthruFun,
37 static ? stdenv.hostPlatform.isStatic,
38 stripBytecode ? reproducibleBuild,
39 rebuildBytecode ? true,
40 reproducibleBuild ? false,
41 enableOptimizations ? false,
42 strip2to3 ? false,
43 stripConfig ? false,
44 stripIdlelib ? false,
45 stripTests ? false,
46 pythonAttr ? "python${sourceVersion.major}${sourceVersion.minor}",
47}:
48
49assert x11Support -> tcl != null && tk != null && libX11 != null;
50
51assert lib.assertMsg (enableOptimizations -> (!stdenv.cc.isClang))
52 "Optimizations with clang are not supported. configure: error: llvm-profdata is required for a --enable-optimizations build but could not be found.";
53
54assert lib.assertMsg (
55 reproducibleBuild -> stripBytecode
56) "Deterministic builds require stripping bytecode.";
57
58assert lib.assertMsg (
59 reproducibleBuild -> (!enableOptimizations)
60) "Deterministic builds are not achieved when optimizations are enabled.";
61
62assert lib.assertMsg (
63 reproducibleBuild -> (!rebuildBytecode)
64) "Deterministic builds are not achieved when (default unoptimized) bytecode is created.";
65
66let
67 buildPackages = pkgsBuildHost;
68 inherit (passthru) pythonOnBuildForHost;
69
70 pythonOnBuildForHostInterpreter =
71 if stdenv.hostPlatform == stdenv.buildPlatform then
72 "$out/bin/python"
73 else
74 pythonOnBuildForHost.interpreter;
75
76 passthru =
77 passthruFun rec {
78 inherit self sourceVersion packageOverrides;
79 implementation = "cpython";
80 libPrefix = "python${pythonVersion}";
81 executable = libPrefix;
82 pythonVersion = with sourceVersion; "${major}.${minor}";
83 sitePackages = "lib/${libPrefix}/site-packages";
84 inherit hasDistutilsCxxPatch pythonAttr;
85 pythonOnBuildForBuild = pkgsBuildBuild.${pythonAttr};
86 pythonOnBuildForHost = pkgsBuildHost.${pythonAttr};
87 pythonOnBuildForTarget = pkgsBuildTarget.${pythonAttr};
88 pythonOnHostForHost = pkgsHostHost.${pythonAttr};
89 pythonOnTargetForTarget = pkgsTargetTarget.${pythonAttr} or { };
90 }
91 // {
92 inherit ucsEncoding;
93 };
94
95 version = with sourceVersion; "${major}.${minor}.${patch}${suffix}";
96
97 # ActiveState is a fork of cpython that includes fixes for security
98 # issues after its EOL
99 src = fetchFromGitHub {
100 owner = "ActiveState";
101 repo = "cpython";
102 rev = "v${version}";
103 inherit hash;
104 };
105
106 hasDistutilsCxxPatch = !(stdenv.cc.isGNU or false);
107 patches = [
108 # Look in C_INCLUDE_PATH and LIBRARY_PATH for stuff.
109 ./search-path.patch
110
111 # Python recompiles a Python if the mtime stored *in* the
112 # pyc/pyo file differs from the mtime of the source file. This
113 # doesn't work in Nix because Nix changes the mtime of files in
114 # the Nix store to 1. So treat that as a special case.
115 ./nix-store-mtime.patch
116
117 # patch python to put zero timestamp into pyc
118 # if DETERMINISTIC_BUILD env var is set
119 ./deterministic-build.patch
120
121 # Fix python bug #27177 (https://bugs.python.org/issue27177)
122 # The issue is that `match.group` only recognizes python integers
123 # instead of everything that has `__index__`.
124 # This bug was fixed upstream, but not backported to 2.7
125 (fetchpatch {
126 name = "re_match_index.patch";
127 url = "https://bugs.python.org/file43084/re_match_index.patch";
128 sha256 = "0l9rw6r5r90iybdkp3hhl2pf0h0s1izc68h5d3ywrm92pq32wz57";
129 })
130
131 # Fix race-condition during pyc creation. Has a slight backwards
132 # incompatible effect: pyc symlinks will now be overridden
133 # (https://bugs.python.org/issue17222). Included in python >= 3.4,
134 # backported in debian since 2013.
135 # https://bugs.python.org/issue13146
136 ./atomic_pyc.patch
137
138 # Backport from CPython 3.8 of a good list of tests to run for PGO.
139 ./profile-task.patch
140
141 # The workaround is for unittests on Win64, which we don't support.
142 # It does break aarch64-darwin, which we do support. See:
143 # * https://bugs.python.org/issue35523
144 # * https://github.com/python/cpython/commit/e6b247c8e524
145 ../3.7/no-win64-workaround.patch
146
147 # fix openssl detection by reverting irrelevant change for us, to enable hashlib which is required by pip
148 (fetchpatch {
149 url = "https://github.com/ActiveState/cpython/pull/35/commits/20ea5b46aaf1e7bdf9d6905ba8bece2cc73b05b0.patch";
150 revert = true;
151 hash = "sha256-Lp5fGlcfJJ6p6vKmcLckJiAA2AZz4prjFE0aMEJxotw=";
152 })
153 ]
154 ++ lib.optionals (x11Support && stdenv.hostPlatform.isDarwin) [
155 ./use-correct-tcl-tk-on-darwin.patch
156
157 ]
158 ++ lib.optionals stdenv.hostPlatform.isLinux [
159
160 # Disable the use of ldconfig in ctypes.util.find_library (since
161 # ldconfig doesn't work on NixOS), and don't use
162 # ctypes.util.find_library during the loading of the uuid module
163 # (since it will do a futile invocation of gcc (!) to find
164 # libuuid, slowing down program startup a lot).
165 ./no-ldconfig.patch
166
167 # Fix ctypes.util.find_library with gcc10.
168 ./find_library-gcc10.patch
169
170 ]
171 ++ lib.optionals stdenv.hostPlatform.isCygwin [
172 ./2.5.2-ctypes-util-find_library.patch
173 ./2.5.2-tkinter-x11.patch
174 ./2.6.2-ssl-threads.patch
175 ./2.6.5-export-PySignal_SetWakeupFd.patch
176 ./2.6.5-FD_SETSIZE.patch
177 ./2.6.5-ncurses-abi6.patch
178 ./2.7.3-dbm.patch
179 ./2.7.3-dylib.patch
180 ./2.7.3-getpath-exe-extension.patch
181 ./2.7.3-no-libm.patch
182 ]
183 ++ lib.optionals hasDistutilsCxxPatch [
184
185 # Patch from http://bugs.python.org/issue1222585 adapted to work with
186 # `patch -p1' and with a last hunk removed
187 # Upstream distutils is calling C compiler to compile C++ code, which
188 # only works for GCC and Apple Clang. This makes distutils to call C++
189 # compiler when needed.
190 ./python-2.7-distutils-C++.patch
191 ]
192 ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
193 ./cross-compile.patch
194 ];
195
196 preConfigure = ''
197 # Purity.
198 for i in /usr /sw /opt /pkg; do
199 substituteInPlace ./setup.py --replace $i /no-such-path
200 done
201 ''
202 + lib.optionalString (stdenv ? cc && stdenv.cc.libc != null) ''
203 for i in Lib/plat-*/regen; do
204 substituteInPlace $i --replace /usr/include/ ${stdenv.cc.libc}/include/
205 done
206 ''
207 + lib.optionalString stdenv.hostPlatform.isDarwin ''
208 substituteInPlace configure --replace '`/usr/bin/arch`' '"i386"'
209 substituteInPlace Lib/multiprocessing/__init__.py \
210 --replace 'os.popen(comm)' 'os.popen("${coreutils}/bin/nproc")'
211 '';
212
213 configureFlags =
214 lib.optionals enableOptimizations [
215 "--enable-optimizations"
216 ]
217 ++ lib.optionals (!static) [
218 "--enable-shared"
219 ]
220 ++ [
221 "--with-threads"
222 "--with-system-ffi"
223 "--with-system-expat"
224 "--enable-unicode=ucs${toString ucsEncoding}"
225 ]
226 ++ lib.optionals stdenv.hostPlatform.isCygwin [
227 "ac_cv_func_bind_textdomain_codeset=yes"
228 ]
229 ++ lib.optionals stdenv.hostPlatform.isDarwin [
230 "--disable-toolbox-glue"
231 ]
232 ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
233 "PYTHON_FOR_BUILD=${lib.getBin buildPackages.python27}/bin/python"
234 "ac_cv_buggy_getaddrinfo=no"
235 # Assume little-endian IEEE 754 floating point when cross compiling
236 "ac_cv_little_endian_double=yes"
237 "ac_cv_big_endian_double=no"
238 "ac_cv_mixed_endian_double=no"
239 "ac_cv_x87_double_rounding=yes"
240 "ac_cv_tanh_preserves_zero_sign=yes"
241 # Generally assume that things are present and work
242 "ac_cv_posix_semaphores_enabled=yes"
243 "ac_cv_broken_sem_getvalue=no"
244 "ac_cv_wchar_t_signed=yes"
245 "ac_cv_rshift_extends_sign=yes"
246 "ac_cv_broken_nice=no"
247 "ac_cv_broken_poll=no"
248 "ac_cv_working_tzset=yes"
249 "ac_cv_have_long_long_format=yes"
250 "ac_cv_have_size_t_format=yes"
251 "ac_cv_computed_gotos=yes"
252 "ac_cv_file__dev_ptmx=yes"
253 "ac_cv_file__dev_ptc=yes"
254 ]
255 # Never even try to use lchmod on linux,
256 # don't rely on detecting glibc-isms.
257 ++ lib.optional stdenv.hostPlatform.isLinux "ac_cv_func_lchmod=no"
258 ++ lib.optional static "LDFLAGS=-static";
259
260 strictDeps = true;
261 buildInputs =
262 lib.optional (stdenv ? cc && stdenv.cc.libc != null) stdenv.cc.libc
263 ++ [
264 bzip2
265 openssl
266 zlib
267 libffi
268 expat
269 db
270 gdbm
271 ncurses
272 sqlite
273 readline
274 ]
275 ++ lib.optionals x11Support [
276 tcl
277 tk
278 libX11
279 ];
280 nativeBuildInputs = [
281 autoreconfHook
282 ]
283 ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
284 buildPackages.stdenv.cc
285 buildPackages.python27
286 ];
287
288 mkPaths = paths: {
289 C_INCLUDE_PATH = lib.makeSearchPathOutput "dev" "include" paths;
290 LIBRARY_PATH = lib.makeLibraryPath paths;
291 };
292
293 # Python 2.7 needs this
294 crossCompileEnv = lib.optionalAttrs (stdenv.hostPlatform != stdenv.buildPlatform) {
295 _PYTHON_HOST_PLATFORM = stdenv.hostPlatform.config;
296 };
297
298 # Build the basic Python interpreter without modules that have
299 # external dependencies.
300
301in
302with passthru;
303stdenv.mkDerivation (
304 {
305 pname = "python";
306 inherit version;
307
308 inherit
309 src
310 patches
311 buildInputs
312 nativeBuildInputs
313 preConfigure
314 configureFlags
315 ;
316
317 LDFLAGS = lib.optionalString (!stdenv.hostPlatform.isDarwin) "-lgcc_s";
318 inherit (mkPaths buildInputs) C_INCLUDE_PATH LIBRARY_PATH;
319
320 env.NIX_CFLAGS_COMPILE =
321 lib.optionalString (stdenv.targetPlatform.system == "x86_64-darwin") "-msse2"
322 + lib.optionalString stdenv.hostPlatform.isMusl " -DTHREAD_STACK_SIZE=0x100000";
323 DETERMINISTIC_BUILD = 1;
324
325 setupHook = python-setup-hook sitePackages;
326
327 postPatch = lib.optionalString (x11Support && ((tclPackages.tix or null) != null)) ''
328 substituteInPlace "Lib/lib-tk/Tix.py" --replace "os.environ.get('TIX_LIBRARY')" "os.environ.get('TIX_LIBRARY') or '${tclPackages.tix}/lib'"
329 '';
330
331 postInstall = ''
332 # needed for some packages, especially packages that backport
333 # functionality to 2.x from 3.x
334 for item in $out/lib/${libPrefix}/test/*; do
335 if [[ "$item" != */test_support.py*
336 && "$item" != */test/support
337 && "$item" != */test/regrtest.py* ]]; then
338 rm -rf "$item"
339 else
340 echo $item
341 fi
342 done
343 touch $out/lib/${libPrefix}/test/__init__.py
344 ln -s $out/lib/${libPrefix}/pdb.py $out/bin/pdb
345 ln -s $out/lib/${libPrefix}/pdb.py $out/bin/pdb${sourceVersion.major}.${sourceVersion.minor}
346 ln -s $out/share/man/man1/{python2.7.1.gz,python.1.gz}
347
348 rm "$out"/lib/python*/plat-*/regen # refers to glibc.dev
349
350 # Determinism: Windows installers were not deterministic.
351 # We're also not interested in building Windows installers.
352 find "$out" -name 'wininst*.exe' | xargs -r rm -f
353 ''
354 + lib.optionalString stripBytecode ''
355 # Determinism: deterministic bytecode
356 # First we delete all old bytecode.
357 find $out -name "*.pyc" -delete
358 ''
359 + lib.optionalString rebuildBytecode ''
360 # We build 3 levels of optimized bytecode. Note the default level, without optimizations,
361 # is not reproducible yet. https://bugs.python.org/issue29708
362 # Not creating bytecode will result in a large performance loss however, so we do build it.
363 find $out -name "*.py" | ${pythonOnBuildForHostInterpreter} -m compileall -q -f -x "lib2to3" -i -
364 find $out -name "*.py" | ${pythonOnBuildForHostInterpreter} -O -m compileall -q -f -x "lib2to3" -i -
365 find $out -name "*.py" | ${pythonOnBuildForHostInterpreter} -OO -m compileall -q -f -x "lib2to3" -i -
366 ''
367 + lib.optionalString stdenv.hostPlatform.isCygwin ''
368 cp libpython2.7.dll.a $out/lib
369 '';
370
371 inherit passthru;
372
373 postFixup = ''
374 # Include a sitecustomize.py file. Note it causes an error when it's in postInstall with 2.7.
375 cp ${../../sitecustomize.py} $out/${sitePackages}/sitecustomize.py
376 ''
377 + lib.optionalString strip2to3 ''
378 rm -R $out/bin/2to3 $out/lib/python*/lib2to3
379 ''
380 + lib.optionalString stripConfig ''
381 rm -R $out/bin/python*-config $out/lib/python*/config*
382 ''
383 + lib.optionalString stripIdlelib ''
384 # Strip IDLE
385 rm -R $out/bin/idle* $out/lib/python*/idlelib
386 ''
387 + lib.optionalString stripTests ''
388 # Strip tests
389 rm -R $out/lib/python*/test $out/lib/python*/**/test{,s}
390 '';
391
392 enableParallelBuilding = true;
393
394 doCheck = false; # expensive, and fails
395
396 meta = {
397 homepage = "http://python.org";
398 description = "High-level dynamically-typed programming language";
399 longDescription = ''
400 Python is a remarkably powerful dynamic programming language that
401 is used in a wide variety of application domains. Some of its key
402 distinguishing features include: clear, readable syntax; strong
403 introspection capabilities; intuitive object orientation; natural
404 expression of procedural code; full modularity, supporting
405 hierarchical packages; exception-based error handling; and very
406 high level dynamic data types.
407 '';
408 license = lib.licenses.psfl;
409 platforms = lib.platforms.all;
410 knownVulnerabilities = [
411 "Python 2.7 has reached its end of life after 2020-01-01. See https://www.python.org/doc/sunset-python-2/."
412 # Quote: That means that we will not improve it anymore after that day,
413 # even if someone finds a security problem in it. You should upgrade to
414 # Python 3 as soon as you can. [..] So, in 2008, we announced that we
415 # would sunset Python 2 in 2015, and asked people to upgrade before
416 # then. Some did, but many did not. So, in 2014, we extended that
417 # sunset till 2020.
418 ];
419 };
420 }
421 // crossCompileEnv
422)