Since my recent system upgrade to 24.05, I slowly grind through each things that broke after the upgrade.
Today I try to fix a python script that is called from waybar to tell the currently playing music. It has not been made by me and worked as intended after the upgrade but do not start anymore.
When launching, I get the following error: ImportError: cannot import name GLib, introspection typelib not found
It seems to come from an from gi import Glib import in the script. When running the same import from an interpreter, I get the same error.
After some search about this error, if found it could be a missing package, like pygobject3 or gobject-introspection, however I have added theses to my default python installation, and it does not change anything.
Here is a snippet of what is in my config:
let
pyPkgs = ps: with ps; [
i3ipc
dbus-python
pygobject3
wrapPython
];
# [...]
in
{
home.packages = with pkgs; [
# [...]
# Python
gobject-introspection
wrapGAppsHook3
(pkgs.python311.withPackages pyPkgs)
];
}
I have a feeling about what is the Glib introspection, but actually no idea of how it works technically with python. Has something changed in 24.05 that could have broke that ?
glib typelib is now in glib not in gobject-introspection (this was a upstream change)
I actually read that PR before and tried in a nix shell with glib installed. No more success…
You’d have to actually package the python script; simply putting dependencies in home.packages won’t help here
I was afraid of this answer… Python things cant’t be easy with nixos. I guess it’s time for me to learn packaging things, all my previous attemps were failed.
Things start to get weird.
I have been able to successfully start the script in a nix-shell with the following shell.nix:
with import <nixpkgs> {};
with pkgs.python3Packages;
buildPythonPackage rec {
name = "mypackage";
src = ./player.py;
propagatedBuildInputs = [ glib gobject-introspection dbus-python pygobject3 ];
}
However, it won’t launch in a nix shell nixpkgs#glib nixpkgs#gobject-introspection nixpkgs#python311 nixpkgs#python311Packages.dbus-python nixpkgs#python311Packages.pygobject3 (fails importing dbus)
And it neither launch when packaging and building the package with the following config:
mkDerivation and similar helpers like buildPythonPackage will run the setup hooks listed in inputs, which will make the environment variables available at build time but it is your responsibility to ensure they are available at runtime.
Hmm, is there a documentation somewhere on how to use theses wrappers or do you know a package in nixpkgs that uses them that I could read ?
I feel a bit lost with what I saw on the Nixpkgs reference.
As the name suggests, setup hooks hook into the generic builder of the stdenv and modify it.
wrapGAppsHook* family, specifically, replaces every executable in $out/bin and other similar directories with a program that sets some environment variables (as mentioned in the reference) and then executes the original program.