Jupyter breaks Matplotlib

I can’t create a devShell with Jupyter and Matplotlib simultaneously. Somehow, this happens when I do:

➜ nix develop --command python -c 'import matplotlib.pyplot as plt; plt.plot([1,2],[3,4]); plt.show()'
qt.qpa.plugin: Could not find the Qt platform plugin "xcb" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

[1]    1157020 IOT instruction (core dumped)  nix develop --command python -c

Here is the flake.nix I am using (minimum not working example)

{
  inputs = {
    flake-utils = {
      url = "github:numtide/flake-utils";
    };
  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      {
        devShell = pkgs.mkShell {
          buildInputs = [
            (nixpkgs.legacyPackages.${system}.python39.withPackages(ps: [
              ps.jupyter
              ps.matplotlib
            ]))
          ];
        };
      });
}

I have come across this page, which quotes the exact error, but I do not understand what it means or how to apply it here.

It’s a known issue: matplotlib is missing GSettings · Issue #25351 · NixOS/nixpkgs · GitHub
Basically, python libraries with runtime dependencies can’t propagate their dependencies to the python interpreter (the one being assembled by python.withPackages). I proposed a solution, but (as things usually go) it was scrapped in favor of something that will take forever to be implemented.

For Qt5 and matplotlib you can probably just add:

export QT_PLUGIN_PATH=${qt5.qtbase}/${qt5.qtbase.qtPluginPrefix}

to the shellHook in you mkShell.

3 Likes