NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.45k stars 13.65k forks source link

`pygobject3` doesn't really seem to work #341038

Open Andy3153 opened 1 week ago

Andy3153 commented 1 week ago

Describe the bug

I'm trying to use pygobject3 and it can't even import GLib out of it. Am I doing anything wrong or is this broken?

Steps To Reproduce

Steps to reproduce the behavior:

  1. Use a devShell flake to get the library:
    # flake.nix
    {
    inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    outputs = { self, nixpkgs }:
    let
    supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
    forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems
    (system: f { pkgs = import nixpkgs { inherit system; }; });
    in
    {
    devShells = forEachSupportedSystem ({ pkgs }:
    {
      default =
      pkgs.mkShell {
        packages = with pkgs;
        [
          (python3.withPackages(python-pkgs: with python-pkgs;
          [
            pygobject3
          ]))
        ];
      };
    });
    };
    }
  2. Try to use the lib:
    $ nix develop
    $ python3 -c "from gi.repository import GLib"
    Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/nix/store/q4r43nbcs39aj9mbfi7iq8d7mxrjf2i0-python3-3.12.5-env/lib/python3.12/site-packages/gi/importer.py", line 133, in create_module
    raise ImportError('cannot import name %s, '
    ImportError: cannot import name GLib, introspection typelib not found

Expected behavior

No errors appear

Notify maintainers

@jtojnar (pygobject3)

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 6.10.7-zen1, NixOS, 24.11 (Vicuna), 24.11.20240906.574d1ea`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.18.5`
 - nixpkgs: `/nix/store/5w3dp0m37794iffsbm9vd9f1xmmhda6i-source`

Add a :+1: reaction to issues you find important.

dotlambda commented 1 week ago

You also need gtk3 and maybe gobject-introspection.

Andy3153 commented 1 week ago

Seems like only gobject-introspection was needed to not get an error on that import... but, point still stands. Why isn't it in the derivation's dependencies list?

dotlambda commented 1 week ago

Why isn't it in the derivation's dependencies list?

It's not a Python package.

Andy3153 commented 1 week ago

Sorry, you're correct. buildInputs?

dotlambda commented 1 week ago

It should maybe be put in propagatedNativeBuildInputs.

Andy3153 commented 1 week ago

In my opinion, wherever it would put the necessary runtime dependencies in the dependency list

dotlambda commented 1 week ago

It's not a runtime dependency.

Andy3153 commented 1 week ago

Is it not..? You can't import the library without an error without that.

dotlambda commented 1 week ago

Is it not..? You can't import the library without an error without that.

That's not true, usually wrapGAppsHook sets everything up.

Andy3153 commented 1 week ago

I don't fully understand, but alright. Is my concern valid though? It should be added to propagatedNativeBuildInputs, right?

jtojnar commented 1 week ago

You need glib package if you want to use it through introspection – just like with most other introspected libraries.


We have not made glib a dependency for pygobject3 when GLib’s typelibs were moved out of gobject-introspection package because I do not think GObject is actually needed for introspected libraries (as evidenced by cairo). Though I acknowledge that it will be required in at least 95% of uses and it does not make much difference to closure size so it is open to re-evaluation.


You may also need gobject-introspection for the setup-hook that sets GI_TYPELIB_PATH. We propagate it from pygobject3 wholesale (until we manage to get https://github.com/NixOS/nixpkgs/pull/308488 work) but I am not sure how it interacts with python3.withPackages.

Andy3153 commented 1 week ago

Ah, so it'll eventually be done. I understand.

jtojnar commented 1 week ago

No, there are currently no plans to change the current behaviour.

Andy3153 commented 1 week ago

I'll give a thumbs-up to the PR because, with how things "just work" in Nix, if you don't understand the underlying debate, it really just feels broken without this.

jtojnar commented 1 week ago

The gobject-introspection is already propagated by pygobject3.

The PR actually narrows the propagation so that only setupHook part of gobject-introspection introspection is propagated. So it will not help you – in fact, it might break packages relying on gobject-introspection itself propagating glib.

Andy3153 commented 1 week ago

I'm a little confused right now... what would even need to be done to be able to make my "Steps To Reproduce" work with just adding pygobjects3 to python-pkgs in the Python package for my devshell?

jtojnar commented 1 week ago

I would say the example should not work without also adding glib to packages explicitly.

Docs might need adjusting to make it clear.