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, tix ? null, libX11 ? null, x11Support ? false
12, zlib
13, self
14, configd, 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 ] ++ lib.optionals (x11Support && stdenv.isDarwin) [
135 ./use-correct-tcl-tk-on-darwin.patch
136
137 ] ++ lib.optionals stdenv.isDarwin [
138 # Fix darwin build https://bugs.python.org/issue34027
139 ../3.7/darwin-libutil.patch
140
141 ] ++ lib.optionals stdenv.isLinux [
142
143 # Disable the use of ldconfig in ctypes.util.find_library (since
144 # ldconfig doesn't work on NixOS), and don't use
145 # ctypes.util.find_library during the loading of the uuid module
146 # (since it will do a futile invocation of gcc (!) to find
147 # libuuid, slowing down program startup a lot).
148 ./no-ldconfig.patch
149
150 # Fix ctypes.util.find_library with gcc10.
151 ./find_library-gcc10.patch
152
153 ] ++ lib.optionals stdenv.hostPlatform.isCygwin [
154 ./2.5.2-ctypes-util-find_library.patch
155 ./2.5.2-tkinter-x11.patch
156 ./2.6.2-ssl-threads.patch
157 ./2.6.5-export-PySignal_SetWakeupFd.patch
158 ./2.6.5-FD_SETSIZE.patch
159 ./2.6.5-ncurses-abi6.patch
160 ./2.7.3-dbm.patch
161 ./2.7.3-dylib.patch
162 ./2.7.3-getpath-exe-extension.patch
163 ./2.7.3-no-libm.patch
164 ] ++ lib.optionals hasDistutilsCxxPatch [
165
166 # Patch from http://bugs.python.org/issue1222585 adapted to work with
167 # `patch -p1' and with a last hunk removed
168 # Upstream distutils is calling C compiler to compile C++ code, which
169 # only works for GCC and Apple Clang. This makes distutils to call C++
170 # compiler when needed.
171 ./python-2.7-distutils-C++.patch
172 ] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
173 ./cross-compile.patch
174 ];
175
176 preConfigure = ''
177 # Purity.
178 for i in /usr /sw /opt /pkg; do
179 substituteInPlace ./setup.py --replace $i /no-such-path
180 done
181 '' + lib.optionalString (stdenv ? cc && stdenv.cc.libc != null) ''
182 for i in Lib/plat-*/regen; do
183 substituteInPlace $i --replace /usr/include/ ${stdenv.cc.libc}/include/
184 done
185 '' + lib.optionalString stdenv.isDarwin ''
186 substituteInPlace configure --replace '`/usr/bin/arch`' '"i386"'
187 substituteInPlace Lib/multiprocessing/__init__.py \
188 --replace 'os.popen(comm)' 'os.popen("${coreutils}/bin/nproc")'
189 '';
190
191 configureFlags = lib.optionals enableOptimizations [
192 "--enable-optimizations"
193 ] ++ lib.optionals (!static) [
194 "--enable-shared"
195 ] ++ [
196 "--with-threads"
197 "--with-system-ffi"
198 "--with-system-expat"
199 "--enable-unicode=ucs${toString ucsEncoding}"
200 ] ++ lib.optionals stdenv.hostPlatform.isCygwin [
201 "ac_cv_func_bind_textdomain_codeset=yes"
202 ] ++ lib.optionals stdenv.isDarwin [
203 "--disable-toolbox-glue"
204 ] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
205 "PYTHON_FOR_BUILD=${lib.getBin buildPackages.python}/bin/python"
206 "ac_cv_buggy_getaddrinfo=no"
207 # Assume little-endian IEEE 754 floating point when cross compiling
208 "ac_cv_little_endian_double=yes"
209 "ac_cv_big_endian_double=no"
210 "ac_cv_mixed_endian_double=no"
211 "ac_cv_x87_double_rounding=yes"
212 "ac_cv_tanh_preserves_zero_sign=yes"
213 # Generally assume that things are present and work
214 "ac_cv_posix_semaphores_enabled=yes"
215 "ac_cv_broken_sem_getvalue=no"
216 "ac_cv_wchar_t_signed=yes"
217 "ac_cv_rshift_extends_sign=yes"
218 "ac_cv_broken_nice=no"
219 "ac_cv_broken_poll=no"
220 "ac_cv_working_tzset=yes"
221 "ac_cv_have_long_long_format=yes"
222 "ac_cv_have_size_t_format=yes"
223 "ac_cv_computed_gotos=yes"
224 "ac_cv_file__dev_ptmx=yes"
225 "ac_cv_file__dev_ptc=yes"
226 ]
227 # Never even try to use lchmod on linux,
228 # don't rely on detecting glibc-isms.
229 ++ lib.optional stdenv.hostPlatform.isLinux "ac_cv_func_lchmod=no"
230 ++ lib.optional static "LDFLAGS=-static";
231
232 strictDeps = true;
233 buildInputs =
234 lib.optional (stdenv ? cc && stdenv.cc.libc != null) stdenv.cc.libc ++
235 [ bzip2 openssl zlib libffi expat db gdbm ncurses sqlite readline ]
236 ++ lib.optionals x11Support [ tcl tk libX11 ]
237 ++ lib.optional (stdenv.isDarwin && configd != null) configd;
238 nativeBuildInputs =
239 [ autoreconfHook ]
240 ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform)
241 [ buildPackages.stdenv.cc buildPackages.python ];
242
243 mkPaths = paths: {
244 C_INCLUDE_PATH = lib.makeSearchPathOutput "dev" "include" paths;
245 LIBRARY_PATH = lib.makeLibraryPath paths;
246 };
247
248 # Python 2.7 needs this
249 crossCompileEnv = lib.optionalAttrs (stdenv.hostPlatform != stdenv.buildPlatform)
250 { _PYTHON_HOST_PLATFORM = stdenv.hostPlatform.config; };
251
252 # Build the basic Python interpreter without modules that have
253 # external dependencies.
254
255in with passthru; stdenv.mkDerivation ({
256 pname = "python";
257 inherit version;
258
259 inherit src patches buildInputs nativeBuildInputs preConfigure configureFlags;
260
261 LDFLAGS = lib.optionalString (!stdenv.isDarwin) "-lgcc_s";
262 inherit (mkPaths buildInputs) C_INCLUDE_PATH LIBRARY_PATH;
263
264 env.NIX_CFLAGS_COMPILE = lib.optionalString (stdenv.targetPlatform.system == "x86_64-darwin") "-msse2"
265 + lib.optionalString stdenv.hostPlatform.isMusl " -DTHREAD_STACK_SIZE=0x100000";
266 DETERMINISTIC_BUILD = 1;
267
268 setupHook = python-setup-hook sitePackages;
269
270 postPatch = lib.optionalString (x11Support && (tix != null)) ''
271 substituteInPlace "Lib/lib-tk/Tix.py" --replace "os.environ.get('TIX_LIBRARY')" "os.environ.get('TIX_LIBRARY') or '${tix}/lib'"
272 '';
273
274 postInstall =
275 ''
276 # needed for some packages, especially packages that backport
277 # functionality to 2.x from 3.x
278 for item in $out/lib/${libPrefix}/test/*; do
279 if [[ "$item" != */test_support.py*
280 && "$item" != */test/support
281 && "$item" != */test/regrtest.py* ]]; then
282 rm -rf "$item"
283 else
284 echo $item
285 fi
286 done
287 touch $out/lib/${libPrefix}/test/__init__.py
288 ln -s $out/lib/${libPrefix}/pdb.py $out/bin/pdb
289 ln -s $out/lib/${libPrefix}/pdb.py $out/bin/pdb${sourceVersion.major}.${sourceVersion.minor}
290 ln -s $out/share/man/man1/{python2.7.1.gz,python.1.gz}
291
292 rm "$out"/lib/python*/plat-*/regen # refers to glibc.dev
293
294 # Determinism: Windows installers were not deterministic.
295 # We're also not interested in building Windows installers.
296 find "$out" -name 'wininst*.exe' | xargs -r rm -f
297 '' + lib.optionalString stripBytecode ''
298 # Determinism: deterministic bytecode
299 # First we delete all old bytecode.
300 find $out -name "*.pyc" -delete
301 '' + lib.optionalString rebuildBytecode ''
302 # We build 3 levels of optimized bytecode. Note the default level, without optimizations,
303 # is not reproducible yet. https://bugs.python.org/issue29708
304 # Not creating bytecode will result in a large performance loss however, so we do build it.
305 find $out -name "*.py" | ${pythonOnBuildForHostInterpreter} -m compileall -q -f -x "lib2to3" -i -
306 find $out -name "*.py" | ${pythonOnBuildForHostInterpreter} -O -m compileall -q -f -x "lib2to3" -i -
307 find $out -name "*.py" | ${pythonOnBuildForHostInterpreter} -OO -m compileall -q -f -x "lib2to3" -i -
308 '' + lib.optionalString stdenv.hostPlatform.isCygwin ''
309 cp libpython2.7.dll.a $out/lib
310 '';
311
312 inherit passthru;
313
314 postFixup = ''
315 # Include a sitecustomize.py file. Note it causes an error when it's in postInstall with 2.7.
316 cp ${../../sitecustomize.py} $out/${sitePackages}/sitecustomize.py
317 '' + lib.optionalString strip2to3 ''
318 rm -R $out/bin/2to3 $out/lib/python*/lib2to3
319 '' + lib.optionalString stripConfig ''
320 rm -R $out/bin/python*-config $out/lib/python*/config*
321 '' + lib.optionalString stripIdlelib ''
322 # Strip IDLE
323 rm -R $out/bin/idle* $out/lib/python*/idlelib
324 '' + lib.optionalString stripTests ''
325 # Strip tests
326 rm -R $out/lib/python*/test $out/lib/python*/**/test{,s}
327 '';
328
329 enableParallelBuilding = true;
330
331 doCheck = false; # expensive, and fails
332
333 meta = {
334 homepage = "http://python.org";
335 description = "A high-level dynamically-typed programming language";
336 longDescription = ''
337 Python is a remarkably powerful dynamic programming language that
338 is used in a wide variety of application domains. Some of its key
339 distinguishing features include: clear, readable syntax; strong
340 introspection capabilities; intuitive object orientation; natural
341 expression of procedural code; full modularity, supporting
342 hierarchical packages; exception-based error handling; and very
343 high level dynamic data types.
344 '';
345 license = lib.licenses.psfl;
346 platforms = lib.platforms.all;
347 maintainers = with lib.maintainers; [ fridh thiagokokada ];
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)