Using Xsession to Set an Environment Variable Without a Shell

The freedesktop project, arguably the most important Linux organization you’ve never heard of, has (among many other noble deeds) done an admirable job clearing up the mess of local cache and config files in one’s home directory.

Among the various specs it has published to enhance interoperability among desktop environments, the basedir spec outlines a set of directories where applications are expected to read and write temporary files and configuration information.

The defaults are .local/share for application data, .config for configuration information, and .cache for non-essential data files — all relative to the user’s home directory. (If you have been wondering recently where these directories have been coming from, now you know.)

The defaults can be overridden using environment variables. To do so can be desirable: these directories can become large, but because they are hidden, this fact easily goes unnoticed. One may therefore want to rename them into something visible, or place them into a different location. Because appropriate environment variables are already defined in the spec, this should be easy.

The challenge is to make sure that these environment variables are set globally, so that all applications see them. Any application that fails to receive them will fall back to the default locations, creating (or: re-creating) the directories in their default locations, if it doesn’t find them there already.

It’s of course trivial to set an environment variable within a shell. The problem is that, in a desktop environment, many applications are launched outside of an explicit shell — for example from the desktop environment’s start-up menu. How does one set environment variables for such a process?

Processes inherit the runtime environment (including environment variables) from their parent process. To ensure that all processes launched in a user session inherit the required environment variables, they must be set early. For a non-graphical login, they can be set in the “login shell”. But for a graphical (X11) login, there is no login shell. Instead, there is an Xsession.

This is simply a shell script that is executed, by the system, when a graphical user session is begun. Any environment variables set at this time will, unless later overridden, be shared by all processes launched by the user.

To allow easy user customization, the Xsession startup process looks for a file called .xessionrc in the user’s home directory. This file will be interpreted as a shell script, and any environment variables found there will be added to the global runtime environment. The syntax is export VAR="val".

So, long story short, create a file .xsessionrc in your home directory, and define any desired environment variables there. These settings will be available to all processes launched thereafter in the desktop session.

The Xsession page on the Debian Wiki has more details on the way a graphical user session is started. The freedesktop basedir-spec spells out the expected directories, their default locations, and the environment variables to customize them.