about summary refs log tree commit diff
path: root/pkgs/development/python-modules/matplotlib/default.nix
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/development/python-modules/matplotlib/default.nix')
-rw-r--r--pkgs/development/python-modules/matplotlib/default.nix340
1 files changed, 187 insertions, 153 deletions
diff --git a/pkgs/development/python-modules/matplotlib/default.nix b/pkgs/development/python-modules/matplotlib/default.nix
index 788df3d9458e..f07acb53dc69 100644
--- a/pkgs/development/python-modules/matplotlib/default.nix
+++ b/pkgs/development/python-modules/matplotlib/default.nix
@@ -1,75 +1,87 @@
-{ lib
-, stdenv
-, fetchPypi
-, writeText
-, buildPythonPackage
-, isPyPy
-, pythonOlder
-
-# build-system
-, certifi
-, pkg-config
-, pybind11
-, setuptools
-, setuptools-scm
-, wheel
-
-# native libraries
-, ffmpeg-headless
-, freetype
-, qhull
-
-# propagates
-, contourpy
-, cycler
-, fonttools
-, kiwisolver
-, numpy
-, packaging
-, pillow
-, pyparsing
-, python-dateutil
-
-# optional
-, importlib-resources
-
-# GTK3
-, enableGtk3 ? false
-, cairo
-, gobject-introspection
-, gtk3
-, pycairo
-, pygobject3
-
-# Tk
-# Darwin has its own "MacOSX" backend, PyPy has tkagg backend and does not support tkinter
-, enableTk ? (!stdenv.isDarwin && !isPyPy)
-, tcl
-, tk
-, tkinter
-
-# Ghostscript
-, enableGhostscript ? true
-, ghostscript
-
-# Qt
-, enableQt ? false
-, pyqt5
-
-# Webagg
-, enableWebagg ? false
-, tornado
-
-# nbagg
-, enableNbagg ? false
-, ipykernel
-
-# darwin
-, Cocoa
-
-# required for headless detection
-, libX11
-, wayland
+{
+  lib,
+  stdenv,
+  fetchPypi,
+  buildPythonPackage,
+  isPyPy,
+  pythonOlder,
+
+  # build-system
+  certifi,
+  pkg-config,
+  pybind11,
+  meson-python,
+  setuptools-scm,
+  pytestCheckHook,
+  python,
+  matplotlib,
+  fetchurl,
+
+  # native libraries
+  ffmpeg-headless,
+  freetype,
+  # By default, almost all tests fail due to the fact we use our version of
+  # freetype. We still define use this argument to define the overriden
+  # derivation `matplotlib.passthru.tests.withoutOutdatedFreetype` - which
+  # builds matplotlib with the freetype version they default to, with which all
+  # tests should pass.
+  doCheck ? false,
+  qhull,
+
+  # propagates
+  contourpy,
+  cycler,
+  fonttools,
+  kiwisolver,
+  numpy,
+  packaging,
+  pillow,
+  pyparsing,
+  python-dateutil,
+
+  # optional
+  importlib-resources,
+
+  # GTK3
+  enableGtk3 ? false,
+  cairo,
+  gobject-introspection,
+  gtk3,
+  pycairo,
+  pygobject3,
+
+  # Tk
+  # Darwin has its own "MacOSX" backend, PyPy has tkagg backend and does not support tkinter
+  enableTk ? (!stdenv.hostPlatform.isDarwin && !isPyPy),
+  tcl,
+  tk,
+  tkinter,
+
+  # Ghostscript
+  enableGhostscript ? true,
+  ghostscript,
+
+  # Qt
+  enableQt ? false,
+  pyqt5,
+
+  # Webagg
+  enableWebagg ? false,
+  tornado,
+
+  # nbagg
+  enableNbagg ? false,
+  ipykernel,
+
+  # darwin
+  Cocoa,
+
+  # required for headless detection
+  libX11,
+  wayland,
+
+  # Reverse dependency
+  sage,
 }:
 
 let
@@ -77,15 +89,15 @@ let
 in
 
 buildPythonPackage rec {
-  version = "3.8.3";
+  version = "3.9.2";
   pname = "matplotlib";
-  format = "pyproject";
+  pyproject = true;
 
   disabled = pythonOlder "3.8";
 
   src = fetchPypi {
     inherit pname version;
-    hash = "sha256-e0FiOemuOL5UsCirv5BIr/UFSpq6VBa+8L0X+RYs4WE=";
+    hash = "sha256-lqtDkGJpymSmNmk0EG+gFTRFSmnkcbe/PXkIOYGqq5I=";
   };
 
   env.XDG_RUNTIME_DIR = "/tmp";
@@ -97,101 +109,123 @@ buildPythonPackage rec {
   # With the following patch we just hard-code these paths into the install
   # script.
   postPatch =
-    let
-      tcl_tk_cache = ''"${tk}/lib", "${tcl}/lib", "${lib.strings.substring 0 3 tk.version}"'';
-    in
-    lib.optionalString enableTk ''
-      sed -i '/self.tcl_tk_cache = None/s|None|${tcl_tk_cache}|' setupext.py
-    '' + lib.optionalString (stdenv.isLinux && interactive) ''
+    ''
+      substituteInPlace pyproject.toml \
+        --replace-fail '"numpy>=2.0.0rc1,<2.3",' ""
+      patchShebangs tools
+    ''
+    + lib.optionalString (stdenv.hostPlatform.isLinux && interactive) ''
       # fix paths to libraries in dlopen calls (headless detection)
-      substituteInPlace src/_c_internal_utils.c \
-        --replace libX11.so.6 ${libX11}/lib/libX11.so.6 \
-        --replace libwayland-client.so.0 ${wayland}/lib/libwayland-client.so.0
+      substituteInPlace src/_c_internal_utils.cpp \
+        --replace-fail libX11.so.6 ${libX11}/lib/libX11.so.6 \
+        --replace-fail libwayland-client.so.0 ${wayland}/lib/libwayland-client.so.0
     '';
 
-  nativeBuildInputs = [
+  nativeBuildInputs = [ pkg-config ] ++ lib.optionals enableGtk3 [ gobject-introspection ];
+
+  buildInputs =
+    [
+      ffmpeg-headless
+      freetype
+      qhull
+    ]
+    ++ lib.optionals enableGhostscript [ ghostscript ]
+    ++ lib.optionals enableGtk3 [
+      cairo
+      gtk3
+    ]
+    ++ lib.optionals enableTk [
+      libX11
+      tcl
+      tk
+    ]
+    ++ lib.optionals stdenv.hostPlatform.isDarwin [ Cocoa ];
+
+  # clang-11: error: argument unused during compilation: '-fno-strict-overflow' [-Werror,-Wunused-command-line-argument]
+  hardeningDisable = lib.optionals stdenv.hostPlatform.isDarwin [ "strictoverflow" ];
+
+  build-system = [
     certifi
     numpy
-    pkg-config
     pybind11
-    setuptools
+    meson-python
     setuptools-scm
-    wheel
-  ] ++ lib.optionals enableGtk3 [
-    gobject-introspection
-  ];
-
-  buildInputs = [
-    ffmpeg-headless
-    freetype
-    qhull
-  ] ++ lib.optionals enableGhostscript [
-    ghostscript
-  ] ++ lib.optionals enableGtk3 [
-    cairo
-    gtk3
-  ] ++ lib.optionals enableTk [
-    libX11
-    tcl
-    tk
-  ] ++ lib.optionals stdenv.isDarwin [
-    Cocoa
-  ];
-
-  # clang-11: error: argument unused during compilation: '-fno-strict-overflow' [-Werror,-Wunused-command-line-argument]
-  hardeningDisable = lib.optionals stdenv.isDarwin [
-    "strictoverflow"
   ];
 
-  propagatedBuildInputs = [
-    # explicit
-    contourpy
-    cycler
-    fonttools
-    kiwisolver
-    numpy
-    packaging
-    pillow
-    pyparsing
-    python-dateutil
-  ] ++ lib.optionals (pythonOlder "3.10") [
-    importlib-resources
-  ] ++ lib.optionals enableGtk3 [
-    pycairo
-    pygobject3
-  ] ++ lib.optionals enableQt [
-    pyqt5
-  ] ++ lib.optionals enableWebagg [
-    tornado
-  ] ++ lib.optionals enableNbagg [
-    ipykernel
-  ] ++ lib.optionals enableTk [
-    tkinter
-  ];
+  dependencies =
+    [
+      # explicit
+      contourpy
+      cycler
+      fonttools
+      kiwisolver
+      numpy
+      packaging
+      pillow
+      pyparsing
+      python-dateutil
+    ]
+    ++ lib.optionals (pythonOlder "3.10") [ importlib-resources ]
+    ++ lib.optionals enableGtk3 [
+      pycairo
+      pygobject3
+    ]
+    ++ lib.optionals enableQt [ pyqt5 ]
+    ++ lib.optionals enableWebagg [ tornado ]
+    ++ lib.optionals enableNbagg [ ipykernel ]
+    ++ lib.optionals enableTk [ tkinter ];
+
+  mesonFlags = lib.mapAttrsToList lib.mesonBool {
+    system-freetype = true;
+    system-qhull = true;
+    # Otherwise GNU's `ar` binary fails to put symbols from libagg into the
+    # matplotlib shared objects. See:
+    # -https://github.com/matplotlib/matplotlib/issues/28260#issuecomment-2146243663
+    # -https://github.com/matplotlib/matplotlib/issues/28357#issuecomment-2155350739
+    b_lto = false;
+  };
 
-  passthru.config = {
-    directories = { basedirlist = "."; };
-    libs = {
-      system_freetype = true;
-      system_qhull = true;
-      # LTO not working in darwin stdenv, see #19312
-      enable_lto = !stdenv.isDarwin;
+  passthru.tests = {
+    inherit sage;
+    withOutdatedFreetype = matplotlib.override {
+      doCheck = true;
+      freetype = freetype.overrideAttrs (_: {
+        src = fetchurl {
+          url = "https://download.savannah.gnu.org/releases/freetype/freetype-old/freetype-2.6.1.tar.gz";
+          sha256 = "sha256-Cjx9+9ptoej84pIy6OltmHq6u79x68jHVlnkEyw2cBQ=";
+        };
+        patches = [ ];
+      });
     };
   };
 
-  env.MPLSETUPCFG = writeText "mplsetup.cfg" (lib.generators.toINI {} passthru.config);
-
-  # Encountering a ModuleNotFoundError, as describved and investigated at:
-  # https://github.com/NixOS/nixpkgs/issues/255262 . It could be that some of
-  # which may fail due to a freetype version that doesn't match the freetype
-  # version used by upstream.
-  doCheck = false;
+  pythonImportsCheck = [ "matplotlib" ];
+  inherit doCheck;
+  nativeCheckInputs = [ pytestCheckHook ];
+  preCheck = ''
+    # https://matplotlib.org/devdocs/devel/testing.html#obtain-the-reference-images
+    find lib -name baseline_images -printf '%P\n' | while read p; do
+      cp -r lib/"$p" $out/${python.sitePackages}/"$p"
+    done
+    # Tests will fail without these files as well
+    cp \
+      lib/matplotlib/tests/{mpltest.ttf,cmr10.pfb,Courier10PitchBT-Bold.pfb} \
+      $out/${python.sitePackages}/matplotlib/tests/
+    # https://github.com/NixOS/nixpkgs/issues/255262
+    cd $out
+  '';
 
   meta = with lib; {
     description = "Python plotting library, making publication quality plots";
     homepage = "https://matplotlib.org/";
     changelog = "https://github.com/matplotlib/matplotlib/releases/tag/v${version}";
-    license = with licenses; [ psfl bsd0 ];
-    maintainers = with maintainers; [ lovek323 veprbl ];
+    license = with licenses; [
+      psfl
+      bsd0
+    ];
+    maintainers = with maintainers; [
+      lovek323
+      veprbl
+    ];
   };
 }