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

Summary

Hi! I use kde plasma & hyprland /w waybar. which is not really that important to this.

I am on the unstable channel if that matters.

when configuring waybar, I found something online that I wanted and it required a python script. it did not work out of the box, so I made a nix shell to troubleshoot. once I got it working, I ported the exact same packages I used in the shell to my hyprland configuration nix file. but for some reason, it didn’t work.

judging from the error message, it doesn’t even see one of the packages.

what happens in shell (expected):

{"text": "\uf144                   ", "class": "perc0-0", "tooltip": "<span></span>"}

it would show more if I had music playing

what happens outside of shell (unexpected):

Traceback (most recent call last):
  File "/home/<user>/.config/waybar/waybar-mediaplayer/src/./mediaplayer", line 26, in <module>
    gi.require_version("Playerctl", "2.0")
  File "/nix/store/flawsk5hh9zizcds4cvlp36dr3cp2dy9-python3-3.12.9-env/lib/python3.12/site-packages/gi/__init__.py", line 122, in require_version
    raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Playerctl not available

with name redacted, of course.

entire shell:

let
  pkgs = import <nixpkgs> {};
in pkgs.mkShell {
  packages = [
    (pkgs.python312.withPackages (python-pkgs: with python-pkgs; [
      pandas
      requests
      syncedlyrics
      pillow
    ])
    )
    (pkgs.python312Packages.pygobject3)
    (pkgs.playerctl)
  ];
}

entire Hyprland config (nix):

################################################
# This is the nix hyprland configuration file  #
# use it to declare packages and edit hyprland #
################################################
{ pkgs, ... }:
{
  programs.hyprland = {
    # Install the packages from nixpkgs
    enable = true;
    # Whether to enable XWayland
    xwayland.enable = true;
  };

  environment.systemPackages = with pkgs; [
    feh
    font-awesome
    foot
    gnome-keyring
    hyprcursor
    hyprgraphics
    hypridle
    hyprland-qt-support
    hyprland-qtutils
    hyprlang
    hyprlock
    hyprls
    hyprpaper
    hyprpicker
    hyprpolkitagent
    hyprshade
    hyprsunset
    hyprsysteminfo
    hyprutils
    hyprwayland-scanner
    iwd
    iwgtk
    kitty
    mako
    mpv
    pavucontrol
    playerctl
    (python312.withPackages (python-pkgs: with python-pkgs; [
      pandas
      requests
      syncedlyrics
      pillow
      pygobject3
    ]))
    waybar
    wlogout
    wofi
    xdg-desktop-portal-hyprland
  ];
}

running python script throws an error when ran outside a shell even though the shell I originally ran it in and my main config have the same packages.

related shell packages:

pkgs = import <nixpkgs> {};
in pkgs.mkShell {
  packages = [
    (pkgs.python312.withPackages (python-pkgs: with python-pkgs; [
      pandas
      requests
      syncedlyrics
      pillow
    ])
    )
    (pkgs.python312Packages.pygobject3)
    (pkgs.playerctl)
  ];
}

related config packages:

environment.systemPackages = with pkgs; [
    playerctl
    (python312.withPackages (python-pkgs: with python-pkgs; [
      pandas
      requests
      syncedlyrics
      pillow
      pygobject3
    ]))
  ];
}

any help would be appreciated.

Shells set certain envvars that would not be set globally. If you want a script to work, package it with nix, rather than randomly installing dependencies and hoping the script finds them. See https://nixos.org/manual/nixpkgs/unstable/#python.

first I’ll need to find out how to do that, but I am going to try it. thank you for helping.