Skip to content

Instantly share code, notes, and snippets.

@slhad
Last active May 7, 2026 09:13
Show Gist options
  • Select an option

  • Save slhad/ff564a03c95691edab0c05544fbb3d78 to your computer and use it in GitHub Desktop.

Select an option

Save slhad/ff564a03c95691edab0c05544fbb3d78 to your computer and use it in GitHub Desktop.
WSL auto install
#!/bin/bash
# remote exec "curl -sL https://gist.github.com/slhad/ff564a03c95691edab0c05544fbb3d78/raw/wsl_install.sh > ./wsl_install.sh && bash ./wsl_install.sh --help"
archPkgs="which docker mise git base-devel aws-cli-v2 xdg-utils firefox"
misePkgs="starship node@22 go rust k9s kubectl helm k3d jq [email protected] gh rg fd neovim"
npmPkgs="pnpm opencode-ai @mariozechner/pi-coding-agent"
aurPkgs="aws-session-manager-plugin google-chrome"
wslName="archlinux-drift"
# Parse arguments: --clean / -c to unregister existing distro; --name to override
CLEAN=0
# Allow skipping creation of the Linux user via env or CLI
SKIP_USER=${SKIP_USER:-0}
# Allow overriding the default user to create (via env or CLI)
DEFAULT_USER=${DEFAULT_USER:-}
while [[ $# -gt 0 ]]; do
case "$1" in
-c|--clean) CLEAN=1; shift ;;
-n|--name) wslName="$2"; shift 2 ;;
-u|--user) DEFAULT_USER="$2"; shift 2 ;;
--skip-user) SKIP_USER=1; shift ;;
-h|--help) echo "Usage: wsl_install.sh [--clean|-c] [--name NAME] [-u|--user USER] [--skip-user]"; exit 0 ;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
done
echo "------------ WSL install for $wslName ------------"
# Check whether the distribution already exists (match exact name)
if wsl -l -v 2>/dev/null | tr -d '\000' | sed 's/^\\* *//' | awk '{print $1}' | grep -F -x "$wslName" >/dev/null 2>&1; then
distro_exists=1
else
distro_exists=0
fi
# If requested, remove existing distro first
if [ "$distro_exists" -eq 1 ] && [ "$CLEAN" -eq 1 ]; then
echo "Cleaning existing WSL distribution '$wslName'..."
wsl --terminate "$wslName" 2>/dev/null || true
wsl --unregister "$wslName"
distro_exists=0
fi
if [ "$distro_exists" -eq 0 ]; then
wsl --install -d ArchLinux --name "$wslName" --web-download archlinux --no-launch
sleep 15 # give it a moment to register before setting default
else
echo "WSL distribution '$wslName' already exists; skipping installation."
fi
# Determine the Linux user to create: prefer DEFAULT_USER if provided, otherwise derive from Windows username
if [ -n "$DEFAULT_USER" ]; then
win_user="$DEFAULT_USER"
else
win_raw="$(whoami 2>/dev/null || echo '')"
if [ -z "$win_raw" ]; then
win_raw="$USER"
fi
# extract part after last '+' or backslash '\\' if present
if [[ "$win_raw" == *"+"* ]]; then
win_user="${win_raw##*+}"
else
win_user="${win_raw##*\\}"
fi
fi
# sanitize: lowercase, allow a-z0-9 and underscore, trim to 31 chars
user_name="$(echo "$win_user" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9_]/_/g' | cut -c1-31)"
if [ -z "$user_name" ]; then
user_name="wsluser"
fi
if [ "$SKIP_USER" = "0" ]; then
echo "Creating user '$user_name' inside WSL distribution '$wslName' (non-interactive)..."
# Run the creation script inside the distro as root via stdin (avoids nested quoting)
wsl -d "$wslName" -u root -- bash -s "$user_name" <<'WSL_SCRIPT'
set -e
USER_NAME="$1"
if ! id -u "$USER_NAME" >/dev/null 2>&1; then
if command -v useradd >/dev/null 2>&1; then
useradd -m -s /bin/bash "$USER_NAME"
else
if ! getent group "$USER_NAME" >/dev/null 2>&1; then
groupadd "$USER_NAME" >/dev/null 2>&1 || true
fi
UID=1000
while getent passwd "$UID" >/dev/null 2>&1; do UID=$((UID+1)); done
echo "$USER_NAME:x:$UID:$UID:User:/home/$USER_NAME:/bin/bash" >> /etc/passwd
mkdir -p /home/$USER_NAME
chown $UID:$UID /home/$USER_NAME || true
fi
mkdir -p /etc/sudoers.d
echo "$USER_NAME ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/$USER_NAME
chmod 0440 /etc/sudoers.d/$USER_NAME
echo -e "[user]\ndefault=$USER_NAME\n" >> /etc/wsl.conf
else
echo "User $USER_NAME already exists inside distro"
fi
WSL_SCRIPT
# Attempt to set the distro default user so subsequent launches are not root
if ! wsl --manage "$wslName" --set-default-user "$user_name" >/dev/null 2>&1; then
echo "Warning: failed setting default user via 'wsl --manage'"
fi
else
echo "SKIP_USER is set; skipping user creation and default-user configuration."
fi
# Launch a normal interactive WSL session so distro auto-init triggers
# User must exit the shell to continue the script.
INTERACTIVE_TIMEOUT=15
if [ "$SKIP_USER" = "0" ]; then
# Prefer the created user; fallback to default if that user cannot be used
if wsl -d "$wslName" -u "$user_name" -- true >/dev/null 2>&1; then
echo "Opening interactive WSL session for user '$user_name' in distro '$wslName' (will auto-close after ${INTERACTIVE_TIMEOUT}s)."
echo "Perform any initial setup (keyring, etc.)."
# Start a watchdog in the background that will terminate the distro after a timeout.
( sleep "$INTERACTIVE_TIMEOUT"; echo "Timeout reached (${INTERACTIVE_TIMEOUT}s) — terminating WSL distro '$wslName'..."; wsl --terminate "$wslName" >/dev/null 2>&1 || true ) &
watchdog_pid=$!
# Launch without passing an explicit command so WSL performs a normal interactive start
wsl -d "$wslName" -u "$user_name"
# If the user exited before the timeout, cancel the watchdog
kill "$watchdog_pid" >/dev/null 2>&1 || true
wait "$watchdog_pid" 2>/dev/null || true
else
echo "Interactive login as '$user_name' failed; opening default user shell instead (will auto-close after ${INTERACTIVE_TIMEOUT}s)."
( sleep "$INTERACTIVE_TIMEOUT"; echo "Timeout reached (${INTERACTIVE_TIMEOUT}s) — terminating WSL distro '$wslName'..."; wsl --terminate "$wslName" >/dev/null 2>&1 || true ) &
watchdog_pid=$!
wsl -d "$wslName"
kill "$watchdog_pid" >/dev/null 2>&1 || true
wait "$watchdog_pid" 2>/dev/null || true
fi
else
echo "SKIP_USER set; opening interactive login as default user in '$wslName' (will auto-close after ${INTERACTIVE_TIMEOUT}s)."
( sleep "$INTERACTIVE_TIMEOUT"; echo "Timeout reached (${INTERACTIVE_TIMEOUT}s) — terminating WSL distro '$wslName'..."; wsl --terminate "$wslName" >/dev/null 2>&1 || true ) &
watchdog_pid=$!
wsl -d "$wslName"
kill "$watchdog_pid" >/dev/null 2>&1 || true
wait "$watchdog_pid" 2>/dev/null || true
fi
echo "Installing packages inside WSL distribution '$wslName'..."
wsl -d "$wslName" -u root -- pacman -Sy --needed --noconfirm $archPkgs
echo "Cloning and installing yay-bin from AUR..."
wsl -d "$wslName" --cd '~' -- eval 'git clone https://aur.archlinux.org/yay-bin.git && cd yay-bin && makepkg -si --noconfirm && cd .. && rm -rf yay-bin'
export MSYS_NO_PATHCONV=1
echo "Installing additional packages via yay..."
wsl -d "$wslName" -u root -- yay -Sy --noconfirm $aurPkgs
echo "Cloning LazyVim starter config into ~/.config/nvim..."
wsl -d "$wslName" --cd '~' -- eval 'git clone https://github.com/LazyVim/starter ~/.config/nvim && rm -rf ~/.config/nvim/.git'
echo "Fixing docker access"
wsl -d "$wslName" -- eval 'sudo usermod -aG docker $USER'
wsl -d "$wslName" -- eval 'sudo systemctl disable systemd-networkd-wait-online.service'
wsl -d "$wslName" -- eval 'sudo systemctl enable docker.service'
echo "Setting up mise..."
# Ensure the user's .bashrc exists and append mise activation safely inside the distro.
wsl -d "$wslName" -u root -- bash -s "$user_name" <<'WSL_SCRIPT'
USER_NAME="$1"
touch "/home/$USER_NAME/.bashrc"
grep -qxF 'eval "$(/usr/sbin/mise activate bash)"' "/home/$USER_NAME/.bashrc" || cat >> "/home/$USER_NAME/.bashrc" <<'MISE_EOF'
eval "$(/usr/sbin/mise activate bash)"
MISE_EOF
chown "$USER_NAME":"$USER_NAME" "/home/$USER_NAME/.bashrc" || true
WSL_SCRIPT
wsl -d "$wslName" -- mise use -g $misePkgs
echo "Setting up starship prompt..."
wsl -d "$wslName" -u root -- bash -s "$user_name" <<'WSL_SCRIPT'
USER_NAME="$1"
touch "/home/$USER_NAME/.bashrc"
grep -qxF 'eval "$(starship init bash)"' "/home/$USER_NAME/.bashrc" || cat >> "/home/$USER_NAME/.bashrc" <<'STARSHIP_EOF'
eval "$(starship init bash)"
STARSHIP_EOF
chown "$USER_NAME":"$USER_NAME" "/home/$USER_NAME/.bashrc" || true
WSL_SCRIPT
echo "Restarting WSL distribution '$wslName' to unlock npm thinking WSL is in version 1"
sleep 15
wsl --terminate "$wslName"
sleep 15
echo "Installing pnpm, opencode-ai, and pi-coding-agent globally via npm..."
wsl -d "$wslName" --cd '~' bash -i -s "$npmPkgs" <<'WSL_SCRIPT'
npm install -g $1
WSL_SCRIPT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment