How to add custom launchers directories on Linux

On Linux, we create application launchers as files with the “.desktop” extension: they contain metadata about the application they are related to, and instructions about how the latter should be executed. In order for a launcher to appear in the application menu of a desktop environment, it must be placed in certain dedicated directories. In this tutorial, we learn what are the default target directories for application launchers according to the Freedesktop.org specification, and how to add custom ones.

In this tutorial you will learn:

  • What are the default launchers directories on Linux
  • How to add a new directory for desktop launchers
How to add custom launchers directories on Linux
How to add custom launchers directories on Linux
Category Requirements, Conventions or Software Version Used
System Distribution independent
Software No specific software required
Other Root privileges
Conventions # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux-commands to be executed as a regular non-privileged user

The Freedesktop.org specification

Freedesktop.org is a project aimed at establishing standard conventions and common functionalities among desktop environments on Linux and other Unix-like operating systems. Among the standards provided by Freedesktop.org, there is the XDG Base Directory specification, which defines where certain type of files should be looked for in the filesystem.



What interests us, in this case, are the directories defined by the XDG_DATA_DIRS environment variable, those under which data files are searched for. The data files we are talking about, in this case, are desktop launchers: files with the .desktop extension, compiled using instructions defined by the Desktop entry specification; they are used as application launchers in graphical environments.

Default launchers directories

According to the XDG Base Directory Specification, paths of data directories are specified via the XDG_DATA_DIRS environment variable. If this variable is not set, or is empty, default directories are used. They are, in order of priority: /usr/local/share and /usr/share. Additionally, user-specific data files are searched in the base-directory defined via the XDG_DATA_HOME variable, the default value being:  ~/.local/share.



If a subdirectory called “applications” exists under those directories, it is searched for desktop launchers: /usr/local/share/applications is conventionally used to host launchers for applications compiled from source, or self-contained third party applications installed under the /opt directory; the /usr/share/applications directory, instead, is used to host launchers for application installed with the package manager. Finally, the ~/.local/share/applications directory is used to host desktop launchers for application installed only for a specific user.

Adding a custom launchers directory

To add a custom directory for desktop launchers, all we have to do is to change the value of the XDG_DATA_DIRS variable. Just as an example, suppose we want to add the /opt/share/applications directory to the list; we can assign a value to the XDG_DATA_DIRS variable in the system-wide environment, by creating a file under the /etc/profile.d directory. Files with the “.sh” extension in this directory, are sourced from /etc/profile, via this snippet, on Red Hat family of distributions:

for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done



A similar logic exists on Debian-based distributions:

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

Just as an example, we will set the variable value in the /etc/profile.d/custom-launcher-dirs.sh file:

export XDG_DATA_DIRS="/opt/share:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"

In this case, we placed our custom directory before every existing paths in the XDG_DATA_DIRS, so that it is searched before the others. We used the “export” keyword to make the variable available to the child processes of the current shell, and used the ${parameter:-word} expansion, to ensure that  “/usr/local/share:/usr/share” (word) is explicitly used as a default value, if XDG_DATA_DIRS (parameter) is empty or not set. To make the change effective, we save the file, and we logout and login again.



Now that we added the directory, each correctly written file with the “.desktop” extension under its “applications” subdirectory, (so, /opt/share/applications), should appear in our desktop environment applications menu.

Conclusions

In this tutorial, we learned what the XDG_DATA_DIRS variable is used for in the context of the Freedesktop specification: the variable defines the directories under which data files are searched. On systems where the XDG_DATA_DIRS variable is not set or empty, the default is to use /usr/local/share and /usr/share. If a subdirectory called “applications” exists under either of those directories, it is searched for applications launchers, which are defined as files with the “.desktop” extension. Here, we learned how to add a custom directory for desktop launchers.



Comments and Discussions
Linux Forum