X.Org/xsession

Introduction
The official guide The X Server Configuration covers X startup, but here there are additional details. The X server can be started from a graphical display manager or from the command line. Gentoo is setup to use startx from the command line. There are other guides for display managers. Here we focus on the command line. Some information here overlaps with graphical display managers, especially using .xinitrc to specify X startup programs. Its also useful to know that KDE and Gnome have ways of specifying additional startup programs after they start. Refer to their documentation to find out about that.

startx
This details the execution steps of startx and the various files and variables to configure its behavior. startx is a shell script located in /usr/bin and depending on conditions described later, other shell scripts will be called. If you ever need more specific details, those scripts are brief and quite readable if you know basic shell scripting. This overview should be able to narrow your focus to only having to read a very small part of them.

startx first authorizes you to connect to the X server that is about to be started, by setting the XAUTHORITY environment variable. It gets some info from xauth, mcookie and pre-defined constants. It uses that information to store the magic cookie for authorization in ~/.serverauth.$$ (The $$ is a shell variable containing the scripts current process id (PID)).

It then runs xinit which handles the rest of the process of starting X. It waits for the xinit PID to exit (the same time the xserver exits) and then cleans up. It removes ~/.serverauth.$$ and calls xauth to remove the authority to connect to the xserver.

xinit
The default arguments startx passes to xinit are

startx picks the arguments to xinit by looking around at some configuration files, its own arguments, and defaults defined in the startx script.

xinit starts the X server and makes the first programs start. Its arguments work like this:

The default client is xterm, default server is "X". In the previously mentioned arguments, the default client is selected because "-nolisten" is seen as the start of the options because it starts with "-", a common convention to specify "option" arguments.

is a shell script. It loads system or user .Xresources with xrdb and .Xmodmap with xmodmap. These are additional settings for the X server, see the man or gentoo wiki pages for details.

Then it runs if it exists. A sample .xinitrc is described further down.

chooser.sh
If doesn't exist, it runs `/etc/X11/chooser.sh`, those backticks means it runs the stdout from that program (whatever it prints out). Or, if that is an empty string, it runs twm and some xterms. It doesn't just run these things, it execs them so they take over and replace the shell process running the script. This makes it so that startx is still looking at a running process that its waiting to finish so it can shut down the X server.

chooser.sh does various checks to find the most sane script to output for xinitrc to run. One of the first things it tries to output for xinit to run is: ${XSESSION}. Thus, you can choose what to run on startup by setting the XSESSION variable to an executable in.

$XSESSION
The variable XSESSION is set in. That file also has some informative comments about the variable. The preferred way to start KDE & Gnome is with XSESSION. There is also a script, which duplicates some of settings loading of xinitrc and also loads setxkbmap settings. It then looks for programs to exec as the first programs in X. It tries in order:, , , , xsm, or if the argument passed to it is "failsafe", just 1 xterm.

~/.xinitrc
The file is useful when not running a grapical login manager, or when X is started directly from an init script. The layout of is basiclly like any other script file. You can, in your file, export environment variables that only affect applications running over X, start background daemons, i.e xscreensaver, and finally and most commonly start the Window Manager.

This is a very simple that starts xscreensaver, exports the made up variable FOO and finally exectues the Fluxbox Window Manager.

Note the ampersand after, this lets X execute the command in the background and move on to the next. If you leave out the ampersand at the end of the line of the command, in this case xscreensaver, it will halt the script until xscreensaver exits. Also note the exec before, this is sometimes not needed, but should always be prefixing the last line of the script, executing the application you want to use and have it run until you exit/logout.

~/.xsession
It's often a good idea to symlink to point to. If you login using a graphical login manager, you have the option to use a "Custom Xsession" it which is usually, which will execute any symlink there, ie script.

Integrate Custom X Session with XDM
DWM or a custom X session(s) is sometimes preferred for reserving hardware resources for other tasks. However, there are times when you might want to use the standard xdm service. For example, the brilliant operator dies leaving his wife to figure out where all the records are on a computer without having a clue how to use DWM. If this happens, she will likely not know how to manage rc scripts.

1) Add an entry into Grub's Menu (or kernel boot parameter) specifying "gentoo=nox". Title this entry with something self explanatory "Gentoo NoX" or "Gentoo without Gnome".  Set this as the default entry if you prefer.  With this method, if you allow others to use your computer, they'll easily comprehend to choose another entry during boot -- other then ones titled as NoX or "without Gnome".  (For reference, I believe gentoo=nox is a x11-apps/xinit package related feature according to documentation or changelogs.)

2) Leave the xdm rc service as default. (ie. #rc-update add xdm default)

3) Configure DWM (or other XYZ desktop) as you wish using your own $HOME/.xinitrc file but do not symlink it to $HOME/.xsession, else, things may conflict when GDM starts. $HOME/.xsession is only executed when GDM or the standard xdm rc service is used.  $HOME/.xinitrc is ignored unless symlinked to $HOME/.xsession -- and you'll notice some recommending to just symlink them.

Using this method, a normal X session will still be easily run if a user so chooses during boot. Hence, allowing smarter people like us to choose to run their own X Session without the fear of losing all our wonderful photos forever.

X.Org/xsession