I have a simple Python script that uses Gtk internally:
https://gitlab.com/moy/recent-files-cli/-/blob/main/recent-files?ref_type=heads
I want to run it on my nixos system (and I’m trying to understand what’s going on as a side effect
).
What works: a shell.nix file
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = [
pkgs.gobject-introspection
pkgs.gtk3
(pkgs.python3.withPackages (p: with p; [
pygobject3
]))
];
}
then
$ nix-shell
$ ./recent-files
# displays my recent files, good
What doesn’t work: configuration.nix
I tried adding the same dependencies to configuration.nix and sudo nixos-rebuild switch:
environment.systemPackages = with pkgs; [
# ...
gobject-introspection
gtk3
(python3.withPackages (p: with p; [
pygobject3
pip
]))
The script finds the Python dependency import gi, but fails to load Gtk:
$ ./recent-files
Traceback (most recent call last):
File "/home/moy/dev/recent-files-cli/./recent-files", line 16, in <module>
gi.require_version("Gtk", "3.0")
File "/nix/store/7bpbfiaksacdzp7n2ycm408djqjjp13a-python3-3.12.11-env/lib/python3.12/site-packages/gi/__init__.py", line 122, in require_version
raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gtk not available
What doesn’t work either: packaging
I also tried to write a real nix package for my script, hoping to be able to install it on my system (and possibly to share it with the community), in a file recent-files.nix:
with import <nixpkgs> {};
with pkgs.python3Packages;
buildPythonPackage rec {
name = "recent-files";
src = ./recent-files;
dontUnpack = true;
pyproject = false;
installPhase = "install -Dm755 ${src} $out/bin/recent-files";
propagatedBuildInputs = [
pkgs.gobject-introspection
pkgs.gtk3
(pkgs.python3.withPackages (p: with p; [
pygobject3
]))
];
}
Unfortunately:
$ nix-build recent-files.nix
/nix/store/80sqgky1d3aqh0pw252h8a9gsgrxdxmc-python3.12-recent-files
$ ./result/bin/recent-files
Traceback (most recent call last):
File "/nix/store/80sqgky1d3aqh0pw252h8a9gsgrxdxmc-python3.12-recent-files/bin/.recent-files-wrapped", line 17, in <module>
gi.require_version("Gtk", "3.0")
File "/nix/store/7zb3n4nqxi3k8fpj9piz41d3pavis2mq-python3-3.12.11-env/lib/python3.12/site-packages/gi/__init__.py", line 122, in require_version
raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gtk not available
and no better luck with nix-shell:
$ nix-shell recent-files.nix
Sourcing python-remove-tests-dir-hook
Sourcing python-catch-conflicts-hook.sh
Sourcing python-remove-bin-bytecode-hook.sh
Sourcing python-imports-check-hook.sh
Using pythonImportsCheckPhase
Sourcing python-namespaces-hook
[nix-shell:~/dev/recent-files-cli]$ ./recent-files
Traceback (most recent call last):
File "/home/moy/dev/recent-files-cli/./recent-files", line 16, in <module>
gi.require_version("Gtk", "3.0")
File "/nix/store/7zb3n4nqxi3k8fpj9piz41d3pavis2mq-python3-3.12.11-env/lib/python3.12/site-packages/gi/__init__.py", line 122, in require_version
raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gtk not available
Apparently, I managed to get the pure Python dependency right, but there is some kind of magic with the native dependencies that I don’t understand.
What am I doing wrong? How can I get my package to work?
Also, I’m a nixos noob, so feel free to comment on whatever else I did wrong and suggest any kind of improvements.
Thanks in advance,