Skip to content

Instantly share code, notes, and snippets.

@depau
Created May 21, 2024 09:30
Show Gist options
  • Save depau/573b324c257a7221adebd2a1e46d243a to your computer and use it in GitHub Desktop.
Save depau/573b324c257a7221adebd2a1e46d243a to your computer and use it in GitHub Desktop.
Fix WSLg graphics servers on WSL2

Fix WSLg on WSL2

This script fixes non-working Wayland and X11 servers on WSL2 distributions that are not distributed by Microsoft and which may not always work.

It works by linking the server sockets to those created by the WSLg system container, mounted in /mnt/wslg and accessible via wsl -d DISTRO_NAME --system.

#!/bin/bash
set -euo pipefail
XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$UID}"
echo "Checking Wayland socket"
if [[ ! -S "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY" ]]; then
ln -s "/mnt/wslg/runtime-dir/$WAYLAND_DISPLAY" "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY"
ln -s "/mnt/wslg/runtime-dir/$WAYLAND_DISPLAY.lock" "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY.lock"
echo "-> Fixed Wayland socket"
else
echo "-> Wayland socket is present"
fi
echo "Checking X11 socket"
if [[ "$DISPLAY" != :* ]]; then
echo "-> X11 display variable not supported"
exit 1
fi
DISP_NUM="${DISPLAY#:}"
if [[ ! -S "/tmp/.X11-unix/X$DISP_NUM" ]]; then
ln -s "/mnt/wslg/.X11-unix/X$DISP_NUM" "/tmp/.X11-unix/X$DISP_NUM"
echo "-> Fixed X11 socket"
else
echo "-> X11 socket is present"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment