Is there a way to get notifications from container guest GUI apps on the host?

When using Wayland or X11 from an Incus container guest, notifications from guest don’t integrate properly with the host. I don’t really know anything about notifications so maybe this is impossible to fix, but right now the situation is particularly frustrating. I’m using HTML5 Web Notifications Test - Bennish.net as a test for instant notifications, but the problem seems to happen with other notifications from other apps (I’ve tried Chrome and Firefox, I don’t have many other ways of testing).

When using Firefox with Wayland, notifications pop up as a new untitled window:

This is particularly annoying because when it shows up as a new window, it steals focus and doesn’t have a consistent location.

X11 at least doesn’t show up as a new window, but that also means you might not notice it:

I’m wondering if anybody knows of a way to send the notifications to the host to show up like any other notifications (such as in the Notifications tray in KDE) from the host. Notifications from a browser in Distrobox work correctly on the host, but I’m not sure if that’s because they’re not using systemd. From what I can gather dbus is used for notifications, but I haven’t yet figured out how/if I can forward specific dbus messages to the host. If anybody has any suggestions, I’d appreciate it. Thanks!

Reproduction steps

This is what I’ve been using to reproduce this.

minimal.yml

config:
  nvidia.driver.capabilities: all
  nvidia.runtime: "true"
  raw.idmap: both 1000 1000
  security.nesting: "true"
  user.user-data: |-
      #cloud-config
      packages:
      - firefox
      - chromium
      - bash-completion
      users:
        - name: guestuser
          plain_text_passwd: 'guestuser'
          shell: /bin/bash
          lock_passwd: True
          groups: [adm, cdrom, dip, sudo, video]
          sudo: ALL=(ALL) NOPASSWD:ALL
      write_files:
        - path: /etc/dnf/dnf.conf
          content: |
            max_parallel_downloads=20
            defaultyes=True
            deltarpm=False
            install_weak_deps=False
            installonly_limit=3
            keepcache=True
          append: true
        - path: /etc/bashrc
          content: |
            export DISPLAY=:0
            uid=$(id -u)
            run_dir=/run/user/$uid
            if [[ -e /mnt/.container_wayland_socket ]]; then
              if [[ ! -e $run_dir/wayland-0 ]]; then
                ln -sf /mnt/.container_wayland_socket $run_dir/wayland-0
              fi
            fi
            if [[ -e $run_dir/wayland-0 ]]; then
              export XDG_SESSION_TYPE=wayland
              export WAYLAND_DISPLAY=wayland-0
            fi
          append: true

devices:
  eth0:
    name: eth0
    network: incusbr0
    type: nic
  mygpu:
    gid: "44"
    type: gpu
  wayland_socket:
    path: /mnt/.container_wayland_socket
    source: /run/user/1000/wayland-0
    type: disk
  x11-socket:
    bind: container
    connect: unix:@/tmp/.X11-unix/X0
    listen: unix:@/tmp/.X11-unix/X0
    type: proxy
  dnf-cache:
    path: /var/cache/dnf
    source: /tmp/dnf-cache
    type: disk
mkdir -p /tmp/dnf-cache
incus launch images:fedora/40/cloud testing < minimal.yml
incus exec testing -- cloud-init status --wait

# For Wayland
incus exec testing -- sudo --login --user guestuser firefox https://www.bennish.net/web-notifications.html

# For X11:
incus exec --env MOZ_ENABLE_WAYLAND=0 testing -- sudo --preserve-env=MOZ_ENABLE_WAYLAND --login --user guestuser firefox https://www.bennish.net/web-notifications.html
1 Like

I managed to get notifications to work, but it’s not very clean. I added this device to the guest:

  dbus-socket:
    path: /mnt/.dbus_socket
    source: /run/user/1000/bus
    type: disk

and appended the following to /etc/bashrc:

if [[ -e /mnt/.dbus_socket ]]; then
  if [[ ! -e $run_dir/host-bus ]]; then
    ln -sf /mnt/.dbus_socket $run_dir/host-bus
  fi
fi
if [[ -e $run_dir/host-bus ]]; then
  export DBUS_SESSION_BUS_ADDRESS=unix:path=$run_dir/host-bus
fi

After doing that, notifications show up on the host. But it doesn’t feel like the right way to do things because it affects other things, such as the default browser. Now when I run xdg-open https://linuxcontainers.org it opens it with the default browser on the host, not the guest. Is there a way to watch for org.freedesktop.Notifications messages on dbus? I could then forward them to my host bus while letting all other dbus message remain in the guest.

1 Like

You would probably need a filtering proxy for D-Bus that will allow and block certain messages. A quick Google search shows this one, GitHub - Pelagicore/dbus-proxy: dbus-proxy is a filtering proxy for D-Bus