Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
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 [
91 # Backports support for LD_LIBRARY_PATH from 3.6
92 ./3.5/ld_library_path.patch
93 ] ++ optionals isPy37 [
94 # Fix darwin build https://bugs.python.org/issue34027
95 (fetchpatch {
96 url = https://bugs.python.org/file47666/darwin-libutil.patch;
97 sha256 = "0242gihnw3wfskl4fydp2xanpl8k5q7fj4dp7dbbqf46a4iwdzpa";
98 })
99 ] ++ optionals (isPy3k && hasDistutilsCxxPatch) [
100 # Fix for http://bugs.python.org/issue1222585
101 # Upstream distutils is calling C compiler to compile C++ code, which
102 # only works for GCC and Apple Clang. This makes distutils to call C++
103 # compiler when needed.
104 (
105 if isPy35 then
106 ./3.5/python-3.x-distutils-C++.patch
107 else
108 fetchpatch {
109 url = "https://bugs.python.org/file48016/python-3.x-distutils-C++.patch";
110 sha256 = "1h18lnpx539h5lfxyk379dxwr8m2raigcjixkf133l4xy3f4bzi2";
111 }
112 )
113 ];
114
115 postPatch = ''
116 '' + optionalString (x11Support && (tix != null)) ''
117 substituteInPlace "Lib/tkinter/tix.py" --replace "os.environ.get('TIX_LIBRARY')" "os.environ.get('TIX_LIBRARY') or '${tix}/lib'"
118 '';
119
120 CPPFLAGS = "${concatStringsSep " " (map (p: "-I${getDev p}/include") buildInputs)}";
121 LDFLAGS = "${concatStringsSep " " (map (p: "-L${getLib p}/lib") buildInputs)}";
122 LIBS = "${optionalString (!stdenv.isDarwin) "-lcrypt"} ${optionalString (ncurses != null) "-lncurses"}";
123 NIX_LDFLAGS = optionalString stdenv.isLinux "-lgcc_s";
124 # Determinism: We fix the hashes of str, bytes and datetime objects.
125 PYTHONHASHSEED=0;
126
127 configureFlags = [
128 "--enable-shared"
129 "--with-threads"
130 "--without-ensurepip"
131 "--with-system-expat"
132 "--with-system-ffi"
133 "--with-openssl=${openssl.dev}"
134 ] ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
135 "ac_cv_buggy_getaddrinfo=no"
136 # Assume little-endian IEEE 754 floating point when cross compiling
137 "ac_cv_little_endian_double=yes"
138 "ac_cv_big_endian_double=no"
139 "ac_cv_mixed_endian_double=no"
140 "ac_cv_x87_double_rounding=yes"
141 "ac_cv_tanh_preserves_zero_sign=yes"
142 # Generally assume that things are present and work
143 "ac_cv_posix_semaphores_enabled=yes"
144 "ac_cv_broken_sem_getvalue=no"
145 "ac_cv_wchar_t_signed=yes"
146 "ac_cv_rshift_extends_sign=yes"
147 "ac_cv_broken_nice=no"
148 "ac_cv_broken_poll=no"
149 "ac_cv_working_tzset=yes"
150 "ac_cv_have_long_long_format=yes"
151 "ac_cv_have_size_t_format=yes"
152 "ac_cv_computed_gotos=yes"
153 "ac_cv_file__dev_ptmx=yes"
154 "ac_cv_file__dev_ptc=yes"
155 ] ++ optionals stdenv.hostPlatform.isLinux [
156 # Never even try to use lchmod on linux,
157 # don't rely on detecting glibc-isms.
158 "ac_cv_func_lchmod=no"
159 ];
160
161 preConfigure = ''
162 for i in /usr /sw /opt /pkg; do # improve purity
163 substituteInPlace ./setup.py --replace $i /no-such-path
164 done
165 '' + optionalString stdenv.isDarwin ''
166 export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -msse2"
167 export MACOSX_DEPLOYMENT_TARGET=10.6
168 '' + optionalString (isPy3k && pythonOlder "3.7") ''
169 # Determinism: The interpreter is patched to write null timestamps when compiling python files.
170 # This way python does not try to update them when we freeze timestamps in nix store.
171 export DETERMINISTIC_BUILD=1;
172 '' + optionalString stdenv.hostPlatform.isMusl ''
173 export NIX_CFLAGS_COMPILE+=" -DTHREAD_STACK_SIZE=0x100000"
174 '';
175
176 setupHook = python-setup-hook sitePackages;
177
178 postInstall = ''
179 # needed for some packages, especially packages that backport functionality
180 # to 2.x from 3.x
181 for item in $out/lib/${libPrefix}/test/*; do
182 if [[ "$item" != */test_support.py*
183 && "$item" != */test/support
184 && "$item" != */test/libregrtest
185 && "$item" != */test/regrtest.py* ]]; then
186 rm -rf "$item"
187 else
188 echo $item
189 fi
190 done
191 touch $out/lib/${libPrefix}/test/__init__.py
192
193 ln -s "$out/include/${executable}m" "$out/include/${executable}"
194
195 # Python on Nix is not manylinux1 compatible. https://github.com/NixOS/nixpkgs/issues/18484
196 echo "manylinux1_compatible=False" >> $out/lib/${libPrefix}/_manylinux.py
197
198 # Determinism: Windows installers were not deterministic.
199 # We're also not interested in building Windows installers.
200 find "$out" -name 'wininst*.exe' | xargs -r rm -f
201
202 # Use Python3 as default python
203 ln -s "$out/bin/idle3" "$out/bin/idle"
204 ln -s "$out/bin/pydoc3" "$out/bin/pydoc"
205 ln -s "$out/bin/python3" "$out/bin/python"
206 ln -s "$out/bin/python3-config" "$out/bin/python-config"
207 ln -s "$out/lib/pkgconfig/python3.pc" "$out/lib/pkgconfig/python.pc"
208
209 # Get rid of retained dependencies on -dev packages, and remove
210 # some $TMPDIR references to improve binary reproducibility.
211 # Note that the .pyc file of _sysconfigdata.py should be regenerated!
212 for i in $out/lib/${libPrefix}/_sysconfigdata*.py $out/lib/${libPrefix}/config-${sourceVersion.major}${sourceVersion.minor}*/Makefile; do
213 sed -i $i -e "s|$TMPDIR|/no-such-path|g"
214 done
215
216 # Further get rid of references. https://github.com/NixOS/nixpkgs/issues/51668
217 find $out/lib/python*/config-* -type f -print -exec nuke-refs -e $out '{}' +
218 find $out/lib -name '_sysconfigdata*.py*' -print -exec nuke-refs -e $out '{}' +
219
220 # Determinism: rebuild all bytecode
221 # We exclude lib2to3 because that's Python 2 code which fails
222 # We rebuild three times, once for each optimization level
223 # Python 3.7 implements PEP 552, introducing support for deterministic bytecode.
224 # This is automatically used when `SOURCE_DATE_EPOCH` is set.
225 find $out -name "*.py" | ${pythonForBuildInterpreter} -m compileall -q -f -x "lib2to3" -i -
226 find $out -name "*.py" | ${pythonForBuildInterpreter} -O -m compileall -q -f -x "lib2to3" -i -
227 find $out -name "*.py" | ${pythonForBuildInterpreter} -OO -m compileall -q -f -x "lib2to3" -i -
228 '';
229
230 preFixup = stdenv.lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
231 # Ensure patch-shebangs uses shebangs of host interpreter.
232 export PATH=${stdenv.lib.makeBinPath [ "$out" bash ]}:$PATH
233 '';
234
235 # Enforce that we don't have references to the OpenSSL -dev package, which we
236 # explicitly specify in our configure flags above.
237 disallowedReferences = [
238 openssl.dev
239 ] ++ stdenv.lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
240 # Ensure we don't have references to build-time packages.
241 # These typically end up in shebangs.
242 pythonForBuild buildPackages.bash
243 ];
244
245 inherit passthru;
246
247 enableParallelBuilding = true;
248
249 meta = {
250 homepage = http://python.org;
251 description = "A high-level dynamically-typed programming language";
252 longDescription = ''
253 Python is a remarkably powerful dynamic programming language that
254 is used in a wide variety of application domains. Some of its key
255 distinguishing features include: clear, readable syntax; strong
256 introspection capabilities; intuitive object orientation; natural
257 expression of procedural code; full modularity, supporting
258 hierarchical packages; exception-based error handling; and very
259 high level dynamic data types.
260 '';
261 license = licenses.psfl;
262 platforms = with platforms; linux ++ darwin;
263 maintainers = with maintainers; [ fridh ];
264 };
265}