GTK3 applications

Why are snaps good for GTK3 applications?

Snapcraft bundles necessary libraries required by the application, and can configure the environment for confinement of applications for end user peace of mind. Developers can ensure their application is delivered pre-packaged with libraries which will not be replaced or superseded by a distribution vendor.

Here are some snap advantages that will benefit many GTK applications:

  • Snaps are easy to discover and install Millions of users can browse and install snaps graphically in the Ubuntu Software Center, the Snap Store or from the command-line.
  • Snaps install and run the same across Linux They bundle the exact version of whatever is required, along with all of your app’s dependencies, be they binaries or system libraries.
  • You control the release schedule You decide when a new version of your application is released without having to wait for distributions to catch up.
  • Snaps automatically update to the latest version Four times a day, users’ systems will check for new versions and upgrade in the background.
  • Upgrades are not disruptive Because upgrades are not in-place, users can keep your app open as it’s upgraded in the background.
  • Upgrades are safe If your app fails to upgrade, users automatically roll back to the previous revision.

Build a snap in 20 minutes

Typically this guide will take around 20 minutes and will result in a working GTK application in a snap. Once complete, you’ll understand how to package GTK3 applications as snaps and deliver them to millions of Linux users. After making the snap available in the store, you’ll get access to installation metrics and tools to directly manage the delivery of updates to Linux users.

For a brief overview of the snap creation process, including how to install snapcraft and how it’s used, see Snapcraft overview. For a more comprehensive breakdown of the steps involved, take a look at Creating a snap.

Getting started

Snaps are defined in a single YAML file placed in the root folder of your project. The following example shows the entire snapcraft.yaml file for Foliate. Don’t worry, we’ll break this down.

Foliate Snap

Snaps are defined in a single yaml file placed in the root of your project. The Foliate example shows the entire snapcraft.yaml for an existing project. We’ll break this down.

snapcraft.yaml for Foliate
name: foliate
grade: stable
adopt-info: foliate
license: GPL-3.0+

base: core20
confinement: strict

apps:
  foliate:
    command: usr/bin/com.github.johnfactotum.Foliate
    extensions: [gnome-3-38]
    plugs:
      - home
    slots:
      - dbus-daemon
    common-id: com.github.johnfactotum.Foliate

parts:
  foliate:
    plugin: meson
    source: https://github.com/johnfactotum/foliate.git
    source-branch: 1.x 
    meson-parameters: [--prefix=/snap/foliate/current/usr]
    override-pull: |
      snapcraftctl pull
      sed -i -e 's|@GJS@|/usr/bin/gjs|g' src/com.github.johnfactotum.Foliate.in
    build-packages:
      - libgjs-dev
      - gettext
    stage-packages:
      - gjs
    organize:
      snap/foliate/current/usr: usr 
    parse-info: [usr/share/metainfo/com.github.johnfactotum.Foliate.appdata.xml]

slots:
  dbus-daemon:
    interface: dbus
    bus: session
    name: com.github.johnfactotum.Foliate

layout:
  /usr/bin/gjs:
    symlink: $SNAP/usr/bin/gjs

The name must be unique in the Snap Store. Valid snap names consist of lower-case alphanumeric characters and hyphens. They cannot be all numbers and they also cannot start or end with a hyphen.

You can also fill in title, version, summary, description and icon. However, Foliate already has this metadata defined using an AppStream metadata file com.github.johnfactotum.Foliate.appdata.xml, so we don’t want to duplicate this data. We use adopt-info to tell Snapcraft to get the metadata from the foliate part. More on this later.

Base

The base keyword defines a special kind of snap that provides a run-time environment with a minimal set of libraries that are common to most applications. They’re transparent to users, but they need to be considered, and specified, when building a snap.

base: core20

core20 is a standard base for snap building and is built on Ubuntu 20.04 LTS.

Security model

We’re going to use strict confinement for Foliate. Strictly confined snaps run in complete isolation, up to a minimal access level that’s deemed always safe.

confinement: strict

Unconfined applications, specified with devmode, are useful while you build a working snap. Devmode snaps cannot be released to the stable channel, do not appear in search results, and do not automatically refresh. But after you get the snap working in devmode confinement, you can switch to strict mode and figure out which interfaces (plugs) the snap uses.

Apps

Apps are the commands and services exposed to end users. If your command name matches the snap name, users will be able run the command directly. If the names differ, then apps are prefixed with the snap name (foliate.command-name, for example). This is to avoid conflicting with apps defined by other installed snaps.

If you don’t want your command prefixed you can request an alias for it on the Snapcraft forum. These are set up automatically when your snap is installed from the Snap Store.

apps:
  foliate:
    command: usr/bin/com.github.johnfactotum.Foliate
    extensions: [gnome-3-38]
    plugs:
      - home
    slots:
      - dbus-daemon
    common-id: com.github.johnfactotum.Foliate

