1{ stdenv, fetchurl, fetchpatch
2, bzip2
3, expat
4, libffi
5, gdbm
6, lzma
7, ncurses
8, openssl
9, readline
10, sqlite
11, tcl ? null, tk ? null, tix ? null, libX11 ? null, xorgproto ? null, x11Support ? false
12, zlib
13, self
14, CF, configd
15, python-setup-hook
16, nukeReferences
17# For the Python package set
18, packageOverrides ? (self: super: {})
19, buildPackages
20, sourceVersion
21, sha256
22, passthruFun
23, bash
24, stripConfig ? false
25, stripIdlelib ? false
26, stripTests ? false
27, stripTkinter ? false
28, rebuildBytecode ? true
29, stripBytecode ? false
30}:
31
32assert x11Support -> tcl != null
33 && tk != null
34 && xorgproto != null
35 && libX11 != null;
36with stdenv.lib;
37
38let
39
40 passthru = passthruFun rec {
41 inherit self sourceVersion packageOverrides;
42 implementation = "cpython";
43 libPrefix = "python${pythonVersion}";
44 executable = libPrefix;
45 pythonVersion = with sourceVersion; "${major}.${minor}";
46 sitePackages = "lib/${libPrefix}/site-packages";
47 inherit hasDistutilsCxxPatch pythonForBuild;
48 };
49
50 version = with sourceVersion; "${major}.${minor}.${patch}${suffix}";
51
52 nativeBuildInputs = [
53 nukeReferences
54 ] ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
55 buildPackages.stdenv.cc
56 pythonForBuild
57 ];
58
59 buildInputs = filter (p: p != null) [
60 zlib bzip2 expat lzma libffi gdbm sqlite readline ncurses openssl ]
61 ++ optionals x11Support [ tcl tk libX11 xorgproto ]
62 ++ optionals stdenv.isDarwin [ CF configd ];
63
64 hasDistutilsCxxPatch = !(stdenv.cc.isGNU or false);
65
66 pythonForBuild = buildPackages.${"python${sourceVersion.major}${sourceVersion.minor}"};
67
68 pythonForBuildInterpreter = if stdenv.hostPlatform == stdenv.buildPlatform then
69 "$out/bin/python"
70 else pythonForBuild.interpreter;
71
72in with passthru; stdenv.mkDerivation {
73 pname = "python3";
74 inherit version;
75
76 inherit buildInputs nativeBuildInputs;
77
78 src = fetchurl {
79 url = with sourceVersion; "https://www.python.org/ftp/python/${major}.${minor}.${patch}/Python-${version}.tar.xz";
80 inherit sha256;
81 };
82
83 prePatch = optionalString stdenv.isDarwin ''
84 substituteInPlace configure --replace '`/usr/bin/arch`' '"i386"'
85 substituteInPlace configure --replace '-Wl,-stack_size,1000000' ' '
86 '' + optionalString (stdenv.isDarwin && x11Support) ''
87 substituteInPlace setup.py --replace /Library/Frameworks /no-such-path
88 '';
89
90 patches = [
91 # Disable the use of ldconfig in ctypes.util.find_library (since
92 # ldconfig doesn't work on NixOS), and don't use
93 # ctypes.util.find_library during the loading of the uuid module
94 # (since it will do a futile invocation of gcc (!) to find
95 # libuuid, slowing down program startup a lot).
96 (./. + "/${sourceVersion.major}.${sourceVersion.minor}/no-ldconfig.patch")
97 ] ++ optionals (isPy35 || isPy36) [
98 # Determinism: Write null timestamps when compiling python files.
99 ./3.5/force_bytecode_determinism.patch
100 ] ++ optionals isPy35 [
101 # Backports support for LD_LIBRARY_PATH from 3.6
102 ./3.5/ld_library_path.patch
103 ] ++ optionals isPy37 [
104 # Fix darwin build https://bugs.python.org/issue34027
105 (fetchpatch {
106 url = https://bugs.python.org/file47666/darwin-libutil.patch;
107 sha256 = "0242gihnw3wfskl4fydp2xanpl8k5q7fj4dp7dbbqf46a4iwdzpa";
108 })
109 ] ++ optionals (isPy3k && hasDistutilsCxxPatch) [
110 # Fix for http://bugs.python.org/issue1222585
111 # Upstream distutils is calling C compiler to compile C++ code, which
112 # only works for GCC and Apple Clang. This makes distutils to call C++
113 # compiler when needed.
114 (
115 if isPy35 then
116 ./3.5/python-3.x-distutils-C++.patch
117 else if isPy37 then
118 ./3.7/python-3.x-distutils-C++.patch
119 else
120 fetchpatch {
121 url = "https://bugs.python.org/file48016/python-3.x-distutils-C++.patch";
122 sha256 = "1h18lnpx539h5lfxyk379dxwr8m2raigcjixkf133l4xy3f4bzi2";
123 }
124 )
125 ];
126
127 postPatch = ''
128 '' + optionalString (x11Support && (tix != null)) ''
129 substituteInPlace "Lib/tkinter/tix.py" --replace "os.environ.get('TIX_LIBRARY')" "os.environ.get('TIX_LIBRARY') or '${tix}/lib'"
130 '';
131
132 CPPFLAGS = concatStringsSep " " (map (p: "-I${getDev p}/include") buildInputs);
133 LDFLAGS = concatStringsSep " " (map (p: "-L${getLib p}/lib") buildInputs);
134 LIBS = "${optionalString (!stdenv.isDarwin) "-lcrypt"} ${optionalString (ncurses != null) "-lncurses"}";
135 NIX_LDFLAGS = optionalString stdenv.isLinux "-lgcc_s";
136 # Determinism: We fix the hashes of str, bytes and datetime objects.
137 PYTHONHASHSEED=0;
138
139 configureFlags = [
140 "--enable-shared"
141 "--with-threads"
142 "--without-ensurepip"
143 "--with-system-expat"
144 "--with-system-ffi"
145 ] ++ optionals (sqlite != null && isPy3k) [
146 "--enable-loadable-sqlite-extensions"
147 ] ++ optionals (openssl != null) [
148 "--with-openssl=${openssl.dev}"
149 ] ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
150 "ac_cv_buggy_getaddrinfo=no"
151 # Assume little-endian IEEE 754 floating point when cross compiling
152 "ac_cv_little_endian_double=yes"
153 "ac_cv_big_endian_double=no"
154 "ac_cv_mixed_endian_double=no"
155 "ac_cv_x87_double_rounding=yes"
156 "ac_cv_tanh_preserves_zero_sign=yes"
157 # Generally assume that things are present and work
158 "ac_cv_posix_semaphores_enabled=yes"
159 "ac_cv_broken_sem_getvalue=no"
160 "ac_cv_wchar_t_signed=yes"
161 "ac_cv_rshift_extends_sign=yes"
162 "ac_cv_broken_nice=no"
163 "ac_cv_broken_poll=no"
164 "ac_cv_working_tzset=yes"
165 "ac_cv_have_long_long_format=yes"
166 "ac_cv_have_size_t_format=yes"
167 "ac_cv_computed_gotos=yes"
168 "ac_cv_file__dev_ptmx=yes"
169 "ac_cv_file__dev_ptc=yes"
170 ] ++ optionals stdenv.hostPlatform.isLinux [
171 # Never even try to use lchmod on linux,
172 # don't rely on detecting glibc-isms.
173 "ac_cv_func_lchmod=no"
174 ];
175
176 preConfigure = ''
177 for i in /usr /sw /opt /pkg; do # improve purity
178 substituteInPlace ./setup.py --replace $i /no-such-path
179 done
180 '' + optionalString stdenv.isDarwin ''
181 export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -msse2"
182 export MACOSX_DEPLOYMENT_TARGET=10.6
183 '' + optionalString (isPy3k && pythonOlder "3.7") ''
184 # Determinism: The interpreter is patched to write null timestamps when compiling Python files
185 # so Python doesn't try to update the bytecode when seeing frozen timestamps in Nix's store.
186 export DETERMINISTIC_BUILD=1;
187 '' + optionalString stdenv.hostPlatform.isMusl ''
188 export NIX_CFLAGS_COMPILE+=" -DTHREAD_STACK_SIZE=0x100000"
189 '';
190
191 setupHook = python-setup-hook sitePackages;
192
193 postInstall = ''
194 # needed for some packages, especially packages that backport functionality
195 # to 2.x from 3.x
196 for item in $out/lib/${libPrefix}/test/*; do
197 if [[ "$item" != */test_support.py*
198 && "$item" != */test/support
199 && "$item" != */test/libregrtest
200 && "$item" != */test/regrtest.py* ]]; then
201 rm -rf "$item"
202 else
203 echo $item
204 fi
205 done
206 touch $out/lib/${libPrefix}/test/__init__.py
207
208 ln -s "$out/include/${executable}m" "$out/include/${executable}"
209
210 # Python on Nix is not manylinux1 compatible. https://github.com/NixOS/nixpkgs/issues/18484
211 echo "manylinux1_compatible=False" >> $out/lib/${libPrefix}/_manylinux.py
212
213 # Determinism: Windows installers were not deterministic.
214 # We're also not interested in building Windows installers.
215 find "$out" -name 'wininst*.exe' | xargs -r rm -f
216
217 # Use Python3 as default python
218 ln -s "$out/bin/idle3" "$out/bin/idle"
219 ln -s "$out/bin/pydoc3" "$out/bin/pydoc"
220 ln -s "$out/bin/python3" "$out/bin/python"
221 ln -s "$out/bin/python3-config" "$out/bin/python-config"
222 ln -s "$out/lib/pkgconfig/python3.pc" "$out/lib/pkgconfig/python.pc"
223
224 # Get rid of retained dependencies on -dev packages, and remove
225 # some $TMPDIR references to improve binary reproducibility.
226 # Note that the .pyc file of _sysconfigdata.py should be regenerated!
227 for i in $out/lib/${libPrefix}/_sysconfigdata*.py $out/lib/${libPrefix}/config-${sourceVersion.major}${sourceVersion.minor}*/Makefile; do
228 sed -i $i -e "s|$TMPDIR|/no-such-path|g"
229 done
230
231 # Further get rid of references. https://github.com/NixOS/nixpkgs/issues/51668
232 find $out/lib/python*/config-* -type f -print -exec nuke-refs -e $out '{}' +
233 find $out/lib -name '_sysconfigdata*.py*' -print -exec nuke-refs -e $out '{}' +
234
235 '' + optionalString stripConfig ''
236 rm -R $out/bin/python*-config $out/lib/python*/config-*
237 '' + optionalString stripIdlelib ''
238 # Strip IDLE (and turtledemo, which uses it)
239 rm -R $out/bin/idle* $out/lib/python*/{idlelib,turtledemo}
240 '' + optionalString stripTkinter ''
241 rm -R $out/lib/python*/tkinter
242 '' + optionalString stripTests ''
243 # Strip tests
244 rm -R $out/lib/python*/test $out/lib/python*/**/test{,s}
245 '' + ''
246 # Include a sitecustomize.py file
247 cp ${../sitecustomize.py} $out/${sitePackages}/sitecustomize.py
248 '' + optionalString rebuildBytecode ''
249
250 # Determinism: rebuild all bytecode
251 # We exclude lib2to3 because that's Python 2 code which fails
252 # We rebuild three times, once for each optimization level
253 # Python 3.7 implements PEP 552, introducing support for deterministic bytecode.
254 # This is automatically used when `SOURCE_DATE_EPOCH` is set.
255 find $out -name "*.py" | ${pythonForBuildInterpreter} -m compileall -q -f -x "lib2to3" -i -
256 find $out -name "*.py" | ${pythonForBuildInterpreter} -O -m compileall -q -f -x "lib2to3" -i -
257 find $out -name "*.py" | ${pythonForBuildInterpreter} -OO -m compileall -q -f -x "lib2to3" -i -
258 '' + optionalString stripBytecode ''
259 find $out -type d -name __pycache__ -print0 | xargs -0 -I {} rm -rf "{}"
260 '';
261
262 preFixup = stdenv.lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
263 # Ensure patch-shebangs uses shebangs of host interpreter.
264 export PATH=${stdenv.lib.makeBinPath [ "$out" bash ]}:$PATH
265 '';
266
267 # Enforce that we don't have references to the OpenSSL -dev package, which we
268 # explicitly specify in our configure flags above.
269 disallowedReferences =
270 stdenv.lib.optionals (openssl != null) [ openssl.dev ]
271 ++ stdenv.lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
272 # Ensure we don't have references to build-time packages.
273 # These typically end up in shebangs.
274 pythonForBuild buildPackages.bash
275 ];
276
277 inherit passthru;
278
279 enableParallelBuilding = true;
280
281 meta = {
282 homepage = http://python.org;
283 description = "A high-level dynamically-typed programming language";
284 longDescription = ''
285 Python is a remarkably powerful dynamic programming language that
286 is used in a wide variety of application domains. Some of its key
287 distinguishing features include: clear, readable syntax; strong
288 introspection capabilities; intuitive object orientation; natural
289 expression of procedural code; full modularity, supporting
290 hierarchical packages; exception-based error handling; and very
291 high level dynamic data types.
292 '';
293 license = licenses.psfl;
294 platforms = with platforms; linux ++ darwin;
295 maintainers = with maintainers; [ fridh ];
296 };
297}