I’m trying to walk through a tutorial on GTK3 in Python and got stuck on the first code sample. The code is very simple:
#!/usr/bin/env nix-shell
#!nix-shell -i python -p python3 gtk3 gobject-introspection python3Packages.pygobject3
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
but it generates a mysterious error:
/nix/store/1p9m2ydf0126n28apjjr8nfww87gpjqm-python3.9-pygobject-3.42.0/lib/python3.9/site-packages/gi/overrides/Gdk.py:438: Warning: invalid cast from 'GIOModule' to 'GTypeModule'
initialized, argv = Gdk.init_check(sys.argv)
/nix/store/1p9m2ydf0126n28apjjr8nfww87gpjqm-python3.9-pygobject-3.42.0/lib/python3.9/site-packages/gi/overrides/Gdk.py:438: Warning: g_type_module_use: assertion 'G_IS_TYPE_MODULE (module)' failed
initialized, argv = Gdk.init_check(sys.argv)
**
dconf:ERROR:../gsettings/dconfsettingsbackend.c:215:g_io_module_unload: code should not be reached
Bail out! dconf:ERROR:../gsettings/dconfsettingsbackend.c:215:g_io_module_unload: code should not be reached
Could not google it, so I suspect that something is wrong with dependencies. What could be done to fix the problem? I’d prefer to run the script via nix-shell shebang.
That sounds like an issue with some environment contaminant. Try running with env GI_TYPELIB_PATH= GIO_EXTRA_MODULES= GDK_PIXBUF_MODULE_FILE= or adding --pure to the nix-shell args for starters.
Thank you very much, using pure shell helped! Could you also give some advice on how to deal with such situations, especially when running inside Gnome? I have very little experience with developing for GTK unfortunately. For the record, I have the following variables set:
Unfortunately, I am not actually sure what is causing this particular error – I was never able to reproduce it. I guess it may have to do with the typelibs being linked against different instance of one library.
Could you check which of the environment variables causes the trouble? If it is GI_TYPELIB_PATH, Could you run nix-store --query --requisites $(echo $GI_TYPELIB_PATH | sed 's/:/\n/g')?
For development, I would recommend clearing those environment variables. For running installed applications, we have an infrastructure that wraps programs in a way that puts package’s own dependencies to the beginning of the environment variable, so this type of conflict should not happen (though environment variables are evil and there is plethora of other ways it can bite you).
It seems that I found the culprit here. I did a nixos-rebuild boot before running, so the system had older libs running compared to my app (which took the freshest from nixpkgs). When having the system fully upgraded and loaded into that configuration, the app runs normally. So nothing interesting here apparently.