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