1{ stdenv, fetchurl, fetchpatch, buildPackages
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, xproto ? null, x11Support ? false
12, zlib
13, callPackage
14, self
15, CF, configd
16, python-setup-hook
17# For the Python package set
18, packageOverrides ? (self: super: {})
19}:
20
21assert x11Support -> tcl != null
22 && tk != null
23 && xproto != null
24 && libX11 != null;
25with stdenv.lib;
26
27let
28 majorVersion = "3.6";
29 minorVersion = "6";
30 minorVersionSuffix = "";
31 version = "${majorVersion}.${minorVersion}${minorVersionSuffix}";
32 libPrefix = "python${majorVersion}";
33 sitePackages = "lib/${libPrefix}/site-packages";
34
35 buildInputs = filter (p: p != null) [
36 zlib bzip2 expat lzma libffi gdbm sqlite readline ncurses openssl ]
37 ++ optionals x11Support [ tcl tk libX11 xproto ]
38 ++ optionals stdenv.isDarwin [ CF configd ];
39
40 nativeBuildInputs =
41 optional (stdenv.hostPlatform != stdenv.buildPlatform) buildPackages.python3;
42
43 hasDistutilsCxxPatch = !(stdenv.cc.isGNU or false);
44
45in stdenv.mkDerivation {
46 name = "python3-${version}";
47 pythonVersion = majorVersion;
48 inherit majorVersion version;
49
50 inherit buildInputs nativeBuildInputs;
51
52 src = fetchurl {
53 url = "https://www.python.org/ftp/python/${majorVersion}.${minorVersion}/Python-${version}.tar.xz";
54 sha256 = "0vz1wqg50zq6g15givdx1s2rq5752y5g2f1978bs6wvf8mfw36yp";
55 };
56
57 NIX_LDFLAGS = optionalString stdenv.isLinux "-lgcc_s";
58
59 # Determinism: The interpreter is patched to write null timestamps when compiling python files.
60 # This way python doesn't try to update them when we freeze timestamps in nix store.
61 DETERMINISTIC_BUILD=1;
62 # Determinism: We fix the hashes of str, bytes and datetime objects.
63 PYTHONHASHSEED=0;
64
65 prePatch = optionalString stdenv.isDarwin ''
66 substituteInPlace configure --replace '`/usr/bin/arch`' '"i386"'
67 substituteInPlace configure --replace '-Wl,-stack_size,1000000' ' '
68 '';
69
70 patches = [
71 ./no-ldconfig.patch
72 ] ++ optionals stdenv.isDarwin [
73 # Fix for https://bugs.python.org/issue24658
74 (fetchpatch {
75 url = "https://bugs.python.org/file45178/issue24658-3-3.6.diff";
76 sha256 = "1x060hs80nl34mcl2ji2i7l4shxkmxwgq8h8lcmav8rjqqz1nb4a";
77 })
78 ] ++ optionals (x11Support && stdenv.isDarwin) [
79 ./use-correct-tcl-tk-on-darwin.patch
80 ] ++ optionals hasDistutilsCxxPatch [
81 # Fix for http://bugs.python.org/issue1222585
82 # Upstream distutils is calling C compiler to compile C++ code, which
83 # only works for GCC and Apple Clang. This makes distutils to call C++
84 # compiler when needed.
85 (fetchpatch {
86 url = "https://bugs.python.org/file47669/python-3.8-distutils-C++.patch";
87 sha256 = "0s801d7ww9yrk6ys053jvdhl0wicbznx08idy36f1nrrxsghb3ii";
88 })
89 ];
90
91 postPatch = ''
92 # Determinism
93 substituteInPlace "Lib/py_compile.py" --replace "source_stats['mtime']" "(1 if 'DETERMINISTIC_BUILD' in os.environ else source_stats['mtime'])"
94 # Determinism. This is done unconditionally
95 substituteInPlace "Lib/importlib/_bootstrap_external.py" --replace "source_mtime = int(st['mtime'])" "source_mtime = 1"
96 '' + optionalString (x11Support && (tix != null)) ''
97 substituteInPlace "Lib/tkinter/tix.py" --replace "os.environ.get('TIX_LIBRARY')" "os.environ.get('TIX_LIBRARY') or '${tix}/lib'"
98 '';
99
100 CPPFLAGS="${concatStringsSep " " (map (p: "-I${getDev p}/include") buildInputs)}";
101 LDFLAGS="${concatStringsSep " " (map (p: "-L${getLib p}/lib") buildInputs)}";
102 LIBS="${optionalString (!stdenv.isDarwin) "-lcrypt"} ${optionalString (ncurses != null) "-lncurses"}";
103
104 configureFlags = [
105 "--enable-shared"
106 "--with-threads"
107 "--without-ensurepip"
108 "--with-system-expat"
109 "--with-system-ffi"
110 ] ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
111 "ac_cv_buggy_getaddrinfo=no"
112 # Assume little-endian IEEE 754 floating point when cross compiling
113 "ac_cv_little_endian_double=yes"
114 "ac_cv_big_endian_double=no"
115 "ac_cv_mixed_endian_double=no"
116 "ac_cv_x87_double_rounding=yes"
117 "ac_cv_tanh_preserves_zero_sign=yes"
118 # Generally assume that things are present and work
119 "ac_cv_posix_semaphores_enabled=yes"
120 "ac_cv_broken_sem_getvalue=no"
121 "ac_cv_wchar_t_signed=yes"
122 "ac_cv_rshift_extends_sign=yes"
123 "ac_cv_broken_nice=no"
124 "ac_cv_broken_poll=no"
125 "ac_cv_working_tzset=yes"
126 "ac_cv_have_long_long_format=yes"
127 "ac_cv_have_size_t_format=yes"
128 "ac_cv_computed_gotos=yes"
129 "ac_cv_file__dev_ptmx=yes"
130 "ac_cv_file__dev_ptc=yes"
131 ]
132 # Never even try to use lchmod on linux,
133 # don't rely on detecting glibc-isms.
134 ++ optional stdenv.hostPlatform.isLinux "ac_cv_func_lchmod=no";
135
136 preConfigure = ''
137 for i in /usr /sw /opt /pkg; do # improve purity
138 substituteInPlace ./setup.py --replace $i /no-such-path
139 done
140 ${optionalString stdenv.isDarwin ''
141 export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -msse2"
142 export MACOSX_DEPLOYMENT_TARGET=10.6
143 ''
144 + optionalString stdenv.hostPlatform.isMusl ''
145 export NIX_CFLAGS_COMPILE+=" -DTHREAD_STACK_SIZE=0x100000"
146 ''}
147 '';
148
149 setupHook = python-setup-hook sitePackages;
150
151 postInstall = ''
152 # needed for some packages, especially packages that backport functionality
153 # to 2.x from 3.x
154 for item in $out/lib/python${majorVersion}/test/*; do
155 if [[ "$item" != */test_support.py*
156 && "$item" != */test/support
157 && "$item" != */test/libregrtest
158 && "$item" != */test/regrtest.py* ]]; then
159 rm -rf "$item"
160 else
161 echo $item
162 fi
163 done
164 touch $out/lib/python${majorVersion}/test/__init__.py
165
166 ln -s "$out/include/python${majorVersion}m" "$out/include/python${majorVersion}"
167 paxmark E $out/bin/python${majorVersion}
168
169 # Python on Nix is not manylinux1 compatible. https://github.com/NixOS/nixpkgs/issues/18484
170 echo "manylinux1_compatible=False" >> $out/lib/${libPrefix}/_manylinux.py
171
172 # Determinism: Windows installers were not deterministic.
173 # We're also not interested in building Windows installers.
174 find "$out" -name 'wininst*.exe' | xargs -r rm -f
175
176 # Use Python3 as default python
177 ln -s "$out/bin/idle3" "$out/bin/idle"
178 ln -s "$out/bin/pydoc3" "$out/bin/pydoc"
179 ln -s "$out/bin/python3" "$out/bin/python"
180 ln -s "$out/bin/python3-config" "$out/bin/python-config"
181 ln -s "$out/lib/pkgconfig/python3.pc" "$out/lib/pkgconfig/python.pc"
182
183 # Get rid of retained dependencies on -dev packages, and remove
184 # some $TMPDIR references to improve binary reproducibility.
185 # Note that the .pyc file of _sysconfigdata.py should be regenerated!
186 for i in $out/lib/python${majorVersion}/_sysconfigdata*.py $out/lib/python${majorVersion}/config-${majorVersion}m*/Makefile; do
187 sed -i $i -e "s|-I/nix/store/[^ ']*||g" -e "s|-L/nix/store/[^ ']*||g" -e "s|$TMPDIR|/no-such-path|g"
188 done
189 '' + optionalString (stdenv.hostPlatform == stdenv.buildPlatform) ''
190 # Determinism: rebuild all bytecode
191 # We exclude lib2to3 because that's Python 2 code which fails
192 # We rebuild three times, once for each optimization level
193 find $out -name "*.py" | $out/bin/python -m compileall -q -f -x "lib2to3" -i -
194 find $out -name "*.py" | $out/bin/python -O -m compileall -q -f -x "lib2to3" -i -
195 find $out -name "*.py" | $out/bin/python -OO -m compileall -q -f -x "lib2to3" -i -
196 '';
197
198 passthru = let
199 pythonPackages = callPackage ../../../../../top-level/python-packages.nix {python=self; overrides=packageOverrides;};
200 in rec {
201 inherit libPrefix sitePackages x11Support hasDistutilsCxxPatch;
202 executable = "${libPrefix}m";
203 buildEnv = callPackage ../../wrapper.nix { python = self; inherit (pythonPackages) requiredPythonModules; };
204 withPackages = import ../../with-packages.nix { inherit buildEnv pythonPackages;};
205 pkgs = pythonPackages;
206 isPy3 = true;
207 isPy36 = true;
208 is_py3k = true; # deprecated
209 interpreter = "${self}/bin/${executable}";
210 };
211
212 enableParallelBuilding = true;
213
214 doCheck = false; # expensive, and fails
215
216 meta = {
217 homepage = http://python.org;
218 description = "A high-level dynamically-typed programming language";
219 longDescription = ''
220 Python is a remarkably powerful dynamic programming language that
221 is used in a wide variety of application domains. Some of its key
222 distinguishing features include: clear, readable syntax; strong
223 introspection capabilities; intuitive object orientation; natural
224 expression of procedural code; full modularity, supporting
225 hierarchical packages; exception-based error handling; and very
226 high level dynamic data types.
227 '';
228 license = licenses.psfl;
229 platforms = with platforms; linux ++ darwin;
230 maintainers = with maintainers; [ fridh kragniz ];
231 };
232}