nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
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, callPackage
14, self
15, CF, configd, coreutils
16, python-setup-hook
17# Some proprietary libs assume UCS2 unicode, especially on darwin :(
18, ucsEncoding ? 4
19# For the Python package set
20, packageOverrides ? (self: super: {})
21, buildPackages
22, sourceVersion
23, sha256
24, passthruFun
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 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 ] ++ optionals (x11Support && stdenv.isDarwin) [
83 ./use-correct-tcl-tk-on-darwin.patch
84 ] ++ optionals stdenv.isLinux [
85
86 # Disable the use of ldconfig in ctypes.util.find_library (since
87 # ldconfig doesn't work on NixOS), and don't use
88 # ctypes.util.find_library during the loading of the uuid module
89 # (since it will do a futile invocation of gcc (!) to find
90 # libuuid, slowing down program startup a lot).
91 ./no-ldconfig.patch
92
93 ] ++ optionals stdenv.hostPlatform.isCygwin [
94 ./2.5.2-ctypes-util-find_library.patch
95 ./2.5.2-tkinter-x11.patch
96 ./2.6.2-ssl-threads.patch
97 ./2.6.5-export-PySignal_SetWakeupFd.patch
98 ./2.6.5-FD_SETSIZE.patch
99 ./2.6.5-ncurses-abi6.patch
100 ./2.7.3-dbm.patch
101 ./2.7.3-dylib.patch
102 ./2.7.3-getpath-exe-extension.patch
103 ./2.7.3-no-libm.patch
104 ] ++ optionals hasDistutilsCxxPatch [
105
106 # Patch from http://bugs.python.org/issue1222585 adapted to work with
107 # `patch -p1' and with a last hunk removed
108 # Upstream distutils is calling C compiler to compile C++ code, which
109 # only works for GCC and Apple Clang. This makes distutils to call C++
110 # compiler when needed.
111 ./python-2.7-distutils-C++.patch
112 ] ++ optional (stdenv.hostPlatform != stdenv.buildPlatform) [
113 ./cross-compile.patch
114 ];
115
116 preConfigure = ''
117 # Purity.
118 for i in /usr /sw /opt /pkg; do
119 substituteInPlace ./setup.py --replace $i /no-such-path
120 done
121 '' + optionalString (stdenv ? cc && stdenv.cc.libc != null) ''
122 for i in Lib/plat-*/regen; do
123 substituteInPlace $i --replace /usr/include/ ${stdenv.cc.libc}/include/
124 done
125 '' + optionalString stdenv.isDarwin ''
126 substituteInPlace configure --replace '`/usr/bin/arch`' '"i386"'
127 substituteInPlace Lib/multiprocessing/__init__.py \
128 --replace 'os.popen(comm)' 'os.popen("${coreutils}/bin/nproc")'
129 '';
130
131 configureFlags = [
132 "--enable-shared"
133 "--with-threads"
134 "--enable-unicode=ucs${toString ucsEncoding}"
135 ] ++ optionals (stdenv.hostPlatform.isCygwin || stdenv.hostPlatform.isAarch64) [
136 "--with-system-ffi"
137 ] ++ optionals stdenv.hostPlatform.isCygwin [
138 "--with-system-expat"
139 "ac_cv_func_bind_textdomain_codeset=yes"
140 ] ++ optionals stdenv.isDarwin [
141 "--disable-toolbox-glue"
142 ] ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
143 "PYTHON_FOR_BUILD=${getBin buildPackages.python}/bin/python"
144 "ac_cv_buggy_getaddrinfo=no"
145 # Assume little-endian IEEE 754 floating point when cross compiling
146 "ac_cv_little_endian_double=yes"
147 "ac_cv_big_endian_double=no"
148 "ac_cv_mixed_endian_double=no"
149 "ac_cv_x87_double_rounding=yes"
150 "ac_cv_tanh_preserves_zero_sign=yes"
151 # Generally assume that things are present and work
152 "ac_cv_posix_semaphores_enabled=yes"
153 "ac_cv_broken_sem_getvalue=no"
154 "ac_cv_wchar_t_signed=yes"
155 "ac_cv_rshift_extends_sign=yes"
156 "ac_cv_broken_nice=no"
157 "ac_cv_broken_poll=no"
158 "ac_cv_working_tzset=yes"
159 "ac_cv_have_long_long_format=yes"
160 "ac_cv_have_size_t_format=yes"
161 "ac_cv_computed_gotos=yes"
162 "ac_cv_file__dev_ptmx=yes"
163 "ac_cv_file__dev_ptc=yes"
164 ]
165 # Never even try to use lchmod on linux,
166 # don't rely on detecting glibc-isms.
167 ++ optional stdenv.hostPlatform.isLinux "ac_cv_func_lchmod=no";
168
169 buildInputs =
170 optional (stdenv ? cc && stdenv.cc.libc != null) stdenv.cc.libc ++
171 [ bzip2 openssl zlib ]
172 ++ optional (stdenv.hostPlatform.isCygwin || stdenv.hostPlatform.isAarch64) libffi
173 ++ optional stdenv.hostPlatform.isCygwin expat
174 ++ [ db gdbm ncurses sqlite readline ]
175 ++ optionals x11Support [ tcl tk xlibsWrapper libX11 ]
176 ++ optionals stdenv.isDarwin ([ CF ] ++ optional (configd != null) configd);
177 nativeBuildInputs =
178 optionals (stdenv.hostPlatform != stdenv.buildPlatform)
179 [ buildPackages.stdenv.cc buildPackages.python ];
180
181 mkPaths = paths: {
182 C_INCLUDE_PATH = makeSearchPathOutput "dev" "include" paths;
183 LIBRARY_PATH = makeLibraryPath paths;
184 };
185
186 # Python 2.7 needs this
187 crossCompileEnv = stdenv.lib.optionalAttrs (stdenv.hostPlatform != stdenv.buildPlatform)
188 { _PYTHON_HOST_PLATFORM = stdenv.hostPlatform.config; };
189
190 # Build the basic Python interpreter without modules that have
191 # external dependencies.
192
193in with passthru; stdenv.mkDerivation ({
194 pname = "python";
195 inherit version;
196
197 inherit src patches buildInputs nativeBuildInputs preConfigure configureFlags;
198
199 LDFLAGS = stdenv.lib.optionalString (!stdenv.isDarwin) "-lgcc_s";
200 inherit (mkPaths buildInputs) C_INCLUDE_PATH LIBRARY_PATH;
201
202 NIX_CFLAGS_COMPILE = optionalString stdenv.isDarwin "-msse2"
203 + optionalString stdenv.hostPlatform.isMusl " -DTHREAD_STACK_SIZE=0x100000";
204 DETERMINISTIC_BUILD = 1;
205
206 setupHook = python-setup-hook sitePackages;
207
208 postPatch = optionalString (x11Support && (tix != null)) ''
209 substituteInPlace "Lib/lib-tk/Tix.py" --replace "os.environ.get('TIX_LIBRARY')" "os.environ.get('TIX_LIBRARY') or '${tix}/lib'"
210 '';
211
212 postInstall =
213 ''
214 # needed for some packages, especially packages that backport
215 # functionality to 2.x from 3.x
216 for item in $out/lib/${libPrefix}/test/*; do
217 if [[ "$item" != */test_support.py*
218 && "$item" != */test/support
219 && "$item" != */test/regrtest.py* ]]; then
220 rm -rf "$item"
221 else
222 echo $item
223 fi
224 done
225 touch $out/lib/${libPrefix}/test/__init__.py
226 ln -s $out/lib/${libPrefix}/pdb.py $out/bin/pdb
227 ln -s $out/lib/${libPrefix}/pdb.py $out/bin/pdb${sourceVersion.major}.${sourceVersion.minor}
228 ln -s $out/share/man/man1/{python2.7.1.gz,python.1.gz}
229
230 # Python on Nix is not manylinux1 compatible. https://github.com/NixOS/nixpkgs/issues/18484
231 echo "manylinux1_compatible=False" >> $out/lib/${libPrefix}/_manylinux.py
232
233 rm "$out"/lib/python*/plat-*/regen # refers to glibc.dev
234
235 # Determinism: Windows installers were not deterministic.
236 # We're also not interested in building Windows installers.
237 find "$out" -name 'wininst*.exe' | xargs -r rm -f
238 '' + optionalString (stdenv.hostPlatform == stdenv.buildPlatform)
239 ''
240 # Determinism: rebuild all bytecode
241 # We exclude lib2to3 because that's Python 2 code which fails
242 # We rebuild three times, once for each optimization level
243 find $out -name "*.py" | $out/bin/python -m compileall -q -f -x "lib2to3" -i -
244 find $out -name "*.py" | $out/bin/python -O -m compileall -q -f -x "lib2to3" -i -
245 find $out -name "*.py" | $out/bin/python -OO -m compileall -q -f -x "lib2to3" -i -
246 '' + optionalString stdenv.hostPlatform.isCygwin ''
247 cp libpython2.7.dll.a $out/lib
248 '';
249
250 inherit passthru;
251
252 enableParallelBuilding = true;
253
254 doCheck = false; # expensive, and fails
255
256 meta = {
257 homepage = http://python.org;
258 description = "A high-level dynamically-typed programming language";
259 longDescription = ''
260 Python is a remarkably powerful dynamic programming language that
261 is used in a wide variety of application domains. Some of its key
262 distinguishing features include: clear, readable syntax; strong
263 introspection capabilities; intuitive object orientation; natural
264 expression of procedural code; full modularity, supporting
265 hierarchical packages; exception-based error handling; and very
266 high level dynamic data types.
267 '';
268 license = stdenv.lib.licenses.psfl;
269 platforms = stdenv.lib.platforms.all;
270 maintainers = with stdenv.lib.maintainers; [ fridh ];
271 # Higher priority than Python 3.x so that `/bin/python` points to `/bin/python2`
272 # in case both 2 and 3 are installed.
273 priority = -100;
274 };
275 } // crossCompileEnv)