Refine dotfiles installer and tmux config

This commit is contained in:
2026-03-27 21:34:52 +01:00
parent 993e307bd3
commit 5f667a5da8
18 changed files with 185 additions and 782 deletions

View File

@@ -1,45 +1,48 @@
#!/bin/sh
#
# This script should be run via curl:
# sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
# or wget:
# sh -c "$(wget -qO- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
# Installs this dotfiles repo by:
# - cloning Oh My Zsh into $ZSH
# - symlinking the managed config files from this repository
# - installing the tmux plugin manager and bundled tmux plugins
# - cloning the powerlevel10k theme
#
# As an alternative, you can first download the install script and run it afterwards:
# wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh
# sh install.sh
# Run it from a checked out copy of this repository:
# sh .config/dotfiles/install.sh
#
# You can tweak the install behavior by setting variables when running the script. For
# example, to change the path to the Oh My Zsh repository:
# ZSH=~/.zsh sh install.sh
# You can tweak the install behavior by setting variables when running the script:
# ZSH=~/.zsh sh .config/dotfiles/install.sh
#
# Respects the following environment variables:
# ZSH - path to the Oh My Zsh repository folder (default: $HOME/.oh-my-zsh)
# REPO - name of the GitHub repo to install from (default: robbyrussell/oh-my-zsh)
# REMOTE - full remote URL of the git repo to install (default: GitHub via HTTPS)
# BRANCH - branch to check out immediately after install (default: master)
# ZSH - path to the Oh My Zsh installation (default: $HOME/.oh-my-zsh)
# REPO - Oh My Zsh GitHub repo name (default: robbyrussell/oh-my-zsh)
# REMOTE - full remote URL of the Oh My Zsh git repo
# BRANCH - Oh My Zsh branch to check out immediately after install
#
# Other options:
# CHSH - 'no' means the installer will not change the default shell (default: yes)
# RUNZSH - 'no' means the installer will not run zsh after the install (default: yes)
#
# You can also pass some arguments to the install script to set some these options:
# You can also pass some arguments to the install script to set some of these options:
# --skip-chsh: has the same behavior as setting CHSH to 'no'
# --unattended: sets both CHSH and RUNZSH to 'no'
# For example:
# sh install.sh --unattended
# sh .config/dotfiles/install.sh --unattended
#
set -e
# Default settings
ZSH=${ZSH:-~/.oh-my-zsh}
REPO=${REPO:-robbyrussell/oh-my-zsh}
REPO=${REPO:-ohmyzsh/ohmyzsh}
REMOTE=${REMOTE:-https://github.com/${REPO}.git}
BRANCH=${BRANCH:-master}
# Other options
CHSH=${CHSH:-yes}
RUNZSH=${RUNZSH:-yes}
DOTFILES_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
TMUX_PLUGIN_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/tmux/plugins"
TMUX_RESURRECT_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/tmux/resurrect"
P10K_REMOTE=${P10K_REMOTE:-https://github.com/romkatv/powerlevel10k.git}
command_exists() {
@@ -69,6 +72,78 @@ setup_color() {
fi
}
apply_dotfiles() {
echo "${BLUE}Applying dotfiles...${RESET}"
if ! command_exists lsd; then
error "lsd is required but not installed. Install it first via your package manager."
exit 1
fi
if ! command_exists tmux; then
error "tmux is required but not installed. Install it first via your package manager."
exit 1
fi
link_dotfile "$DOTFILES_DIR/.zshrc" "$HOME/.zshrc"
link_dotfile "$DOTFILES_DIR/.vimrc" "$HOME/.vimrc"
link_dotfile "$DOTFILES_DIR/.zprofile" "$HOME/.zprofile"
link_dotfile "$DOTFILES_DIR/.gitconfig" "$HOME/.gitconfig"
link_dotfile "$DOTFILES_DIR/.p10k.zsh" "$HOME/.p10k.zsh"
mkdir -p "$HOME/.config"
link_dotfile "$DOTFILES_DIR/tmux" "$HOME/.config/tmux"
install_tmux_plugins
install_powerlevel10k
echo
}
replace_dir() {
source_path=$1
target_path=$2
if [ -e "$target_path" ] || [ -L "$target_path" ]; then
rm -rf "$target_path"
fi
mv "$source_path" "$target_path"
}
link_dotfile() {
source_path=$1
target_path=$2
if [ -L "$target_path" ]; then
current_target=$(readlink "$target_path")
if [ "$current_target" = "$source_path" ]; then
echo "${GREEN}$target_path already linked.${RESET}"
return
fi
rm -f "$target_path"
elif [ -e "$target_path" ]; then
echo "${YELLOW}Removing existing $target_path${RESET}"
rm -rf "$target_path"
fi
ln -s "$source_path" "$target_path"
echo "${GREEN}Linked $target_path -> $source_path${RESET}"
}
install_tmux_plugins() {
echo "${BLUE}Installing tmux plugins...${RESET}"
rm -rf "$TMUX_PLUGIN_DIR"
mkdir -p "$TMUX_PLUGIN_DIR" "$TMUX_RESURRECT_DIR"
git clone --depth=1 https://github.com/tmux-plugins/tpm \
"$TMUX_PLUGIN_DIR/tpm"
git clone --depth=1 https://github.com/tmux-plugins/tmux-resurrect \
"$TMUX_PLUGIN_DIR/tmux-resurrect"
git clone --depth=1 https://github.com/tmux-plugins/tmux-continuum \
"$TMUX_PLUGIN_DIR/tmux-continuum"
echo
}
setup_ohmyzsh() {
# Prevent the cloned repository from having insecure permissions. Failing to do
# so causes compinit() calls to fail with "command not found: compdef" errors
@@ -90,50 +165,40 @@ setup_ohmyzsh() {
exit 1
fi
zsh_parent=$(dirname "$ZSH")
mkdir -p "$zsh_parent"
zsh_stage=$(mktemp -d "${zsh_parent%/}/.oh-my-zsh.XXXXXX")
git clone -c core.eol=lf -c core.autocrlf=false \
-c fsck.zeroPaddedFilemode=ignore \
-c fetch.fsck.zeroPaddedFilemode=ignore \
-c receive.fsck.zeroPaddedFilemode=ignore \
--depth=1 --branch "$BRANCH" "$REMOTE" "$ZSH" || {
--depth=1 --branch "$BRANCH" "$REMOTE" "$zsh_stage" || {
rm -rf "$zsh_stage"
error "git clone of oh-my-zsh repo failed"
exit 1
}
replace_dir "$zsh_stage" "$ZSH"
echo
}
setup_zshrc() {
# Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones
# with datestamp of installation that moved them aside, so we never actually
# destroy a user's original zshrc
echo "${BLUE}Looking for an existing zsh config...${RESET}"
install_powerlevel10k() {
theme_dir="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k"
theme_parent=$(dirname "$theme_dir")
mkdir -p "$theme_parent"
theme_stage=$(mktemp -d "${theme_parent%/}/.powerlevel10k.XXXXXX")
# Must use this exact name so uninstall.sh can find it
OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh
if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
if [ -e "$OLD_ZSHRC" ]; then
OLD_OLD_ZSHRC="${OLD_ZSHRC}-$(date +%Y-%m-%d_%H-%M-%S)"
if [ -e "$OLD_OLD_ZSHRC" ]; then
error "$OLD_OLD_ZSHRC exists. Can't back up ${OLD_ZSHRC}"
error "re-run the installer again in a couple of seconds"
exit 1
fi
mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}"
echo "${BLUE}Installing powerlevel10k...${RESET}"
echo "${YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \
"${GREEN}Backing up to ${OLD_OLD_ZSHRC}${RESET}"
fi
echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Backing up to ${OLD_ZSHRC}${RESET}"
mv ~/.zshrc "$OLD_ZSHRC"
fi
git clone --depth=1 "$P10K_REMOTE" "$theme_stage" || {
rm -rf "$theme_stage"
error "git clone of powerlevel10k repo failed"
exit 1
}
echo "${GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}"
cp "$ZSH/templates/zshrc.zsh-template" ~/.zshrc
sed "/^export ZSH=/ c\\
export ZSH=\"$ZSH\"
" ~/.zshrc > ~/.zshrc-omztemp
mv -f ~/.zshrc-omztemp ~/.zshrc
replace_dir "$theme_stage" "$theme_dir"
echo
}
@@ -239,18 +304,11 @@ main() {
exit 1
fi
if [ -d "$ZSH" ]; then
cat <<-EOF
${YELLOW}You already have Oh My Zsh installed.${RESET}
You'll need to remove '$ZSH' if you want to reinstall.
EOF
exit 1
fi
setup_ohmyzsh
setup_zshrc
setup_shell
apply_dotfiles
printf "$GREEN"
cat <<-'EOF'
__ __
@@ -261,11 +319,7 @@ main() {
/____/ ....is now installed!
Please look over the ~/.zshrc file to select plugins, themes, and options.
p.s. Follow us on https://twitter.com/ohmyzsh
p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh
Your dotfiles have been installed to your home directory.
EOF
printf "$RESET"