Correct way to pack a executable and application

I am new to snap and i want to package 2 files within a snap. A webassembly execution runtime: wasmtime (which is a pre-built binary). There are no additional dependencies And an application it must execute: hello.wasm, which prints say “hello world” On a regular system I would say

#wasmtime hello.wasm

I tarred these 2 files and want the snap to execute this command when launched. This is my snapcraft file. There was no error while creating the snap, but the command was not executed on launching the snap. Please let me know how can i achieve this

name: hellowasm

version: “0.1”

grade: stable

summary: Test snap

description: |

Test snap to run with wasmtime

base: core20

confinement: strict

apps:

hellowasm:

command: wasmtime hello.wasm

parts:

wasmtime:

plugin: dump

source: ./hellowasm.tar

helloworld:

plugin: dump

source: ./hellowasm.tar

The output in the terminal when you try run this snap would be very helpful. However I’d assume at the very least, you’d need to specify

apps:
  hellowasm:
    command: wasmtime hello.wasm

as

apps:
  hellowasm:
    command: wasmtime $SNAP/hello.wasm

The wasmtime component will be searched relative to $SNAP by default, but any other parameters will be searched relative to the current working directory unless specified as an absolute path (e.g, with the $SNAP variable).

1 Like

Thanks James. That helped, but I still need your help a bit more. With the current configuration, the snap is installed, but the message is only printed after running the command hellowasm on the shell. Is there a way to automatically invoke the command, when the snap is installed. Here is my modified snapcraft file. I am storing both wasmtime and hello.wasm directly in the current directory

name: hellowasm

version: “0.1”

grade: stable

summary: Test snap

description: | Test snap to run with wasmtime

base: core20

confinement: strict

apps:

hellowasm:

command: wasmtime $SNAP/hello.wasm

parts:

wasmtime:

plugin: dump

source: .

I’m not sure you can explicitly start a snap when it is installed. The closest to starting a snap automatically would be either making it work as a system daemon (so it launches with systemd as a background service) or using https://snapcraft.io/docs/snap-format#heading--autostart to launch it as part of the desktop session when logging in.

Depending on use cases, it may make more sense to have other approaches. For example, users might add it to .bashrc in $HOME, but they’d need to go out their way to do something like this because the sandbox would prevent you doing it on their behalf.

1 Like

Thanks a lot for your help ! Will check out the other approaches too!