Score:4

How to add a folder with .desktop files?

cn flag

I know that if you want to add a .desktop entry it must be added in /usr/share/applications, however, what I want is to add an extra folder where it can host .desktop files.

If I run update-desktop-database you can see that programs like snapd or flatpak create their own directories where they host their .desktop

The databases in [/usr/share/ubuntu/applications, /home/cactus/.local/share/flatpak/exports/share/applications, /var/lib/flatpak/exports/share/applications, /usr/local/share/applications, /usr/share/applications, /var/lib/snapd/desktop/applications] could not be updated.

My idea is to add a folder /opt/applications where I can save .desktop of my self-compiled applications.

Score:10
hr flag

According to GNOME Desktop System Administration Guide Customizing Menus : Desktop Entry Files

Desktop entry files must reside in the $XDG_DATA_DIRS/applications directory and must have a .desktop file extension. If $XDG_DATA_DIRS 1 is not set, then the default path is /usr/share is used. This also implies that user specific desktop entries may be located at $XDG_DATA_HOME/applications which is searched first. If $XDG_DATA_HOME is not set, then the default path ~/.local/share is used. Desktop entries are collected from all directories in the $XDG_DATA_DIRS environment variable. Directories which appear first in $XDG_DATA_DIRS are given precedence when there are several .desktop files with the same name.

1 $XDG_DATA_DIRS is the environment variable defined in the XDG base directory specification.

So if $XDG_DATA_HOME does not say otherwise, you can place per-user .desktop files in ~/.local/share/applications. If you want a system-wide custom location, you should be able to add it to $XDG_DATA_DIRS ex.

XDG_DATA_DIRS=/opt:$XDG_DATA_DIRS

See also XDG Base Directory Specification

cn flag
That works perfect. What I did was add the following in /etc/profile: "XDG_DATA_DIRS=/opt:$XDG_DATA_DIRS". Now all applications in /opt/applications are loaded by gnome, and I can see them in the launcher. Many thanks!
vanadium avatar
cn flag
A more elegant approach rather than directly editing a system file that may be overwritten by an update would be to add a one-liner script in /etc/profile.d. All these scripts are sourced by /etc/profile
Score:5
ng flag

You can use ~/.local/share/applications under your user's home folder to keep .desktop files that you want to have available for that user only.

cn flag
Thanks very much for the answer, but that's not what I want to do. My idea is to have the applications isolated, to be able to copy them easily from one computer to another. However, the answer below is exactly what I was looking for. Thanks again.
muru avatar
us flag
@SebastiánCastro you can put a directory *inside* `~/.local/share/applications` (e.g., `~/.local/share/applications/my-special-applications`). Personally, I'd do that instead of modifying environment variables - you just need to copy the directory in the new system's `~/.local/share/applications`.
Score:2
cn flag

Not a fundamental answer to your actual question (i.e., how to add your custom directory to host .desktop launchers), but I suggest you could use /usr/local/share/applications for launchers of your self compiled applications that should be available system wide.

This directory is configured by default in Ubuntu in XDG_DATA_DIRS, and its purpose is exactly for what you are looking for, i.e., make your manually installed .desktop files available in the menu systems of any user.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.