🕌

Snapの作り方

2025/01/12に公開

初めに

  • 本記事では、Snapパッケージの作り方について紹介します。
  • "Hello World!"と出力するhello.shをSnapでパッケージングして実行します。

実行環境

  • 本記事ではWSLを使用します。
     - Ubuntu20.04

事前準備

  • Snapcraftをインストールします。
$ sudo apt install snapcraft

snapcraft.yamlの作成

  • Snapcraftのインストールが完了したら、作業用のディレクトリを作成して、snapcraft.yamlを生成します。
$ mkdir workspace
$ cd workspace
$ snapcraft init
  • コマンドを実行すると、下記のようにsnapcraft.yamlが生成されます。
$ tree
.
└── snap
    └── snapcraft.yaml

1 directory, 1 file
  • snapcraft.yamlには下記のように記述します。
name: hello-world # you probably want to 'snapcraft register <name>'
base: core20 # the base snap is the execution environment for this snap
version: '0.1' # just for humans, typically '1.2+git' or '1.3.2'
summary: Single-line elevator pitch for your amazing snap # 79 char long summary
description: |
  This is my-snap's description. You have a paragraph or two to tell the
  most important story about your snap. Keep it under 100 words though,
  we live in tweetspace and your description wants to look good in the snap
  store.

grade: devel # must be 'stable' to release into candidate/stable channels
confinement: devmode # use 'strict' once you have the right plugs and slots

apps:
  hello:
    command: hello.sh
parts:
  hello:
    # See 'snapcraft plugins'
    plugin: dump
    source: .
  • hello.shを作成します。
$ vi hello.sh
#!/bin/bash
echo "Hello World!"

注:)hello.shはsnapディレクトリと同じ階層に配置してください。

$ tree
.
├── hello.sh
└── snap
    └── snapcraft.yaml

1 directory, 2 files
  • hello.shに実行権限を付与します。
$ sudo chmod +x hello.sh
$ ls -l hello.sh
-rwxr-xr-x 2 User User 32 Jan 11 12:45 hello.sh

snapcraft.yamlの内容

  • nameにはSnap名を記述します。
name: hello-world
  • baseには使用するSnapの環境を指定します。
    core20の場合は、Ubuntu20をベースにしたSnapを作成するという意味になります。
base: core20
  • versionにはSnapのバージョンを指定します。
    Snap StoreにアップロードしているSnapを更新したい場合、versionの値を変更してSnap Storeにアップロードします。
version: 0.1
  • summaryにはこのSnapの概要について記載します。
summary: Single-line elevator pitch for your amazing snap # 79 char long summary
  • descriptionはこのSnapの詳細を説明します。
description: |
  This is my-snap's description. You have a paragraph or two to tell the
  most important story about your snap. Keep it under 100 words though,
  we live in tweetspace and your description wants to look good in the snap
  store.
  • gradeは作成しているSnapの状態を示します。
    develは開発時に使用され、Snap Storeにアップロードされているものはstableが使用されています。
grade: devel # must be 'stable' to release into candidate/stable channels
  • confinementはSnapのアクセス制限について記載します。
    devmodeは開発時に使用され、アクセスへの制限はされませんが、Snap Storeへアップロード出来ません。
confinement: devmode # use 'strict' once you have the right plugs and slots

他にもstrictとclassicがあり、
strict:一般的に使用される制限です。他のリソースへアクセスする際にユーザの許可が必要になります。
classic:ホストのリソースを使用する際に指定します。Snap Storeへアップロードする際にはStore側からのレビューをクリアする必要があります。

  • appsにはSnap化するアプリ名と実行するコマンドを記載します。
apps:
  hello: # アプリ名
    command: hello.sh # helloが実行するコマンド
  • partsではappsで定義したアプリのファイルなどの取得方法やビルド方法等について記載します。
parts:
  hello:
    # See 'snapcraft plugins'
    plugin: dump # dumpは何も行わず、sourceで取得したファイル等をそのまま取り込みます。
    source: . # snapディレクトリの位置がカレントディレクトリになります。

pluginが何を提供されているのか確認するには、"snapcraft plugins"を実行することで、確認が出来ます。

$ snapcraft plugins
Displaying plugins available for 'core20'
autotools  catkin-tools  colcon  crystal  go      make   nil  python  rust
catkin     cmake         conda   dump     kernel  meson  npm  qmake

Snapのビルド

  • Snapをビルドします。

hiramats@MyComputer:~/workspace$ snapcraft --destructive-mode
Pulling hello
+ snapcraftctl pull
Building hello
+ snapcraftctl build
+ cp --archive --link --no-dereference . /home/User/workspace/parts/hello/install
Staging hello
+ snapcraftctl stage
Priming hello
+ snapcraftctl prime
Snapping |
Snapped hello-world_0.1_amd64.snap
  • コマンド実行後、.snapファイルが生成されていたら、ビルド成功です。
$ ls *.snap
hello-world_0.1_amd64.snap

Snapのインストール

  • Snapのビルドが完了しても、このままではSnapは実行できません。
    Snapを実行するために、ビルドしたSnapをインストールします。
$ snap install hello-world_0.1_amd64.snap --dangerous --devmode

"hello-world 0.1 installed"が出力されたら、Snapのインストール成功です。

  • インストールしたSnapは"snap list"コマンドで確認することが出来ます。
$ snap list
Name         Version         Rev    Tracking         Publisher   Notes
hello-world  0.1             x1     -                -           devmode

Snapの実行

  • インストールしたSnapは<snap名>.<apps名>で実行することが出来ます。
$ hello-world.hello 
Hello World!

最後に

  • 以上がSnapを作り方の紹介でした。
  • 閲覧いただきありがとうございました。

Discussion