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?
About this post
- Posted
- Archived
- Score
- 9 · 100% up
- Kind
- Text
- ID
- d6ca8b
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 simpleshould do the trick. Variables will be set in the environment after login.
Do I do this for every environment variable I want to set ? And will this work for wayland ?
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.
You can set it in
/etc/environmentor$HOME/.pam_environmentasVARIABLE=valueor in$HOME/.bash_profile,$HOME/.profile, or$HOME/.bashrcasexport VARIABLE=value.For Desktop Environment variables it might be better to set it in
$HOME/.xinitrcor$HOME/.xprofileasexport VARIABLE=valueReference: Environment_variables xinitrc xprofile
Exporting them in
~/.bashrcdoes not make them available to GUI apps, only in terminal emulators/TTY and only if bash is your default shell.Exporting from
~/.profileonly 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
~/.xprofileonly applies if you use some Display Managers as it is explained in the wiki. If you usestartxorxinit, you should export them in your~/.xinitrcor source your~/.xprofilefrom there. Some DMs only source~/.xsessioninstead, so in that case, one should just symlink your~/.xprofileto~/.xsession.Exporting them in
~/.xinitrconly works if one usesstartx. Both~/.xinitrcand~/.xprofileare 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.
Yikes. This kind of thing has been biting me. Right now I've got
.profilesetting environment variables I want to always be set, I've got.bashrcsetting my aliases and stuff for interactive shell use, and I've got.bash_profilesourcing.profileunconditionally, and sourcing.bashrconly if bash is running in interactive mode.I think this is right. For a while ttys didn't have
.bashrcsourced, until I realised that.bash_profileneeded 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.
Does this also work when utilizing wayland ? I can just create the .xinitrc file and add it in that way ?
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.
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?
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”
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.