This application uses the gnome-3-38 extension. This will make GTK and Gnome libraries available to the snap at runtime and it will configure the runtime environment of the application so that all desktop functionality is correctly initialised.

The common-id property is used to link this application to the AppStream metadata specified further down below. This will cause this app to use the .desktop launcher specified in the AppStream file.

Parts

Parts define how to build your app. Parts can be anything: programs, libraries, or other assets needed to create and run your application. In this case we have one: the Foliate source release tarball. In other cases these can point to local directories, remote git repositories or other revision control systems.

Before building the part, the dependencies listed as build-packages are installed. The Meson plugin then uses meson, ninja build and ninja install to build the part.

parts:
  foliate:
    plugin: meson
    source: https://github.com/johnfactotum/foliate.git
    source-branch: 1.x 
    meson-parameters: [--prefix=/snap/foliate/current/usr]
    override-pull: |
      snapcraftctl pull
      sed -i -e 's|@GJS@|/usr/bin/gjs|g' src/com.github.johnfactotum.Foliate.in
    build-packages:
      - libgjs-dev
      - gettext
    stage-packages:
      - gjs
    organize:
      snap/foliate/current/usr: usr 
    parse-info: [usr/share/metainfo/com.github.johnfactotum.Foliate.appdata.xml]

override-pull isn’t often needed but it replaces the default pull lifecycle step with our own actions. We’re using it to augment the default snapcraftctl pull step with a sed command to replace the GJS path embedded within the launch file with the path to gjs after it’s installed as a stage-package within the snap.

built-packages are the packages needed for a project to successfully build. This will be listed in a project’s documentation, or known by the development team.

stage-packages are required by Foliate to run, and mirror the same packages required by the binary on a standard distribution installation. We only need to install the gjs package, as mentioned earlier.

parse-info points to the AppStream metadata file. Since we used adopt-info: foliate in the top-level metadata, the AppStream file of the foliate part will be used to fill in the summary, description and icon of this snap. See Using AppStream metadata for more information.

Slots

Many GTK3 applications require access to DBus in order to run correctly. However, snap blocks this access by default so you need to explicitly define that this application is allowed access to dbus.

slots:
  dbus-daemon:
    interface: dbus
    bus: session
    name: com.github.johnfactotum.Foliate

Layouts

The final section uses Snap layouts to make the gjs executable appear in the correct location within the snap:

layout:
  /usr/bin/gjs:
    symlink: $SNAP/usr/bin/gjs

Building the snap

To build the snap, create a new directory and run snapcraft init inside it. This will create a template snapcraft.yaml inside a snap directory:

$ mkdir foliate
$ cd foliate
$ snapcraft init
Created snap/snapcraft.yaml.
Go to https://docs.snapcraft.io/the-snapcraft-format/8337 for more information about the snapcraft.yaml format.

Replace the contents of snap/snapcraft.yaml with our example above. You can now build the snap by running the snapcraft command:

$ snapcraft
[...]
Snapping |
Snapped foliate_1.5.3_amd64.snap

The resulting snap can be installed locally. This requires the --dangerous flag because the snap is not signed by the Snap Store. If we’d built the snap with devmode confinement, we’d also have to add the --devmode flag:

$  sudo snap install ./foliate*.snap --dangerous
foliate 1.5.3 installed

You can then try it out:

$ foliate

Removing the snap is simple too:

$  sudo snap remove foliate

You can clean up the build environment with the following command:

$ snapcraft clean

By default, when you make a change to snapcraft.yaml, snapcraft only builds the parts that have changed. Cleaning a build, however, forces your snap to be rebuilt in a clean environment and will take longer.

Publishing your snap

To share your snaps you need to publish them in the Snap Store. First, create an account on the dashboard. Here you can customise how your snaps are presented, review your uploads and control publishing.

You’ll need to choose a unique “developer namespace” as part of the account creation process. This name will be visible by users and associated with your published snaps.

Make sure the snapcraft command is authenticated using the email address attached to your Snap Store account:

$ snapcraft login

Reserve a name for your snap

You can publish your own version of a snap, provided you do so under a name you have rights to. You can register a name on dashboard.snapcraft.io, or by running the following command:

$ snapcraft register mysnap

Be sure to update the name: in your snapcraft.yaml to match this registered name, then run snapcraft again.

Upload your snap

Use snapcraft to push the snap to the Snap Store.

$ snapcraft upload --release=edge mysnap_*.snap

If you’re happy with the result, you can commit the snapcraft.yaml to your GitHub repo and turn on automatic builds so any further commits automatically get released to edge, without requiring you to manually build locally.

Congratulations! You’ve just built and published your first GTK 3 snap. For a more in-depth overview of the snap building process, see Creating a snap.


Last updated 11 months ago.