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