Desktop files for menu integration

Desktop entry files are used to add an application to the desktop menu. These files specify the name and icon of your application, the categories it belongs to, related search keywords and more. These files have the extension .desktop and follow the XDG Desktop Entry Specification version 1.1.

Note: The application icon specified in the desktop entry will be used in the desktop menu and the dock, but not in the snap store and other graphical store frontends. The snap store uses the icon specified in the icon: field in snapcraft.yaml

This documentation explains how to add these desktop files to your snap so that your application is automatically added to the desktop menu during installation.

There are three methods to tell snapcraft which desktop entry files to use.

Desktop entry files in the `snap/gui` directory

The desktop file and logo should be in the folder snap/gui/ in the source folder for your snap. They should be named snap-name.desktop and snap-name.png where snap-name matches the name: entry in the snapcraft.yaml.

Note: When you run snapcraft, the entire contents of snap/gui will be copied into the meta/gui/ folder of the resulting snap.

The Exec= line is used to specify which application to run when the user clicks on this desktop entry. It should point to the application in the apps: section of snapcraft.yaml.

Exec=app-name

Where app-name matches the name of the program in the apps: section of snapcraft.yaml or an approved alias. Note that this is the same name used to run the snapped application from the commandline.

The Icon= line specifies the absolute path of the icon of the application. This icon will represent the application in the desktop menu and the dock. This path should point to the location of the icon after the snap is installed.

Icon=${SNAP}/meta/gui/snapname.png

Since snapcraft copies all the contents of the snap/gui/ folder to meta/gui, the absolute path of the icon in the installed snap will be ${SNAP}/meta/gui/snapname.png.

Use the `desktop` key to point to the entry file

Some applications already generate desktop files as part of the build process. In that case, it might be easier to use the desktop key of the application because this takes a path relative to the prime directory, so you can insert a path to the generated desktop entry file.

Using this method, the desktop entry file can have any name. During a build, snapcraft will properly rename the desktop launcher, based on which app definition the desktop key is part of.

In this example, the desktop file is generated by the build system and placed in the folder usr/share/applications/, relative from the root of the resulting snap. Since the prime folder is what eventually becomes the snap, we specify usr/share/applications/com.github.johnfactotum.Foliate.desktop as the path to the desktop file.

apps:
  foliate:
    command: desktop-launch $SNAP/usr/bin/com.github.johnfactotum.Foliate
    desktop: usr/share/applications/com.github.johnfactotum.Foliate.desktop
    plugs:
      - desktop
      - desktop-legacy
    ...

During a build, snapcraft will also try to change the Icon= path in the desktop entry file. However, you need to make sure that the Icon= path is accessible from the prime folder. This example replaces the icon path after pulling the source.

parts:
  foliate:
    plugin: meson
    meson-parameters: [--prefix=/snap/foliate/current/usr]
    override-pull: |
      snapcraftctl pull

      # Point icon to the correct location
      sed -i.bak -e 's|Icon=com.github.johnfactotum.Foliate|Icon=/usr/share/icons/hicolor/scalable/apps/com.github.johnfactotum.Foliate.svg|g' data/com.github.johnfactotum.Foliate.desktop.in
    ...

What happens during installation of your snap

During installation, snapd copies the desktop files of the snap to /var/lib/snapd/desktop/applications/. The keys DBusActivatable, TryExec and Implements are currently not supported and will be silently removed from the desktop file on install. Lines with unknown keys will also be silently removed.

Further reading

3 Likes

I was successfully able to make a .desktop file that launched a Chromium web app by placing a .desktop file in ~/.local/share/applications/. However it didn’t work to to include /snap/chromium/current in the Exec= line of the .desktop file. What worked was target a specific version with /snap/chromium/821 as a prefix on the Exec= line. This worked fine until Chromium automatically got upgraded. This broke the ~10 web apps I had created. It will be a real pain if I have to manually update the Exec= lines each time that Chromium upgrades. Is there a way I can get a stable Exec= line for .desktop files that reference my chromium snap?

Please note that if the Exec line is pointing directly into /snap/chromium/…, then you are launching that snap without confinement when this desktop file is used. To launch with confinement, you should use ‘snap run …’ or point to something in /snap/bin/…

Thanks. This is a bug in the Chromium snap then. To reproduce run the snap and then in Chromium go to More Tools > Create Shortcut. The result for me is a file in my ~/Desktop folder with an Exec line as follows:

Exec=/snap/chromium/861/usr/lib/chromium-browser/chrome "--profile-directory=Profile 1" --app-id=fkdicmhnifiabiocmjagmfcbdhcokhno

Is this an “confinement escape bug” since enticing the user to user to use such a shortcut would result in escaping confinement?

1 Like

This is indeed a confinement escape bug. Details in bug #1732482.