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, pkgs, 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 = "4";
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
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 = "0k68ai0a204piwibz013ds6ck7hgj9gk4nin2259y41vpgx3pncl";
52 };
53
54 NIX_LDFLAGS = optionalString stdenv.isLinux "-lgcc_s";
55
56 # Determinism: The interpreter is patched to write null timestamps when compiling python files.
57 # This way python doesn't try to update them when we freeze timestamps in nix store.
58 DETERMINISTIC_BUILD=1;
59 # Determinism: We fix the hashes of str, bytes and datetime objects.
60 PYTHONHASHSEED=0;
61
62 prePatch = optionalString stdenv.isDarwin ''
63 substituteInPlace configure --replace '`/usr/bin/arch`' '"i386"'
64 substituteInPlace configure --replace '-Wl,-stack_size,1000000' ' '
65 '';
66
67 patches = [
68 ./no-ldconfig.patch
69 ];
70
71 postPatch = ''
72 # Determinism
73 substituteInPlace "Lib/py_compile.py" --replace "source_stats['mtime']" "(1 if 'DETERMINISTIC_BUILD' in os.environ else source_stats['mtime'])"
74 # Determinism. This is done unconditionally
75 substituteInPlace "Lib/importlib/_bootstrap_external.py" --replace "source_mtime = int(st['mtime'])" "source_mtime = 1"
76 '' + optionalString (x11Support && (tix != null)) ''
77 substituteInPlace "Lib/tkinter/tix.py" --replace "os.environ.get('TIX_LIBRARY')" "os.environ.get('TIX_LIBRARY') or '${tix}/lib'"
78 '';
79
80 CPPFLAGS="${concatStringsSep " " (map (p: "-I${getDev p}/include") buildInputs)}";
81 LDFLAGS="${concatStringsSep " " (map (p: "-L${getLib p}/lib") buildInputs)}";
82 LIBS="${optionalString (!stdenv.isDarwin) "-lcrypt"} ${optionalString (ncurses != null) "-lncurses"}";
83
84 configureFlags = [
85 "--enable-shared"
86 "--with-threads"
87 "--without-ensurepip"
88 "--with-system-expat"
89 "--with-system-ffi"
90 ];
91
92 preConfigure = ''
93 for i in /usr /sw /opt /pkg; do # improve purity
94 substituteInPlace ./setup.py --replace $i /no-such-path
95 done
96 ${optionalString stdenv.isDarwin ''
97 export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -msse2"
98 export MACOSX_DEPLOYMENT_TARGET=10.6
99 ''}
100 '';
101
102 setupHook = python-setup-hook sitePackages;
103
104 postInstall = ''
105 # needed for some packages, especially packages that backport functionality
106 # to 2.x from 3.x
107 for item in $out/lib/python${majorVersion}/test/*; do
108 if [[ "$item" != */test_support.py*
109 && "$item" != */test/support
110 && "$item" != */test/libregrtest
111 && "$item" != */test/regrtest.py* ]]; then
112 rm -rf "$item"
113 else
114 echo $item
115 fi
116 done
117 touch $out/lib/python${majorVersion}/test/__init__.py
118
119 ln -s "$out/include/python${majorVersion}m" "$out/include/python${majorVersion}"
120 paxmark E $out/bin/python${majorVersion}
121
122 # Python on Nix is not manylinux1 compatible. https://github.com/NixOS/nixpkgs/issues/18484
123 echo "manylinux1_compatible=False" >> $out/lib/${libPrefix}/_manylinux.py
124
125 # Determinism: Windows installers were not deterministic.
126 # We're also not interested in building Windows installers.
127 find "$out" -name 'wininst*.exe' | xargs -r rm -f
128
129 # Use Python3 as default python
130 ln -s "$out/bin/idle3" "$out/bin/idle"
131 ln -s "$out/bin/pydoc3" "$out/bin/pydoc"
132 ln -s "$out/bin/python3" "$out/bin/python"
133 ln -s "$out/bin/python3-config" "$out/bin/python-config"
134 ln -s "$out/lib/pkgconfig/python3.pc" "$out/lib/pkgconfig/python.pc"
135
136 # Get rid of retained dependencies on -dev packages, and remove
137 # some $TMPDIR references to improve binary reproducibility.
138 # Note that the .pyc file of _sysconfigdata.py should be regenerated!
139 for i in $out/lib/python${majorVersion}/_sysconfigdata.py $out/lib/python${majorVersion}/config-${majorVersion}m/Makefile; do
140 sed -i $i -e "s|-I/nix/store/[^ ']*||g" -e "s|-L/nix/store/[^ ']*||g" -e "s|$TMPDIR|/no-such-path|g"
141 done
142
143 # Determinism: rebuild all bytecode
144 # We exclude lib2to3 because that's Python 2 code which fails
145 # We rebuild three times, once for each optimization level
146 find $out -name "*.py" | $out/bin/python -m compileall -q -f -x "lib2to3" -i -
147 find $out -name "*.py" | $out/bin/python -O -m compileall -q -f -x "lib2to3" -i -
148 find $out -name "*.py" | $out/bin/python -OO -m compileall -q -f -x "lib2to3" -i -
149 '';
150
151 passthru = let
152 pythonPackages = callPackage ../../../../../top-level/python-packages.nix {python=self; overrides=packageOverrides;};
153 in rec {
154 inherit libPrefix sitePackages x11Support;
155 executable = "${libPrefix}m";
156 buildEnv = callPackage ../../wrapper.nix { python = self; };
157 withPackages = import ../../with-packages.nix { inherit buildEnv pythonPackages;};
158 pkgs = pythonPackages;
159 isPy3 = true;
160 isPy35 = true;
161 interpreter = "${self}/bin/${executable}";
162 };
163
164 enableParallelBuilding = true;
165
166 meta = {
167 homepage = http://python.org;
168 description = "A high-level dynamically-typed programming language";
169 longDescription = ''
170 Python is a remarkably powerful dynamic programming language that
171 is used in a wide variety of application domains. Some of its key
172 distinguishing features include: clear, readable syntax; strong
173 introspection capabilities; intuitive object orientation; natural
174 expression of procedural code; full modularity, supporting
175 hierarchical packages; exception-based error handling; and very
176 high level dynamic data types.
177 '';
178 license = licenses.psfl;
179 platforms = with platforms; linux ++ darwin;
180 maintainers = with maintainers; [ chaoflow domenkozar cstrahan ];
181 };
182}