Cannot run reproducible PyGObject example script

When I try to run the following script:

#! /usr/bin/env nix-shell
#! nix-shell -i python -p gtk4 python310 python310Packages.pygobject3
import gi
gi.require_version('Gtk', '4.0')
from gi.repository import Gtk

def on_activate(app):
    win = Gtk.ApplicationWindow(application=app)
    win.present()

app = Gtk.Application()
app.connect('activate', on_activate)
app.run(None)

I get the following error:
Traceback (most recent call last):
File “/home/elzair/Development/python/./nixtest.py”, line 4, in
gi.require_version(‘Gtk’, ‘4.0’)
File “/nix/store/gwq3inwc86axcxb82jpwkzci3hjpw1xd-python3.10-pygobject-3.42.2/lib/python3.10/site-packages/gi/init.py”, line 126, in require_version
raise ValueError(‘Namespace %s not available’ % namespace)
ValueError: Namespace Gtk not available

What seems to be the problem? Am I just missing some dependencies?

In this simple case, you need gobject-introspection for its setup hook to set GI_TYPELIB_PATH variable. But for more complex examples, you will need other variables as well, and that is not possible to express in shebang easily – a variable extraction similar to Python + Qt woes - #2 by jtojnar will be required.