stevej / WSL2_4_nix_nerds

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Table of Contents

WSL2_4_Nix_Nerds - Introduction

A quickstart guide to getting Debian in WSL2 to an acceptable working state. If you have VMWare Workstation, Docker For Windows, or VirtualBox then the "VirtualMachinePlatform" feature MAY affect them by not allowing you to start up the VMs you have already built as this is run via Hyper-V. See FAQ

Pre-requisites

Install WSL2

Really just follow the steps (https://docs.microsoft.com/en-us/windows/wsl/install-win10), but here's the quick steps I used: In PowerShell as Administrator

  • dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  • dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  • Restart-Computer
  • Install updated Linux Kernel : https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
  • Set default WSL to version 2 : wsl --set-default-version 2
  • Install your preferred distro from the MS Store
  • Run your installation and go through the install and initialization steps, username and password
  • (Optional) VSCode On Windows (PowerShell as Administrator) - choco install vscode

Post-Install (the stuff you're here for)

Windows Terminal (WT) Settings

  • WT Settings - Can be reached via Windows Terminal Settings, Ctrl+, or click on the down chevron next to the + symbol in the tabline. If you haven't configured an editor for .json files, it will ask you for one. I'm using VSCode.
  • See the Windows Terminal page for detailed customization options. This is where behaviour and look and feel (background images, transparency etc) are defined.
  • Useful trick: If you have Windows Terminal running and while you have the configuration file open, when you save settings, Terminal will reload them right away and you can see what effect you've had on it right away.

Two ways to alter what your default profile will be. Right now it will launch PowerShell for you. To set it to Debian, from within PowerShell wsl -s Debian. You can also set the "defaultProfile" attribute to the GUID value of the Unix install from within the Settings json configuration file. Altering the order of the profile list will alter the order of your shell choices when you right click the WT icon.

My WT Settings

  • Launch your Debian instance via right-click on the Windows Terminal icon and choosing your Debian instance
  • Choose your font, font size, and set your default start directory will be the c:\users\<username>\ directory. If you perfer good old fashioned /home/<username> then set it as per below. There are different ways fonts could be installed, either via choco or manually downloaded from Nerd Fonts. Make sure you have the correct font name by using the "Font" tool in the Start menu. You can put these in the "default" section of the "profiles", or per profile. These examples are per profile and in the "name":"Debian" profile for myself. Order of attributes doesn't seem to matter.
      "startingDirectory": "//wsl$//Debian//home//gclair",
      //"fontFace": "MesloLGS NF",
      "fontFace": "CaskaydiaCove Nerd Font Mono",
      "fontSize": 10,
  • Copy on Selection: "copyOnSelect": true defaults to false. Selection is copied to clipboard on selection. Right click will then always paste.
  • Paste Warnings (can be annoying): "largePasteWarning" or "multiLinePasteWarning" set to true or false per users choice. Defaults to true

Linux Configuration

  • DNS is usually broken - Networking info is obtained via WSL Ethernet interface and gets its own private host only subnet. Edit /etc/wsl.conf with the following to "permanently" fix it:
[network]
generateResolvConf = false
  • Restart your Debian instance (Powershell): wsl -t Debian, launch WT Debian instance
  • Fix resolv.conf (should no longer be updating): sudo rm /etc/resolv.conf && sudo echo nameserver 8.8.4.4 >/etc/resolv.conf - Yes its Google's NS, but i have flaky issues with Cloudflare it seems.
  • Debian doesn't install man by default or other useful packages: sudo apt update && sudo apt install keychain dnsutils vim man curl wget zsh git build-essential
  • Homebrew on Linux - I find distribution repos don't necessarily have all the up to date/best versions of software. Brew won't install until you fix your networking and get curl or wget installed. Add brew and its packages to your $PATH : /home/linuxbrew/.linuxbrew/bin
  • FUN FACT You can launch your Windows Executables from here. Try it! If you have VSCode in Windows just run code. It will launch the Windows version of code that recognizes WSL! See the YouTube link in the references. For fun, hack yourself! calc.exe .. Yes its a joke.

SSH Agent

The accepted way to use SSH-agent and keep your sessions configured via tmux seems to be keychain.

Added to my .zshrc:

HOSTNAME=`hostname`
/usr/bin/keychain --nogui $HOME/.ssh/<MY_SSH_PRIVKEY>
source $HOME/.keychain/$HOSTNAME-sh

HOSTNAME is not set in WSL2, so if you don't change it often, hardcoding it will be fine.

ZSH

  • Switch to zsh: chsh -s /bin/zsh <yourusername> , restart WT session.

I like the usage of plugins with ZSH using Oh My Zsh! and Powerlevel10k which really exercise those NerdFonts well. They recommend using the Meslo Nerd Font.

VIM

I am a user of SpaceVim. It looks pretty and works relatively well for me. I didn't have to do many changes here. If you're an experienced dev, you have all your customizations in your github ready to go. I however, no not ¯\_(ツ)_/¯

Tmux

This needs a section all its own, but if you got this far you know what you're doing.

Terminal Setting

I think the important part of tmux configuration is setting the terminal in your ~/.tmux.conf.local OR ~/.tmux.conf :
set -g default-terminal "xterm-256color" or to "screen-256color". The xterm-256color seems to work for me.

Cut and Paste

When I installed powerlevel10k and configured it with p10k configure, the tmux.conf is configurues is very clever and includes cut and paste to the OS clipboard.

    if -b 'command -v clip.exe > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | clip.exe"'
    if -b '[ -c /dev/clipboard ]' 'bind y run -b "tmux save-buffer - > /dev/clipboard"'

Controlling your instances

  • Instances are managed via powershell (admin) with wsl.
  • wsl --list --verbose OR wsl -l -v : list your installed instances
  • wsl -s Debian : Set your default distribution to start to the named "Debian" distro.
  • wsl -t Debian : Terminate the instance. Sometimes this is the 'reset' button you need
  • I've never had to start the WSL2 instance as this is done automatically when you start Windows Terminal *nix instance.

References

About