Category Archives: Snapcraft

Cross-compiling with snapcraft: iota

Last week I snapped iota, a small editor with emacs and vi modes.

I decided I wanted to try it out on a Raspberry Pi before pushing it to the snappy store, for that I need to cross-compile. Conveniently I recently implemented support for that in snapcraft.

If you didn’t follow the previous step from my previous blog post, to try what I’m about to do, you’ll need a fresh clone of my iota branch.

git clone
cd iota
git checkout snapcraft

If on the other hand, like me, you built a snap for another architecture before, in my case amd64, I need to clean the build files.

snapcraft clean

Now on to the most difficult step:

snapcraft --target=arch=armhf

Time for a cup of coffee!

Once it’s done, you can use SSH (or whatever your preferred method is) to copy the snap to your Raspberry Pi 2 or 3 running Ubuntu Core.

snapcraft install --dangerous --classic iota_0+git.67643b3_armhf.snap

Now I can use iota as my editor on the Raspberry Pi!


Packaging rust with snapcraft: iota

Iota is a small editor for your terminal, with support for emacs hotkeys or vi-style commands, syntax highlighting, and easily hackable, written in Rust… now wouldn’t it be awesome if I could install it easily as a snap? So I figured I’d contribute a snapcraft YAML in my lunch break. Here’s how I did it.

Head to and hit Fork on the top right. Let GitHub work its magic. Once it’s done I can checkout the code.

git clone
cd iota
snapcraft init

At this stage I’ve got a local copy of iota and a snap/snapcraft.yaml that was automatically created for me. Some editing is needed.

name: iota
summary: Iota is a terminal-based text-editor written in Rust
description: |
  100% open source
  highly extensible/customisable/scriptable
  fast & efficient - designed with modern hardware in mind
  cross platform - it should work anywhere
  developer friendly - it should just "get out of the way"
  Rust tooling integration

The fields name, summary and description are pretty self-explanatory, especially given the template snapcraft init creates. I just picked some wording from iota’s

version: git
grade: devel
confinement: classic

I used version: git here because it just picks up the tag or commit hash, and it’s good enough for an unstable build anyway. confinement: classic is much more interesting here because this is an editor, and I want to be able to edit all the files on my system. confinement: strict wouldn’t allow that as it restricts access to specific folders only used by the snap itself.

    command: bin/iota

Note how I’m specifying bin here explicitly. Classic snaps don’t get any path magic unlike their strict counterparts.

    plugin: rust
    rust-channel: nightly

Now as for the actual build instructions I don’t really have to do much. iota specifies all its dependencies in its Cargo.toml so no build-packages or stage-packages are necessary. The only thing I need to specify here is the nightly channel since iota requires unstable rust features.


This takes a while as rustup downloads the toolchain and crates needed behind the scenes. High time to grab that lunch my break is meant for.

Fast-forward, I’m installing the result of that build.

snapcraft install --dangerous --classic iota_0+git.67643b3_amd64.snap

Now I can use iota as my editor!

Last but not least, after some testing:

git add snap/snapcraft.yaml
git commit -p -v
git checkout -B snapcraft
git push origin snapcraft

Finally, I’m opening a pull request on GitHub.

How to cross-compile snapcraft plugins

I implemented support for cross-compiling when using the Go plugin and the Rust plugin (bonus points for using a LXD container) in snapcraft. On the part of the user of snapcraft it’s completely transparent but each plugin needs to explicitly support this, so here’s how you enable your favorite snapcraft plugin to make this work.

snapcraft --target-arch=armhf --debug

This will result in the following error if a part uses a plugin that doesn’t support cross-compilation yet:

Setting target machine to 'armhf'
Building for a different target architecture requires a plugin specific implementation in the 'foo' plugin

Let’s enable the fictional foo plugin.


First of all, we tell snapcraft that we know how to cross-compile by implementing one additional method. We don’t need to do anything here. Although I’ve found one good use of the method to be checking that the requested target is supported by the toolchain. For a language like Rust or Go the target is different to the Debian architecture. Rust even supports using a different libc (glibc or musl). That means the plugin needs to map the target and raise an error if it can’t. In the following example I’ll also be seeting self._target so that it can be used in other steps of the lifecycle, for example to set environment variables or when installing target-specific dependencies of the toolchain.

def enable_cross_compilation(self):
    # Cf. rustc --print target-list
    rust_targets = {
        'armhf': 'armv7-{}-{}eabihf',
        'arm64': 'aarch64-{}-{}',
        'i386': 'i686-{}-{}',
        'amd64': 'x86_64-{}-{}',
        'ppc64el': 'powerpc64le-{}-{}',
    self._target = rust_targets.get(self.project.deb_arch).format(
        'unknown-linux', 'gnu')
    if not self._target:
        raise NotImplementedError(
            '{!r} is not supported as a target architecture when '
            'cross-compiling with the rust plugin'.format(

Second of all, we’ll want to adjust the environment variables. This depends on the build system. Typical candidates are compiler names and pkgconfig – we don’t need to worry about the latter since snapcraft already takes care of it. Go for example would also require GOARCH to be set.

In the following code I’m assuming there’s a self._goarch variable that was set in enable_cross_compilation.

def env(self, root):
    env = super().env(root)
    if self.project.is_cross_compiling:
    return env

The example uses the triplet of the target architecture. Commonly useful values here are, using armhf values for demonstration purposes:

cross_compiler_prefix: arm-linux-gnueabihf-
arch_triplet: arm-linux-gnueabihf
deb_arch: armhf
kernel_arch: arm

Note that snapcraft will automatically install the compiler and libc headers needed. Should you need extra packages it’s only one command away:


It goes without saying that we’ll want to have an integration test checking that this new code actually continues to work in the future.


You’ll need to have a snap in integration_tests/snaps/foo which you may already have. Otherwise you’ll need to add it. It’s a good idea to try and make use of more advanced use cases that may need special care in the context of cross-compilation, such as using C code in the case of Go.

import snapcraft
from snapcraft.tests.matchers import HasArchitecture
def test_cross_compiling(self):
    if snapcraft.ProjectOptions().deb_arch != 'amd64':
        self.skipTest('The test only handles amd64 to armhf')

    arch = 'arm64'
    self.run_snapcraft('stage', 'foo', '--target={}'.format(arch))
    binary = os.path.join(self.parts_dir, 'foo', 'install', 'bin',
    self.assertThat(binary, HasArchitecture('aarch64'))

You’ll also want to unit test the new use cases supported by the plugin. There will be a snapcraft/tests/plugins/ if it’s an existing plugin where you can verify the build environment and commands used for building insofar as they differ (Go would require you to use go build for example instead of go install).

I’m not going to go into all the details here since this is very plugin-specific but one thing you’ll generally want to add is scenarios and mock is_cross_compiling:

scenarios = [
    ('armv7l', dict(deb_arch='armhf', go_arch='arm')),
    ('aarch64', dict(deb_arch='arm64', go_arch='arm64')),
    ('i386', dict(deb_arch='i386', go_arch='386')),
    ('x86_64', dict(deb_arch='amd64', go_arch='amd64')),
    ('ppc64le', dict(deb_arch='ppc64el', go_arch='ppc64le')),

def setUp(self):

    self.project_options = snapcraft.ProjectOptions(
    patcher = mock.patch('snapcraft.ProjectOptions.is_cross_compiling')

If all went well I hope you’re going to propose a PR to snapcraft adding support for the foo plugin – a real one of course, not the fictional one that doesn’t exist