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