Ubuntu Core – how to enable aliases for your snaps commands

David Callé

David Callé

on 28 January 2017

We are happy to announce that a new version of Ubuntu Core, based on snapd 2.21, has been released to the stable snaps channel yesterday.

As with any stable release, your Ubuntu Core devices will update and reboot automatically. If you are using snaps on the desktop, the release will reach you through a snapd package update on Ubuntu 16.04, 16.10 and 17.04.

This release comes with several improvements you can read about in the changelog, but let’s focus on a big feature that will help people who snap very large software, especially software that comes with many commands (such as OpenStack, ImageMagick, most databases…) and their users.

Introducing snap aliases

When you launch a snap from the command line, you need to use the name of the snap, then the name of a command it contains. In most cases, you don’t notice it, because snapd simplifies the process by collapsing <snap-name>.<command-name> into <command-name>, when both are the same. This way, you don’t need to type inkscape.inkscape, but simply inkscape and get a familiar software experience.

But when a snap contains multiple commands, with various names, things can become less familiar. If we take the PostgreSQL snap as an example, we can see it comes with many commands: initdb, createdb, etc. In this case, you have to run postgresql96.initdb, postgresql96.createdb, etc.

The alias feature of snapd lets snaps declare their own aliases, for users to manually enable after install or for snap stores to declare as “auto-aliases” that will be enabled upon install.

How to manually enable aliases

To have an overview of all available aliases on a system, you can use the snap aliases command. For this example, I’ve installed a modified version of the postgresql96 snap with aliases for all commands.

$ snap aliases
App                            Alias             Notes
postgresql96.clusterdb         clusterdb         -
postgresql96.createdb          createdb          -
postgresql96.createlang        createlang        -
postgresql96.createuser        createuser        -
postgresql96.dropdb            dropdb            -
postgresql96.droplang          droplang          -
postgresql96.dropuser          dropuser          -
postgresql96.ecpg              ecpg              -
postgresql96.initdb            initdb            -

As you can see, the snap now exposes aliases to snapd. They are not enabled yet, but it’s simply a matter of running snap alias <snap-name> <alias>. Note that you can pass multiple space-separated aliases to this command.

Let’s enable the createuser alias.

$ snap alias postgresql96 createuser
$ snap aliases | grep createuser
postgresql96.createuser        createuser        enabled

I can now ensure that there is indeed a createuser command coming from a snap:

$ which createuser

And of course, run it:

$ createuser --help
createuser creates a new PostgreSQL role.

To disable an alias you can use the snap unalias command.

How to declare aliases

Declaring a new alias in your snap is as easy as adding an aliases entry to your snapcraft.yaml apps keys. Let’s look at the createuser command in our snap:

       command: usr/bin/createuser
       plugs: [network]
       aliases: [createuser]

And build your snap as usual with snapcraft, snapcraft cleanbuild or let it flow through your CI.

See the full example for this snap in this pull request.

That’s it, heads-on to tutorials.ubuntu.com to make your own snap from scratch and give aliases a try!

Internet of Things

From home control to drones, robots and industrial systems, Ubuntu Core and Snaps provide robust security, app stores and reliable updates for all your IoT devices.

Sign up for email updates

Choose the topics you're interested in


Related posts

Eclipse 2018 survey: The IoT landscape, what it empirically looks like

Every year the Eclipse Foundation along with other sponsors conduct an online survey of the IoT market looking at what technologies are being used and how. The 2018 edition of that survey has just been made available and I thought it would…

The Nextcloud Box: a review of building an IoT device with snaps

In 2016, Canonical, Nextcloud and WDLabs introduced the Nextcloud Box, the first IoT style device running with snaps out of the box. Besides sales of nearly 2K boxes before Western Digital shut down their research division WDLabs late last…

Your first robot: Sharing with others [5/5]

This article originally appeared on Kyle Fazzari’s blog and is the fifth and final installment in a series. This is the fifth (and final) blog post in this series about creating your first robot with ROS and Ubuntu Core. In the previous…