1{ lib 2, stdenv 3, fetchPypi 4, writeText 5, buildPythonPackage 6, isPyPy 7, pythonOlder 8 9# https://github.com/matplotlib/matplotlib/blob/main/doc/devel/dependencies.rst 10# build-system 11, certifi 12, oldest-supported-numpy 13, pkg-config 14, pybind11 15, setuptools 16, setuptools-scm 17, wheel 18 19# native libraries 20, ffmpeg-headless 21, freetype 22, qhull 23 24# propagates 25, contourpy 26, cycler 27, fonttools 28, kiwisolver 29, numpy 30, packaging 31, pillow 32, pyparsing 33, python-dateutil 34 35# optional 36, importlib-resources 37 38# GTK3 39, enableGtk3 ? false 40, cairo 41, gobject-introspection 42, gtk3 43, pycairo 44, pygobject3 45 46# Tk 47# Darwin has its own "MacOSX" backend, PyPy has tkagg backend and does not support tkinter 48, enableTk ? (!stdenv.isDarwin && !isPyPy) 49, tcl 50, tk 51, tkinter 52 53# Ghostscript 54, enableGhostscript ? true 55, ghostscript 56 57# Qt 58, enableQt ? false 59, pyqt5 60 61# Webagg 62, enableWebagg ? false 63, tornado 64 65# nbagg 66, enableNbagg ? false 67, ipykernel 68 69# darwin 70, Cocoa 71 72# required for headless detection 73, libX11 74, wayland 75}: 76 77let 78 interactive = enableTk || enableGtk3 || enableQt; 79in 80 81buildPythonPackage rec { 82 version = "3.8.0"; 83 pname = "matplotlib"; 84 format = "pyproject"; 85 86 disabled = pythonOlder "3.8"; 87 88 src = fetchPypi { 89 inherit pname version; 90 hash = "sha256-34UF4cGdXCwmr/NJeny9PM/C6XBD0eTbPnavo5kWS2k="; 91 }; 92 93 env.XDG_RUNTIME_DIR = "/tmp"; 94 95 # Matplotlib tries to find Tcl/Tk by opening a Tk window and asking the 96 # corresponding interpreter object for its library paths. This fails if 97 # `$DISPLAY` is not set. The fallback option assumes that Tcl/Tk are both 98 # installed under the same path which is not true in Nix. 99 # With the following patch we just hard-code these paths into the install 100 # script. 101 postPatch = 102 let 103 tcl_tk_cache = ''"${tk}/lib", "${tcl}/lib", "${lib.strings.substring 0 3 tk.version}"''; 104 in 105 lib.optionalString enableTk '' 106 sed -i '/self.tcl_tk_cache = None/s|None|${tcl_tk_cache}|' setupext.py 107 '' + lib.optionalString (stdenv.isLinux && interactive) '' 108 # fix paths to libraries in dlopen calls (headless detection) 109 substituteInPlace src/_c_internal_utils.c \ 110 --replace libX11.so.6 ${libX11}/lib/libX11.so.6 \ 111 --replace libwayland-client.so.0 ${wayland}/lib/libwayland-client.so.0 112 '' + 113 # bring our own system libraries 114 # https://github.com/matplotlib/matplotlib/blob/main/doc/devel/dependencies.rst#c-libraries 115 '' 116 echo "[libs] 117 system_freetype=true 118 system_qhull=true" > mplsetup.cfg 119 ''; 120 121 nativeBuildInputs = [ 122 certifi 123 numpy 124 oldest-supported-numpy # TODO remove after updating to 3.8.0 125 pkg-config 126 pybind11 127 setuptools 128 setuptools-scm 129 wheel 130 ] ++ lib.optionals enableGtk3 [ 131 gobject-introspection 132 ]; 133 134 buildInputs = [ 135 ffmpeg-headless 136 freetype 137 qhull 138 ] ++ lib.optionals enableGhostscript [ 139 ghostscript 140 ] ++ lib.optionals enableGtk3 [ 141 cairo 142 gtk3 143 ] ++ lib.optionals enableTk [ 144 libX11 145 tcl 146 tk 147 ] ++ lib.optionals stdenv.isDarwin [ 148 Cocoa 149 ]; 150 151 # clang-11: error: argument unused during compilation: '-fno-strict-overflow' [-Werror,-Wunused-command-line-argument] 152 hardeningDisable = lib.optionals stdenv.isDarwin [ 153 "strictoverflow" 154 ]; 155 156 propagatedBuildInputs = [ 157 # explicit 158 contourpy 159 cycler 160 fonttools 161 kiwisolver 162 numpy 163 packaging 164 pillow 165 pyparsing 166 python-dateutil 167 ] ++ lib.optionals (pythonOlder "3.10") [ 168 importlib-resources 169 ] ++ lib.optionals enableGtk3 [ 170 pycairo 171 pygobject3 172 ] ++ lib.optionals enableQt [ 173 pyqt5 174 ] ++ lib.optionals enableWebagg [ 175 tornado 176 ] ++ lib.optionals enableNbagg [ 177 ipykernel 178 ] ++ lib.optionals enableTk [ 179 tkinter 180 ]; 181 182 passthru.config = { 183 directories = { basedirlist = "."; }; 184 libs = { 185 system_freetype = true; 186 system_qhull = true; 187 } // lib.optionalAttrs stdenv.isDarwin { 188 # LTO not working in darwin stdenv, see #19312 189 enable_lto = false; 190 }; 191 }; 192 193 env.MPLSETUPCFG = writeText "mplsetup.cfg" (lib.generators.toINI {} passthru.config); 194 195 # Matplotlib needs to be built against a specific version of freetype in 196 # order for all of the tests to pass. 197 doCheck = false; 198 199 meta = with lib; { 200 description = "Python plotting library, making publication quality plots"; 201 homepage = "https://matplotlib.org/"; 202 changelog = "https://github.com/matplotlib/matplotlib/releases/tag/v${version}"; 203 license = with licenses; [ psfl bsd0 ]; 204 maintainers = with maintainers; [ lovek323 veprbl ]; 205 }; 206}