Session start
Traditional session setup
Under X11, starting a graphical session typically involves running a tower of shell scripts starting with /etc/X11/xinit/xinitrc or similar. Another noteworthy point is that the shell running these scripts is a login shell, which means that is sources /etc/profile, ~/.profile, and so on.
These scripts traditionally have a number of tasks:
- Setting up environment variables
- Starting session services, such as input method servers
- Loading X resources
Environment variables are problematic for various reasons (not the least that they are (often unintentionally) inherited along the process tree), and there is no good mechanism for updating them. But nevertheless, setting up some environment variables such as DISPLAY or LANG is required for a fully functional desktop session, and this will likely continue to be the case.
Distributions usually provide a way for third-party packages to execute shell code in the X session initialization (for example /etc/xinitrc.d on Red Hat derivatives, /etc/X11/Xsession.d on Debian derivatives). https://codesearch.debian.net/perpackage-results/Xsession.d demonstrates some of the things this is used for. (Is there a Red Hat equivalent?)
Wayland alternatives
The wayland session is not running a login shell and is not sourcing /etc/X11/xinit/xinitrc.
Environment variables
The simplest way to set environment variables for a single user under systemd (version 233 or later), is to create a .conf file in ~/.config/environment.d, containing lines of the form NAME=VAL. See the environment.d documentation for more details.
GDM also supports the /usr/share/gdm/env.d/ drop-in directory. Files dropped in there also contain lines of the form NAME=VAL. These affect all users of the system.
A complication with environment variables is that both systemd -user and D-Bus have their own 'activation environments': the environments that are inherited by the services they start. Use dbus-update-activation-environment --systemd DISPLAY LANG WHATEVER or dbus-update-activation-environment --systemd --all to update both.
Starting session services
For starting services, there are various alternatives: D-Bus activation, systemd user services, xdg autostart files. Autostart files and systemd services can have conditions that control their activation.