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