Skip to content

Environment Variables

I read on the wiki that I can set environment variables for GUI libraries . I was just wondering where would I set such a variable ; for example I am trying to set “GDK_BACKEND=x11”; where exactly would this go? Would it go in /etc/environment ? Or Somewhere else?

9 points12 comments
Open on Reddit
About this post
Posted
Archived
Score
9 · 100% up
Kind
Text
ID
d6ca8b

Author

8
posts
76
comments
2
media
View profile

12 comments

So many ways to approach this. My go-to strategy is to add a .sh file in /etc/profile.d/. In your case, a simple

/etc/profile.d/gdk.sh
GDK_BACKEND=x11
export GDK_BACKEND

should do the trick. Variables will be set in the environment after login.

5 points

Do I do this for every environment variable I want to set ? And will this work for wayland ?

1 points

For sure, this approach works for wayland too. And yes, you can do this for every environment variable you want. If there are a few variables that make sense to keep together, it's good practice to set & export them all from the same .sh file.

2 points

You can set it in /etc/environment or $HOME/.pam_environment as VARIABLE=value or in $HOME/.bash_profile, $HOME/.profile, or $HOME/.bashrc as export VARIABLE=value.

For Desktop Environment variables it might be better to set it in $HOME/.xinitrc or $HOME/.xprofile as export VARIABLE=value

Reference: Environment_variables xinitrc xprofile

2 points

Exporting them in ~/.bashrc does not make them available to GUI apps, only in terminal emulators/TTY and only if bash is your default shell.

Exporting from ~/.profile only works if you do not boot directly to a Display Manager but to a TTY shell instead. And even so, only if your default shell sources that file at login, which some shells do not (e.g. fish).

Exporting them in ~/.xprofile only applies if you use some Display Managers as it is explained in the wiki. If you use startx or xinit, you should export them in your ~/.xinitrc or source your ~/.xprofile from there. Some DMs only source ~/.xsession instead, so in that case, one should just symlink your ~/.xprofile to ~/.xsession.

Exporting them in ~/.xinitrc only works if one uses startx. Both ~/.xinitrc and ~/.xprofile are Xorg specific and won't work under Wayland or other Display Servers. I honestly have no idea what is the cleanest way of doing it under wayland and those are the kinds of things that keep me from it.

I know you were probably addressing the most comment scenario but I feel that I should include these corrections for the sake of completeness.

6 points

Yikes. This kind of thing has been biting me. Right now I've got .profile setting environment variables I want to always be set, I've got .bashrc setting my aliases and stuff for interactive shell use, and I've got .bash_profile sourcing .profile unconditionally, and sourcing .bashrc only if bash is running in interactive mode.

I think this is right. For a while ttys didn't have .bashrc sourced, until I realised that .bash_profile needed to source .bashrc. I forget whether it mattered if these files were set to executable and what their shebangs are. I also noticed different behaviour based on whether GDM was starting a wayland or Xorg gnome-shell session.

It's madness.

1 points

Does this also work when utilizing wayland ? I can just create the .xinitrc file and add it in that way ?

1 points

Ahh, my apologies. I overlooked that you wanted to use it with wayland. Systemd also has the ability to set environment variables that work in wayland according to this blog post. The man page for it seems pretty straightforward. Hope it works as expected.

2 points

Depends do you want them exported globally? Just for one specific application? Just for your user?

What part from https://wiki.archlinux.org/index.php/Environment_variables is unclear to you in this regard?

2 points

That’s the link I had read ; I want it globally so I edited the environment files and set the gdk backend . I was wondering if that was wrong or the correct way to do it . Since I simply just added to the environment file “GDK_BACKEND=x11”

1 points

Yes that is one of the ways to do it. Why would it be wrong? If you want to globally export them that is a file where it's good to go to, and since /etc/environment is read by PAM it should be invoked by every conceivable way to log in.

2 points
$ cat ~/.config/environment.d/myexemple.conf
MY_VAR=myvalue
2 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.