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