pythonPackages.matplotlib: fix headless detection

The default backend is chosen based on the content of the $DISPLAY
variable *and* a successfull call to libX11, loaded via dlopen().
The test fails because dlopen looks in /usr/lib and /lib, so matplotlib
falls back to a headless backend.

To reproduce try running:

    $ nix-shell -I nixpkgs=$PWD -p \
      'python3.withPackages (p: [ p.matplotlib ])' --run python
    >>> import matplotlib.pyplot as plt
    >>> assert plt.get_backend() == "TkAgg"
This commit is contained in:
rnhmjoj 2021-05-26 01:24:27 +02:00
parent 7e54c9b956
commit 7e2ec8f8a1
No known key found for this signature in database
GPG key ID: BFBAF4C975F76450

View file

@ -5,12 +5,18 @@
, enableGhostscript ? true, ghostscript, gtk3
, enableGtk3 ? false, cairo
# darwin has its own "MacOSX" backend
, enableTk ? !stdenv.isDarwin, tcl, tk, tkinter, libX11
, enableTk ? !stdenv.isDarwin, tcl, tk, tkinter
, enableQt ? false, pyqt5
# required for headless detection
, libX11, wayland
, Cocoa
, pythonOlder
}:
let
interactive = enableTk || enableGtk3 || enableQt;
in
buildPythonPackage rec {
version = "3.4.1";
pname = "matplotlib";
@ -62,8 +68,14 @@ buildPythonPackage rec {
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 enableTk ''
sed -i '/self.tcl_tk_cache = None/s|None|${tcl_tk_cache}|' setupext.py
'' + lib.optionalString (stdenv.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
'';
# Matplotlib needs to be built against a specific version of freetype in
# order for all of the tests to pass.