Environment variables in Sway

I’m starting experimenting with Sway. Coming from Hyprland, where setting environment variables is trivial (just put them in the main config file), I seem to understand that in Sway, instead, environment variables are hard to deal with.

I trie the EndeavourOS Sway edition, though I know it’s not “official”, the GitHub repo is still there, so I installed it from there.

I saw that that edition uses https://github.com/EndeavourOS-Community-Editions/sway/tree/main/.config/environment.d which I also tried myself in another VM to set environment variables in Sway, without success. Apparently, it doesn’t work in the Sway edition either.

Am I missing something?

Are the environment variables in .conf files inside ~/.config/environment.d meant to work?

thanks in advance

Will this help: https://github.com/swaywm/sway/wiki/Setting-environmental-variables ?

Look like you could set them in the config file in Sway as well.

I forgot to mention that link…

quite useless…

I’d like to avoid setting environment variables in things like .bash_profile; and of course setting them in /etc/environment sounds definitely wrong.

the other solution about ~/.pam_environment is deprecated and going to be removed.

Then we have

Set the Sway Config

Technically, you could set them in your sway config.

which is pure mistery! :smiley: What does that mean? And most of all, how could I set them in sway config? Running exec export ... doesn’t work.


User Service

You can set variables using the environment.d. Environment variables set there will be read by the --user systemd

I assumed that’s the solution used by the Sway community edition (see my original post), but it doesn’t work as I said. And I still don’t understand how it’s supposed to work…

1 Like

Will something like: set $variable_name "value" work?

That only defines a variable inside Sway configuration: it does not create/export an environment variable

1 Like

Sorry to bother again :blush:

In /etc/sway/config:


Logo key. Use Mod1 for Alt.

set $mod Mod4

Home row direction keys, like vim

set $left h
set $down j
set $up k
set $right l

Your preferred terminal emulator

set $term foot

Your preferred application launcher

Note: pass the final command to swaymsg so that the resulting window can be opened

on the original workspace that the command was run on.

set $menu dmenu_path | wmenu | xargs swaymsg exec –

But this is not what you have in mind?

I’m talking about environment variables, not about variables usable only in sway config.

For example, looking at https://github.com/EndeavourOS-Community-Editions/sway/blob/main/.config/environment.d/30-envvars.conf


Is not set if you open a terminal in sway. So it won’t be picked by other programs either. That’s just an example. To be clear, Firefox doesn’t need that anymore as it starts in Wayland by default.

1 Like

In my case, it would be in ~/.xprofile, which I use for my user only, /etc/environment is for system wide variables.

The most common way I have seen people configure their environment variables in Sway is to launch Sway with a wrapper script, as described in the Greetd wiki:


How to set XDG_SESSION_TYPE=wayland/…?

While you could set this in your .profile, the recommended way to handle this is with a wrapper script. Doing it this way allows you to start multiple login environments without having weird env vars mess with things.

  • /usr/local/bin/sway-run (should be made executable)

# Session
export XDG_SESSION_TYPE=wayland

# Wayland stuff
export QT_QPA_PLATFORM=wayland
export SDL_VIDEODRIVER=wayland

exec sway "$@"

# If you use systemd and want sway output to go to the journal, use this
# instead of the `exec sway "$@"` above:
#    exec systemd-cat --identifier=sway sway "$@"

Simply use sway-run instead of sway to log in.

Other than that, depending on if you decide to set up a display manager or not you may have other options available through the greeter. For example, the Greetd greeters ReGreet and nwg-hello allow setting environment variables directly in the config.

So, basically, what’s done here https://github.com/EndeavourOS-Community-Editions/sway doesn’t work, does it?

Does .xprofile work out of the box in Sway?

.xprofile works in X11 not in Wayland

That looks like it should work because SDDM has been set as the display manager, and ~/.profile is being sourced at login through a script in /usr/share/sddm/scripts/wayland-session:

By default, SDDM doesn’t source ~/.profile, but since it has been set up this way (to be explicitly sourced in the script) it should work fine.

~/.xprofile should not be getting sourced at all. It is typically used for X11 sessions, not Wayland.

But not what’s defined here https://github.com/EndeavourOS-Community-Editions/sway/tree/main/.config/environment.d

The environment variables specified there are ignored (see Environment variables in Sway - #7 by lorebett)

It seems ~/.profile works for both, X11 and Wayland.

That should work too, as long as systemd is in use.

After adding or modifying files in ~/.config/environment.d/ you may need to reload the systemd user services.

systemctl --user daemon-reload

~/.profile may need to be explicitly sourced in order to work, depending on how the system has been configured. For example, when Bash is invoked as a login shell, after reading /etc/profile it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile (I believe in that order), and reads and executes commands from the first one it finds. If ~/.profile is not the first one it finds, it won’t read it unless it is explicitly sourced elsewhere.

Some login managers like GDM or LightDM will automatically source ~/.profile, and some like SDDM will not.

But how to make sure it’s in use? Should I add that systemctl command in sway config as exec always?

Check with: systemctl --user show-environment

You can confirm it is working by checking a variable you are setting this way in the terminal.


If it isn’t working, check for problems.

systemctl --user status

No, you would only need to reload the daemon after making a change to the configuration file. Systemd should read that file automatically when you log in.

1 Like

Yes, it’s known that regarding your system configuration, you need to source it or not, in OP case, it should work in Wayland.

That’s right, ~/.profile is compatible with Wayland.

If you are explicitly sourcing the file, you can use any file you like. There is nothing special about ~/.profile in this regard. You can make a file called ~/barrel_of_monkeys, stick your environment variables in there, and source that file if you want to.