How to snap wxPython app which uses webkit

How do I stage webkit incl. something called WebKitNetworkProcess with my wxPython app which uses the webkit based html wxPython widget wx.html2.WebView.

Staging libwebkit2gtk-4.0-37 doesn’t seem to be enough and doesn’t supply the file WebKitNetworkProcess. My snap always crashes with

$ snap-webkit-how.gitm

** (gitm:116459): ERROR **: 22:42:28.444: 
Unable to spawn a new child process: 
Failed to spawn child process “/usr/lib/x86_64-linux-gnu/webkit2gtk-4.0/WebKitNetworkProcess” (No such file or directory)

Repro on Ubuntu 22.04

  1. Run python3 main.py and see it runs ok.
  2. Build and install the snap, run snap-webkit-how.gitm and watch it crash

main.py

Here is a barebones example, main.py which runs OK python3 main.py but when snapped, crashes.

import wx
import wx.html # old, doesn't support css and javascript
import wx.html2 # modern, webkit, supports css and javascript

class MyFrame(wx.Frame):
    def __init__(self):
        super().__init__(None, title="HTML Panel App")
        
        # Create HTML panel
        self.html_panel = wx.html2.WebView.New(self) # fails in snap, causes Failed to spawn child process “/usr/lib/x86_64-linux-gnu/webkit2gtk-4.0/WebKitNetworkProcess” (No such file or directory)
        # self.html_panel = wx.html.HtmlWindow(self) # worksin snap
        
        # Create text entry area
        self.text_entry = wx.TextCtrl(self)
        
        # Create vertical sizer to stack panels
        self.vert_sizer = wx.BoxSizer(wx.VERTICAL)
        self.vert_sizer.Add(self.html_panel, 1, wx.EXPAND)
        self.vert_sizer.Add(self.text_entry, 0, wx.EXPAND)
        
        # Set main sizer for frame
        self.SetSizer(self.vert_sizer)
        
        # Load initial HTML content
        self.html_panel.SetPage("<html><body><h1>Hello, World!</h1></body></html>", "")
        # self.html_panel.SetPage("<html><body><h1>Hello, World!</h1></body></html>")

def run():
    app = wx.App()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()
    
if __name__ == '__main__':
    run()

setup.py

import os
from setuptools import setup, find_packages
import sys
setup(
    name='snap-webkit-how',
    version='1.0.0',
    url='https://github.com/blah/snap-webkit-how',
    author='Blah',
    author_email='blah@gmail.com',
    py_modules=['main'],
    entry_points={
        'console_scripts': [
            'gitm=main:run',
        ],
    },    
)

snapcraft.yaml

# Requires snapcraft snapcraft 7.3.1.post21+gitfa823013 from edge or later to build this snap
name: snap-webkit-how
version: git
summary: 'Blah'
description: |
     Blah blah:

grade: stable
confinement: devmode
base: core22
architectures:
  - build-on: [amd64]

apps:
  gitm:
    command: bin/gitm
    plugs:
      - desktop
      - desktop-legacy
      - x11
      - home
      - gsettings
      - network
      - network-bind
      - removable-media
    extensions:
      - gnome

parts:
  build-the-python-stuff-please:
    plugin: python
    source: .
    stage-packages:
      - libwebkit2gtk-4.0-37
      - libsdl2-2.0-0
    python-packages:
      - https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-22.04/wxPython-4.2.0-cp310-cp310-linux_x86_64.whl

Repository with this code: https://github.com/abulka/snap-webkit-how

Does sudo dmesg show any AppArmor denial messages? That’s strange that /usr/lib/x86_64-linux-gnu/webkit2gtk-4.0/WebKitNetworkProcess doesn’t exist despite having libwebkit2gtk-4.0-37 in stage-packages.

I ran apt rdepends libwebkit2gtk-4.0-37 and noticed a package depends on it named libwxgtk-webview3.0-gtk3-0v5 so maybe you need that for wxWidgets to be able to use the webkit framework?

Adding that

stage-packages:
  - libwebkit2gtk-4.0-37
  - libwxgtk-webview3.0-gtk3-0v5    <-----
  - libsdl2-2.0-0

certainly supplies the missing WebKitNetworkProcess file, which is promising, but I’m still getting the same crash when I run the snap. Running unsquashfs -l *.snap shows

squashfs-root/usr/lib/x86_64-linux-gnu/webkit2gtk-4.0/WebKitNetworkProcess

I tried explicitly added the path to the snapcraft.yaml:

environment:
  LD_LIBRARY_PATH: $LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu/webkit2gtk-4.0/

which didn’t help.

Running sudo dmesg and grepping for apparmor="DENIED" gives me a bunch of lines I don’t understand, but which might be clues:

    [41192.071626] audit: type=1400 audit(1680060135.599:726): apparmor="DENIED" operation="mount" class="mount" info="failed flags match" error=-13 profile="lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_</var/snap/lxd/common/lxd>" name="/run/systemd/unit-root/proc/" pid=132477 comm="(networkd)" fstype="proc" srcname="proc" flags="rw, nosuid, nodev, noexec"
    [41192.134397] audit: type=1400 audit(1680060135.659:727): apparmor="DENIED" operation="mount" class="mount" info="failed flags match" error=-13 profile="lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_</var/snap/lxd/common/lxd>" name="/run/systemd/unit-root/proc/" pid=132502 comm="(resolved)" fstype="proc" srcname="proc" flags="rw, nosuid, nodev, noexec"
    [41289.936981] audit: type=1400 audit(1680060233.448:740): apparmor="DENIED" operation="mount" class="mount" info="failed flags match" error=-13 profile="lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_</var/snap/lxd/common/lxd>" name="/" pid=134400 comm="mount" flags="rw, remount, noatime"
    [41290.051986] audit: type=1400 audit(1680060233.564:746): apparmor="DENIED" operation="mount" class="mount" info="failed flags match" error=-13 profile="lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_</var/snap/lxd/common/lxd>" name="/run/systemd/unit-root/proc/" pid=134494 comm="(networkd)" fstype="proc" srcname="proc" flags="rw, nosuid, nodev, noexec"
    [41290.108531] audit: type=1400 audit(1680060233.620:747): apparmor="DENIED" operation="mount" class="mount" info="failed flags match" error=-13 profile="lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_</var/snap/lxd/common/lxd>" name="/run/systemd/unit-root/proc/" pid=134521 comm="(resolved)" fstype="proc" srcname="proc" flags="rw, nosuid, nodev, noexec"
    [41361.916373] audit: type=1400 audit(1680060305.423:760): apparmor="DENIED" operation="mount" class="mount" info="failed flags match" error=-13 profile="lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_</var/snap/lxd/common/lxd>" name="/" pid=136719 comm="mount" flags="rw, remount, noatime"
    [41362.013903] audit: type=1400 audit(1680060305.519:766): apparmor="DENIED" operation="mount" class="mount" info="failed flags match" error=-13 profile="lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_</var/snap/lxd/common/lxd>" name="/run/systemd/unit-root/proc/" pid=136766 comm="(networkd)" fstype="proc" srcname="proc" flags="rw, nosuid, nodev, noexec"
    [41362.084268] audit: type=1400 audit(1680060305.591:767): apparmor="DENIED" operation="mount" class="mount" info="failed flags match" error=-13 profile="lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_</var/snap/lxd/common/lxd>" name="/run/systemd/unit-root/proc/" pid=136785 comm="(resolved)" fstype="proc" srcname="proc" flags="rw, nosuid, nodev, noexec"
    [41388.038147] audit: type=1400 audit(1680060331.545:782): apparmor="DENIED" operation="file_inherit" class="net" namespace="root//lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_<var-snap-lxd-common-lxd>" profile="/snap/snapd/18596/usr/lib/snapd/snap-confine" pid=137628 comm="snap-confine" family="netlink" sock_type="raw" protocol=15 requested_mask="send receive" denied_mask="send receive"
    [41395.761792] audit: type=1400 audit(1680060339.269:787): apparmor="DENIED" operation="file_inherit" class="net" namespace="root//lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_<var-snap-lxd-common-lxd>" profile="/snap/snapd/18596/usr/lib/snapd/snap-confine" pid=138388 comm="snap-confine" family="netlink" sock_type="raw" protocol=15 requested_mask="send receive" denied_mask="send receive"
    [112130.168190] audit: type=1400 audit(1680230653.468:817): apparmor="DENIED" operation="mount" class="mount" info="failed flags match" error=-13 profile="lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_</var/snap/lxd/common/lxd>" name="/" pid=151722 comm="mount" flags="rw, remount, noatime"
    [112130.310022] audit: type=1400 audit(1680230653.613:823): apparmor="DENIED" operation="mount" class="mount" info="failed flags match" error=-13 profile="lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_</var/snap/lxd/common/lxd>" name="/run/systemd/unit-root/proc/" pid=151833 comm="(networkd)" fstype="proc" srcname="proc" flags="rw, nosuid, nodev, noexec"
    [112130.368709] audit: type=1400 audit(1680230653.670:824): apparmor="DENIED" operation="mount" class="mount" info="failed flags match" error=-13 profile="lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_</var/snap/lxd/common/lxd>" name="/run/systemd/unit-root/proc/" pid=151860 comm="(resolved)" fstype="proc" srcname="proc" flags="rw, nosuid, nodev, noexec"
    [112139.753795] audit: type=1400 audit(1680230663.116:839): apparmor="DENIED" operation="file_inherit" class="net" namespace="root//lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_<var-snap-lxd-common-lxd>" profile="/snap/snapd/18596/usr/lib/snapd/snap-confine" pid=152530 comm="snap-confine" family="netlink" sock_type="raw" protocol=15 requested_mask="send receive" denied_mask="send receive"
    [112148.639036] audit: type=1400 audit(1680230672.064:844): apparmor="DENIED" operation="file_inherit" class="net" namespace="root//lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_<var-snap-lxd-common-lxd>" profile="/snap/snapd/18596/usr/lib/snapd/snap-confine" pid=153276 comm="snap-confine" family="netlink" sock_type="raw" protocol=15 requested_mask="send receive" denied_mask="send receive"
    [113479.868866] audit: type=1400 audit(1680232003.043:866): apparmor="DENIED" operation="mount" class="mount" info="failed flags match" error=-13 profile="lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_</var/snap/lxd/common/lxd>" name="/" pid=160029 comm="mount" flags="rw, remount, noatime"
    [113479.993942] audit: type=1400 audit(1680232003.168:872): apparmor="DENIED" operation="mount" class="mount" info="failed flags match" error=-13 profile="lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_</var/snap/lxd/common/lxd>" name="/run/systemd/unit-root/proc/" pid=160121 comm="(networkd)" fstype="proc" srcname="proc" flags="rw, nosuid, nodev, noexec"
    [113480.051786] audit: type=1400 audit(1680232003.224:873): apparmor="DENIED" operation="mount" class="mount" info="failed flags match" error=-13 profile="lxd-snapcraft_snapcraft-snap-webkit-how-on-amd64-for-amd64-11802732_</var/snap/lxd/common/lxd>" name="/run/systemd/unit-root/proc/" pid=160141 comm="(resolved)" fstype="proc" srcname="proc" flags="rw, nosuid, nodev, noexec"

Because the lib does not live in /usr… but in $SNAP/usr…

(Also, do not prefix it with $LD_LIBRARY_PATH: … snapcraft already does that automatically)

With

environment:
  LD_LIBRARY_PATH: $SNAP/usr/lib/x86_64-linux-gnu/webkit2gtk-4.0/

I get ImportError: libSDL2-2.0.so.0: cannot open shared object file: No such file or directory.

With

environment:
  LD_LIBRARY_PATH: $LD_LIBRARY_PATH:$SNAP/usr/lib/x86_64-linux-gnu/webkit2gtk-4.0/

I still get ERROR **: 18:27:34.999: Unable to spawn a new child process: Failed to spawn child process “/usr/lib/x86_64-linux-gnu/webkit2gtk-4.0/WebKitNetworkProcess” (No such file or directory) Trace/breakpoint trap (core dumped)