Resolving gobject-2.0-0 library issues with python in nix-shell

I started out on Reddit (link), but with the limited feedback I received, I still could not get my setup working.

I am new to NixOS. For a few weeks now, I have been having fun and am ready to migrate some projects to my new install. Unfortunately, I am stuck getting some python projects to work.

I want to setup a nix-shell for a MkDocs python project that uses mkdocs-with-pdf. I have got MkDocs working where I can “build” and “serve” the html version of the documentation, but the PDF stuff always gets stuck at OSError: cannot load library 'gobject-2.0-0':.... It appears that this is related to weasyprint needing this library.

I would greatly appreciate any input on how to get this library issue configured correctly.

This is the full error as the last line on the stack trace:
OSError: cannot load library 'gobject-2.0-0': gobject-2.0-0: cannot open shared object file: No such file or directory. Additionally, ctypes.util.find_library() did not manage to locate a library called 'gobject-2.0-0'

I have tried a bunch of different shell.nix at this point. For now, while I get the feeling that one day I can replace my virtualenv entirely with a nix-shell, I have found that this basic version works okay if still do the whole python -m venv .venv with pip install -r requirements.txt once I am in the nix-shell as I would have done on my last setup.

{ pkgs ? import <nixpkgs> { } }:
pkgs.mkShell {
  buildInputs = with pkgs; [
    pkgs.python3
  ];
}

If it is needed currently, the NixOS stateVersion is “23.05”.

So that doesn’t cause gobject problems? That’s surprising. If anything, I would expect that to break C library support, since the default implementation of ctypes.util.find_library() is too naïve to find libraries on NixOS. Last time I looked it was patched in nixpkgs, at least.

If you’re using a proper python shell, just adding gtk3 might be enough, otherwise you might have to patch your cython sources and maybe recompile python as much as this is possible with a virtualenv.

Anyway, even then gobject stuff is very awkward. You usually need to have a bunch of environment variables set to make gobject things work on NixOS, since they default to fhs paths which do not exist on this distro.

When packaging things you just use wrapGAppsHook, but afaik that doesn’t work in a nix shell: Nixpkgs 23.05 manual | Nix & NixOS

I think this means you need to manually set all the variables wrapGAppsHook sets, and make sure all the relevant libraries are in your packages.

I can’t find anything that suggests otherwise, but one of the gnome maintainers will probably come by and say I’m completely wrong.

This hasn’t cause problems yet. Though, I am still learning, new to nixos, and have yet to do anything too complex at all. This MkDocs PDF package issue is the first project that I have migrated to really halt my progress. Also, its been difficult to find much online even related.

I did see online that people were recommending gtk3, but that by itself hasn’t work. A Redditor said gtk4 and some other stuff, but that too hasn’t work.

This is basically a foreign language to me. Maybe this is why my basic searches about nix-shell, nix, python, pip, gobject, etc didn’t turn up positive results. Time to go search for some learning materials and look into wrapGAppsHook also.

Thanks for the advice!

Basically, this would be missing in a non-nixpkgs python: https://github.com/NixOS/nixpkgs/blob/9fdfaeb7b96f05e869f838c73cde8d98c640c649/pkgs/development/interpreters/python/cpython/default.nix#L249

But now that I’m properly awake I realize that virtualenv still uses the nixpkgs python, so that’s not the problem.

Have you tried gtk2? The message says gobject-2 is missing, I’m not sure if that number refers to the gtk major version.