Skip to content

Instantly share code, notes, and snippets.

@mfkenson
Last active Oct 24, 2021
Embed
What would you like to do?
wslg snapd ubuntu 20.04
# https://github.com/microsoft/wslg/issues/479
# https://github.com/arkane-systems/genie/issues/175#issuecomment-922526126
# Append below to your .bashrc to fix wslg
wsl_mount_root=/mnt
wsl_windir="${wsl_mount_root}/c/WINDOWS/System32"
wsl_distro=Ubuntu-20.04
wsl_wslg_display=0
# check if in-built wslg socket exists at all
if test -S $wsl_mount_root/wslg/.X11-unix/X0; then
# fix is needed in case system socket in /tmp doesn't exist or it's true, canonical path isn't the wslg socket's one
if ! test -e /tmp/.X11-unix/X${wsl_wslg_display} || ! test "$(readlink -f /tmp/.X11-unix/X${wsl_wslg_display})" = "$wsl_mount_root/wslg/.X11-unix/X0"; then
echo -e "* WSL: restoring WSLG X11 server socket on /tmp/.X11-unix/X${wsl_wslg_display}"
# recreate /tmp/.X11-unix directory if needed
if test ! -d /tmp/.X11-unix; then
$wsl_windir/wsl.exe -d $wsl_distro -u root install -dm 1777 -o root /tmp/.X11-unix
fi
# delete existing socket if needed. this must obviously break something that created that socket
# and you'd rather define display not colliding with other sockets
if test -e /tmp/.X11-unix/X${wsl_wslg_display}; then
$wsl_windir/wsl.exe -d $wsl_distro -u root rm -Rf /tmp/.X11-unix/X${wsl_wslg_display} /tmp/.X${wsl_wslg_display}-lock
fi
# if desired display is different than wslg's default 0, check if WSLG socket doesn't already exist on X0
# if yes, delete it aswell to avoid having the same wslg socket bound to two separate display numbers
if test $wsl_wslg_display -ne 0; then
if test -e /tmp/.X11-unix/X0 && test "$(readlink -f /tmp/.X11-unix/X0)" = "$wsl_mount_root/wslg/.X11-unix/X0"; then
$wsl_windir/wsl.exe -d $wsl_distro -u root rm -Rf /tmp/.X11-unix/X0 /tmp/.X0-lock
fi
fi
# create symlink to wslg socket
$wsl_windir/wsl.exe -d $wsl_distro -u root ln -s $wsl_mount_root/wslg/.X11-unix/X0 /tmp/.X11-unix/X${wsl_wslg_display}
# export updated display number for user's convenience
export DISPLAY=:${wsl_wslg_display}
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment