Launching external apps from inside a classic snap

Background

Generally snaps are completely self-contained. However, if you’re snapping a launcher, a desktop environment or something else that runs programs installed by the user, you might run into some trouble. To even attempt to do such a thing you’ll need to enable classic confinement. After that external programs will sometimes launch, but will often crash or misbehave.

I ran into this problem when snapping MATE panel for mate-wayland. The panel could launch apps, but GTK3 apps would segfault immediately.

Problem

My investication concluded it was because the apps were loading the GTK3 library shipped with the snap, instead of the one on the system. Even though classic snaps can launch external programs, those programs will inherit the snap environment. That is, environment variables such as SNAP, LD_LIBRARY_PATH, PATH and XDG_DATA_DIRS are set to values that only make sense in the snap. If external programs are run with the snap environment, they load the wrong libraries and use the wrong config files.

My Solution

To fix this, I created snap-out, It uses /proc/.../environ to detect what environment variables the snap added or changed, and reverts them before running the given program. With the -s option it can also dump the changes to the environment it would make (this is what I use for MATE panel). It detects everything dynamically, so it doesn’t need to get patched every time snapcraft changes the environment it sets up.

Ideal Solution

snap-out works, but it is a bit hacky. Ideally there would be an API to launch external programs, which could work even for confined snaps. Is there any such project in progress? Also, if there’s a easier way to do this I missed, let me know.

3 Likes

snapcraft does not set any environment for classic snaps for this reason and instead focuses on setting up RPATH and linker loaders.

We have no API for launching apps, but this is highly desired, this would make the plasma shell snap really useful. I suspect this starts with conversations with @jdstrand and @zyga-snapd