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, 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;
25
26with stdenv.lib;
27
28let
29 majorVersion = "3.5";
30 minorVersion = "6";
31 minorVersionSuffix = "";
32 version = "${majorVersion}.${minorVersion}${minorVersionSuffix}";
33 libPrefix = "python${majorVersion}";
34 sitePackages = "lib/${libPrefix}/site-packages";
35
36 buildInputs = filter (p: p != null) [
37 zlib bzip2 expat lzma libffi gdbm sqlite readline ncurses openssl ]
38 ++ optionals x11Support [ tcl tk libX11 xproto ]
39 ++ optionals stdenv.isDarwin [ CF configd ];
40
41 hasDistutilsCxxPatch = !(stdenv.cc.isGNU or false);
42
43in stdenv.mkDerivation {
44 name = "python3-${version}";
45 pythonVersion = majorVersion;
46 inherit majorVersion version;
47
48 inherit buildInputs;
49
50 src = fetchurl {
51 url = "https://www.python.org/ftp/python/${majorVersion}.${minorVersion}/Python-${version}.tar.xz";
52 sha256 = "0pqmf51zy2lzhbaj4yya2py2qr653j9152d0rg3p7wi1yl2dwp7m";
53 };
54
55 NIX_LDFLAGS = optionalString stdenv.isLinux "-lgcc_s";
56
57 # Determinism: The interpreter is patched to write null timestamps when compiling python files.
58 # This way python doesn't try to update them when we freeze timestamps in nix store.
59 DETERMINISTIC_BUILD=1;
60 # Determinism: We fix the hashes of str, bytes and datetime objects.
61 PYTHONHASHSEED=0;
62
63 prePatch = optionalString stdenv.isDarwin ''
64 substituteInPlace configure --replace '`/usr/bin/arch`' '"i386"'
65 substituteInPlace configure --replace '-Wl,-stack_size,1000000' ' '
66 '';
67
68 patches = [
69 ./no-ldconfig.patch
70 ./ld_library_path.patch
71 ] ++ optionals stdenv.isDarwin [
72 # Fix for https://bugs.python.org/issue24658
73 (fetchpatch {
74 url = "https://bugs.python.org/file45178/issue24658-3-3.6.diff";
75 sha256 = "1x060hs80nl34mcl2ji2i7l4shxkmxwgq8h8lcmav8rjqqz1nb4a";
76 })
77 ] ++ optionals (x11Support && stdenv.isDarwin) [
78 ./use-correct-tcl-tk-on-darwin.patch
79 ] ++ optionals hasDistutilsCxxPatch [
80 # Fix for http://bugs.python.org/issue1222585
81 # Upstream distutils is calling C compiler to compile C++ code, which
82 # only works for GCC and Apple Clang. This makes distutils to call C++
83 # compiler when needed.
84 (fetchpatch {
85 url = "https://bugs.python.org/file47046/python-3.x-distutils-C++.patch";
86 sha256 = "0dgdn9k2kmw4wh90vdnjcrnn97ylxgx7mbn9l87fwz6j501jqvk8";
87 extraPrefix = "";
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 ]
111 # Never even try to use lchmod on linux,
112 # don't rely on detecting glibc-isms.
113 ++ optional stdenv.hostPlatform.isLinux "ac_cv_func_lchmod=no";
114
115 preConfigure = ''
116 for i in /usr /sw /opt /pkg; do # improve purity
117 substituteInPlace ./setup.py --replace $i /no-such-path
118 done
119 ${optionalString stdenv.isDarwin ''
120 export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -msse2"
121 export MACOSX_DEPLOYMENT_TARGET=10.6
122 ''
123 + optionalString stdenv.hostPlatform.isMusl ''
124 export NIX_CFLAGS_COMPILE+=" -DTHREAD_STACK_SIZE=0x100000"
125 ''}
126 '';
127
128 setupHook = python-setup-hook sitePackages;
129
130 postInstall = ''
131 # needed for some packages, especially packages that backport functionality
132 # to 2.x from 3.x
133 for item in $out/lib/python${majorVersion}/test/*; do
134 if [[ "$item" != */test_support.py*
135 && "$item" != */test/support
136 && "$item" != */test/libregrtest
137 && "$item" != */test/regrtest.py* ]]; then
138 rm -rf "$item"
139 else
140 echo $item
141 fi
142 done
143 touch $out/lib/python${majorVersion}/test/__init__.py
144
145 ln -s "$out/include/python${majorVersion}m" "$out/include/python${majorVersion}"
146 paxmark E $out/bin/python${majorVersion}
147
148 # Python on Nix is not manylinux1 compatible. https://github.com/NixOS/nixpkgs/issues/18484
149 echo "manylinux1_compatible=False" >> $out/lib/${libPrefix}/_manylinux.py
150
151 # Determinism: Windows installers were not deterministic.
152 # We're also not interested in building Windows installers.
153 find "$out" -name 'wininst*.exe' | xargs -r rm -f
154
155 # Use Python3 as default python
156 ln -s "$out/bin/idle3" "$out/bin/idle"
157 ln -s "$out/bin/pydoc3" "$out/bin/pydoc"
158 ln -s "$out/bin/python3" "$out/bin/python"
159 ln -s "$out/bin/python3-config" "$out/bin/python-config"
160 ln -s "$out/lib/pkgconfig/python3.pc" "$out/lib/pkgconfig/python.pc"
161
162 # Get rid of retained dependencies on -dev packages, and remove
163 # some $TMPDIR references to improve binary reproducibility.
164 # Note that the .pyc file of _sysconfigdata.py should be regenerated!
165 for i in $out/lib/python${majorVersion}/_sysconfigdata.py $out/lib/python${majorVersion}/config-${majorVersion}m/Makefile; do
166 sed -i $i -e "s|-I/nix/store/[^ ']*||g" -e "s|-L/nix/store/[^ ']*||g" -e "s|$TMPDIR|/no-such-path|g"
167 done
168
169 # Determinism: rebuild all bytecode
170 # We exclude lib2to3 because that's Python 2 code which fails
171 # We rebuild three times, once for each optimization level
172 find $out -name "*.py" | $out/bin/python -m compileall -q -f -x "lib2to3" -i -
173 find $out -name "*.py" | $out/bin/python -O -m compileall -q -f -x "lib2to3" -i -
174 find $out -name "*.py" | $out/bin/python -OO -m compileall -q -f -x "lib2to3" -i -
175 '';
176
177 passthru = let
178 pythonPackages = callPackage ../../../../../top-level/python-packages.nix {python=self; overrides=packageOverrides;};
179 in rec {
180 inherit libPrefix sitePackages x11Support hasDistutilsCxxPatch;
181 executable = "${libPrefix}m";
182 buildEnv = callPackage ../../wrapper.nix { python = self; inherit (pythonPackages) requiredPythonModules; };
183 withPackages = import ../../with-packages.nix { inherit buildEnv pythonPackages;};
184 pkgs = pythonPackages;
185 isPy3 = true;
186 isPy35 = true;
187 interpreter = "${self}/bin/${executable}";
188 };
189
190 enableParallelBuilding = true;
191
192 doCheck = false; # expensive, and fails
193
194 meta = {
195 homepage = http://python.org;
196 description = "A high-level dynamically-typed programming language";
197 longDescription = ''
198 Python is a remarkably powerful dynamic programming language that
199 is used in a wide variety of application domains. Some of its key
200 distinguishing features include: clear, readable syntax; strong
201 introspection capabilities; intuitive object orientation; natural
202 expression of procedural code; full modularity, supporting
203 hierarchical packages; exception-based error handling; and very
204 high level dynamic data types.
205 '';
206 license = licenses.psfl;
207 platforms = with platforms; linux ++ darwin;
208 maintainers = with maintainers; [ fridh ];
209 };
210}