Setting environment variables in Hyprland is easy: just put them in its config file. Setting environment variables in Sway is a bit harder, and there seems to be some confusion. The documentation is unclear about that, at least in my humble opinion.
This blog post documents my way of setting environment variables in Sway.
Let’s install qt6ct for some experiments.
1 |
sudo pacman -S qt6ct |
If we run it, we get the following problem:
Where can environment variables be specified when using Sway?
In my experiments, “~/.profile” is not loaded (either when logging in or using SDDM). Instead, “~/.bash_profile” and “~/.zshenv” should be used for BASH and ZSH, respectively.
So, in those files, source “~/.profile” explicitly
1 |
[[ -f ~/.profile ]] && . ~/.profile |
Let’s put the environment variable declaration and export into “~/.profile”
1 2 |
# For styling export QT_QPA_PLATFORMTHEME=qt5ct |
Exit Sway and get back in, and now qt6ct is happy:
If we want also to have our environment variables available to systemd, we can follow another strategy to specify environment variables in one place.
First of all, let’s create a file (or any, if you want to have several environment variables categorized in different files) that ends with “.conf” in the directory “~/.config/environment.d/”.
For example, I have “~/.config/environment.d/10-env.conf” (these files are read by systemd alphabetically); I move the environment variable definition in this file (so, I remove it from “~/.profile”):
1 2 |
# For styling QT_QPA_PLATFORMTHEME=qt5ct |
Note that in this file, you only have to use the syntax “key=value” (to see the full syntax: https://www.freedesktop.org/software/systemd/man/latest/environment.d.html)
Each time you modify that directory, you have to run this command:
1 |
systemctl --user daemon-reload |
To verify that systemd read this file, run
1 |
systemctl --user show-environment |
And you should see the line:
1 |
QT_QPA_PLATFORMTHEME=qt5ct |
If you restart Sway and run qt6ct, you will see that qt6ct complains again. The environment variable is only visible to the systemd. We have to export the variables read by systemd.
To avoid duplication, we can use the program “/usr/lib/systemd/user-environment-generators/30-systemd-environment-d-generator”, which does precisely that (of course, it handles all the environment variables in the “.conf” files in the environment.d directory). In “~/.profile” we now put this line:
1 |
export $(/usr/lib/systemd/user-environment-generators/30-systemd-environment-d-generator) |
Restart Sway and run qt6ct, and now everything went back to work!
You can also open a terminal from Sway and verify the environment variable is there!
I hope you find this blog post helpful 🙂