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 icon 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 (case sensitive) 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


Last updated 3 years ago.