1{ stdenv, fetchurl
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.4";
30 minorVersion = "8";
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 = "http://www.python.org/ftp/python/${version}/Python-${version}.tar.xz";
51 sha256 = "1sn3i9z9m56inlfrqs250qv8snl8w211wpig2pfjlyrcj3x75919";
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 ./ld_library_path.patch
70 ];
71
72 postPatch = ''
73 # Determinism
74 substituteInPlace "Lib/py_compile.py" --replace "source_stats['mtime']" "(1 if 'DETERMINISTIC_BUILD' in os.environ else source_stats['mtime'])"
75 # Determinism. This is done unconditionally
76 substituteInPlace "Lib/importlib/_bootstrap.py" --replace "source_mtime = int(source_stats['mtime'])" "source_mtime = 1"
77 '' + optionalString (x11Support && (tix != null)) ''
78 substituteInPlace "Lib/tkinter/tix.py" --replace "os.environ.get('TIX_LIBRARY')" "os.environ.get('TIX_LIBRARY') or '${tix}/lib'"
79 ''
80 # Avoid picking up getentropy() from glibc >= 2.25, as that would break
81 # on older kernels. http://bugs.python.org/issue29157
82 + optionalString stdenv.isLinux ''
83 substituteInPlace Python/random.c --replace 'defined(HAVE_GETENTROPY)' '0'
84 cat Python/random.c
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 ];
98
99 preConfigure = ''
100 for i in /usr /sw /opt /pkg; do # improve purity
101 substituteInPlace ./setup.py --replace $i /no-such-path
102 done
103 ${optionalString stdenv.isDarwin ''
104 export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -msse2"
105 export MACOSX_DEPLOYMENT_TARGET=10.6
106 ''}
107 '';
108
109 setupHook = python-setup-hook sitePackages;
110
111 postInstall = ''
112 # needed for some packages, especially packages that backport functionality
113 # to 2.x from 3.x
114 for item in $out/lib/python${majorVersion}/test/*; do
115 if [[ "$item" != */test_support.py*
116 && "$item" != */test/support
117 && "$item" != */test/libregrtest
118 && "$item" != */test/regrtest.py* ]]; then
119 rm -rf "$item"
120 else
121 echo $item
122 fi
123 done
124 touch $out/lib/python${majorVersion}/test/__init__.py
125
126 ln -s "$out/include/python${majorVersion}m" "$out/include/python${majorVersion}"
127 paxmark E $out/bin/python${majorVersion}
128
129 # Python on Nix is not manylinux1 compatible. https://github.com/NixOS/nixpkgs/issues/18484
130 echo "manylinux1_compatible=False" >> $out/lib/${libPrefix}/_manylinux.py
131
132 # Determinism: Windows installers were not deterministic.
133 # We're also not interested in building Windows installers.
134 find "$out" -name 'wininst*.exe' | xargs -r rm -f
135
136 # Use Python3 as default python
137 ln -s "$out/bin/idle3" "$out/bin/idle"
138 ln -s "$out/bin/pydoc3" "$out/bin/pydoc"
139 ln -s "$out/bin/python3" "$out/bin/python"
140 ln -s "$out/bin/python3-config" "$out/bin/python-config"
141 ln -s "$out/lib/pkgconfig/python3.pc" "$out/lib/pkgconfig/python.pc"
142
143 # Get rid of retained dependencies on -dev packages, and remove
144 # some $TMPDIR references to improve binary reproducibility.
145 # Note that the .pyc file of _sysconfigdata.py should be regenerated!
146 for i in $out/lib/python${majorVersion}/_sysconfigdata.py $out/lib/python${majorVersion}/config-${majorVersion}m/Makefile; do
147 sed -i $i -e "s|-I/nix/store/[^ ']*||g" -e "s|-L/nix/store/[^ ']*||g" -e "s|$TMPDIR|/no-such-path|g"
148 done
149
150 # Determinism: rebuild all bytecode
151 # We exclude lib2to3 because that's Python 2 code which fails
152 # We rebuild three times, once for each optimization level
153 find $out -name "*.py" | $out/bin/python -m compileall -q -f -x "lib2to3" -i -
154 find $out -name "*.py" | $out/bin/python -O -m compileall -q -f -x "lib2to3" -i -
155 find $out -name "*.py" | $out/bin/python -OO -m compileall -q -f -x "lib2to3" -i -
156 '';
157
158 passthru = let
159 pythonPackages = callPackage ../../../../../top-level/python-packages.nix {python=self; overrides=packageOverrides;};
160 in rec {
161 inherit libPrefix sitePackages x11Support;
162 executable = "${libPrefix}m";
163 buildEnv = callPackage ../../wrapper.nix { python = self; inherit (pythonPackages) requiredPythonModules; };
164 withPackages = import ../../with-packages.nix { inherit buildEnv pythonPackages;};
165 pkgs = pythonPackages;
166 isPy3 = true;
167 isPy34 = true;
168 is_py3k = true; # deprecated
169 interpreter = "${self}/bin/${executable}";
170 };
171
172 enableParallelBuilding = true;
173
174 meta = {
175 homepage = http://python.org;
176 description = "A high-level dynamically-typed programming language";
177 longDescription = ''
178 Python is a remarkably powerful dynamic programming language that
179 is used in a wide variety of application domains. Some of its key
180 distinguishing features include: clear, readable syntax; strong
181 introspection capabilities; intuitive object orientation; natural
182 expression of procedural code; full modularity, supporting
183 hierarchical packages; exception-based error handling; and very
184 high level dynamic data types.
185 '';
186 license = licenses.psfl;
187 platforms = with platforms; linux ++ darwin;
188 maintainers = with maintainers; [ fridh ];
189 };
190}