Skip to content

WSL 🍻 ​

I LOVE WSL!

I like windows's UI simple I can do jobs faster than mac and adding linux terminal on that is perfect work environment for me.

Some benefist of this setup:

  • Use new windows terminal (best terminal ever) setting default terminal to wsl
  • Run docker under wsl
  • Vscode and see the wsl file system
  • View Linux gui apps under wsl
  • Runnable .exe files from terminal

There is still some cons, need to much more flexibility of the wsl machine like networking, passthrough devices.

Issues ​

Cannot execute windows binaries ​

Old trick, looks not needed anymore

microsoft/WSL#8843 (comment)

sh
sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'

After shutdown and restart WSL, you can execute windows binaries.

sh
wsl --shutdown

WSL - hard disk got all space ​

First install Optimize-VHD and it is come with Microsoft-Hyper-V check powershell and if not exist than install it:

Remember to run powershell as administrator

powershell
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All

Or just management tools:

powershell
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-Management-PowerShell

Than in your ext4.vhdx file location run (this takes time):

powershell
Optimize-VHD -Path "C:\Users\rytsh\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_42thkp1fnegsc\LocalState\ext4.vhdx" -Mode Full

Use windirstat program easily to find big files https://windirstat.net/

xdg-open ​

Redirect xdg-open to powershell.exe start command, so you can open files from wsl terminal.

sh
sudo tee /usr/local/bin/xdg-open <<EOF
#!/bin/sh

powershell.exe -c start "'\$@'"
EOF
sudo chmod +x /usr/local/bin/xdg-open

https://stackoverflow.com/questions/56633006/open-windows-native-exe-program-via-xdg-open-in-wsl

Set browser so golang use that one directly:

sh
sudo tee /usr/local/bin/windows-browser >/dev/null <<'EOF'
#!/bin/sh

url="$1"

case "$url" in
  file://*)
    path="${url#file://}"
    distro="$(wsl.exe -l -q | tr -d '\r' | head -n1)"
    winpath="\\\\wsl.localhost\\${distro}$(printf '%s' "$path" | sed 's#/#\\#g')"
    powershell.exe -NoProfile -Command "Start-Process '$winpath'"
    ;;
  *)
    powershell.exe -NoProfile -Command "Start-Process '$url'"
    ;;
esac
EOF

sudo chmod +x /usr/local/bin/windows-browser

cat <<'EOF' >> ~/.bashrc
export BROWSER=/usr/local/bin/windows-browser
EOF