How can I use LightDM for user-defined sessions?

Create a new file /usr/share/xsessions/custom.desktop with:

[Desktop Entry]
Name=Xsession
Exec=/etc/X11/Xsession

You should now have a new session option during login, Xsession will load the user's ~/.xinitrc.


Create a new file /usr/share/xsessions/default.desktop (The name is up to you, but it has to have .desktop suffix):

[Desktop Entry]
Name=Default
Comment=This runs user session and logs you into Ubuntu
Exec=default
Icon=

Now when you select this session in lightdm and log in, your ~/.xsession file, if exists, will be executed.

Note that the user's file that is executed is ~/.xsession, and not ~/.xinitrc, which was mentioned in the question. (~/.xinitrc is used when the X session is started using the startx script from a command line.)

Some explanations: The Name entry is what lightdm would display for this session. The Exec entry is the important thing, and it should be the name of the program that starts the actual session. When you log in, lightdm calls the /etc/X11/Xsession script, passing it the value of Exec as an argument, and Xsession will, eventually, execute this program (for example, it could be startxfce4 for starting a xfce4 session). If the Exec entry is the special string default, then Xsession will execute the user's ~/.xsession file. (Xsession would also execute ~/.xsession if it's called without aruments.)

Sorry, I'm new here and hence cannot comment on other people's answers, but I'd like to comment on the accepted answer, to make things clearer. As per my explanation above, what will happen with that answer's method is that Xsession would be called by lightdm with an argument which is (the full path of) Xsession. So Xession will run and then call itself, this time with no arguments. The second Xsession, being called with no arguments, will execute the user's ~/.xsession. So the desired action will, eventually, be taken, but in a bit awkward way.

Tags:

Lightdm