Skip to content

Instantly share code, notes, and snippets.

@scowalt
Last active March 2, 2025 08:26
Show Gist options
  • Save scowalt/5f3e0d7736c573cf33897252b85e3811 to your computer and use it in GitHub Desktop.
Save scowalt/5f3e0d7736c573cf33897252b85e3811 to your computer and use it in GitHub Desktop.
Machine setup scripts

Machine setup scripts

Idempotent scripts I use to set up my machines.

Windows

iwr -useb https://scripts.scowalt.com/setup/win.ps1 | iex

WSL

curl -sL https://scripts.scowalt.com/setup/wsl.sh | bash

MacOS

curl -sL https://scripts.scowalt.com/setup/mac.sh | bash

Ubuntu

curl -sL https://scripts.scowalt.com/setup/ubuntu.sh | bash
#!/bin/bash
# Define colors
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Print functions for readability
print_message() { echo -e "${CYAN}\u27A1 ${1}${NC}"; }
print_success() { echo -e "${GREEN}\u2714 ${1}${NC}"; }
print_warning() { echo -e "${YELLOW}\u2757 ${1}${NC}"; }
print_error() { echo -e "${RED}\u2718 ${1}${NC}"; }
# Install core packages with Homebrew if missing
install_core_packages() {
print_message "Checking and installing core packages as needed..."
# Define an array of required packages
local packages=("git" "curl" "fish" "tmux" "1password-cli" "gh" "chezmoi" "starship")
local to_install=()
# Check each package and add missing ones to the to_install array
for package in "${packages[@]}"; do
if ! brew list "$package" &> /dev/null; then
to_install+=("$package")
else
print_warning "$package is already installed."
fi
done
# Install any packages that are not yet installed
if [ "${#to_install[@]}" -gt 0 ]; then
print_message "Installing missing packages: ${to_install[*]}"
brew install "${to_install[@]}" > /dev/null
print_success "Missing core packages installed."
else
print_success "All core packages are already installed."
fi
}
# Check and set up SSH key
setup_ssh_key() {
print_message "Checking for existing SSH key associated with GitHub..."
# Retrieve GitHub-associated keys
local existing_keys
existing_keys=$(curl -s https://github.com/scowalt.keys)
if [ -f ~/.ssh/id_rsa.pub ]; then
local local_key
local_key=$(awk '{print $2}' ~/.ssh/id_rsa.pub)
if echo "$existing_keys" | grep -q "$local_key"; then
print_success "Existing SSH key recognized by GitHub."
else
print_error "SSH key not recognized by GitHub. Please add it manually."
exit 1
fi
else
print_warning "No SSH key found. Generating a new SSH key..."
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -N ""
print_success "SSH key generated."
print_message "Please add the following SSH key to GitHub:"
cat ~/.ssh/id_rsa.pub
exit 1
fi
}
# Install Homebrew if not installed
install_homebrew() {
if ! command -v brew &> /dev/null; then
print_message "Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" > /dev/null
eval "$(/opt/homebrew/bin/brew shellenv)"
print_success "Homebrew installed."
else
print_warning "Homebrew is already installed."
eval "$(/opt/homebrew/bin/brew shellenv)"
fi
}
# Initialize chezmoi if not already initialized
initialize_chezmoi() {
if [ ! -d ~/.local/share/chezmoi ]; then
print_message "Initializing chezmoi with scowalt/dotfiles..."
chezmoi init --apply scowalt/dotfiles --ssh > /dev/null
print_success "chezmoi initialized with scowalt/dotfiles."
else
print_warning "chezmoi is already initialized."
fi
}
# Configure chezmoi for auto commit, push, and pull
configure_chezmoi_git() {
local chezmoi_config=~/.config/chezmoi/chezmoi.toml
if [ ! -f "$chezmoi_config" ]; then
print_message "Configuring chezmoi with auto-commit, auto-push, and auto-pull..."
mkdir -p ~/.config/chezmoi
cat <<EOF > "$chezmoi_config"
[git]
autoCommit = true
autoPush = true
autoPull = true
EOF
print_success "chezmoi configuration set."
else
print_warning "chezmoi configuration already exists."
fi
}
# Set Fish as the default shell if it isn't already
set_fish_as_default_shell() {
if [ "$SHELL" != "/opt/homebrew/bin/fish" ]; then
print_message "Setting Fish as the default shell..."
if ! grep -Fxq "/opt/homebrew/bin/fish" /etc/shells; then
echo "/opt/homebrew/bin/fish" | sudo tee -a /etc/shells > /dev/null
fi
chsh -s /opt/homebrew/bin/fish
print_success "Fish shell set as default."
else
print_warning "Fish shell is already the default shell."
fi
}
# Install tmux plugins for session persistence
install_tmux_plugins() {
local plugin_dir=~/.tmux/plugins
if [ ! -d "$plugin_dir/tpm" ]; then
print_message "Installing tmux plugin manager..."
git clone -q https://github.com/tmux-plugins/tpm "$plugin_dir/tpm"
print_success "tmux plugin manager installed."
else
print_warning "tmux plugin manager already installed."
fi
for plugin in tmux-resurrect tmux-continuum; do
if [ ! -d "$plugin_dir/$plugin" ]; then
print_message "Installing $plugin..."
git clone -q https://github.com/tmux-plugins/$plugin "$plugin_dir/$plugin"
print_success "$plugin installed."
else
print_warning "$plugin already installed."
fi
done
tmux source ~/.tmux.conf 2> /dev/null || print_warning "tmux not started; source tmux.conf manually if needed."
~/.tmux/plugins/tpm/bin/install_plugins > /dev/null
print_success "tmux plugins installed and updated."
}
update_brew() {
print_message "Updating Homebrew..."
brew update > /dev/null
print_message "Upgrading outdated packages..."
brew upgrade > /dev/null
print_success "Homebrew updated."
}
# Run the setup tasks
print_message "Version 4 (macOS)"
install_homebrew
install_core_packages
setup_ssh_key
initialize_chezmoi
configure_chezmoi_git
chezmoi apply
set_fish_as_default_shell
install_tmux_plugins
update_brew
#!/bin/bash
# Define colors
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Print functions for readability
print_message() { echo -e "${CYAN}==> ${1}${NC}"; }
print_success() { echo -e "${GREEN}✔ ${1}${NC}"; }
print_warning() { echo -e "${YELLOW}! ${1}${NC}"; }
print_error() { echo -e "${RED}✘ ${1}${NC}"; }
# Update dependencies non-silently
update_dependencies() {
print_message "Updating package lists..."
sudo apt update
sudo apt upgrade -y
sudo apt autoremove -y
print_success "Package lists updated."
}
# Update and install core dependencies silently
update_and_install_core() {
print_message "Checking and installing core packages as needed..."
# Define an array of required packages
local packages=("git" "curl" "fish" "tmux" "fonts-firacode")
local to_install=()
# Check each package and add missing ones to the to_install array
for package in "${packages[@]}"; do
if ! dpkg -s "$package" &> /dev/null; then
to_install+=("$package")
else
print_warning "$package is already installed."
fi
done
# Install any packages that are not yet installed
if [ "${#to_install[@]}" -gt 0 ]; then
print_message "Installing missing packages: ${to_install[*]}"
sudo apt update -qq > /dev/null
sudo apt install -qq -y "${to_install[@]}" > /dev/null
print_success "Missing core packages installed."
else
print_success "All core packages are already installed."
fi
}
# Check and set up SSH key
setup_ssh_key() {
print_message "Checking for existing SSH key associated with GitHub..."
local existing_keys
existing_keys=$(curl -s https://github.com/scowalt.keys)
# Remember - chezmoi will set up authorized_keys for you
# Check if a local SSH key exists
if [ -f ~/.ssh/id_rsa.pub ]; then
# Extract only the actual key part from id_rsa.pub and log for debugging
local local_key
local_key=$(awk '{print $2}' ~/.ssh/id_rsa.pub)
# Verify if the extracted key part matches any of the GitHub keys
if echo "$existing_keys" | grep -q "$local_key"; then
print_success "Existing SSH key recognized by GitHub."
else
print_error "SSH key not recognized by GitHub. Please add it manually."
exit 1
fi
else
# Generate a new SSH key and log details
print_warning "No SSH key found. Generating a new SSH key..."
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -N "" -C "scowalt@wsl"
print_success "SSH key generated."
print_message "Please add the following SSH key to GitHub:"
cat ~/.ssh/id_rsa.pub
exit 1
fi
}
# Install Starship if not installed
install_starship() {
if ! command -v starship &> /dev/null; then
print_message "Installing Starship prompt..."
sh -c "$(curl -fsSL https://starship.rs/install.sh)"
print_success "Starship installed."
else
print_warning "Starship is already installed."
fi
}
# Install chezmoi if not installed
install_chezmoi() {
if ! command -v chezmoi &> /dev/null; then
print_message "Installing chezmoi..."
sh -c "$(curl -fsLS get.chezmoi.io)"
# Add ~/bin to PATH if not already present
if ! grep -q "PATH=\$HOME/bin" ~/.bashrc; then
echo 'export PATH=$HOME/bin:$PATH' >> ~/.bashrc
fi
print_success "chezmoi installed."
else
print_warning "chezmoi is already installed."
fi
}
# Initialize chezmoi if not already initialized
initialize_chezmoi() {
if [ ! -d ~/.local/share/chezmoi ]; then
print_message "Initializing chezmoi with scowalt/dotfiles..."
chezmoi init --apply scowalt/dotfiles --ssh > /dev/null
print_success "chezmoi initialized with scowalt/dotfiles."
else
print_warning "chezmoi is already initialized."
fi
}
# Configure chezmoi for auto commit, push, and pull
configure_chezmoi_git() {
local chezmoi_config=~/.config/chezmoi/chezmoi.toml
if [ ! -f "$chezmoi_config" ]; then
print_message "Configuring chezmoi with auto-commit, auto-push, and auto-pull..."
mkdir -p ~/.config/chezmoi
cat <<EOF > "$chezmoi_config"
[git]
autoCommit = true
autoPush = true
autoPull = true
EOF
print_success "chezmoi configuration set."
else
print_warning "chezmoi configuration already exists."
fi
}
# Set Fish as the default shell if it isn't already
set_fish_as_default_shell() {
if [ "$(getent passwd $USER | cut -d: -f7)" != "/usr/bin/fish" ]; then
print_message "Setting Fish as the default shell..."
if ! grep -Fxq "/usr/bin/fish" /etc/shells; then
echo "/usr/bin/fish" | sudo tee -a /etc/shells > /dev/null
fi
chsh -s /usr/bin/fish
print_success "Fish shell set as default."
else
print_warning "Fish shell is already the default shell."
fi
}
# Install tmux plugins for session persistence
install_tmux_plugins() {
local plugin_dir=~/.tmux/plugins
if [ ! -d "$plugin_dir/tpm" ]; then
print_message "Installing tmux plugin manager..."
git clone -q https://github.com/tmux-plugins/tpm "$plugin_dir/tpm"
print_success "tmux plugin manager installed."
else
print_warning "tmux plugin manager already installed."
fi
for plugin in tmux-resurrect tmux-continuum; do
if [ ! -d "$plugin_dir/$plugin" ]; then
print_message "Installing $plugin..."
git clone -q https://github.com/tmux-plugins/$plugin "$plugin_dir/$plugin"
print_success "$plugin installed."
else
print_warning "$plugin already installed."
fi
done
tmux source ~/.tmux.conf 2> /dev/null || print_warning "tmux not started; source tmux.conf manually if needed."
~/.tmux/plugins/tpm/bin/install_plugins > /dev/null
print_success "tmux plugins installed and updated."
}
update_dependencies # I do this first b/c on raspberry pi, it's slow
update_and_install_core
setup_ssh_key
install_starship
install_chezmoi
initialize_chezmoi
configure_chezmoi_git
chezmoi apply
set_fish_as_default_shell
install_tmux_plugins
$wingetPackages = (
"tailscale.tailscale",
"Readdle.Spark",
"Google.Chrome",
"TheBrowserCompany.Arc",
"Schniz.fnm",
"twpayne.chezmoi",
"Git.Git",
"Tyrrrz.LightBulb",
"Microsoft.VisualStudioCode",
"Microsoft.PowerToys",
"File-New-Project.EarTrumpet",
"AgileBits.1Password",
"Starship.Starship",
"mulaRahul.Keyviz",
"GitHub.cli",
"Anysphere.Cursor",
"Oven-sh.Bun",
"Beeper.Beeper",
"Flow-Launcher.Flow-Launcher",
"gerardog.gsudo",
"GnuWin32.Which",
"strayge.tray-monitor",
"DEVCOM.JetBrainsMonoNerdFont"
)
# Define Nerd Font symbols using Unicode code points
$arrow = [char]0xf0a9 # Arrow icon for actions
$success = [char]0xf00c # Checkmark icon for success
$warnIcon = [char]0xf071 # Warning icon for warnings
$failIcon = [char]0xf00d # Cross icon for errors
function Install-Chezmoi {
if (-not (Get-Command chezmoi -ErrorAction SilentlyContinue)) {
Write-Host "$failIcon Failed to install chezmoi." -ForegroundColor Red
exit 1
}
else {
Write-Host "$warnIcon chezmoi is already installed." -ForegroundColor Yellow
}
# Initialize chezmoi if not already initialized
$chezmoiConfigPath = "$HOME\AppData\Local\chezmoi"
if (-not (Test-Path $chezmoiConfigPath)) {
Write-Host "$arrow Initializing chezmoi with scowalt/dotfiles..." -ForegroundColor Cyan
chezmoi init --apply scowalt/dotfiles --ssh
Write-Host "$success chezmoi initialized with scowalt/dotfiles." -ForegroundColor Green
}
else {
Write-Host "$warnIcon chezmoi is already initialized." -ForegroundColor Yellow
}
# Configure chezmoi for auto-commit, auto-push, and auto-pull
$chezmoiTomlPath = "$HOME\.config\chezmoi\chezmoi.toml"
if (-not (Test-Path $chezmoiTomlPath)) {
Write-Host "$arrow Configuring chezmoi with auto-commit, auto-push, and auto-pull..." -ForegroundColor Cyan
New-Item -ItemType Directory -Force -Path (Split-Path $chezmoiTomlPath)
@"
[git]
autoCommit = true
autoPush = true
autoPull = true
"@ | Set-Content -Path $chezmoiTomlPath
Write-Host "$success chezmoi configuration set." -ForegroundColor Green
}
else {
Write-Host "$warnIcon chezmoi configuration already exists." -ForegroundColor Yellow
}
Write-Host "$arrow Applying chezmoi dotfiles..." -ForegroundColor Cyan
chezmoi apply
Write-Host "$success chezmoi dotfiles applied." -ForegroundColor Green
}
$githubUsername = "scowalt"
$githubKeysUrl = "https://github.com/$githubUsername.keys"
$localKeyPath = "$HOME\.ssh\id_rsa.pub"
function Test-GithubSSHKeyAlreadyAdded {
# Fetch existing GitHub SSH keys
try {
$githubKeys = Invoke-RestMethod -Uri $githubKeysUrl -ErrorAction Stop
$githubKeyPortions = $githubKeys -split "`n" | ForEach-Object { ($_ -split " ")[1] }
}
catch {
Write-Host "$failIcon Failed to fetch SSH keys from GitHub." -ForegroundColor Red
exit 1
}
$localKeyContent = Get-Content -Path $localKeyPath
# Extract the actual key portion (second field in the file)
$localKeyValue = ($localKeyContent -split " ")[1]
# Compare local key with each GitHub key portion
if ($githubKeyPortions -contains $localKeyValue) {
Write-Host "$success Existing SSH key is recognized by GitHub." -ForegroundColor Green
return $true
}
else {
Write-Host "$failIcon SSH key not recognized by GitHub. Please add it manually." -ForegroundColor Red
Write-Host "Public key content to add:" -ForegroundColor Yellow
Write-Host $localKeyContent -ForegroundColor Yellow
return $false
}
}
# Function to check and set up SSH key for GitHub
function Test-GitHubSSHKey {
Write-Host "$arrow Checking for existing SSH key associated with GitHub..." -ForegroundColor Cyan
# Check for existing SSH key locally
if (Test-Path $localKeyPath) {
# no need to generate
}
else {
# Generate a new SSH key if none exists
Write-Host "$warnIcon No SSH key found. Generating a new SSH key..." -ForegroundColor Yellow
# Create the .ssh folder if it doesn't exist
if (-not (Test-Path "$HOME\.ssh")) {
New-Item -ItemType Directory -Force -Path "$HOME\.ssh"
}
& ssh-keygen -t rsa -b 4096 -f $localKeyPath.Replace(".pub", "") -N `"`" -C "$githubUsername@windows"
Write-Host "$success SSH key generated." -ForegroundColor Green
Write-Host "Please add the following SSH key to GitHub:" -ForegroundColor Cyan
Get-Content -Path $localKeyPath
}
$keyadded = $false
do {
$keyadded = Test-GithubSSHKeyAlreadyAdded
if ($keyadded -eq $false) {
Write-Host "Press Enter to check if the key has been added to GitHub..."
[void][System.Console]::ReadLine()
}
} while ($keyadded -eq $false)
}
# Function to add Starship initialization to PowerShell profile
function Set-StarshipInit {
$profilePath = $PROFILE
$starshipInitCommand = 'Invoke-Expression (&starship init powershell)'
$escapedPattern = [regex]::Escape($starshipInitCommand)
if (-not (Select-String -Path $profilePath -Pattern $escapedPattern -Quiet)) {
Add-Content -Path $profilePath -Value "`n$starshipInitCommand"
Write-Host "$success Starship initialization command added to PowerShell profile." -ForegroundColor Green
}
else {
Write-Host "$warnIcon Starship initialization command is already in PowerShell profile." -ForegroundColor Yellow
}
}
function Install-WingetPackages {
Write-Host "$arrow Checking for missing winget packages..." -ForegroundColor Cyan
# Get installed packages
$installedPackages = @()
try {
# Export the list to a temporary JSON file to handle large outputs
$tempFile = [System.IO.Path]::GetTempFileName()
$null = winget export -o $tempFile --accept-source-agreements 2>&1
if (Test-Path $tempFile) {
$jsonContent = Get-Content $tempFile -Raw | ConvertFrom-Json
$installedPackages = $jsonContent.Sources.Packages | ForEach-Object { $_.PackageIdentifier }
Remove-Item $tempFile -Force
Write-Host "$success Found $($installedPackages.Count) installed packages." -ForegroundColor Green
}
}
catch {
Write-Host "$warnIcon Could not get list of installed packages: $($_.Exception.Message)" -ForegroundColor Yellow
Write-Host "$arrow Will check each package individually..." -ForegroundColor Cyan
}
# Install missing packages
foreach ($package in $wingetPackages) {
$isInstalled = $false
# First check our cached list
if ($installedPackages -contains $package) {
$isInstalled = $true
}
else {
# Fallback to direct check if cached list failed
$searchResult = winget list --id $package --exact --accept-source-agreements
$isInstalled = $searchResult -like "*$package*"
}
if (-not $isInstalled) {
Write-Host "$arrow Installing $package..." -ForegroundColor Cyan
winget install -e --id $package --silent --accept-package-agreements --accept-source-agreements
if ($?) {
Write-Host "$success $package installed." -ForegroundColor Green
} else {
Write-Host "$failIcon Failed to install $package." -ForegroundColor Red
}
}
# else {
# Write-Host "$warnIcon $package is already installed." -ForegroundColor Yellow
# }
}
}
function Install-WingetUpdates {
Write-Host "$arrow Checking for available WinGet updates..." -ForegroundColor Cyan
gsudo winget upgrade --all
if ($?) {
Write-Host "$success WinGet updates installed." -ForegroundColor Green
}
else {
Write-Host "$failIcon Error installing WinGet updates" -ForegroundColor Yellow
}
}
function Install-WindowsUpdates {
Write-Host "$arrow Installing Windows updates..." -ForegroundColor Cyan
gsudo {
Install-Module -Name PSWindowsUpdate;
Import-Module PSWindowsUpdate;
Get-WindowsUpdate;
Install-WindowsUpdate -AcceptAll
}
}
function Set-WindowsTerminalConfiguration {
Write-Host "$arrow Configuring Windows Terminal settings..." -ForegroundColor Cyan
$settingsPath = "$env:LocalAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"
$settings = Get-Content -Path $settingsPath | ConvertFrom-Json
# Ensure profiles, defaults, and font objects exist
if (-not $settings.profiles) {
$settings | Add-Member -MemberType NoteProperty -Name profiles -Value @{}
}
if (-not $settings.profiles.defaults) {
$settings.profiles | Add-Member -MemberType NoteProperty -Name defaults -Value @{}
}
if (-not $settings.profiles.defaults.font) {
$settings.profiles.defaults | Add-Member -MemberType NoteProperty -Name font -Value @{}
}
# Set the font face
$settings.profiles.defaults.font.face = "JetBrainsMono Nerd Font Mono"
$settings | ConvertTo-Json -Depth 10 | Set-Content -Path $settingsPath
Write-Host "$success Windows Terminal settings updated." -ForegroundColor Green
}
# Main setup function to call all necessary steps
function Initialize-WindowsEnvironment {
Write-Host "$arrow Starting Windows setup v18" -ForegroundColor Cyan
Install-WingetPackages
Test-GitHubSSHKey # this needs to be run before chezmoi to get access to dotfiles
Install-Chezmoi
Set-StarshipInit
Set-WindowsTerminalConfiguration
Install-WingetUpdates
Install-WindowsUpdates # this should always be LAST since it may prompt a system reboot
Write-Host "$success Done" -ForegroundColor Green
}
# Run the main setup function
Initialize-WindowsEnvironment
#!/bin/bash
# Define colors
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Print functions for readability
print_message() { echo -e "${CYAN}==> ${1}${NC}"; }
print_success() { echo -e "${GREEN}${1}${NC}"; }
print_warning() { echo -e "${YELLOW}! ${1}${NC}"; }
print_error() { echo -e "${RED}${1}${NC}"; }
# Update and install core dependencies silently
update_and_install_core() {
print_message "Checking and installing core packages as needed..."
# Define an array of required packages
local packages=("git" "curl" "fish" "tmux")
local to_install=()
# Check each package and add missing ones to the to_install array
for package in "${packages[@]}"; do
if ! dpkg -s "$package" &> /dev/null; then
to_install+=("$package")
else
print_warning "$package is already installed."
fi
done
# Install any packages that are not yet installed
if [ "${#to_install[@]}" -gt 0 ]; then
print_message "Installing missing packages: ${to_install[*]}"
sudo apt update -qq > /dev/null
sudo apt install -qq -y "${to_install[@]}" > /dev/null
print_success "Missing core packages installed."
else
print_success "All core packages are already installed."
fi
}
# Check and set up SSH key
setup_ssh_key() {
print_message "Checking for existing SSH key associated with GitHub..."
# Retrieve GitHub-associated keys and log for debug purposes
local existing_keys
existing_keys=$(curl -s https://github.com/scowalt.keys)
# Check if a local SSH key exists
if [ -f ~/.ssh/id_rsa.pub ]; then
# Extract only the actual key part from id_rsa.pub and log for debugging
local local_key
local_key=$(awk '{print $2}' ~/.ssh/id_rsa.pub)
# Verify if the extracted key part matches any of the GitHub keys
if echo "$existing_keys" | grep -q "$local_key"; then
print_success "Existing SSH key recognized by GitHub."
else
print_error "SSH key not recognized by GitHub. Please add it manually."
exit 1
fi
else
# Generate a new SSH key and log details
print_warning "No SSH key found. Generating a new SSH key..."
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -N "" -C "scowalt@wsl"
print_success "SSH key generated."
print_message "Please add the following SSH key to GitHub:"
cat ~/.ssh/id_rsa.pub
exit 1
fi
}
# Install Homebrew if not installed
install_homebrew() {
if ! command -v brew &> /dev/null; then
print_message "Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" > /dev/null
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
print_success "Homebrew installed."
else
print_warning "Homebrew is already installed."
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
fi
}
# Install Starship if not installed
install_starship() {
if ! command -v starship &> /dev/null; then
print_message "Installing Starship prompt..."
brew install starship > /dev/null
print_success "Starship installed."
else
print_warning "Starship is already installed."
fi
}
# Install chezmoi if not installed
install_chezmoi() {
if ! command -v chezmoi &> /dev/null; then
print_message "Installing chezmoi..."
brew install chezmoi > /dev/null
print_success "chezmoi installed."
else
print_warning "chezmoi is already installed."
fi
}
# Initialize chezmoi if not already initialized
initialize_chezmoi() {
if [ ! -d ~/.local/share/chezmoi ]; then
print_message "Initializing chezmoi with scowalt/dotfiles..."
chezmoi init --apply scowalt/dotfiles --ssh > /dev/null
print_success "chezmoi initialized with scowalt/dotfiles."
else
print_warning "chezmoi is already initialized."
fi
}
# Configure chezmoi for auto commit, push, and pull
configure_chezmoi_git() {
local chezmoi_config=~/.config/chezmoi/chezmoi.toml
if [ ! -f "$chezmoi_config" ]; then
print_message "Configuring chezmoi with auto-commit, auto-push, and auto-pull..."
mkdir -p ~/.config/chezmoi
cat <<EOF > "$chezmoi_config"
[git]
autoCommit = true
autoPush = true
autoPull = true
EOF
print_success "chezmoi configuration set."
else
print_warning "chezmoi configuration already exists."
fi
}
# Set Fish as the default shell if it isn't already
set_fish_as_default_shell() {
if [ "$(getent passwd $USER | cut -d: -f7)" != "/usr/bin/fish" ]; then
print_message "Setting Fish as the default shell..."
if ! grep -Fxq "/usr/bin/fish" /etc/shells; then
echo "/usr/bin/fish" | sudo tee -a /etc/shells > /dev/null
fi
chsh -s /usr/bin/fish
print_success "Fish shell set as default."
else
print_warning "Fish shell is already the default shell."
fi
}
# Install tmux plugins for session persistence
install_tmux_plugins() {
local plugin_dir=~/.tmux/plugins
if [ ! -d "$plugin_dir/tpm" ]; then
print_message "Installing tmux plugin manager..."
git clone -q https://github.com/tmux-plugins/tpm "$plugin_dir/tpm"
print_success "tmux plugin manager installed."
else
print_warning "tmux plugin manager already installed."
fi
for plugin in tmux-resurrect tmux-continuum; do
if [ ! -d "$plugin_dir/$plugin" ]; then
print_message "Installing $plugin..."
git clone -q https://github.com/tmux-plugins/$plugin "$plugin_dir/$plugin"
print_success "$plugin installed."
else
print_warning "$plugin already installed."
fi
done
tmux source ~/.tmux.conf 2> /dev/null || print_warning "tmux not started; source tmux.conf manually if needed."
~/.tmux/plugins/tpm/bin/install_plugins > /dev/null
print_success "tmux plugins installed and updated."
}
update_packages() {
print_message "Updating all packages..."
brew update
brew upgrade
sudo apt update
sudo apt upgrade -y
sudo apt autoremove -y
print_success "All packages updated."
}
# Run the setup tasks
print_message "WSL Setup v2"
update_and_install_core
setup_ssh_key
install_homebrew
install_starship
install_chezmoi
initialize_chezmoi
configure_chezmoi_git
chezmoi apply
set_fish_as_default_shell
install_tmux_plugins
update_packages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment