Is it possible to access host libgl libraries within a snap confined environment?

I am trying to make a cross-platform application bundler for Julia GUI applications (see AppBundler.jl), and I have chosen a snap as a deployment target for Linux platforms. I have managed to run a QML and GTK application in a snap-confined environment by setting __EGL_VENDOR_LIBRARY_DIRS and LIBGL_DRIVERS_PATH to point to local snap store locations generated by snapcraft.

It’s somewhat unsatisfying that the bundle contains graphics drivers, which are always expected to be present in a desktop system unless the application is targeted in kiosk mode, which is a rather particular case. Also, I would like to implement snap bundling manually without using Snapcraft as I would like to make snaps for instance from macOS, which is possible as I have access to mksquashfs on it and Julia precompilation can be delayed until configuration step or until the first run.

I noticed that there are graphics-core22 and mesa-core22 interfaces. The chatGPT was somewhat vague on how these can be used in a snap.yaml file and what they would do. In particular, can graphics-core22 be used to access the shared libraries for libgl drivers of the host system?

the opengl interface gives you full access to the hosts GL libraries and also access to various graphics devices in /dev if the card provides them … the mesa-core22 snap (which provides the graphics-core22 interface) is there to be able to switch out GL libs seamlessly underneath your snap and independent from the host OS …

regarding the env variables you should take a look at the old desktop aunchers:

https://github.com/ubuntu/snapcraft-desktop-helpers/tree/master

and craft your own command-chain wrapper to pick up the right paths …

Thanks for the pointer. I saw that the desktop-exports file uses /var/lib/snapd/lib/gl and /var/lib/snapd/lib/glvnd locations. When I enter into the snap environment using snap run --shell my-app I see theese directories as empty although I do have opengl listed under plugs. Is that expected and then what is the list of libraries accessable with opengl interface?

Chaotically I have figured out that I can make Julia GUI application to run with a following snap.yaml:

name: my-app
version: '0.1'
summary: Single-line elevator pitch for your amazing 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.
architectures:
- amd64
base: core22
apps:
  my-app:
    command: bin/my-app
    plugs:
    - opengl
    - wayland
    - network
    - graphics-core22
    - desktop
confinement: strict
grade: devel
environment:
  __EGL_VENDOR_LIBRARY_DIRS: $SNAP/graphics/usr/share/glvnd/egl_vendor.d   
  LIBGL_DRIVERS_PATH: $SNAP/graphics/usr/lib/x86_64-linux-gnu/dri
  LD_LIBRARY_PATH: $SNAP/graphics/usr/lib/:$SNAP/graphics/usr/lib/x86_64-linux-gnu/
  PATH: $SNAP/bin/:$PATH
  QT_QPA_PLATFORM: wayland
plugs:
  graphics-core22:
    interface: content
    target: $SNAP/graphics
    default-provider: mesa-core22
layout:
  /usr/share/libdrm:
    bind: $SNAP/graphics/libdrm
  /usr/share/drirc.d:
    bind: $SNAP/graphics/drirc.d

Could there be any drawbacks with this approach?

Is the plug also connected?

Nothing changes in the /var/lib/snapd/lib/gl and /var/lib/snapd/lib/glvnd locations if before getting into the snap shell I do explicit connection with snap connect qmlapp-strict:opengl.

I actually encoutered an issue while using mesa-core22. The application window starts but does not render anything on it. I get a warning from Qt:

Qt Warning: Unrecognized OpenGL version ((null):0, (null))

I opppened an issue for QML.jl library here: https://github.com/JuliaGraphics/QML.jl/issues/191

ah, sorry, i just checked the opengl interface again and indeed i was wrong, /var/lib/snapd/lib/glis used to re-map some nvidia stuff, for something less specific there is access via: /var/lib/snapd/hostfs/usr/lib/x86_64-linux-gnu/ …

like:

$ snap run --shell telegram-desktop -c 'ls /var/lib/snapd/hostfs/usr/lib/x86_64-linux-gnu/libGL.so'
/var/lib/snapd/hostfs/usr/lib/x86_64-linux-gnu/libGL.so

https://github.com/snapcore/snapd/blob/master/interfaces/builtin/opengl.go#L79