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