Skip to content

Instantly share code, notes, and snippets.

@diegopaludo
Created April 2, 2021 05:20
Show Gist options
  • Save diegopaludo/58a70f61fd167f7934cba87c22ef5d9b to your computer and use it in GitHub Desktop.
Save diegopaludo/58a70f61fd167f7934cba87c22ef5d9b to your computer and use it in GitHub Desktop.
Configurations for WSL2 run X applications
#!/bin/bash
#
# Prepare WSL and Windows to run X applications
# Install on Windows: https://sourceforge.net/projects/vcxsrv/
WIN_USER_PROFILE=$(wslpath $(/mnt/c/Windows/System32/cmd.exe /C "echo %USERPROFILE%" | tr -d '\r\n'))
echo '[wsl2]
memory=4GB
swap=0
localhostForwarding=true' > $WIN_USER_PROFILE/.wslconfig
sudo apt install -y xauth xutils coreutils gawk dbus-x11
export DISPLAY=$(/sbin/ip route | awk '/default/ { print $3 }'):0
SOME_THING=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 18 | head -n 1)
MAGIC_COOKIE=$(echo $SOME_THING | tr -d '\n\r' | md5sum | gawk '{print $1}')
touch $HOME/.Xauthority
xauth add "$DISPLAY" . "$MAGIC_COOKIE"
cp $HOME/.Xauthority "$WIN_USER_PROFILE"
touch $WIN_USER_PROFILE/VcXSrv.log
echo 'if [ -z "$DISPLAY" ]; then
export DISPLAY=$(/sbin/ip route | awk '\''/default/ { print $3 }'\''):0.0
fi' >> $HOME/.bashrc
# Start VcXsrv on Windows startup
echo 'set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Startup")
set oShellLink = WshShell.CreateShortcut(strDesktop & "\VcXsrv.lnk")
oShellLink.TargetPath = "%ProgramFiles%\VcXSrv\VcXSrv.exe"
oShellLink.WorkingDirectory = "%ProgramFiles%\VcXSrv"
oShellLink.Arguments = "-multiwindow -clipboard -wgl -auth %USERPROFILE%\.Xauthority -logfile %USERPROFILE%\VcXSrv.log -logverbose 5"
oShellLink.Save' > ./vcxsrv_shortcut.vbs
wscript.exe ./vcxsrv_shortcut.vbs
rm ./vcxsrv_shortcut.vbs
exit 0
@diegopaludo
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment