Files
tooling/wsl-setup.org
2020-11-04 17:01:09 +13:00

8.6 KiB

Windows Subsystem for Linux Setup

This guide will walk through setting up Windows Subsystem for Linux on Windows 10. This particular setup contains my opinionated view of a good foundation and layers on some pairing and development orientated tooling over top.

Caveats: Please note this guide is written for the [[https://www.microsoft.com/store/productId/9NBLGGH4MSV6][Ubuntu WSL distribution].

Acknowledgements: Large elements of this wsl setup came about through collaboration with the great people at ii.coop. I encourage you to explore and contribute to their work on gitlab as many elements form a core part of this setup and workflow.

Step 1 - Setup home folder structure

After installing the Debian WSL distribution no folders are present in your home folder.

In this section we create some quick standard folders to keep our home folder somewhat organised.

# Ensure we are in our home folder
cd ~/
   
# Create a documents folder for our git repositories
mkdir Documents

# Create a downloads folder for temporary objects
mkdir Downloads

Step 2 - Update and install packages

To get started we ensure the package manager is up to date.

sudo apt-get update && sudo apt-get upgrade

Next we install a series of standard packages that form part of our workflow or are dependencies for other tools in our environment.

sudo apt-get install -y git locales curl wget xclip xsel tmux tmate net-tools less wget htop screenfetch zip openssh-client dictd knockd python3-pip emacs apt-transport-https software-properties-common ca-certificates dirmngr xterm xtermcontrol

We use docker to run containers or kind (kubernetes in docker) locally.

# Download and add Docker's official public PGP key.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# Add the `stable` channel's Docker upstream repository.
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

# Update the apt package list and install docker packages.
sudo apt-get update -y && sudo apt-get install -y docker-ce docker-ce-cli containerd.io

# Allow your user to access the Docker CLI without needing root access.
sudo usermod -aG docker $USER

# Ensure the docker service is started
sudo service docker start

For additional package management namely bitwarden we use node package manager. The code below installs node 12.

# Curl down the shell script for adding version 12 of nodejs to apt
sudo curl  -sL https://deb.nodesource.com/setup_12.x | sudo bash -
   
# Install the nodejs package via apt
sudo apt-get install -y nodejs

For managing secrets we use bitwarden which provides a great cli utility.

This section should be expanded in future to cover setting alias for common bitwarden tasks.

# Install the bitwarden cli via node package manager
sudo npm install -g @bitwarden/cli

# Test login to bitwarden
bw login mail@jamesblair.net

For working with google cloud platform we use the GCP SDK, which provides our cli tools.

# Download the sdk archive
curl -o gcpsdk.tar -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-311.0.0-linux-x86_64.tar.gz

# Extract to a folder in path
sudo tar xvf gcpsdk.tar -C /usr/local/

# Correct folder permissions
sudo chown -R $USER:$USER /usr/local/google-cloud-sdk

# Run the install script
/usr/local/google-cloud-sdk/install.sh

For working with Amazon Web Services we need the AWS CLI.

# Download the binary
cd ~/Downloads/
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

# Install
unzip awscliv2.zip
sudo ./aws/install

# Clean up
rm -rf ~/Downloads/aws*

For cloud infrastructure deployments we use terraforms.

# Download the binary
wget 'https://releases.hashicorp.com/terraform/0.12.24/terraform_0.12.24_linux_amd64.zip'

# Unzip it
unzip *.zip

# Move the binary to path
sudo mv terraform /usr/local/bin/

# Clean up
rm *amd64.zip

For ad-hoc system administration we use ansible. We install ansible via pip to ensure any modules or additional packages required at a later date can be easily managed.

For significant ansible or python projects a virtual environment for python is suggested to keep project packages separate from system python packages.

pip3 install ansible

Step 3 - Setup environment dotfiles

Within wsl we can use .dotfiles to further customise our environment. The script below restores my versions of key dotfiles automatically.

Note: The git clone below relies on having permission to clone the repository referenced. For me this means having an ssh key present which has been added to gitlab.

Obtain ssh keys from bitwarden

In order to be able to clone the repository in the next step we need to obtain our ssh keys from bitwarden. Given we have installed the bitwarden cli we can mostly automte this process minus the initial login to bitwarden.

# Ensure we have an ssh-agent running
eval `ssh-agent`

# Generate a new blank key to overwrite
ssh-keygen -t rsa -f ~/.ssh/james -q -P ""

# Ensure we have an active bitwarden session
export BW_SESSION=$(bw unlock --raw > ~/.bw_session && cat ~/.bw_session)

# Export both keys
export key=$(bw get item desktop --pretty | grep notes)

# Extract private key
export private=${key:12}
export private=${private/END RSA*/END RSA PRIVATE KEY-----}
echo $private | awk '{gsub(/\\n/,"\n")}1' > ~/.ssh/james

# Extract public key
export public=${key/*ssh-rsa/ssh-rsa} && echo ${public::-2} | awk '{gsub(/\\n/,"\n")}1' > ~/.ssh/james.pub

Clone and restore dotfiles

Once our keys are available to us we can clone down our dotfiles and get back to our comfortable normal terminal environment.

# Remove the interactive host prompt
ssh-keyscan -p 2224 gitlab.jamma.life >> ~/.ssh/known_hosts

# Clone down this repository
git clone ssh://git@gitlab.jamma.life:2224/jmhbnz/tooling.git ~/Documents/tooling/
   
# Restore all dotfiles
cp ~/Documents/tooling/.* ~/
   
# Reload bashrc with updated version
source ~/.bashrc

Step 4 - Install humacs editor

An integral part of our pair development workflow is humacs. Below are the instructions to install this directly in WSL, note there are other options to install in the documentation linked above.

# Clone down humacs
git clone --recursive https://github.com/humacs/humacs /home/$USER/


# Need to ensure environment variables are set for load path
export EMACSLOADPATH=/home/$USER/humacs

Step 5 - Setup mutt email client

For reading email we ideally use a cli based client for fast searching and lightweight mail reading.

The mutt mail client fills these roles well for imap mailboxes.

The first step to setup mutt is to ensure it is installed.

sudo apt-get install -y mutt urlscan

After installing mutt we then need to create configuration directories and files.

mkdir -p ~/.mutt/cache/headers
mkdir ~/.mutt/cache/bodies
touch ~/.mutt/certificates

One configuration folders and files exist we just need to populate our user mutt configuration file with a configuration for our particular mail provider.

The example provided in this repository utilises the bitwarden cli utility for secrets to ensure these are securely gathered at runtime and not stored in the file.