Configuration system overhaul

This commit is contained in:
Fernando Schauenburg 2021-08-10 18:04:12 +02:00
parent 1622e4abde
commit b30948b552
8 changed files with 294 additions and 253 deletions

4
.gitignore vendored
View file

@ -1,3 +1 @@
dotfiles/.vim/bundle/
dotfiles/.vim/.netrwhist
dotfiles/.vim/viminfo
install/config

View file

@ -1,8 +1,8 @@
#!/bin/bash
# Called by bash (1) on for login shells.
# Source:
# 1. profile if present (XDG_CONFIG_HOME is not defined yet)
# 1. copy environment from zsh
# 2. ~/.bashrc if shell is interactive.
[ -f "${HOME}/.config/shell/profile" ] && source "${HOME}/.config/shell/profile"
[[ "$-" == *i* ]] && [ -f "${HOME}/.bashrc" ] && source "${HOME}/.bashrc"
[ -f "$ZDOTDIR/zshenv" ] && source "$ZDOTDIR/zshenv"
[[ "$-" == *i* ]] && [ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc"

View file

@ -2,8 +2,8 @@
# Set up bash for interactive use (options, prompt, aliases, etc.)
while read -r f; do [ -f "$f" ] && source "$f"; done <<EOL
${XDG_CONFIG_HOME}/shell/aliases
${XDG_CONFIG_HOME}/shell/solarized
$ZDOTDIR/aliases
$ZDOTDIR/solarized
EOL
unset f

240
bootstrap
View file

@ -1,240 +0,0 @@
#!/bin/sh
set -eu
usage() {
echo "Usage: $(basename $0) [-h] [-f]"
echo ""
echo " -h print this help and exit"
echo " -f modify filesystem rather than dry run"
}
DRY_RUN=true
while getopts 'fh' opt; do case "$opt" in
f) DRY_RUN=false;;
h) usage; exit 0;;
*) usage; exit 1;;
esac done
DEFAULT_GIT_USER="Fernando Schauenburg"
DEFAULT_GIT_EMAIL="fernando@schauenburg.me"
XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
DOTFILES_DIR="$(dirname "$(realpath "$0")")"
DIRS=$(cat <<EOF
${HOME}/.local/bin
${HOME}/.local/etc/git
${HOME}/.jupyter/custom
${HOME}/.jupyter/nbconfig
${HOME}/.ssh
${XDG_CACHE_HOME}/zsh
${XDG_CONFIG_HOME}/alacritty
${XDG_CONFIG_HOME}/git
${XDG_CONFIG_HOME}/mintty
${XDG_CONFIG_HOME}/python
${XDG_CONFIG_HOME}/readline
${XDG_CONFIG_HOME}/tmux
${XDG_CONFIG_HOME}/nvim
${XDG_CONFIG_HOME}/nvim/autoload
${XDG_CONFIG_HOME}/shell
${XDG_CONFIG_HOME}/zsh
${XDG_DATA_HOME}/bash
${XDG_DATA_HOME}/bash-completion/completions
${XDG_DATA_HOME}/less
${XDG_DATA_HOME}/python
${XDG_DATA_HOME}/nvim
${XDG_DATA_HOME}/nvim/plugged
${XDG_DATA_HOME}/nvim/shada
EOF
)
FILES="$DOTFILES_DIR/files"
DOTFILES=$(cat <<EOF
${XDG_CONFIG_HOME}/alacritty/alacritty.yml $FILES/alacritty.yml
${XDG_CONFIG_HOME}/git/config $FILES/gitconfig
${XDG_CONFIG_HOME}/git/ignore $FILES/gitignore
${XDG_CONFIG_HOME}/readline/inputrc $FILES/inputrc
${XDG_CONFIG_HOME}/mintty/config $FILES/minttyrc
${XDG_CONFIG_HOME}/nvim/autoload/plug.vim $FILES/plug.vim
${XDG_CONFIG_HOME}/python/startup.py $FILES/python-startup.py
${XDG_CONFIG_HOME}/tmux/tmux.conf $FILES/tmux.conf
${XDG_CONFIG_HOME}/nvim/init.vim $FILES/init.vim
${XDG_CONFIG_HOME}/shell/aliases $FILES/aliases.sh
${XDG_CONFIG_HOME}/shell/profile $FILES/profile.sh
${XDG_CONFIG_HOME}/shell/solarized $FILES/solarized.sh
${XDG_CONFIG_HOME}/zsh/.zshrc $FILES/zshrc
${HOME}/.XCompose $FILES/xcompose
${HOME}/.bashrc $FILES/bashrc
${HOME}/.bash_profile $FILES/bash_profile
${HOME}/.jupyter/custom/custom.js $FILES/jupyter_custom.js
${HOME}/.jupyter/nbconfig/notebook.json $FILES/jupyter_notebook.json
${HOME}/.zprofile ${XDG_CONFIG_HOME}/shell/profile
${HOME}/.ssh/config $FILES/ssh_config
EOF
)
rst=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
heading() { printf '%s\n' "${blue}===== $1 ==========${rst}"; }
info() { printf '%s\n' "${green}${1}${rst}"; }
warn() { printf '%s\n' "${yellow}${1}${rst}"; }
error() { printf '%s\n' "${red}${1}${rst}"; }
directory_present() { # Make sure directory $1 exists.
if [ -d "$1" ]; then
info "OK: $1"
else
warn "creating $1"
$DRY_RUN || mkdir -p "$1"
fi
}
file_absent() { # Make sure file $1 does not exist.
if [ ! -f "$1" ]; then
info "OK: $1"
else
warn "removing $1"
$DRY_RUN || rm "$1"
fi
}
file_touch() { # Make sure file $1 exists (content is irrelevant).
if [ -f "$1" ]; then
info "OK: $1"
else
warn "creating $1"
$DRY_RUN || touch "$1"
fi
}
prune_link() { # Remove $1 if it's a broken link to a dotfile script.
if [ -h "$1" ]; then # it's a symbolic link
target="$(readlink "$1")"
case "$target" in
"$DOTFILES_DIR/bin/"*) # it points into dotfiles bin
if [ ! -e "$1" ]; then # target of the link missing
warn "pruning $1 -> $target"
$DRY_RUN || rm -f "$1"
fi
;;
esac
fi
}
file_link() { # Make sure $1 is a link to $2.
target="$(realpath -s "$2")"
if [ "$(readlink "$1")" = "$target" ]; then
info "OK: $1"
else
warn "linking $1 -> $target"
$DRY_RUN || ln -sf "$target" "$1"
fi
}
file_content() { # Ensure $1 and $2 contents are equal, updating $1 if needed.
if diff "$1" "$2" >/dev/null 2>&1; then
info "OK: $1"
else
warn "overwriting $1 with $2:"
cat "$2"
$DRY_RUN || cp -f "$2" "$1"
fi
}
task_gather_user_info() {
heading "Gather user information"
printf 'git name [%s]: ' "$DEFAULT_GIT_USER"
read -r git_user
printf 'git e-mail [%s]: ' "$DEFAULT_GIT_EMAIL"
read -r git_email
}
task_remove_bash_profile() {
heading "Remove pre-installed bash profile"
for f in .bash_logout .profile; do
file_absent "$HOME/$f"
done
}
task_ensure_directories() {
heading "Ensure existence of directories used by dotfiles"
echo "$DIRS" | while read -r d; do directory_present "$d"; done
}
task_link_dotfiles() {
heading "Link dotfiles"
echo "$DOTFILES" | while read -r src dest; do
file_link "$src" "$dest"
done
}
task_deploy_templates() {
heading "Deploy templates"
temp_git="$(mktemp)"
cat >"$temp_git" <<EOF
# *************************************
# * DO NOT EDIT THIS FILE *
# *************************************
#
# This file was generated by the bootstrap script and any changes will be
# overwritten the next time it is run. For local settings, use this instead:
#
# ~/.local/etc/git/config
#
EOF
git config -f "$temp_git" user.name "${git_user:-$DEFAULT_GIT_USER}"
git config -f "$temp_git" user.email "${git_email:-$DEFAULT_GIT_EMAIL}"
file_content "$HOME/.local/etc/git/config.host" "$temp_git"
}
task_prune_broken_bin() {
heading "Prune broken links to commands"
for cmd in $HOME/.local/bin/*; do
prune_link "$cmd"
done
}
task_link_local_bin() {
heading "Link local commands"
for cmd in "$DOTFILES_DIR/bin/"*; do
file_link "$HOME/.local/bin/$(basename "$cmd")" "$cmd"
done
}
task_make_logins_silent() {
heading "Make logins silent"
file_touch "$HOME/.hushlogin"
}
task_install_nvim_plugins() {
heading "Install neovim plugins"
if command -v nvim >/dev/null 2>&1; then
warn "Installing neovim plugins"
$DRY_RUN || nvim -nes -u "$XDG_CONFIG_HOME/nvim/init.vim" -c 'PlugInstall | qall!'
else
error "neovim is not installed; skipping plugin installation..."
fi
}
main() {
$DRY_RUN && warn "Dry run: no changes will actually be made."
task_gather_user_info
task_remove_bash_profile
task_ensure_directories
task_link_dotfiles
task_deploy_templates
task_prune_broken_bin
task_link_local_bin
task_make_logins_silent
task_install_nvim_plugins
}
main

View file

@ -75,5 +75,5 @@
required = true
[include]
path = ~/.local/etc/git/config.host # host-specific (from template)
path = ~/.local/etc/git/config.user # user name & e-mail (from template)
path = ~/.local/etc/git/config # optional manual configurations

37
install.sh Executable file
View file

@ -0,0 +1,37 @@
#!/bin/sh
set -eu
export DOTFILES="$(dirname "$(realpath "$0")")"
. "$DOTFILES/zsh/zshenv"
. "$DOTFILES/install/functions"
DRY_RUN=yes
while getopts 'fh' opt; do case "$opt" in
f) DRY_RUN= ;;
h) usage; exit 0;;
*) usage; exit 1;;
esac done
export DEFAULT_GIT_USER="Fernando Schauenburg"
export DEFAULT_GIT_EMAIL="fernando@schauenburg.me"
# Oportunity to set GIT_USER and GIT_EMAIL
[ -f "$DOTFILES/install/config" ] && . "$DOTFILES/install/config"
greeting
deploy_alacritty
deploy_bash
deploy_bin
deploy_git
deploy_jupyter
deploy_mintty
deploy_misc
deploy_nvim
deploy_python
deploy_readline
deploy_ssh
deploy_tmux
deploy_x11
deploy_zsh

246
install/functions Normal file
View file

@ -0,0 +1,246 @@
#!/bin/sh
dry_run() { [ -n "$DRY_RUN" ]; }
usage() {
echo "Usage: $(basename $0) [-h] [-f]"
echo ""
echo " -h print this help and exit"
echo " -f modify filesystem rather than dry run"
}
###############################################################################
# Helper Functions
###############################################################################
if [ -t 1 ]; then
rst=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
magenta=$(tput setaf 5)
cyan=$(tput setaf 6)
else
rst=''
red=''
green=''
yellow=''
blue=''
magenta=''
cyan=''
fi
heading() { printf '%s\n' "${blue}===== $1 ==========${rst}"; }
info() { printf '%s ' "$@"; printf '\n'; }
ok() { printf '%s ' "${green}OK:${rst}" "$@"; printf '\n'; }
warn() { printf '%s ' "${yellow}$@${rst}"; printf '\n'; }
error() { printf '%s ' "${red}$@${rst}"; printf '\n'; }
is_installed() {
command -v nvim >/dev/null 2>&1
}
greeting() {
dry_run && info "Dry run: no changes will be made to filesytem (use -f to override)."
info "Deploying with git user $yellow${GIT_USER:-$DEFAULT_GIT_USER} <${GIT_EMAIL:-$DEFAULT_GIT_EMAIL}>$rst"
[ -t 0 ] && {
info "Press ENTER to continue (CTRL-C to cancel)..."
read k
}
}
# Make sure directory $1 exists.
ensure_directory() {
if [ -d "$1" ]; then
ok "$1/"
else
warn "creating $1/"
dry_run || mkdir -p "$1/"
fi
}
# Make sure file $1 does not exist.
remove_file() {
if [ ! -f "$1" ]; then
ok "$1"
else
warn "removing $1"
dry_run || rm "$1"
fi
}
# Make sure file $1 exists (content is irrelevant).
touch_file() {
if [ -f "$1" ]; then
ok "$1"
else
warn "creating $1"
dry_run || touch "$1"
fi
}
# Remove $1 if it's a broken link to a dotfile script.
prune_cmd() {
if [ -h "$1" ]; then # it's a symbolic link...
target="$(readlink "$1")"
case "$target" in
"$DOTFILES/bin/"*) # ... pointing into dotfiles bin
if [ ! -e "$1" ]; then # target of the link missing
warn "removing stale link $1 -> $target"
dry_run || rm -f "$1"
fi
;;
esac
fi
}
# Make sure $2 is a link to $1.
link() {
target="$(realpath -s "$1")"
if [ "$(readlink "$2")" = "$target" ]; then
ok "$2"
else
warn "linking $2 -> $target"
dry_run || ln -sf "$target" "$2"
fi
}
# Ensure $1 and $2 contents are equal, updating $1 if needed.
equal_content() {
if diff "$1" "$2" >/dev/null 2>&1; then
ok "$1"
else
warn "overwriting $1 with $2:"
cat "$2"
dry_run || cp -f "$2" "$1"
fi
}
###############################################################################
# Configuration Deployments
###############################################################################
deploy_alacritty() {
heading 'alacritty'
ensure_directory "$XDG_CONFIG_HOME/alacritty"
link "$DOTFILES/alacritty/alacritty.yml" "$XDG_CONFIG_HOME/alacritty/alacritty.yml"
}
deploy_bash() {
heading 'bash'
for f in .bash_logout .profile; do remove_file "$HOME/$f"; done
ensure_directory "$XDG_DATA_HOME/bash" # for history
ensure_directory "$XDG_DATA_HOME/bash-completion/completions"
link "$DOTFILES/bash/bashrc" "${HOME}/.bashrc"
link "$DOTFILES/bash/bash_profile" "${HOME}/.bash_profile"
}
deploy_bin() {
heading 'bin'
ensure_directory "$HOME/.local/bin"
for cmd in $HOME/.local/bin/*; do prune_cmd "$cmd"; done
for cmd in "$DOTFILES/bin/"*; do
link "$cmd" "$HOME/.local/bin/$(basename "$cmd")"
done
}
deploy_git() {
heading 'git'
ensure_directory "$XDG_CONFIG_HOME/git"
ensure_directory "${HOME}/.local/etc/git"
link "$DOTFILES/git/gitconfig" "$XDG_CONFIG_HOME/git/config"
link "$DOTFILES/git/gitignore" "$XDG_CONFIG_HOME/git/ignore"
temp_git="$(mktemp)"
cat >"$temp_git" <<EOF
# *************************************
# * DO NOT EDIT THIS FILE *
# *************************************
#
# This file was generated by the bootstrap script and any changes will be
# overwritten the next time it is run. For local settings, use this instead:
#
# ~/.local/etc/git/config
#
EOF
git config -f "$temp_git" user.name "${GIT_USER:-$DEFAULT_GIT_USER}"
git config -f "$temp_git" user.email "${GIT_EMAIL:-$DEFAULT_GIT_EMAIL}"
equal_content "$HOME/.local/etc/git/config.user" "$temp_git"
}
deploy_jupyter() {
heading 'jupyter'
ensure_directory "${HOME}/.jupyter/custom"
ensure_directory "${HOME}/.jupyter/nbconfig"
link "$DOTFILES/jupyter/custom.js" "${HOME}/.jupyter/custom/custom.js"
link "$DOTFILES/jupyter/notebook.json" "${HOME}/.jupyter/nbconfig/notebook.json"
}
deploy_mintty() {
heading 'mintty'
ensure_directory "$XDG_CONFIG_HOME/mintty"
link "$DOTFILES/mintty/minttyrc" "$XDG_CONFIG_HOME/mintty/config"
}
deploy_misc() {
heading 'miscelaneous'
ensure_directory "$XDG_DATA_HOME/less" # for history
touch_file "$HOME/.hushlogin"
}
deploy_nvim() {
heading 'nvim'
ensure_directory "$XDG_CONFIG_HOME/nvim/autoload"
ensure_directory "$XDG_DATA_HOME/nvim/plugged"
ensure_directory "$XDG_DATA_HOME/nvim/shada"
link "$DOTFILES/nvim/plug.vim" "$XDG_CONFIG_HOME/nvim/autoload/plug.vim"
link "$DOTFILES/nvim/init.vim" "$XDG_CONFIG_HOME/nvim/init.vim"
if is_installed nvim; then
warn "installing neovim plugins..."
dry_run || nvim -nes -u "$XDG_CONFIG_HOME/nvim/init.vim" -c 'PlugInstall | qall!'
else
error "neovim is not installed; skipping plugin installation..."
fi
}
deploy_python() {
heading 'python'
ensure_directory "$XDG_CONFIG_HOME/python"
ensure_directory "$XDG_DATA_HOME/python" # for history
link "$DOTFILES/python/startup.py" "$XDG_CONFIG_HOME/python/startup.py"
}
deploy_readline() {
heading 'readline'
ensure_directory "$XDG_CONFIG_HOME/readline"
link "$DOTFILES/readline/inputrc" "$XDG_CONFIG_HOME/readline/inputrc"
}
deploy_ssh() {
heading 'ssh'
ensure_directory "${HOME}/.ssh"
link "$DOTFILES/ssh/ssh_config" "${HOME}/.ssh/config"
}
deploy_tmux() {
ensure_directory "$XDG_CONFIG_HOME/tmux"
link "$DOTFILES/tmux/tmux.conf" "$XDG_CONFIG_HOME/tmux/tmux.conf"
}
deploy_x11() {
heading 'X11'
link "$DOTFILES/x11/xcompose" ${HOME}/.XCompose
}
deploy_zsh() {
heading 'zsh'
ensure_directory "$ZDOTDIR"
ensure_directory "$XDG_DATA_HOME/zsh" # for history
link "$DOTFILES/zsh/zshenv" "$HOME/.zshenv"
link "$DOTFILES/zsh/zshrc" "$ZDOTDIR/.zshrc"
link "$DOTFILES/zsh/aliases" "$ZDOTDIR/aliases"
link "$DOTFILES/zsh/solarized" "$ZDOTDIR/solarized"
}

View file

@ -3,16 +3,16 @@
# Source additional configurations if available.
while read -r f; do [ -f "$f" ] && source "$f"; done <<EOL
${XDG_CONFIG_HOME}/shell/aliases
${XDG_CONFIG_HOME}/shell/solarized
$ZDOTDIR/aliases
$ZDOTDIR/solarized
EOL
unset f
# Prevent ctrl-s from freezing the terminal.
stty stop undef
# Save a lot of history in the cache directory.
HISTFILE="${XDG_CACHE_HOME:-$HOME/.cache}/zsh/history"
# Save a lot of history.
HISTFILE="${XDG_DATA_HOME:-$HOME/.local/share}/zsh/history"
HISTSIZE=1000000
SAVEHIST=1000000