1{ stdenv, hostPlatform, buildPlatform, buildPackages, fetchurl
2, bzip2
3, gdbm
4, fetchpatch
5, ncurses
6, openssl
7, readline
8, sqlite
9, tcl ? null, tk ? null, tix ? null, xlibsWrapper ? null, libX11 ? null, x11Support ? false
10, zlib
11, callPackage
12, self
13, gettext
14, db
15, expat
16, libffi
17, CF, configd, coreutils
18, python-setup-hook
19# Some proprietary libs assume UCS2 unicode, especially on darwin :(
20, ucsEncoding ? 4
21# For the Python package set
22, pkgs, packageOverrides ? (self: super: {})
23}:
24
25assert x11Support -> tcl != null
26 && tk != null
27 && xlibsWrapper != null
28 && libX11 != null;
29
30with stdenv.lib;
31
32let
33 majorVersion = "2.7";
34 minorVersion = "14";
35 minorVersionSuffix = "";
36 pythonVersion = majorVersion;
37 version = "${majorVersion}.${minorVersion}${minorVersionSuffix}";
38 libPrefix = "python${majorVersion}";
39 sitePackages = "lib/${libPrefix}/site-packages";
40
41 src = fetchurl {
42 url = "https://www.python.org/ftp/python/${majorVersion}.${minorVersion}/Python-${version}.tar.xz";
43 sha256 = "0rka541ys16jwzcnnvjp2v12m4cwgd2jp6wj4kj511p715pb5zvi";
44 };
45
46 hasDistutilsCxxPatch = !(stdenv.cc.isGNU or false);
47 patches =
48 [ # Look in C_INCLUDE_PATH and LIBRARY_PATH for stuff.
49 ./search-path.patch
50
51 # Python recompiles a Python if the mtime stored *in* the
52 # pyc/pyo file differs from the mtime of the source file. This
53 # doesn't work in Nix because Nix changes the mtime of files in
54 # the Nix store to 1. So treat that as a special case.
55 ./nix-store-mtime.patch
56
57 # patch python to put zero timestamp into pyc
58 # if DETERMINISTIC_BUILD env var is set
59 ./deterministic-build.patch
60
61 ./properly-detect-curses.patch
62
63 ] ++ optionals stdenv.isLinux [
64
65 # Disable the use of ldconfig in ctypes.util.find_library (since
66 # ldconfig doesn't work on NixOS), and don't use
67 # ctypes.util.find_library during the loading of the uuid module
68 # (since it will do a futile invocation of gcc (!) to find
69 # libuuid, slowing down program startup a lot).
70 ./no-ldconfig.patch
71
72 ] ++ optionals hostPlatform.isCygwin [
73 ./2.5.2-ctypes-util-find_library.patch
74 ./2.5.2-tkinter-x11.patch
75 ./2.6.2-ssl-threads.patch
76 ./2.6.5-export-PySignal_SetWakeupFd.patch
77 ./2.6.5-FD_SETSIZE.patch
78 ./2.6.5-ncurses-abi6.patch
79 ./2.7.3-dbm.patch
80 ./2.7.3-dylib.patch
81 ./2.7.3-getpath-exe-extension.patch
82 ./2.7.3-no-libm.patch
83 ] ++ optionals hasDistutilsCxxPatch [
84
85 # Patch from http://bugs.python.org/issue1222585 adapted to work with
86 # `patch -p1' and with a last hunk removed
87 # Upstream distutils is calling C compiler to compile C++ code, which
88 # only works for GCC and Apple Clang. This makes distutils to call C++
89 # compiler when needed.
90 ./python-2.7-distutils-C++.patch
91 ];
92
93 preConfigure = ''
94 # Purity.
95 for i in /usr /sw /opt /pkg; do
96 substituteInPlace ./setup.py --replace $i /no-such-path
97 done
98 '' + optionalString (stdenv ? cc && stdenv.cc.libc != null) ''
99 for i in Lib/plat-*/regen; do
100 substituteInPlace $i --replace /usr/include/ ${stdenv.cc.libc}/include/
101 done
102 '' + optionalString stdenv.isDarwin ''
103 substituteInPlace configure --replace '`/usr/bin/arch`' '"i386"'
104 substituteInPlace Lib/multiprocessing/__init__.py \
105 --replace 'os.popen(comm)' 'os.popen("${coreutils}/bin/nproc")'
106 '';
107
108 configureFlags = [
109 "--enable-shared"
110 "--with-threads"
111 "--enable-unicode=ucs${toString ucsEncoding}"
112 ] ++ optionals (hostPlatform.isCygwin || hostPlatform.isAarch64) [
113 "--with-system-ffi"
114 ] ++ optionals hostPlatform.isCygwin [
115 "--with-system-expat"
116 "ac_cv_func_bind_textdomain_codeset=yes"
117 ] ++ optionals stdenv.isDarwin [
118 "--disable-toolbox-glue"
119 ] ++ optionals (hostPlatform != buildPlatform) [
120 "PYTHON_FOR_BUILD=${getBin buildPackages.python}/bin/python"
121 "ac_cv_buggy_getaddrinfo=no"
122 # Assume little-endian IEEE 754 floating point when cross compiling
123 "ac_cv_little_endian_double=yes"
124 "ac_cv_big_endian_double=no"
125 "ac_cv_mixed_endian_double=no"
126 "ac_cv_x87_double_rounding=yes"
127 "ac_cv_tanh_preserves_zero_sign=yes"
128 # Generally assume that things are present and work
129 "ac_cv_posix_semaphores_enabled=yes"
130 "ac_cv_broken_sem_getvalue=no"
131 "ac_cv_wchar_t_signed=yes"
132 "ac_cv_rshift_extends_sign=yes"
133 "ac_cv_broken_nice=no"
134 "ac_cv_broken_poll=no"
135 "ac_cv_working_tzset=yes"
136 "ac_cv_have_long_long_format=yes"
137 "ac_cv_have_size_t_format=yes"
138 "ac_cv_computed_gotos=yes"
139 "ac_cv_file__dev_ptmx=yes"
140 "ac_cv_file__dev_ptc=yes"
141 ];
142
143 postConfigure = if hostPlatform.isCygwin then ''
144 sed -i Makefile -e 's,PYTHONPATH="$(srcdir),PYTHONPATH="$(abs_srcdir),'
145 '' else null;
146
147 buildInputs =
148 optional (stdenv ? cc && stdenv.cc.libc != null) stdenv.cc.libc ++
149 [ bzip2 openssl zlib ]
150 ++ optional (hostPlatform.isCygwin || hostPlatform.isAarch64) libffi
151 ++ optional hostPlatform.isCygwin expat
152 ++ [ db gdbm ncurses sqlite readline ]
153 ++ optionals x11Support [ tcl tk xlibsWrapper libX11 ]
154 ++ optionals stdenv.isDarwin ([ CF ] ++ optional (configd != null) configd);
155 nativeBuildInputs =
156 optionals (hostPlatform != buildPlatform)
157 [ buildPackages.stdenv.cc buildPackages.python ];
158
159 mkPaths = paths: {
160 C_INCLUDE_PATH = makeSearchPathOutput "dev" "include" paths;
161 LIBRARY_PATH = makeLibraryPath paths;
162 };
163
164 # Build the basic Python interpreter without modules that have
165 # external dependencies.
166
167in stdenv.mkDerivation {
168 name = "python-${version}";
169 pythonVersion = majorVersion;
170
171 inherit majorVersion version src patches buildInputs nativeBuildInputs
172 preConfigure configureFlags;
173
174 LDFLAGS = stdenv.lib.optionalString (!stdenv.isDarwin) "-lgcc_s";
175 inherit (mkPaths buildInputs) C_INCLUDE_PATH LIBRARY_PATH;
176
177 NIX_CFLAGS_COMPILE = optionalString stdenv.isDarwin "-msse2";
178 DETERMINISTIC_BUILD = 1;
179
180 setupHook = python-setup-hook sitePackages;
181
182 postPatch = optionalString (x11Support && (tix != null)) ''
183 substituteInPlace "Lib/lib-tk/Tix.py" --replace "os.environ.get('TIX_LIBRARY')" "os.environ.get('TIX_LIBRARY') or '${tix}/lib'"
184 '';
185
186 postInstall =
187 ''
188 # needed for some packages, especially packages that backport
189 # functionality to 2.x from 3.x
190 for item in $out/lib/python${majorVersion}/test/*; do
191 if [[ "$item" != */test_support.py*
192 && "$item" != */test/support
193 && "$item" != */test/regrtest.py* ]]; then
194 rm -rf "$item"
195 else
196 echo $item
197 fi
198 done
199 touch $out/lib/python${majorVersion}/test/__init__.py
200 ln -s $out/lib/python${majorVersion}/pdb.py $out/bin/pdb
201 ln -s $out/lib/python${majorVersion}/pdb.py $out/bin/pdb${majorVersion}
202 ln -s $out/share/man/man1/{python2.7.1.gz,python.1.gz}
203
204 paxmark E $out/bin/python${majorVersion}
205
206 # Python on Nix is not manylinux1 compatible. https://github.com/NixOS/nixpkgs/issues/18484
207 echo "manylinux1_compatible=False" >> $out/lib/${libPrefix}/_manylinux.py
208
209 rm "$out"/lib/python*/plat-*/regen # refers to glibc.dev
210
211 # Determinism: Windows installers were not deterministic.
212 # We're also not interested in building Windows installers.
213 find "$out" -name 'wininst*.exe' | xargs -r rm -f
214 '' + optionalString (stdenv.hostPlatform == stdenv.buildPlatform)
215 ''
216 # Determinism: rebuild all bytecode
217 # We exclude lib2to3 because that's Python 2 code which fails
218 # We rebuild three times, once for each optimization level
219 find $out -name "*.py" | $out/bin/python -m compileall -q -f -x "lib2to3" -i -
220 find $out -name "*.py" | $out/bin/python -O -m compileall -q -f -x "lib2to3" -i -
221 find $out -name "*.py" | $out/bin/python -OO -m compileall -q -f -x "lib2to3" -i -
222 '' + optionalString hostPlatform.isCygwin ''
223 cp libpython2.7.dll.a $out/lib
224 '';
225
226 passthru = let
227 pythonPackages = callPackage ../../../../../top-level/python-packages.nix {python=self; overrides=packageOverrides;};
228 in rec {
229 inherit libPrefix sitePackages x11Support hasDistutilsCxxPatch ucsEncoding;
230 executable = libPrefix;
231 buildEnv = callPackage ../../wrapper.nix { python = self; inherit (pythonPackages) requiredPythonModules; };
232 withPackages = import ../../with-packages.nix { inherit buildEnv pythonPackages;};
233 pkgs = pythonPackages;
234 isPy2 = true;
235 isPy27 = true;
236 interpreter = "${self}/bin/${executable}";
237 };
238
239 enableParallelBuilding = true;
240
241 meta = {
242 homepage = http://python.org;
243 description = "A high-level dynamically-typed programming language";
244 longDescription = ''
245 Python is a remarkably powerful dynamic programming language that
246 is used in a wide variety of application domains. Some of its key
247 distinguishing features include: clear, readable syntax; strong
248 introspection capabilities; intuitive object orientation; natural
249 expression of procedural code; full modularity, supporting
250 hierarchical packages; exception-based error handling; and very
251 high level dynamic data types.
252 '';
253 license = stdenv.lib.licenses.psfl;
254 platforms = stdenv.lib.platforms.all;
255 maintainers = with stdenv.lib.maintainers; [ fridh ];
256 # Higher priority than Python 3.x so that `/bin/python` points to `/bin/python2`
257 # in case both 2 and 3 are installed.
258 priority = -100;
259 };
260 }