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;
25with stdenv.lib;
26
27let
28 majorVersion = "3.7";
29 minorVersion = "0";
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 hasDistutilsCxxPatch = !(stdenv.cc.isGNU or false);
41
42in stdenv.mkDerivation {
43 name = "python3-${version}";
44 pythonVersion = majorVersion;
45 inherit majorVersion version;
46
47 inherit buildInputs;
48
49 src = fetchurl {
50 url = "https://www.python.org/ftp/python/${majorVersion}.${minorVersion}/Python-${version}.tar.xz";
51 sha256 = "0j9mic5c9lbd2b20wka7hily7szz740wy9ilfrczxap63rnrk0h3";
52 };
53
54 NIX_LDFLAGS = optionalString stdenv.isLinux "-lgcc_s";
55
56 # Determinism: We fix the hashes of str, bytes and datetime objects.
57 PYTHONHASHSEED=0;
58
59 prePatch = optionalString stdenv.isDarwin ''
60 substituteInPlace configure --replace '`/usr/bin/arch`' '"i386"'
61 substituteInPlace configure --replace '-Wl,-stack_size,1000000' ' '
62 '';
63
64 patches = [
65 ./no-ldconfig.patch
66 # Fix darwin build https://bugs.python.org/issue34027
67 (fetchpatch {
68 url = https://bugs.python.org/file47666/darwin-libutil.patch;
69 sha256 = "0242gihnw3wfskl4fydp2xanpl8k5q7fj4dp7dbbqf46a4iwdzpa";
70 })
71 ] ++ optionals hasDistutilsCxxPatch [
72 # Fix for http://bugs.python.org/issue1222585
73 # Upstream distutils is calling C compiler to compile C++ code, which
74 # only works for GCC and Apple Clang. This makes distutils to call C++
75 # compiler when needed.
76 (fetchpatch {
77 url = "https://bugs.python.org/file47669/python-3.8-distutils-C++.patch";
78 sha256 = "0s801d7ww9yrk6ys053jvdhl0wicbznx08idy36f1nrrxsghb3ii";
79 })
80 ];
81
82 postPatch = ''
83 '' + optionalString (x11Support && (tix != null)) ''
84 substituteInPlace "Lib/tkinter/tix.py" --replace "os.environ.get('TIX_LIBRARY')" "os.environ.get('TIX_LIBRARY') or '${tix}/lib'"
85 '';
86
87 CPPFLAGS="${concatStringsSep " " (map (p: "-I${getDev p}/include") buildInputs)}";
88 LDFLAGS="${concatStringsSep " " (map (p: "-L${getLib p}/lib") buildInputs)}";
89 LIBS="${optionalString (!stdenv.isDarwin) "-lcrypt"} ${optionalString (ncurses != null) "-lncurses"}";
90
91 configureFlags = [
92 "--enable-shared"
93 "--with-threads"
94 "--without-ensurepip"
95 "--with-system-expat"
96 "--with-system-ffi"
97 "--with-openssl=${openssl.dev}"
98 ];
99
100 preConfigure = ''
101 for i in /usr /sw /opt /pkg; do # improve purity
102 substituteInPlace ./setup.py --replace $i /no-such-path
103 done
104 ${optionalString stdenv.isDarwin ''
105 export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -msse2"
106 export MACOSX_DEPLOYMENT_TARGET=10.6
107 ''}
108 '';
109
110 setupHook = python-setup-hook sitePackages;
111
112 postInstall = ''
113 # needed for some packages, especially packages that backport functionality
114 # to 2.x from 3.x
115 for item in $out/lib/python${majorVersion}/test/*; do
116 if [[ "$item" != */test_support.py*
117 && "$item" != */test/support
118 && "$item" != */test/libregrtest
119 && "$item" != */test/regrtest.py* ]]; then
120 rm -rf "$item"
121 else
122 echo $item
123 fi
124 done
125 touch $out/lib/python${majorVersion}/test/__init__.py
126
127 ln -s "$out/include/python${majorVersion}m" "$out/include/python${majorVersion}"
128 paxmark E $out/bin/python${majorVersion}
129
130 # Python on Nix is not manylinux1 compatible. https://github.com/NixOS/nixpkgs/issues/18484
131 echo "manylinux1_compatible=False" >> $out/lib/${libPrefix}/_manylinux.py
132
133 # Determinism: Windows installers were not deterministic.
134 # We're also not interested in building Windows installers.
135 find "$out" -name 'wininst*.exe' | xargs -r rm -f
136
137 # Use Python3 as default python
138 ln -s "$out/bin/idle3" "$out/bin/idle"
139 ln -s "$out/bin/pydoc3" "$out/bin/pydoc"
140 ln -s "$out/bin/python3" "$out/bin/python"
141 ln -s "$out/bin/python3-config" "$out/bin/python-config"
142 ln -s "$out/lib/pkgconfig/python3.pc" "$out/lib/pkgconfig/python.pc"
143
144 # Get rid of retained dependencies on -dev packages, and remove
145 # some $TMPDIR references to improve binary reproducibility.
146 # Note that the .pyc file of _sysconfigdata.py should be regenerated!
147 for i in $out/lib/python${majorVersion}/_sysconfigdata*.py $out/lib/python${majorVersion}/config-${majorVersion}m*/Makefile; do
148 sed -i $i -e "s|-I/nix/store/[^ ']*||g" -e "s|-L/nix/store/[^ ']*||g" -e "s|$TMPDIR|/no-such-path|g"
149 done
150
151 # Determinism: rebuild all bytecode
152 # We exclude lib2to3 because that's Python 2 code which fails
153 # We rebuild three times, once for each optimization level
154 # Python 3.7 implements PEP 552, introducing support for deterministic bytecode.
155 # This is automatically used when `SOURCE_DATE_EPOCH` is set.
156 find $out -name "*.py" | $out/bin/python -m compileall -q -f -x "lib2to3" -i -
157 find $out -name "*.py" | $out/bin/python -O -m compileall -q -f -x "lib2to3" -i -
158 find $out -name "*.py" | $out/bin/python -OO -m compileall -q -f -x "lib2to3" -i -
159 '';
160
161 passthru = let
162 pythonPackages = callPackage ../../../../../top-level/python-packages.nix {python=self; overrides=packageOverrides;};
163 in rec {
164 inherit libPrefix sitePackages x11Support hasDistutilsCxxPatch;
165 executable = "${libPrefix}m";
166 buildEnv = callPackage ../../wrapper.nix { python = self; inherit (pythonPackages) requiredPythonModules; };
167 withPackages = import ../../with-packages.nix { inherit buildEnv pythonPackages;};
168 pkgs = pythonPackages;
169 isPy3 = true;
170 isPy37 = true;
171 is_py3k = true; # deprecated
172 interpreter = "${self}/bin/${executable}";
173 };
174
175 enableParallelBuilding = true;
176
177 meta = {
178 homepage = http://python.org;
179 description = "A high-level dynamically-typed programming language";
180 longDescription = ''
181 Python is a remarkably powerful dynamic programming language that
182 is used in a wide variety of application domains. Some of its key
183 distinguishing features include: clear, readable syntax; strong
184 introspection capabilities; intuitive object orientation; natural
185 expression of procedural code; full modularity, supporting
186 hierarchical packages; exception-based error handling; and very
187 high level dynamic data types.
188 '';
189 license = licenses.psfl;
190 platforms = with platforms; linux ++ darwin;
191 maintainers = with maintainers; [ fridh kragniz ];
192 };
193}