|
| 1 | +## Why are snaps good for Qt5 and KDE Frameworks applications? |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +Here are some snap advantages that will benefit many Qt applications: |
| 6 | + |
| 7 | +* **Snaps are easy to discover and install** |
| 8 | + Millions of users can browse and install snaps graphically in the Ubuntu Software Center, the Snap Store or from the command-line. |
| 9 | +* **Snaps install and run the same across Linux** |
| 10 | + They bundle the latest version of Qt5 and KDE Frameworks, along with all of your app's dependencies, be they binaries or system libraries. |
| 11 | +* **You control the release schedule** |
| 12 | + You decide when a new version of your application is released without having to wait for distributions to catch up. |
| 13 | +* **Snaps automatically update to the latest version** |
| 14 | + Four times a day, users' systems will check for new versions and upgrade in the background. |
| 15 | +* **Upgrades are safe** |
| 16 | + If your app fails to upgrade, users automatically roll back to the previous revision. |
| 17 | + |
| 18 | +### Build a snap in 20 minutes |
| 19 | + |
| 20 | +Typically this guide will take around 20 minutes and will result in a working Qt5 application in a snap. Once complete, you'll understand how to package Qt5 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. |
| 21 | + |
| 22 | +> ⓘ For a brief overview of the snap creation process, including how to install *snapcraft* and how it's used, see [Snapcraft overview](/t/snapcraft-overview/8940). For a more comprehensive breakdown of the steps involved, take a look at [Creating a snap](/t/creating-a-snap/6799). |
| 23 | +
|
| 24 | +## Getting started |
| 25 | + |
| 26 | +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 KCalc. Don't worry, we’ll break this down. |
| 27 | + |
| 28 | +### KCalc Snap |
| 29 | + |
| 30 | +Snaps are defined in a single yaml file placed in the root of your project. The Kcalc example shows the entire snapcraft.yaml for an existing project. We'll break this down. |
| 31 | + |
| 32 | +[details=snapcraft.yaml for KCalc] |
| 33 | + |
| 34 | +```yaml |
| 35 | +name: kcalc |
| 36 | +version: '19.08.0' |
| 37 | +grade: stable |
| 38 | +adopt-info: kcalc |
| 39 | + |
| 40 | +confinement: strict |
| 41 | +base: core18 |
| 42 | + |
| 43 | +apps: |
| 44 | + kcalc: |
| 45 | + common-id: org.kde.kcalc.desktop |
| 46 | + command: kcalc |
| 47 | + extensions: |
| 48 | + - kde-neon |
| 49 | + plugs: |
| 50 | + - kde-frameworks-5-plug |
| 51 | + - home |
| 52 | + - opengl |
| 53 | + - network |
| 54 | + - network-bind |
| 55 | + - pulseaudio |
| 56 | + |
| 57 | +slots: |
| 58 | + session-dbus-interface: |
| 59 | + interface: dbus |
| 60 | + name: org.kde.kcalc.desktop |
| 61 | + bus: session |
| 62 | + |
| 63 | +parts: |
| 64 | + kcalc: |
| 65 | + parse-info: |
| 66 | + - usr/share/metainfo/org.kde.kcalc.appdata.xml |
| 67 | + build-snaps: |
| 68 | + - kde-frameworks-5-core18-sdk |
| 69 | + - kde-frameworks-5-core18 |
| 70 | + plugin: cmake |
| 71 | + build-packages: |
| 72 | + - libmpfr-dev |
| 73 | + - libgmp-dev |
| 74 | + - kdoctools-dev |
| 75 | + stage-packages: |
| 76 | + - libmpfr6 |
| 77 | + - libgmp10 |
| 78 | + source: https://download.kde.org/stable/applications/19.08.0/src/kcalc-19.08.0.tar.xz |
| 79 | + configflags: |
| 80 | + - "-DKDE_INSTALL_USE_QT_SYS_PATHS=ON" |
| 81 | + - "-DCMAKE_INSTALL_PREFIX=/usr" |
| 82 | + - "-DCMAKE_BUILD_TYPE=Release" |
| 83 | + - "-DENABLE_TESTING=OFF" |
| 84 | + - "-DBUILD_TESTING=OFF" |
| 85 | + - "-DKDE_SKIP_TEST_SETTINGS=ON" |
| 86 | +``` |
| 87 | +
|
| 88 | +[/details] |
| 89 | +
|
| 90 | +#### Metadata |
| 91 | +
|
| 92 | +The `snapcraft.yaml` file starts with a small amount of human-readable metadata, which usually can be lifted from the GitHub description or project README.md. This data is used in the presentation of your app in the Snap Store. |
| 93 | + |
| 94 | +```yaml |
| 95 | +name: kcalc |
| 96 | +version: '19.08.0' |
| 97 | +grade: stable |
| 98 | +adopt-info: kcalc |
| 99 | +``` |
| 100 | + |
| 101 | +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. |
| 102 | + |
| 103 | +The `version` is a "human readable" version string. It contains no semantic meaning, its purpose is to inform users of which version of the application they are installing. |
| 104 | + |
| 105 | +You can also fill in the `title`, `summary` and `description`. However, KCalc already has this metadata defined using an [AppStream](https://www.freedesktop.org/wiki/Distributions/AppStream/) metadata file `org.kde.kcalc.appdata.xml`, so we don't want to duplicate this data. We use `adopt-info` to tell Snapcraft to get the metadata from the part itself. More on this later. |
| 106 | + |
| 107 | +#### Base |
| 108 | + |
| 109 | +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. |
| 110 | + |
| 111 | +```yaml |
| 112 | +base: core18 |
| 113 | +``` |
| 114 | + |
| 115 | +[`core18`](https://snapcraft.io/core18) is the current standard base for snap building and is based on [Ubuntu 18.04 LTS](http://releases.ubuntu.com/18.04/). |
| 116 | + |
| 117 | +#### Security model |
| 118 | + |
| 119 | +To get started, we won’t [confine](/t/snap-confinement/6233) this application. Unconfined applications, specified with `devmode`, can only be released to the hidden “edge” channel where you and other developers can install them. After you get the snap working in `devmode` confinement, you can switch to strict mode and figure out which interfaces (plugs) the snap uses. |
| 120 | + |
| 121 | +```yaml |
| 122 | +confinement: devmode |
| 123 | +``` |
| 124 | + |
| 125 | +#### Parts |
| 126 | + |
| 127 | +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 two: the KCalc source release tarball and a number of runtime dependencies of KCalc. In other cases these can point to local directories, remote git repositories or other revision control systems. |
| 128 | + |
| 129 | +Before building the part, the build dependencies listed as `build-packages` and `build-snaps` are installed. [The CMake plugin](/t/the-cmake-plugin/8621) then uses `cmake` to build the part. The `kde-frameworks-5-core18-sdk` snap contains most build dependencies to build Qt5 and KDE applications. However, this snap also requires some tools from the `kde-frameworks-5-core18` runtime itself. |
| 130 | + |
| 131 | +```yaml |
| 132 | +parts: |
| 133 | + kcalc: |
| 134 | + parse-info: |
| 135 | + - usr/share/metainfo/org.kde.kcalc.appdata.xml |
| 136 | + plugin: cmake |
| 137 | + build-snaps: |
| 138 | + - kde-frameworks-5-core18-sdk |
| 139 | + - kde-frameworks-5-core18 |
| 140 | + build-packages: |
| 141 | + - libmpfr-dev |
| 142 | + - libgmp-dev |
| 143 | + - kdoctools-dev |
| 144 | + stage-packages: |
| 145 | + - libmpfr6 |
| 146 | + - libgmp10 |
| 147 | + source: https://download.kde.org/stable/applications/19.08.0/src/kcalc-19.08.0.tar.xz |
| 148 | + configflags: |
| 149 | + - "-DKDE_INSTALL_USE_QT_SYS_PATHS=ON" |
| 150 | + - "-DCMAKE_INSTALL_PREFIX=/usr" |
| 151 | + - "-DCMAKE_BUILD_TYPE=Release" |
| 152 | + - "-DENABLE_TESTING=OFF" |
| 153 | + - "-DBUILD_TESTING=OFF" |
| 154 | + - "-DKDE_SKIP_TEST_SETTINGS=ON" |
| 155 | +``` |
| 156 | + |
| 157 | +`stage-packages` are the packages required by KCalc to run, and mirror the same packages required by the binary on a standard distribution installation. |
| 158 | + |
| 159 | +`parse-info` points to the AppStream metadata file. Since we used `adopt-info: kcalc` in the metadata, the AppStream file of the `kcalc` part will be used to fill in the `title`, `summary` and `description` of this snap. See [Using AppStream metadata](/t/using-external-metadata/4642#heading--appstream) for more information. |
| 160 | + |
| 161 | +#### Apps |
| 162 | + |
| 163 | +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` (`KCalc.command-name`, for example). This is to avoid conflicting with apps defined by other installed snaps. |
| 164 | + |
| 165 | +If you don’t want your command prefixed you can request an alias for it on the [Snapcraft forum](https://forum.snapcraft.io/t/process-for-reviewing-aliases-auto-connections-and-track-requests/455). These are set up automatically when your snap is installed from the Snap Store. |
| 166 | + |
| 167 | +```yaml |
| 168 | +apps: |
| 169 | + kcalc: |
| 170 | + common-id: org.kde.kcalc.desktop |
| 171 | + command: kcalc |
| 172 | + extensions: |
| 173 | + - kde-neon |
| 174 | + plugs: |
| 175 | + - home |
| 176 | + - opengl |
| 177 | + - network |
| 178 | + - network-bind |
| 179 | + - pulseaudio |
| 180 | +``` |
| 181 | + |
| 182 | +You can see we use the [`kde-neon` extension](/t/the-kde-neon-extension/13752). This extension will make Qt5 and KDE libraries available to the snap at run time and it will configure the run time environment of the application so that all desktop functionality is correctly initialised. |
| 183 | + |
| 184 | +The `common-id` field is used to link the AppStream metadata to this application. As a result, we don't need to [manually specify the `.desktop` entry file](/t/desktop-files-for-menu-integration/13115) because it's already defined in AppStream. See [Using AppStream metadata](/t/using-external-metadata/4642#heading--appstream) for more information. |
| 185 | + |
| 186 | + |
| 187 | +### Building the snap |
| 188 | + |
| 189 | +You can download the example repository with the following command: |
| 190 | + |
| 191 | +```bash |
| 192 | +$ git clone https://github.com/galgalesh/kcalc.git |
| 193 | +``` |
| 194 | + |
| 195 | +After you've created the *snapcraft.yaml*, you can build the snap by simply executing the *snapcraft* command in the project directory: |
| 196 | + |
| 197 | +```bash |
| 198 | +$ snapcraft |
| 199 | +Using 'snapcraft.yaml': Project assets will be searched for from the 'snap' directory. |
| 200 | +Launching a VM. |
| 201 | +[...] |
| 202 | +Snapped kcalc_19.08.0_amd64.snap |
| 203 | +``` |
| 204 | + |
| 205 | +> :warning: The extension used in this example currently only works on amd64 systems. Other architectures like arm are not supported. |
| 206 | + |
| 207 | +The resulting snap can be installed locally. This requires the `--dangerous` flag because the snap is not signed by the Snap Store. The `--devmode` flag acknowledges that you are installing an unconfined application: |
| 208 | + |
| 209 | +```bash |
| 210 | +$ sudo snap install kcalc_19.08.0_amd64.snap --devmode --dangerous |
| 211 | +``` |
| 212 | + |
| 213 | +You can then try it out: |
| 214 | + |
| 215 | +```bash |
| 216 | +$ snap run kcalc |
| 217 | +``` |
| 218 | + |
| 219 | +Removing the snap is simple too: |
| 220 | + |
| 221 | +```bash |
| 222 | +$ sudo snap remove kcalc |
| 223 | +``` |
| 224 | + |
| 225 | +You can clean up the build environment with the following command: |
| 226 | + |
| 227 | +```bash |
| 228 | +$ snapcraft clean |
| 229 | +``` |
| 230 | + |
| 231 | +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. |
| 232 | + |
| 233 | +## Publishing your snap |
| 234 | + |
| 235 | +To share your snaps you need to publish them in the Snap Store. First, create an account on [the dashboard](https://dashboard.snapcraft.io/dev/account/). Here you can customise how your snaps are presented, review your uploads and control publishing. |
| 236 | + |
| 237 | +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. |
| 238 | + |
| 239 | +Make sure the `snapcraft` command is authenticated using the email address attached to your Snap Store account: |
| 240 | + |
| 241 | +```bash |
| 242 | +$ snapcraft login |
| 243 | +``` |
| 244 | + |
| 245 | +### Reserve a name for your snap |
| 246 | + |
| 247 | +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](https://dashboard.snapcraft.io/register-snap/), or by running the following command: |
| 248 | + |
| 249 | +```bash |
| 250 | +$ snapcraft register mysnap |
| 251 | +``` |
| 252 | + |
| 253 | +Be sure to update the `name:` in your `snapcraft.yaml` to match this registered name, then run `snapcraft` again. |
| 254 | + |
| 255 | +### Upload your snap |
| 256 | + |
| 257 | +Use snapcraft to push the snap to the Snap Store. |
| 258 | + |
| 259 | +```bash |
| 260 | +$ snapcraft upload --release=edge mysnap_*.snap |
| 261 | +``` |
| 262 | + |
| 263 | +If you’re happy with the result, you can commit the snapcraft.yaml to your GitHub repo and [turn on automatic builds](https://build.snapcraft.io) so any further commits automatically get released to edge, without requiring you to manually build locally. |
| 264 | + |
| 265 | +Congratulations! You've just built and published your first Go snap. For a more in-depth overview of the snap building process, see [Creating a snap](/t/creating-a-snap/6799). |
0 commit comments