Running python scripts (works different in config than in shell)

hey again! im assuming this is the error you’re getting when trying to run the script:

# ./result/bin/waybar-mediaplayer-script
Traceback (most recent call last):
  File "/nix/store/7p2nrk1csx7rf4rxk0ygn8s9fg3rgz6d-waybar-mediaplayer-script-0.0.1/bin/.waybar-mediaplayer-script-wrapped", line 28, in <module>
    gi.require_version("Playerctl", "2.0")
  File "/nix/store/fr216icfk9iii3fw2yyfij4r3zddaa9s-python3.11-pygobject-3.48.2/lib/python3.11/site-packages/gi/__init__.py", line 122, in require_version
    raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Playerctl not available

so playerctl is not in the gi namespace, so your program can’t find it. (dont do the following, it’s just to illustrate the problem) we could fix this by manually exposing the path through an environment variable:

    wrapProgram "$out/bin/${pname}" \
      --prefix GI_TYPELIB_PATH : "${pkgs.playerctl}/lib/girepository-1.0 \
      --prefix LD_LIBRARY_PATH : "${pkgs.playerctl}/lib

but then your program also depends on glib, so we’d have to do the same for that and any other dependencies. luckily, instead of manually enumerating these paths and modifying environment variables, we can use gobject-introspection combined with wrapGAppsHook to expose the gi libraries to the application.

doing this worked for me:

{ pkgs
, gobject-introspection
, wrapGAppsHook
}:
pkgs.python3Packages.buildPythonApplication rec {
  pname = "waybar-mediaplayer-script";
  version = "0.0.1";
  pyproject = false;

  src = ./.;

  propagatedBuildInputs = with pkgs; [
    playerctl
    glib
    python3Packages.pandas
    python3Packages.requests
    python3Packages.syncedlyrics
    python3Packages.pillow
    python3Packages.pygobject3
  ];

  nativeBuildInputs = [wrapGAppsHook gobject-introspection];

  dontUnpack = true;
  installPhase = ''
    install -Dm755 "${./${pname}}" "$out/bin/${pname}"
  '';
}

also gobject-introspection and wrapGAppsHook are provided by nixpkgs, but i’ve added them to the inputs because when you use callPackage ./file.nix {}; it will automatically pass them in when it sees that they’re required.

finally, running the app now also seems to error because it’s looking for a config file, you might want to either pass the location of the config file via a command line argument or include it in the package files with another install line in the installPhase