Trying to run xvfb(-run) inside a snap, to run on an headless ubuntu server

I’m trying to run an application with xvfb inside a snap. (without using classic mode) I can’t get it to work.

Snapcraft.yaml

name: pdf-generator
version: '2.0.0-beta1'
summary: PDF Generator
description: |
    PDF Generator with REST Api
base: core22
confinement: strict # could xvfb-ron not to work in strict mode
grade: stable
compression: lzo

layout:
    /etc/fonts:
        bind: $SNAP/etc/fonts

apps:
    pdf-generator:
        command: pdf-generator/pdf-generator $SNAP/pdf-generator/resources/app/app.js
        environment:
            ELECTRON_RUN_AS_NODE: 1
    
    service:
        command: /usr/bin/xvfb-run -a $SNAP/pdf-generator/pdf-generator --no-sandbox
        daemon: simple
        # no plugs needed in classic mode
        plugs:
            - browser-support
            - network
            - network-bind
            - cups-control
        environment:
        restart-condition: always
        restart-delay: 2s

parts:
    pdf-generator:
        plugin: nil
        source: .
        override-build: |
            npm install
            npx electron-builder

            rm -f ./*.snap
            cp -rv ./dist/linux-unpacked $SNAPCRAFT_PART_INSTALL/pdf-generator

            # msttcorefonts
            echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | debconf-set-selections
            apt -y install ttf-mscorefonts-installer
            cp -r /usr/share/fonts/truetype/msttcorefonts $SNAPCRAFT_PART_INSTALL/usr/share/fonts/truetype
        build-snaps:
            - node/16/stable
        stage-packages:
            - libnss3
            - libnspr4
            - libatk-bridge2.0-0
            - libcups2
            - libdrm2
            - libgtk-3-0
            - libgbm1
            - libasound2
            - xvfb

1 First try: I received the following error:

/snap/pdf-generator/x1/usr/bin/xvfb-run: 155: mcookie: Permission denied

2 adding “util-linux” package fort mcookie command Error:

Fatal server error:
(EE) Failed to activate virtual core keyboard: 2(EE) 
_XSERVTransSocketCreateListener: failed to bind listener
_XSERVTransSocketUNIXCreateListener: ...SocketCreateListener() failed
_XSERVTransMakeAllCOTSServerListeners: failed to create listener for local
XKB: Failed to compile keymap
Keyboard initialization failed. This could be a missing or incorrect setup of xkeyboard-config.

3 Adding “x11-common” package Same error as in 2)

4 Adding X11 to plugs New error:

Fatal server error:
(EE) Cannot establish any listening sockets - Make sure an X server isn't already running(EE) 
_XSERVTransSocketCreateListener: failed to bind listener
_XSERVTransSocketUNIXCreateListener: ...SocketCreateListener() failed
_XSERVTransMakeAllCOTSServerListeners: failed to create listener for local
_XSERVTransSocketCreateListener: failed to bind listener
_XSERVTransSocketUNIXCreateListener: ...SocketCreateListener() failed
_XSERVTransMakeAllCOTSServerListeners: failed to create listener for unix

5 I don’t know what to do next …

i just had to do a proof of concept of a similar setup:

feel free to steal ideas from there :slight_smile:

(it uses an x11 slot though … not sure how easy you will get permission for using that slot in the global store for a non-canonical made snap … and it is designed for UbuntuCore, i have not tested it at all on a classic server install)