Setting environment variables in Linux the proper way

Home

This post will show the way to set environment variables in Linux per user.

Setting environment variables in Linux has been easy on the command line. Just update your ~/.bashrc and you are good to go. While this is enough for terminal usage, the variables don’t get transfered to Emacs and other graphical software.

I had been struggling to find the correct way to fix this issue by using ~/.profile but it wasn’t working. I thought that ~/.profile was the main place for all system wide environment variables. While writing this post I found out that ~/.profile is actually the configuration for sh which is also compatible with bash. (source)

Finding a solution wasn’t easy. I had to search specifically for setting environment variables on Wayland which led me to a StackExchange answer thus resolving my issue. I just needed to add the variables in ~/.config/environment.d/*.conf.

My ~/.config/environment.d/env.conf:

EDITOR=/usr/bin/vim
QT_QPA_PLATFORMTHEME="qt5ct"
QT_AUTO_SCREEN_SCALE_FACTOR=0
GTK2_RC_FILES="$HOME/.gtkrc-2.0"

PATH="$HOME/.yarn/bin:$PATH"
PATH="$HOME/.local/bin:$PATH"

# emacs
EMACS_SERVER_FILE="$HOME/.emacs.d/.cache/server/server"
LSP_USE_PLISTS=true

# guix
GUIX_PROFILE="$HOME/.guix-profile"
GUIX_LOCPATH="$HOME/.guix-profile/lib/locale"
PATH=$GUIX_PROFILE/bin:$PATH

# flutter
CHROME_EXECUTABLE=/usr/bin/chromium
PATH="$HOME/.flutter-sdk/bin:$PATH"

# ruby
RBENV_ROOT=$HOME/.rbenv

Note

This works only on systemd which is the default for most Linux distributions.