Simple and easy steps to convert a script to snap app

Hi, so I’ve been trying to figure out how to publish my script ( bash script ) to snap, I’ve searched everywhere and spent hours on this but I just can’t seem to find a correct guide which nicely and simply explains how to convert your simple script to snap app. I’d really appreciate if anyone could guide me. Thank you in advance.

does this help ?

(if you look at my gitub, there are also a few other snapped shell scripts there)

Hi, I found your blog useful but do you have any repository on github which has a simple bash script that I can refer to? My script doesn’t really have a gui or anything it just notifies about important stuff whenever a user is logs into their desktop. Also do I need to have the snap directory in my root directory or it doesn’t really matter if I mention everything correctly in the snapcraft.yaml file.

these two below are both non-gui:

your snapcraft.yaml can also live in the toplevel of the source tree … snap/snapcraft.yaml is just where snapcraft init puts the boilerplate template file if you call it on an empty dir (which is how i usually create my initial tree)

a quick question, what exactly am I supposed to put in the apps: section because I keep getting this error Failed to generate snap metadata: The specified command 'app-name' defined in the app 'app-name' does not exist. and also I’m not sure how to use the autostart I believe its for adding desktop entry but I can’t seem to figure out how to do that

Also after building my app ( with snapcraft --use-lxd ) I ended up having a .snap file which was expected but when I opened it with snapcraft store it said Failed to install file: not supported

The command in the snapcraft.yaml's apps.[appname] block should be the path inside the snap filesystem to the executable file. e.g. if you installed the executable into bin/ then you want to set command: bin/appname-executable-file (with the correct filename after bin/ - this was an example so yours WILL be different).

What command did you execute to install the .snap file? It should be similar to:

sudo snap install appname_0.1_amd64.snap

where:

  • appname is the value of the name: field in your yaml, and
  • 0.1 is the value of the version: field in the yaml if you’ve used it or the version set with a snapcraftctl set-version or craftctl set version= command in an override-build or override-pull script when combined with the adopt-info: setting in the yaml (whichever base you’re targeting will determine with of those commands are appropriate: i.e. base snaps are core, core18, or core22).

for a snap installed from a local file that should be:

sudo snap install --dangerous appname_0.1_amd64.snap

I finally got my snap to build successfully, now I want my app to launch every time a user logs into their desktop, I know it can be done with autostart but I’m not sure how to add my source bash file to the startup apps

Edit: I got these errors while running my app

/snap/app-name/x1/usr/bin/app-name: line 19: curl: command not found
/snap/app-name/x1/usr/bin/app-name: line 20: jq: command not found
/snap/app-name/x1/usr/bin/app-name: line 21: jq: command not found
...

and all the linux commands don’t seem to run in my app

You need to add these applications into your snap with either separate part entries to build them yourself, or with stage-packages: added to your existing part that lists the Debian packages from the Ubuntu repository containing them to reuse Ubuntu’s prebuilt versions.

1 Like

Finally, adding the stage-packages worked for me. Thanks! Also would you by any chance have an idea on how can I make my app run on its own everytime a user logs into their session?

See those real-world examples on GitHub to learn how to implement autostart: https://github.com/search?q="autostart%3A"++filename%3Asnapcraft.yaml

I did but I’m not sure where should I put my app-name.desktop file

Refer to this script, which enables autostart by copying the desktop file to the correct location: https://github.com/ppd/syncthing-snap/blob/main/scripts/enable-autostart.sh

Thanks, that helped a lot! However I’m still trying to figure out how to run my app by its own whenever a user logs into their desktop session

By copying the desktop file that contains your launching instructions (for example, refer to how syncthing does it: https://github.com/zbazztian/codeql-tools/blob/main/test-projects/syncthing/etc/linux-desktop/syncthing-start.desktop) to the location the script specifies. You offer a command (like syncthing does) to enable autostart, or you could copy this file when the user runs your main/default command.

At the very least, you need the user to run the command that copies the desktop file once.

Yes, I did check that repo and tried copying that file but it didn’t let me because of missing permissions

I made some changes and I get this error now:

cp: cannot open 'snap/local/.config/autostart/app-name.desktop' for reading: Permission denied

I think you need to show a Git repo with your code to make some progress here.

Sure, here’s the repo:

In your shell script, you need to construct paths relative to your snap’s install location, i.e. relative to $SNAP. In your example above, assuming you simply dump the desktop file into your snap without extra directories, you’d use cp $SNAP/daily-quotes.desktop $SNAP_USER_DATA/.config/autostart/daily-quotes.desktop.

Put the desktop file into a directory, e.g. snap/local/autostart and install the desktop file via a part:

parts:
  desktop-file:
    plugin: dump
    source: snap/local/autostart

I always get confused between what $SNAP and $SNAP_USER_DATA return but thanks anyways. Also whenever I install my app from the my-app*.snapfile, it installs just fine with everything but it doesn’t seem to send the notification, would you by any chance have idea why? I suspect it is because of the application I’m using for sending the notification but I’m not sure