Last active
May 15, 2025 13:06
-
-
Save alexlovelltroy/70af48b195bec4f9c7d0faaa9dd217b3 to your computer and use it in GitHub Desktop.
Portable dotfiles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
# 1) Create ~/.bashrc.d and an options.sh | |
mkdir -p "${HOME}/.bashrc.d" | |
if [[ ! -f "${HOME}/.bashrc.d/options.sh" ]]; then | |
cat << 'EOF' > "${HOME}/.bashrc.d/options.sh" | |
# ~/.bashrc.d/options.sh | |
# Add your custom environment variables, aliases, etc. here. | |
# Set some basic helpful options | |
# don't put duplicate lines or lines starting with space in the history. | |
# See bash(1) for more options | |
HISTCONTROL=ignoreboth | |
# append to the history file, don't overwrite it | |
shopt -s histappend | |
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1) | |
HISTSIZE=1000 | |
HISTFILESIZE=2000 | |
# Update the window size after each command | |
shopt -s checkwinsize | |
# Case-insensitive globbing (used in pathname expansion) | |
shopt -s nocaseglob | |
# Append to the Bash history file, rather than overwriting it | |
shopt -s histappend | |
# Autocorrect typos in path names when using `cd` | |
shopt -s cdspell | |
EOF | |
echo "Created ~/.bashrc.d/options.sh" | |
fi | |
if [[ ! -f "${HOME}/.bashrc.d/path.sh" ]]; then | |
cat << 'EOF' > "${HOME}/.bashrc.d/path.sh" | |
# Function to add directories to PATH if they exist and are not already in PATH | |
add_to_path_if_exists() { | |
for dir in "$@"; do | |
if [ -d "$dir" ] && [[ ":$PATH:" != *":$dir:"* ]]; then | |
export PATH="$dir:$PATH" | |
fi | |
done | |
} | |
# User specific environment | |
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]] | |
then | |
PATH="$HOME/.local/bin:$HOME/bin:$PATH" | |
fi | |
export PATH | |
# add go path | |
add_to_path_if_exists /usr/local/go/bin | |
add_to_path_if_exists "${GOPATH}/bin:${PATH}" | |
# add local things to path | |
add_to_path_if_exists /usr/local/bin | |
add_to_path_if_exists /usr/local/sbin | |
EOF | |
echo "Created ~/.bashrc.d/path.sh" | |
fi | |
# 2) Install Starship and apply the bracketed-segments preset | |
if ! command -v starship &>/dev/null; then | |
echo "Installing Starship..." | |
curl -sS https://starship.rs/install.sh | sh -s -- --yes | |
else | |
echo "Starship already installed, skipping." | |
fi | |
mkdir -p "${HOME}/.config" | |
starship preset bracketed-segments -o "${HOME}/.config/starship.toml" | |
# 3) Install Zellij | |
if ! command -v zellij &>/dev/null; then | |
echo "Installing Zellij..." | |
dir="${HOME}/.zellij/bootstrap" | |
mkdir -p "$dir" | |
fi | |
case $(uname -m) in | |
"x86_64"|"aarch64") | |
arch=$(uname -m) | |
;; | |
"arm64") | |
arch="aarch64" | |
;; | |
*) | |
echo "Unsupported cpu arch: $(uname -m)" | |
exit 2 | |
;; | |
esac | |
case $(uname -s) in | |
"Linux") | |
sys="unknown-linux-musl" | |
;; | |
"Darwin") | |
sys="apple-darwin" | |
;; | |
*) | |
echo "Unsupported system: $(uname -s)" | |
exit 2 | |
;; | |
esac | |
url="https://github.com/zellij-org/zellij/releases/latest/download/zellij-$arch-$sys.tar.gz" | |
curl --location "$url" | tar -C "$dir" -xz | |
if [[ $? -ne 0 ]] | |
then | |
echo | |
echo "Extracting binary failed, cannot launch zellij :(" | |
echo "One probable cause is that a new release just happened and the binary is currently building." | |
echo "Maybe try again later? :)" | |
exit 1 | |
fi | |
sudo cp "${HOME}/.zellij/bootstrap/zellij" /usr/local/bin/zellij | |
# 4) Backup and update ~/.bashrc | |
BASHRC="${HOME}/.bashrc" | |
BACKUP="${HOME}/.bashrc.bak.$(date +%Y%m%d%H%M%S)" | |
cp -a "$BASHRC" "$BACKUP" | |
echo "Backed up existing .bashrc to $BACKUP" | |
# Ensure Starship init is at the end | |
if ! grep -q 'starship init bash' "$BASHRC"; then | |
{ | |
echo | |
echo '# Initialize Starship prompt' | |
echo 'eval "$(starship init bash)"' | |
} >> "$BASHRC" | |
echo "Appended Starship init to $BASHRC" | |
fi | |
# 5) Fetch SSH keys from GitHub and populate authorized_keys | |
SSH_DIR="${HOME}/.ssh" | |
mkdir -p "$SSH_DIR" | |
chmod 700 "$SSH_DIR" | |
curl -fSsL "https://github.com/alexlovelltroy.keys" >> "${SSH_DIR}/authorized_keys" | |
chmod 600 "${SSH_DIR}/authorized_keys" | |
echo "Fetched GitHub SSH keys into ~/.ssh/authorized_keys" | |
echo "Bootstrap complete! Please open a new shell or run 'source ~/.bashrc'." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment