Error when trying to use PyQt5 backend of matplotlib

Hello,

I’m on NixOS and I have a Python project which uses the PyQt5 backend of matplotlib. When I launch it, it crashes when it reaches plt.figure() and outputs the following error:

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb, webgl.

I create my python environment with python3.9 -m venv venv and then install the projects’s dependencies with pip install -r requirements.txt. I also use nix-ld in my NixOS config and run python with LD_LIBRARY_PATH=$NIX_LD_LIBRARY_PATH.

It seems the issue is with Qt, but every solution I’ve tried so far has failed. What would be the nix way of solving this issue?

I faced the same problem at some point. It might differ from yours, because I was not using nix-ld or the LD_LIBRARY_PATH variable, but I’ll mention my solution in case it helps sparking some idea. Also, not sure it’s the right Nix-way of solving it.

I wrapped the Python I was using in a derivation, while explicityl setting variables for loading the xcb plugin:

wrappedPython = with pkgs; runCommand "wrappedPython"
  {
    buildInputs = [ makeBinaryWrapper ];
  }
  ''
    mkdir -p $out/bin
    makeBinaryWrapper ${python}/bin/python3 $out/bin/python3 \
      --set QT_QPA_PLATFORM_PLUGIN_PATH ${pkgs.qt5.qtbase.bin}/lib/qt-${pkgs.qt5.qtbase.version}/plugins \
      --set QT_QPA_PLATFORM xcb 
  '';
1 Like

Thanks for the answer. It lead me to dig around a bit and I came up with a shell.nix that somewhat works:

let
  pkgs = import <nixpkgs> {};

  libs = with pkgs;[
    fontconfig
    freetype
    xorg.libX11
    xorg.libxcb
    xorg.xcbutilwm
    xorg.xcbutilimage
    xorg.xcbutil
    xorg.xcbutilkeysyms
    xorg.xcbutilrenderutil
    libxkbcommon
    stdenv.cc.cc
    dbus
    libGL
    glib
    zlib
  ];
in
pkgs.mkShell {
  buildInputs = with pkgs; [
    libsForQt5.qt5.qtbase
    python310Packages.python
  ];

  shellHook =
    ''
      export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath libs}:$LD_LIBRARY_PATH
      export QT_PLUGIN_PATH=venv/lib/python3.10/site-packages/PyQt5/Qt5/plugins
    '';

}

You’ll obviously need to create your venv yourself and install pyqt5 with pip. You also shouldn’t use --pure when launching nix-shell.

This is a known issue, sadly: matplotlib is missing GSettings · Issue #25351 · NixOS/nixpkgs · GitHub