How to access environement variable inside a Gnome-Shell extension

644 views Asked by At

I want to access environment variable from a Gnome-Shell extension.

Some context: I want to create an extension adding "Xorg" or "Wayland" to the panel, based upon the environment variable $XDG_SESSION_TYPE. I have to do this because I frequently switch (Wine has issues with Wayland), and I don't always remember which type of session is running.

2

There are 2 answers

2
cronvel On

I was able to find how to get environment variables in the source code of the Argos extension:

const GLib = imports.gi.GLib ;
const rawEnv = GLib.get_environ() ;

Also the environment variable are stored as an array of strings and should be parsed, i.e.: split on the first '=' occurrence to separate the key and the value.

EDIT: GLib.getenv('ENV_VAR_NAME') is more convenient because it avoids parsing the environment variable (thanks to @andyholmes).

0
GdH On

Another way to detect Wayland is to ask the window manager:

Meta.is_wayland_compositor()

Tested in GS 3.36, 3.38 and 40.

https://gjs-docs.gnome.org/meta3~3.30.2/meta.is_wayland_compositor

Edit: From extension only, not available from prefs.