Files
dotfiles/bin/dot
T

434 lines
18 KiB
Bash
Executable File

#!/usr/bin/env bash
# dot — manage this dev environment. Run `dot help`.
set -euo pipefail
DOTFILES_DIR="${DOTFILES_DIR:-$HOME/.dotfiles}"
STATE_DIR="$HOME/.config/dotfiles"
SECRETS_ENV="$STATE_DIR/secrets.env"
GITEA_URL="https://gitea.godemperordoge.org"
WALLPAPER_REPO="$GITEA_URL/ziadarafat/wallpapers.git"
WALLPAPER_DIR="$HOME/Pictures/Wallpapers"
# Where wallpapers originally live on the Deck's external drive.
WALLPAPER_SRC_WORK="${WALLPAPER_SRC_WORK:-/run/media/deck/41e37da2-f67a-4c8c-96e8-78b21eab5bf6/backup/Pictures/ac_walls}"
WALLPAPER_SRC_PERSONAL="${WALLPAPER_SRC_PERSONAL:-/run/media/deck/41e37da2-f67a-4c8c-96e8-78b21eab5bf6/backup/Pictures/pokewalls/unprocessed}"
mkdir -p "$STATE_DIR"
c_blue=$'\e[34m'; c_yel=$'\e[33m'; c_red=$'\e[31m'; c_grn=$'\e[32m'; c_off=$'\e[0m'
step() { printf '%s==>%s %s\n' "$c_blue" "$c_off" "$*"; }
warn() { printf '%s!!%s %s\n' "$c_yel" "$c_off" "$*" >&2; }
die() { printf '%sxx%s %s\n' "$c_red" "$c_off" "$*" >&2; exit 1; }
ok() { printf '%sok%s %s\n' "$c_grn" "$c_off" "$*"; }
have() { command -v "$1" >/dev/null 2>&1; }
os_id() { (. /etc/os-release 2>/dev/null && echo "${ID:-unknown}") || echo unknown; }
has_systemd() { [ -d /run/systemd/system ]; }
in_container() { [ -f /run/.containerenv ] || [ -f /.dockerenv ]; }
source_nix() {
have nix && return 0
for f in "$HOME/.nix-profile/etc/profile.d/nix.sh" /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh; do
# shellcheck disable=SC1090
[ -e "$f" ] && . "$f" && break
done
have nix
}
# sudo that works as root, with a TTY, or not at all.
SUDO() {
if [ "$(id -u)" = 0 ]; then "$@"; elif have sudo; then sudo "$@"; else die "need root for: $*"; fi
}
# ---------------------------------------------------------------- profile
detect_profile() {
if [ "${XDG_CURRENT_DESKTOP:-}" = KDE ] || pgrep -x plasmashell >/dev/null 2>&1 || have plasmashell; then
echo full
else
echo headless
fi
}
profile() {
if [ -s "$STATE_DIR/profile" ]; then cat "$STATE_DIR/profile"; else detect_profile; fi
}
flake_attr() {
local p; p="$(profile)"
if [ "$p" = headless ] && [ "$(uname -m)" = aarch64 ]; then echo headless-aarch64; else echo "$p"; fi
}
cmd_profile() {
case "${1:-}" in
full|headless) echo "$1" >"$STATE_DIR/profile"; ok "profile set to $1 (run: dot switch)";;
auto) rm -f "$STATE_DIR/profile"; ok "profile follows auto-detection ($(detect_profile))";;
"") echo "$(profile)$( [ -s "$STATE_DIR/profile" ] || echo ' (auto)')";;
*) die "usage: dot profile [full|headless|auto]";;
esac
}
# ---------------------------------------------------------------- nix
cmd_nix() {
if source_nix; then ok "nix $(nix --version | awk '{print $NF}') present"; return; fi
local os; os="$(os_id)"
if [ "$os" = steamos ] || ! has_systemd || in_container; then
# Single-user: no daemon, nothing in /etc, so SteamOS updates can't break it.
# SteamOS bind-mounts /nix from /home/.steamos/offload/nix, so it persists.
step "installing Nix (single-user) into /nix"
if [ ! -w /nix ] 2>/dev/null; then
step "making /nix owned by $USER (one-time sudo)"
SUDO install -d -m 0755 -o "$(id -u)" -g "$(id -g)" /nix
SUDO chown "$(id -u):$(id -g)" /nix
fi
curl -fsSL https://nixos.org/nix/install | sh -s -- --no-daemon --no-modify-profile
else
step "installing Nix (multi-user, Determinate installer)"
curl -fsSL https://install.determinate.systems/nix | sh -s -- install --no-confirm
fi
source_nix || die "nix installed but not on PATH; open a new shell and re-run"
mkdir -p "$HOME/.config/nix"
grep -q flakes "$HOME/.config/nix/nix.conf" 2>/dev/null || echo "experimental-features = nix-command flakes" >>"$HOME/.config/nix/nix.conf"
}
# ---------------------------------------------------------------- home-manager
cmd_switch() {
[ -n "${1:-}" ] && cmd_profile "$1"
source_nix || die "nix missing: run dot nix"
local attr; attr="$(flake_attr)"
step "home-manager switch → $attr"
export DOTFILES_DIR
if have home-manager; then
home-manager switch --flake "$DOTFILES_DIR#$attr" --impure -b hm-bak
else
nix run --inputs-from "$DOTFILES_DIR" home-manager -- switch --flake "$DOTFILES_DIR#$attr" --impure -b hm-bak
fi
}
# ---------------------------------------------------------------- distro layer (sudo)
install_pkgs() {
local os; os="$(os_id)"
case "$os" in
steamos) return 0 ;; # read-only /usr; SteamOS already ships zsh, podman, distrobox, flatpak, Konsole
fedora)
local p=(zsh git curl podman distrobox flatpak)
[ "$(profile)" = full ] && p+=(konsole spectacle)
SUDO dnf install -y "${p[@]}" ;;
arch|cachyos|endeavouros|manjaro)
local p=(zsh git curl podman distrobox flatpak xz)
[ "$(profile)" = full ] && p+=(konsole spectacle kate)
SUDO pacman -Sy --needed --noconfirm "${p[@]}" ;;
almalinux|rhel|rocky)
SUDO dnf install -y zsh git curl podman flatpak ;;
debian|ubuntu)
SUDO apt-get update && SUDO apt-get install -y zsh git curl podman distrobox flatpak xz-utils ;;
*) warn "unknown distro '$os': install zsh, git, curl, podman, distrobox, flatpak yourself" ;;
esac
}
browser_policies() {
# Flathub Chrome/Edge merge host policies from /run/host/etc/opt/<browser>/policies.
for b in chrome edge; do
SUDO install -d -m 0755 "/etc/opt/$b/policies/managed"
SUDO install -m 0644 "$DOTFILES_DIR/config/browsers/$b-pwas.json" "/etc/opt/$b/policies/managed/dotfiles-pwas.json"
done
ok "Chrome/Edge PWA policies installed (apply on next browser start)"
}
netbird_service() {
local bin="$HOME/.nix-profile/bin/netbird"
[ -x "$bin" ] || { warn "netbird not in nix profile; skipping"; return; }
has_systemd || { warn "no systemd; skipping netbird service"; return; }
sed "s|@NETBIRD@|$bin|" "$DOTFILES_DIR/config/netbird/netbird.service" | SUDO tee /etc/systemd/system/netbird.service >/dev/null
SUDO systemctl daemon-reload
SUDO systemctl enable --now netbird.service
ok "netbird daemon running ($bin)"
}
cmd_system() {
local skip_pkgs=0 netbird=0
for a in "$@"; do case "$a" in --no-pkgs) skip_pkgs=1;; --netbird) netbird=1;; esac; done
[ "$(profile)" = full ] && netbird=1
step "distro layer for $(os_id) (needs sudo)"
[ $skip_pkgs = 1 ] || install_pkgs
if [ "$(profile)" = full ]; then
browser_policies
fi
[ $netbird = 1 ] && netbird_service
local zsh_path; zsh_path="$(command -v zsh || true)"
if [ -n "$zsh_path" ] && [ "$(getent passwd "$USER" | cut -d: -f7)" != "$zsh_path" ]; then
grep -qx "$zsh_path" /etc/shells || echo "$zsh_path" | SUDO tee -a /etc/shells >/dev/null
SUDO chsh -s "$zsh_path" "$USER" && ok "login shell → $zsh_path" || warn "chsh failed; bash will hand off to zsh instead"
fi
}
# ---------------------------------------------------------------- user apps
flatpak_has() { flatpak info "$1" >/dev/null 2>&1; }
cmd_apps() {
if [ "$(profile)" = full ] && have flatpak; then
step "Flatpaks (user scope)"
flatpak remote-add --user --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
local missing=()
while read -r app; do
[ -z "$app" ] || [[ "$app" == \#* ]] && continue
flatpak_has "$app" || missing+=("$app")
done <"$DOTFILES_DIR/config/flatpaks.txt"
if [ ${#missing[@]} -gt 0 ]; then
flatpak install --user --noninteractive --or-update flathub "${missing[@]}" || warn "some Flatpaks failed (sandboxed containers can't run flatpak)"
fi
ok "Flatpaks: ${#missing[@]} installed, rest already present"
seed_xpipe
fi
step "mise tools"
if have mise; then
mise install -y || warn "mise install had failures"
fi
if [ ! -x "$HOME/.local/bin/claude" ]; then
step "Claude Code (native installer)"
curl -fsSL https://claude.ai/install.sh | bash || warn "Claude Code install failed"
else
ok "Claude Code present"
fi
}
seed_xpipe() {
local prefs="$HOME/.xpipe/settings/preferences.json"
[ -e "$prefs" ] && return
mkdir -p "$(dirname "$prefs")"
cp "$DOTFILES_DIR/config/xpipe/preferences.json" "$prefs"
ok "XPipe set to sync from RustyRaptor/xpipe-sync (enter the GitHub token on first launch)"
}
cmd_boxes() {
have distrobox || die "distrobox missing (dot system)"
distrobox assemble create --file "$HOME/.config/distrobox/distrobox.ini"
}
# ---------------------------------------------------------------- secrets (Bitwarden)
BW_PREFIX="dotfiles"
bw_ready() {
have bw || die "bitwarden-cli missing (dot switch)"
local status; status="$(bw status | jq -r .status)"
case "$status" in
unlocked) return 0 ;;
unauthenticated) die "run: bw login then: export BW_SESSION=\$(bw unlock --raw)" ;;
locked) die "run: export BW_SESSION=\$(bw unlock --raw)" ;;
esac
}
bw_find() { bw list items --search "$1" | jq -c --arg n "$1" 'first(.[] | select(.name == $n)) // empty'; }
bw_put_note() { # name, notes-file
local name="$1" file="$2" existing
existing="$(bw_find "$name")"
if [ -n "$existing" ]; then
echo "$existing" | jq --rawfile n "$file" '.notes = $n' | bw encode | bw edit item "$(echo "$existing" | jq -r .id)" >/dev/null
else
bw get template item | jq --arg name "$name" --rawfile n "$file" \
'.type = 2 | .secureNote = {type: 0} | .name = $name | .notes = $n | .login = null' \
| bw encode | bw create item >/dev/null
fi
ok "bitwarden: $name"
}
bw_get_note() { bw_find "$1" | jq -r '.notes // empty'; }
secrets_push() {
bw_ready; bw sync >/dev/null
[ -s "$SECRETS_ENV" ] && bw_put_note "$BW_PREFIX/secrets.env" "$SECRETS_ENV"
for k in "$HOME"/.ssh/id_ed25519 "$HOME"/.ssh/id_rsa; do
[ -f "$k" ] && bw_put_note "$BW_PREFIX/ssh/$(basename "$k")" "$k"
[ -f "$k.pub" ] && bw_put_note "$BW_PREFIX/ssh/$(basename "$k").pub" "$k.pub"
done
if have nmcli; then
local tmp; tmp="$(mktemp -d)"
while IFS=: read -r name type; do
[ "$type" = wireguard ] || continue
SUDO cat "/etc/NetworkManager/system-connections/$name.nmconnection" >"$tmp/$name" 2>/dev/null \
&& bw_put_note "$BW_PREFIX/wireguard/$name.nmconnection" "$tmp/$name"
done < <(nmcli -t -f NAME,TYPE connection show)
rm -rf "$tmp"
fi
}
secrets_pull() {
bw_ready; bw sync >/dev/null
local v
v="$(bw_get_note "$BW_PREFIX/secrets.env")"
if [ -n "$v" ]; then (umask 077; printf '%s\n' "$v" >"$SECRETS_ENV"); ok "secrets.env"; fi
mkdir -p "$HOME/.ssh"; chmod 700 "$HOME/.ssh"
while read -r name; do
[ -n "$name" ] || continue
local f="$HOME/.ssh/${name##*/}"
(umask 077; bw_get_note "$name" >"$f")
[[ "$f" == *.pub ]] && chmod 644 "$f"
ok "$f"
done < <(bw list items --search "$BW_PREFIX/ssh/" | jq -r '.[].name' | grep "^$BW_PREFIX/ssh/")
if [ "${1:-}" = --wireguard ] && have nmcli; then
while read -r name; do
[ -n "$name" ] || continue
local base="${name##*/}"
bw_get_note "$name" | SUDO install -m 600 /dev/stdin "/etc/NetworkManager/system-connections/$base"
ok "wireguard $base"
done < <(bw list items --search "$BW_PREFIX/wireguard/" | jq -r '.[].name' | grep "^$BW_PREFIX/wireguard/")
SUDO nmcli connection reload
fi
secrets_render
}
# Fill ${VAR} placeholders in MCP templates from secrets.env and merge them in.
secrets_render() {
[ -s "$SECRETS_ENV" ] || { warn "no $SECRETS_ENV (dot secrets pull)"; return; }
set -a; # shellcheck disable=SC1090
. "$SECRETS_ENV"; set +a
local render='walk(if type == "string" then gsub("\\$\\{(?<v>[A-Z_]+)\\}"; "\(env[.v] // "")") else . end)'
local claude_json="$HOME/.claude.json"
[ -f "$claude_json" ] || echo '{}' >"$claude_json"
jq --slurpfile m <(jq "$render" "$DOTFILES_DIR/config/claude/mcp.json") '.mcpServers = ((.mcpServers // {}) + $m[0])' \
"$claude_json" >"$claude_json.tmp" && mv "$claude_json.tmp" "$claude_json" && chmod 600 "$claude_json"
ok "Claude MCP servers"
mkdir -p "$HOME/.pi/agent"
(umask 077; jq "$render" "$DOTFILES_DIR/config/pi/mcp.json" >"$HOME/.pi/agent/mcp.json")
ok "pi MCP servers"
# HTTPS git auth for the forges (credential.helper=store).
(umask 077; {
[ -n "${GITEA_DOGE_TOKEN:-}" ] && echo "https://ziadarafat:$GITEA_DOGE_TOKEN@gitea.godemperordoge.org"
[ -n "${ETV_GITEA_TOKEN:-}" ] && echo "https://ziadarafat:$ETV_GITEA_TOKEN@gitea.etvamerica.com"
[ -n "${GITHUB_ACCESS_TOKEN:-}" ] && echo "https://RustyRaptor:$GITHUB_ACCESS_TOKEN@github.com"
} >"$HOME/.git-credentials")
ok "git credentials"
}
cmd_secrets() {
case "${1:-}" in
push) secrets_push ;;
pull) shift; secrets_pull "$@" ;;
render) secrets_render ;;
*) die "usage: dot secrets push|pull [--wireguard]|render" ;;
esac
}
# ---------------------------------------------------------------- wallpapers
cmd_wallpapers() {
if [ -d "$WALLPAPER_SRC_WORK" ] || [ -d "$WALLPAPER_SRC_PERSONAL" ]; then
have wallpaper-sort || die "wallpaper-sort missing (full profile)"
[ -d "$WALLPAPER_SRC_WORK" ] && wallpaper-sort "$WALLPAPER_SRC_WORK" "$WALLPAPER_DIR/work"
[ -d "$WALLPAPER_SRC_PERSONAL" ] && wallpaper-sort "$WALLPAPER_SRC_PERSONAL" "$WALLPAPER_DIR/personal"
if [ -d "$WALLPAPER_DIR/.git" ] && [ "${1:-}" = --push ]; then
git -C "$WALLPAPER_DIR" add -A && git -C "$WALLPAPER_DIR" commit -qm "wallpapers: resort $(date -I)" && git -C "$WALLPAPER_DIR" push -q || true
fi
elif [ ! -d "$WALLPAPER_DIR/.git" ]; then
step "cloning wallpapers"
git clone --depth 1 "$WALLPAPER_REPO" "$WALLPAPER_DIR" || warn "wallpaper clone failed (dot secrets pull for git credentials?)"
else
git -C "$WALLPAPER_DIR" pull -q --ff-only || true
fi
have daynight-hooks && daynight-hooks --once >/dev/null 2>&1 || true
}
# ---------------------------------------------------------------- sounds
cmd_sounds() {
have build-sound-theme || die "build-sound-theme missing (full profile)"
build-sound-theme "$@"
}
# ---------------------------------------------------------------- update / doctor
cmd_update() {
step "pull dotfiles"
git -C "$DOTFILES_DIR" pull --rebase --autostash || warn "git pull failed; continuing with local tree"
if [ "${1:-}" = --inputs ]; then
step "nix flake update"
source_nix && nix flake update --flake "$DOTFILES_DIR"
fi
cmd_switch
[ "$(profile)" = full ] && have flatpak && { step "flatpak update"; flatpak update --user -y --noninteractive || true; }
have mise && { step "mise upgrade"; mise upgrade -y || true; }
have claude && { step "claude update"; claude update || true; }
cmd_apps
}
cmd_doctor() {
local p; p="$(profile)"
printf 'os=%s profile=%s container=%s systemd=%s\n' "$(os_id)" "$p" "$(in_container && echo yes || echo no)" "$(has_systemd && echo yes || echo no)"
chk() { if eval "$2" >/dev/null 2>&1; then ok "$1"; else warn "$1 — $3"; fi; }
chk "nix" "source_nix" "dot nix"
chk "home-manager" "have home-manager" "dot switch"
chk "zsh + oh-my-zsh" "have zsh && [ -d \"\$(dirname \"\$(readlink -f ~/.zshrc)\")\" ]" "dot switch"
chk "mise" "have mise" "dot switch"
chk "uv" "have uv || mise which uv" "dot apps"
chk "pi" "have pi || mise which pi" "dot apps"
chk "pokemon-go-colorscripts" "have pokemon-go-colorscripts || mise which pokemon-go-colorscripts" "dot apps"
chk "claude" "[ -x ~/.local/bin/claude ]" "dot apps"
chk "podman" "have podman" "dot system"
chk "distrobox" "have distrobox" "dot system"
chk "ssh key" "[ -f ~/.ssh/id_ed25519 ]" "dot secrets pull"
chk "secrets.env" "[ -s $SECRETS_ENV ]" "dot secrets pull"
if [ "$p" = full ]; then
chk "flatpak" "have flatpak" "dot system"
while read -r app; do
[ -z "$app" ] || [[ "$app" == \#* ]] && continue
chk "flatpak $app" "flatpak_has $app" "dot apps"
done <"$DOTFILES_DIR/config/flatpaks.txt"
chk "Chrome PWA policy" "[ -f /etc/opt/chrome/policies/managed/dotfiles-pwas.json ]" "dot system"
chk "Edge PWA policy" "[ -f /etc/opt/edge/policies/managed/dotfiles-pwas.json ]" "dot system"
chk "wallpapers" "[ -d $WALLPAPER_DIR/personal ]" "dot wallpapers"
chk "konsole-daynight service" "systemctl --user is-active konsole-daynight" "dot switch, then log in to Plasma"
chk "daynight-hooks service" "systemctl --user is-active daynight-hooks" "dot switch, then log in to Plasma"
chk "netbird daemon" "systemctl is-active netbird" "dot system --netbird"
fi
}
cmd_bootstrap() {
local sys=1; [ "${DOTFILES_SKIP_SYSTEM:-0}" = 1 ] && sys=0
[ $sys = 1 ] && { install_pkgs || warn "distro packages incomplete; re-run: dot system"; }
cmd_nix
cmd_switch
[ $sys = 1 ] && { cmd_system --no-pkgs || warn "system layer incomplete; re-run: dot system"; }
# The new profile puts mise & friends on PATH.
export PATH="$HOME/.nix-profile/bin:$HOME/.local/bin:$PATH"
cmd_apps
[ "$(profile)" = full ] && { cmd_wallpapers || true; }
cat <<EOF
${c_grn}Done.${c_off} Next, once per machine:
bw login && export BW_SESSION=\$(bw unlock --raw) && dot secrets pull # SSH keys, MCP tokens, git creds
dot boxes # create the general/etv distroboxes
dot doctor # see what's left
Then sign in: Edge (work), Chrome + Firefox (personal), XPipe git sync token, Bitwarden.
EOF
}
usage() {
cat <<'EOF'
dot — portable dev environment
dot bootstrap first-time setup (nix → system → switch → apps)
dot update [--inputs] pull repo, (bump flake inputs), switch, update flatpaks/mise/claude
dot switch [profile] apply the home-manager config
dot profile [full|headless|auto]
dot system [--netbird] sudo layer: distro packages, browser PWA policies, netbird, login shell
dot nix install Nix if missing
dot apps Flatpaks, mise tools, Claude Code
dot secrets push|pull|render Bitwarden-backed SSH keys, tokens, WireGuard
dot wallpapers [--push] sort wallpapers into light/dark (or clone them)
dot sounds build the Portal 2 sound theme
dot boxes create distroboxes from distrobox.ini
dot doctor check what's installed
EOF
}
main() {
local cmd="${1:-help}"; shift || true
case "$cmd" in
bootstrap|update|switch|profile|system|nix|apps|secrets|wallpapers|sounds|boxes|doctor) "cmd_$cmd" "$@" ;;
help|-h|--help) usage ;;
*) usage; exit 1 ;;
esac
}
main "$@"