1{ stdenv, fetchPypi, python, buildPythonPackage, pycairo, backports_functools_lru_cache
2, which, cycler, dateutil, nose, numpy, pyparsing, sphinx, tornado, kiwisolver
3, freetype, libpng, pkgconfig, mock, pytz, pygobject3, gobject-introspection, functools32, subprocess32
4, fetchpatch
5, enableGhostscript ? false, ghostscript ? null, gtk3
6, enableGtk3 ? false, cairo
7# darwin has its own "MacOSX" backend
8, enableTk ? !stdenv.isDarwin, tcl ? null, tk ? null, tkinter ? null, libX11 ? null
9, enableQt ? false, pyqt4
10, libcxx
11, Cocoa
12, pythonOlder
13}:
14
15assert enableGhostscript -> ghostscript != null;
16assert enableTk -> (tcl != null)
17 && (tk != null)
18 && (tkinter != null)
19 && (libX11 != null)
20 ;
21assert enableQt -> pyqt4 != null;
22
23buildPythonPackage rec {
24 version = "2.2.3";
25 pname = "matplotlib";
26
27 src = fetchPypi {
28 inherit pname version;
29 sha256 = "7355bf757ecacd5f0ac9dd9523c8e1a1103faadf8d33c22664178e17533f8ce5";
30 };
31
32 NIX_CFLAGS_COMPILE = stdenv.lib.optionalString stdenv.isDarwin "-I${libcxx}/include/c++/v1";
33
34 XDG_RUNTIME_DIR = "/tmp";
35
36 nativeBuildInputs = [ pkgconfig ];
37
38 buildInputs = [ python which sphinx stdenv ]
39 ++ stdenv.lib.optional enableGhostscript ghostscript
40 ++ stdenv.lib.optional stdenv.isDarwin [ Cocoa ];
41
42 propagatedBuildInputs =
43 [ cycler dateutil nose numpy pyparsing tornado freetype kiwisolver
44 libpng mock pytz ]
45 ++ stdenv.lib.optional (pythonOlder "3.3") backports_functools_lru_cache
46 ++ stdenv.lib.optionals enableGtk3 [ cairo pycairo gtk3 gobject-introspection pygobject3 ]
47 ++ stdenv.lib.optionals enableTk [ tcl tk tkinter libX11 ]
48 ++ stdenv.lib.optionals enableQt [ pyqt4 ]
49 ++ stdenv.lib.optionals python.isPy2 [ functools32 subprocess32 ];
50
51 patches = [
52 ./basedirlist.patch
53
54 # https://github.com/matplotlib/matplotlib/pull/12478
55 (fetchpatch {
56 name = "numpy-1.16-compat.patch";
57 url = "https://github.com/matplotlib/matplotlib/commit/2980184d092382a40ab21f95b79582ffae6e19d6.patch";
58 sha256 = "1c0wj28zy8s5h6qiavx9zzbhlmhjwpzbc3fyyw9039mbnqk0spg2";
59 })
60 ];
61
62 # Matplotlib tries to find Tcl/Tk by opening a Tk window and asking the
63 # corresponding interpreter object for its library paths. This fails if
64 # `$DISPLAY` is not set. The fallback option assumes that Tcl/Tk are both
65 # installed under the same path which is not true in Nix.
66 # With the following patch we just hard-code these paths into the install
67 # script.
68 postPatch =
69 let
70 inherit (stdenv.lib.strings) substring;
71 tcl_tk_cache = ''"${tk}/lib", "${tcl}/lib", "${substring 0 3 tk.version}"'';
72 in
73 stdenv.lib.optionalString enableTk
74 "sed -i '/self.tcl_tk_cache = None/s|None|${tcl_tk_cache}|' setupext.py";
75
76 checkPhase = ''
77 ${python.interpreter} tests.py
78 '';
79
80 # Test data is not included in the distribution (the `tests` folder
81 # is missing)
82 doCheck = false;
83
84 prePatch = ''
85 # Failing test: ERROR: matplotlib.tests.test_style.test_use_url
86 sed -i 's/test_use_url/fails/' lib/matplotlib/tests/test_style.py
87 # Failing test: ERROR: test suite for <class 'matplotlib.sphinxext.tests.test_tinypages.TestTinyPages'>
88 sed -i 's/TestTinyPages/fails/' lib/matplotlib/sphinxext/tests/test_tinypages.py
89 # Transient errors
90 sed -i 's/test_invisible_Line_rendering/noop/' lib/matplotlib/tests/test_lines.py
91 '';
92
93 meta = with stdenv.lib; {
94 description = "Python plotting library, making publication quality plots";
95 homepage = "https://matplotlib.org/";
96 maintainers = with maintainers; [ lovek323 ];
97 };
98
99}