1
sudo snap install snapcraft --classic

Creating a snap

Creating snapcraft.yaml

1
2
mkdir a2fa-snap
cd a2fa-snap
1
snapcraft init
1
vim snap/snapcraft.yaml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
name: a2fa
base: core22
version: git
summary: a2fa is a command line tool for generating and validating one-time password. 
description: |
  a2fa means annoying two-factor authentication. a2fa is a command line tool for generating 
  and validating one-time password. Its purpose is to get rid of phones and be able to 
  authenticate easily. It keeps synced with Google Authenticator, Microsoft Authenticator. 

grade: stable
confinement: devmode 

apps:
  a2fa:
    command: bin/a2fa

parts:
  a2fa:
    plugin: go
    build-packages:
      - golang-go
    source-type: git
    source: https://github.com/csyezheng/a2fa
    source-tag: v0.16.5

See Go applications for a simple example, or search GitHub 4 for projects already using the plugin.

Building your snap

Snap configuration

check the provider

1
snap get snapcraft provider
1
snap set snapcraft provider=lxd
1
snap set snapcraft provider=multipass

unset the provider

1
snap unset snapcraft provider

Install LXD

1
sudo snap install lxd
1
lxd version
1
sudo lxd init

Group permissions

If you want to build snaps as a non-root user, which is advised, then you need to add your user account to the lxd group:

1
sudo usermod -a -G lxd ${USER}

Install Multipass

1
sudo snap install multipass
1
2
export SNAPCRAFT_BUILD_ENVIRONMENT_CPU=8 
export SNAPCRAFT_BUILD_ENVIRONMENT_MEMORY=16G

Interacting with instances

Entering the build environment

Debugging a problematic build can require entering the build environment. Snapcraft provides the commands --shell, --shell-after, and --debug to allow the developer to quickly enter a shell inside the build environment.

1
snapcraft --debug --use-lxd
Cleaning the build environment
1
snapcraft clean
1
snapcraft clean <part-name>

Building manually

create and run a new container based on Ubuntu 22.04 LTS, and named container as mysnapcraft

1
lxc launch ubuntu:22.04 mysnapcraft

Copy your snap’s snapcraft.yaml to this new container

1
lxc file push snap/snapcraft.yaml mysnapcraft/home/ubuntu/

Now open an interactive shell within your container and install snapcraft

1
2
lxc exec mysnapcraft -- /bin/bash 
snap install snapcraft --classic

staying within the container, start the build by running snapcraft with the –destructive-mode argument

1
2
cd /home/ubuntu
snapcraft --destructive-mode

With the build complete, you can copy your new snap to your native environment with the following command:

1
lxc file pull mysnapcraft/home/ubuntu/mysnap.snap .

Testing your snap locally

1
sudo snap install a2fa_0.16.5_amd64.snap --devmode

Releasing a snap

Reference

Snapcraft overview

Creating a snap

Releasing your app

详解 Ubuntu snap 包的制作过程