diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9715446..3f45699 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,12 +11,14 @@ jobs: - name: Tools run: sudo apt-get update && sudo apt-get install -y shellcheck lua5.4 jq - name: shellcheck - run: shellcheck -x -S warning install.sh uninstall.sh lib/common.sh lib/pkg.sh linux/*.sh linux/debian/*.sh tests/clean-rootfs.sh bin/web bin/firefox-for-browsh + run: shellcheck -x -S warning install.sh uninstall.sh lib/*.sh linux/*.sh linux/debian/*.sh macos/*.sh tests/*.sh bin/web bin/firefox-for-browsh - name: Syntax run: | - for f in install.sh uninstall.sh lib/common.sh lib/pkg.sh linux/*.sh linux/debian/*.sh; do bash -n "$f"; done + for f in install.sh uninstall.sh lib/*.sh linux/*.sh linux/debian/*.sh macos/*.sh; do bash -n "$f"; done jq . config/caelestia/shell.json >/dev/null for f in config/nvim/dotfiles.lua config/caelestia/*.lua; do luac5.4 -p "$f" 2>&1 || lua5.4 -e "assert(loadfile('$f'))"; done + - name: Menu (scripted answers) + run: tests/menu.sh - name: Dry run (everything except the desktop, which needs Debian 13) run: ./install.sh --dry-run --yes --only base,shell,tmux,nvim,terminal,browsers - name: Real run of the user-level parts in a throw-away HOME @@ -26,3 +28,31 @@ jobs: test -L "$HOME/.zshrc" && test -L "$HOME/.config/tmux/tmux.conf" "$GITHUB_WORKSPACE/uninstall.sh" --yes test ! -e "$HOME/.zshrc" + + # macOS ships bash 3.2; everything here runs with /bin/bash on purpose (the runner's PATH has a newer bash first). + macos: + runs-on: macos-latest + steps: + - uses: actions/checkout@v4 + - name: bash 3.2 syntax + run: | + /bin/bash --version | head -n1 + for f in install.sh uninstall.sh lib/common.sh lib/menu.sh lib/parts.sh linux/uninstall.sh macos/*.sh tests/menu.sh; do /bin/bash -n "$f"; done + - name: Menu (scripted answers, bash 3.2) + run: /bin/bash tests/menu.sh + - name: Dry run of every macOS component + run: /bin/bash ./install.sh --dry-run --yes --all + - name: Linux-only parts are refused + run: | + if /bin/bash ./install.sh --dry-run --yes --only desktop; then echo "desktop was accepted on macOS"; exit 1; fi + if /bin/bash ./install.sh --dry-run --yes --only apps; then echo "apps was accepted on macOS"; exit 1; fi + - name: Real run of shell, tmux, browsers and rust in a throw-away HOME + run: | + export HOME="$(mktemp -d)" + /bin/bash "$GITHUB_WORKSPACE/install.sh" --yes --only shell,tmux,browsers,rust + test -L "$HOME/.zshrc" && test -L "$HOME/.config/tmux/tmux.conf" && test -d "$HOME/.oh-my-zsh" + command -v starship tmux w3m + { command -v cargo || ls "$HOME/.cargo/bin/cargo"; } && cargo --version # the runner may already have rustup + zsh -ic 'echo zsh loads the config: $STARSHIP_CONFIG' + /bin/bash "$GITHUB_WORKSPACE/uninstall.sh" --yes + test ! -e "$HOME/.zshrc" diff --git a/README.md b/README.md index ff063ae..7ad12e4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Linux dotfiles for Debian, Ubuntu, Arch, Fedora and openSUSE: zsh, tmux, Neovim, Ghostty and Hyprland in one command +# Dotfiles for Linux (Debian, Ubuntu, Arch, Fedora, openSUSE) and macOS: zsh, tmux, Neovim, Ghostty and Hyprland in one command [![ci](https://github.com/rhmatzeka/dotfile/actions/workflows/ci.yml/badge.svg)](https://github.com/rhmatzeka/dotfile/actions/workflows/ci.yml) [![License: MIT](https://img.shields.io/github/license/rhmatzeka/dotfile)](LICENSE) @@ -7,9 +7,11 @@ ![Arch Linux](https://img.shields.io/badge/Arch-tested-1793D1?logo=archlinux&logoColor=white) ![Fedora 44](https://img.shields.io/badge/Fedora-44-51A2DA?logo=fedora&logoColor=white) ![openSUSE Tumbleweed](https://img.shields.io/badge/openSUSE-Tumbleweed-73BA25?logo=opensuse&logoColor=white) +![macOS beta](https://img.shields.io/badge/macOS-beta-000000?logo=apple&logoColor=white) -A one-command **Linux dotfiles installer** (Debian/Ubuntu, Arch, Fedora, openSUSE) for a fast terminal and an optional **Hyprland** desktop with the -**Caelestia** shell. It sets up **zsh** (Oh My Zsh, starship), **tmux**, **Neovim** with **NvChad** (tree-sitter, LSP), +A one-command **dotfiles installer** for Linux (Debian/Ubuntu, Arch, Fedora, openSUSE) and **macOS** (beta) for a fast +terminal and, on Linux, an optional **Hyprland** desktop with the **Caelestia** shell. A checklist lets you pick only the +parts you want, for example just the terminal setup on a Mac. It sets up **zsh** (Oh My Zsh, starship), **tmux**, **Neovim** with **NvChad** (tree-sitter, LSP), **Ghostty**, terminal browsers (Browsh, elinks, w3m) and a Dracula + cyan theme. Nothing is overwritten without a backup, and everything can be undone. @@ -69,10 +71,68 @@ Every step can be rehearsed without changing anything: bash <(curl -fsSL https://dotfiles.rahmateka.my.id) --dry-run --all ``` +### The menu + +The installer shows a checklist. Type a number to check or uncheck a part, then press Enter. Parts that are already +installed say so and start unchecked. Parts that cannot work on your system are listed separately and **cannot be +selected** (on a Mac: the Hyprland desktop and the Microsoft 365 web-app launchers), so "all" never picks them by +accident. Before anything is installed you get a summary with the big downloads and one more "Continue?". + +``` + dotfiles installer - macOS (beta) + + Choose what to install (type a number to check or uncheck it): + + 1. [x] base Homebrew, git, ripgrep, fzf, eza, zoxide and the JetBrainsMono Nerd Font + 2. [x] shell zsh + Oh My Zsh + starship prompt (installed) + 3. [x] tmux tmux with a cyan/Dracula theme + 4. [x] nvim Neovim (NvChad) with tree-sitter parsers and LSP servers + 5. [x] terminal Ghostty + its config (Vim-style keys, dark theme) + 6. [ ] browsers Terminal browser: w3m (elinks and Browsh are Linux only: Homebrew has no elinks) + 7. [ ] web Web development: PHP, Composer, MariaDB, phpMyAdmin (services stay off unless you say yes) + 8. [ ] rust Rust toolchain via rustup (rustc, cargo, clippy, rustfmt, rust-analyzer) + + Not available on this system (cannot be selected): + - desktop Hyprland + Caelestia desktop (Linux only) + - apps Microsoft 365 web-app launchers, Thunar menu (Linux only; on a Mac install Office from the App Store) + + number check/uncheck | a all | n none | Enter continue | q quit +``` + The short domain redirects to the script in this repository (see [docs/short-domain.md](docs/short-domain.md)). If it ever fails, the same script is at `https://raw.githubusercontent.com/rhmatzeka/dotfile/main/install.sh`. +## Moving to a Mac + +On a new MacBook, open **Terminal** and run the same command: + +```bash +bash <(curl -fsSL https://dotfiles.rahmateka.my.id) +``` + +1. If Apple's Command Line Tools are missing, a dialog opens: click **Install** and wait (git and the compiler come + from there). +2. The checklist appears with the terminal set (`base`, `shell`, `tmux`, `nvim`, `terminal`) already checked. Add + `web` or `rust` if you want them; the Linux-only parts are shown but cannot be picked. +3. Homebrew is installed first if it is missing (it asks for your password once). Everything else comes from Homebrew, + so nothing needs `sudo` afterwards. +4. Open **Ghostty** from Spotlight. It starts zsh with the same prompt, tmux, Neovim and colours as on Linux. + +Only the terminal setup, without asking: `bash <(curl -fsSL https://dotfiles.rahmateka.my.id) --yes`. Just a few +parts: `... --only shell,tmux,terminal`. The configs are the same files on both systems, so a change you push here +shows up on the other machine after the next install (or `git pull` in `~/.local/share/rhmatzeka-dotfile`). + +| Part | Linux | macOS | +|---|---|---| +| `base`, `shell`, `tmux`, `nvim`, `terminal`, `rust` | yes | yes (Homebrew; Ghostty itself is installed too) | +| `browsers` | Browsh, elinks, w3m | w3m only (Homebrew has no elinks; no Browsh) | +| `web` | PHP, Composer, MariaDB, Apache, phpMyAdmin | PHP, Composer, MariaDB, phpMyAdmin (served with `php -S`, no Apache) | +| `apps` (Microsoft 365 web-app launchers) | yes | no (install Office for Mac instead) | +| `desktop` (Hyprland + Caelestia) | Debian 13 | no | + +Your SSH keys, `.env` files and app logins are not part of this repository; copy them over yourself. + ## What you get | Component | Contents | @@ -88,7 +148,8 @@ ever fails, the same script is at | `rust` | Rust through the official rustup installer: stable toolchain, clippy, rustfmt, rust-analyzer | | `desktop` | Hyprland + Caelestia shell with see-through, blurred panels and a few small patches: Bluetooth lists (bar panel and the pair-new-device page) hide devices that only show a MAC address and show the device type instead; the Bluetooth panel says so when no adapter is found instead of showing dead switches; WiFi password fields get an eye button to show what you typed; text on the transparent panels gets a thin halo so it stays readable on bright wallpapers; notifications that contain HTML, such as Instagram's, show clean text; hibernation at critical battery gets enough free memory to succeed (`/sys/power/image_size` = 0) and the laptop powers off if it still fails, instead of staying on with a black screen (AMD GPUs do not come back from a failed hibernation), plus Caelestia's btop, fastfetch, foot, micro, thunar, zed and fish configs (only where you have none). **Debian 13 only, beta.** Builds Qt 6.11 from source (1 to 2 hours) | -The default selection is everything except `desktop`. +Checked by default: `base`, `shell`, `tmux`, `nvim` and `terminal` (plus `apps` on Linux when a graphical session is +running). `web`, `rust`, `browsers` and `desktop` are only installed when you check them. ## Memory usage @@ -129,7 +190,8 @@ If RAM is tight: | Arch Linux (and Manjaro, EndeavourOS, ... via `ID_LIKE=arch`) | Installed and checked from scratch in the official Arch bootstrap image. Only Arch itself was tested. `web` and `rust` also installed and checked from scratch | | Fedora 44 (and RHEL-likes via `ID_LIKE`) | Installed and checked from scratch in the official Fedora container image. `starship` is not in the Fedora repositories: its official installer is used. Only Fedora itself was tested. `web` and `rust` also installed and checked from scratch | | openSUSE Tumbleweed | Installed and checked from scratch in the official Tumbleweed image. Leap and SLE were not tested. `web` and `rust` also installed and checked from scratch | -| Alpine, NixOS, Void, Gentoo, macOS | **Not supported.** Contributions are welcome (see below) | +| macOS 13 or newer (Apple Silicon and Intel) | **Beta.** Everything except `desktop` and `apps`, through Homebrew. Checked on every push by GitHub Actions on `macos-latest` with Apple's own `/bin/bash` 3.2: the menu, a dry run of every part, and a real install of `shell`, `tmux`, `browsers` and `rust` followed by `uninstall`. Not yet run by hand on a real Mac; `nvim`, `terminal` (Ghostty) and `web` were only dry-run | +| Alpine, NixOS, Void, Gentoo | **Not supported.** Contributions are welcome (see below) | `desktop` (Hyprland + Caelestia) is **Debian 13 only** because it depends on `trixie-backports` and on pinned build versions. On the other distributions it is not offered; install Hyprland from your repositories and follow @@ -156,7 +218,8 @@ installed from the repositories when available. the previous Debian-only revision. Repeat it yourself with `tests/clean-rootfs.sh` (see below). - A user who **already has their own config files**: they are backed up, a second install does not back them up again, and `uninstall` puts the originals back. -- `--dry-run` for every component, shellcheck, and a GitHub Actions run on Ubuntu for every push. +- `--dry-run` for every component, shellcheck, scripted answers for the menu (`tests/menu.sh`), and GitHub Actions + runs on Ubuntu and macOS for every push. - Failures are not swallowed: a failed package step fails its component and the installer exits non-zero. **Not tested:** "Open with Microsoft 365" with a work or school (Microsoft 365 Business) account (it was tested end to end with one personal account: PDF converted, uploaded and opened), the extra Caelestia application configs installed by `desktop`, the `desktop` source build from scratch, the Microsoft 365 web apps opening in a real browser @@ -167,7 +230,7 @@ above, and ARM machines. ```bash ./install.sh # interactive menu -./install.sh --all # every component, including desktop +./install.sh --all # every component this system supports (on Debian 13 that includes desktop) ./install.sh --only shell,nvim # pick components ./install.sh --dry-run --all # show what would happen, change nothing ./install.sh --yes # no questions, use the default answers @@ -196,18 +259,23 @@ are left in place; the list is printed at the end. ## How it works The top-level `install.sh` detects the OS, fetches this repository to `~/.local/share/rhmatzeka-dotfile`, and hands over to the installer for -that platform (`linux/install.sh`, which uses `lib/pkg.sh` to talk to apt, pacman, dnf or zypper). Configs live in `config/` and are installed as symlinks, so editing them +that platform: `linux/install.sh`, which uses `lib/pkg.sh` to talk to apt, pacman, dnf or zypper, or `macos/install.sh`, +which uses Homebrew. Both use the same checklist (`lib/menu.sh`) and the same steps for Oh My Zsh, NvChad, elinks and +rustup (`lib/parts.sh`). Everything the Mac runs is kept compatible with the bash 3.2 that macOS ships. Configs live in `config/` and are installed as symlinks, so editing them in that directory takes effect immediately. Shared helpers (backup, symlinks, dry run) are in `lib/common.sh`. ``` install.sh uninstall.sh entry points (OS detection) lib/common.sh lib/pkg.sh shared helpers; pkg.sh maps package names per distribution +lib/menu.sh lib/parts.sh the checklist, and the steps Linux and macOS share linux/ installer for every family: install.sh, uninstall.sh +macos/ macOS installer (Homebrew); uninstall.sh reuses linux/uninstall.sh linux/debian/desktop.sh Hyprland + Caelestia build (Debian 13 only) config/ zsh, starship, tmux, nvim, ghostty, elinks, caelestia bin/ small scripts (web, firefox-for-browsh) docs/ preview placeholder, how-tos tests/clean-rootfs.sh install into a clean Debian/Ubuntu/Arch/Fedora/openSUSE root filesystem and check it +tests/menu.sh drive the checklist with scripted answers ``` ## Adding another distribution diff --git a/config/zsh/zshrc b/config/zsh/zshrc index 3e43786..1b708dd 100644 --- a/config/zsh/zshrc +++ b/config/zsh/zshrc @@ -2,6 +2,11 @@ # ── PATH ───────────────────────────────────────────────────────────────────── typeset -U path # no duplicates +# macOS: Homebrew (Apple Silicon: /opt/homebrew, Intel: /usr/local). Nothing happens on Linux. +for _brew in /opt/homebrew/bin/brew /usr/local/bin/brew; do + [[ -x $_brew ]] && { eval "$($_brew shellenv)"; break; } +done +unset _brew path=("$HOME/.local/bin" "$HOME/.cargo/bin" "$HOME/.bun/bin" $path) export EDITOR=nvim @@ -47,7 +52,8 @@ alias ...='cd ../..' command -v zoxide >/dev/null 2>&1 && eval "$(zoxide init zsh)" # fzf key bindings: Ctrl-R history, Ctrl-T files, Alt-C directories -for f in /usr/share/doc/fzf/examples/key-bindings.zsh /usr/share/fzf/key-bindings.zsh; do +for f in /usr/share/doc/fzf/examples/key-bindings.zsh /usr/share/fzf/key-bindings.zsh \ + ${HOMEBREW_PREFIX:+$HOMEBREW_PREFIX/opt/fzf/shell/key-bindings.zsh}; do [[ -r $f ]] && { source "$f"; break; } done diff --git a/install.sh b/install.sh index 8edac69..438703f 100755 --- a/install.sh +++ b/install.sh @@ -5,7 +5,7 @@ # bash <(curl -fsSL https://dotfiles.rahmateka.my.id) --dry-run --all # # `bash <(...)` instead of `curl | bash` keeps the terminal attached, so the menu and sudo prompts work. -# This file only detects the distribution family, fetches the repository to ~/.local/share/rhmatzeka-dotfile and hands over to the +# This file only detects the system (macOS, or which Linux family), fetches the repository to ~/.local/share/rhmatzeka-dotfile and hands over to the # installer for that platform. Everything it will do is printed first; --dry-run changes nothing. set -euo pipefail @@ -30,15 +30,14 @@ printf '\n%s%s dotfiles %s%s\n' "$BD" "$CY" "$ACTION" "$OFF" printf '%s https://github.com/rhmatzeka/dotfile%s\n\n' "$DM" "$OFF" # --- which OS? --------------------------------------------------------------- -# Only Linux families are implemented: debian (Debian, Ubuntu, Mint...), arch (Arch, Manjaro, EndeavourOS...), -# fedora (Fedora, RHEL-likes), suse (openSUSE). The real work is in linux/install.sh; this only says hello. -FAMILY="" +# macOS (beta, terminal and developer tools; the real work is in macos/install.sh), and the Linux families +# debian (Debian, Ubuntu, Mint...), arch (Arch, Manjaro, EndeavourOS...), fedora (Fedora, RHEL-likes) and +# suse (openSUSE), handled by linux/install.sh. This file only says hello. +# Keep it bash 3.2 compatible: that is the bash macOS ships, and `bash <(curl ...)` runs it. +FAMILY=""; PLATFORM=linux case "$(uname -s)" in Darwin) - bad "macOS is not supported yet." - say "Supported: Debian/Ubuntu, Arch, Fedora and openSUSE families. Contributions are welcome:" - say "https://github.com/rhmatzeka/dotfile/issues" - exit 1 ;; + PLATFORM=macos; FAMILY=macos ;; Linux) ids=" $(. /etc/os-release 2>/dev/null; printf '%s %s' "${ID:-}" "${ID_LIKE:-}") " case "$ids" in @@ -47,21 +46,37 @@ case "$(uname -s)" in *" fedora "*|*" rhel "*|*" centos "*) FAMILY=fedora ;; *" suse "*|*" opensuse "*|*" sles "*) FAMILY=suse ;; *) bad "This Linux distribution is not supported yet (found:$ids)." - say "Supported: Debian/Ubuntu, Arch, Fedora and openSUSE families." + say "Supported: macOS, and the Debian/Ubuntu, Arch, Fedora and openSUSE families." exit 1 ;; esac ;; *) bad "Unsupported OS: $(uname -s)"; exit 1 ;; esac -good "Detected: Linux / $FAMILY family" +if [ "$PLATFORM" = macos ]; then + good "Detected: macOS $(sw_vers -productVersion 2>/dev/null) ($(uname -m))" + # git, and the compiler Homebrew and Neovim's parsers need, come with Apple's Command Line Tools. + # /usr/bin/git exists even without them (it only opens the installer dialog), so ask xcode-select instead. + if ! xcode-select -p >/dev/null 2>&1; then + say "Installing Apple's Command Line Tools (git, compiler). A dialog opens: click Install and wait." + xcode-select --install >/dev/null 2>&1 || true + waited=0 + until xcode-select -p >/dev/null 2>&1; do + [ "$waited" -ge 3600 ] && { bad "The Command Line Tools are still missing after an hour; install them and run this again."; exit 1; } + sleep 10; waited=$((waited + 10)) + done + good "Command Line Tools installed" + fi +else + good "Detected: Linux / $FAMILY family" +fi # --- where does the repository live? ------------------------------------------ # Run from a checkout (bash ./install.sh)? Use it as is. Run through curl? Fetch it to ~/.local/share/rhmatzeka-dotfile. SELF="${BASH_SOURCE[0]:-}" -if [ -n "$SELF" ] && [ -f "$SELF" ] && [ -f "$(dirname "$SELF")/linux/install.sh" ] && [ -z "${DOTFILES_FORCE_CLONE:-}" ]; then +if [ -n "$SELF" ] && [ -f "$SELF" ] && [ -f "$(dirname "$SELF")/$PLATFORM/install.sh" ] && [ -z "${DOTFILES_FORCE_CLONE:-}" ]; then DOTFILES_DIR="$(cd "$(dirname "$SELF")" && pwd)" good "Using this checkout: $DOTFILES_DIR" else - if ! command -v git >/dev/null 2>&1; then + if ! command -v git >/dev/null 2>&1 && [ "$PLATFORM" = linux ]; then say "git is needed to fetch the repository." command -v sudo >/dev/null 2>&1 || { bad "git and sudo are both missing; install git first."; exit 1; } case "$FAMILY" in @@ -92,7 +107,8 @@ else fi export DOTFILES_DIR -TARGET="$DOTFILES_DIR/linux/$ACTION.sh" +TARGET="$DOTFILES_DIR/$PLATFORM/$ACTION.sh" [ -f "$TARGET" ] || { bad "Missing $TARGET"; exit 1; } printf '\n' -exec bash "$TARGET" "$@" +# the same bash that runs this file (on macOS that is /bin/bash 3.2, which the installers are written for) +exec "${BASH:-bash}" "$TARGET" "$@" diff --git a/lib/common.sh b/lib/common.sh index 31d2034..bafc053 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -41,8 +41,18 @@ run() { fi } -# interactive -> 0 when a person can answer: stdin is a terminal, or /dev/tty can really be opened -interactive() { [ -t 0 ] || { : /dev/null; } +# Answers are read from the terminal even when the script itself came through a pipe. Tests point DOTFILES_TTY at a +# file of answers; it is opened once (fd 7) so each question reads the next line instead of the first one again. +DOTFILES_TTY="${DOTFILES_TTY:-/dev/tty}" + +# interactive -> 0 when a person can answer: stdin is a terminal, or the terminal can really be opened +interactive() { [ -t 0 ] || { : <"$DOTFILES_TTY"; } 2>/dev/null; } + +# tty_read VAR -> one line of input into VAR +tty_read() { + if [ -z "${_TTY_OPEN:-}" ]; then exec 7<"$DOTFILES_TTY" || return 1; _TTY_OPEN=1; fi + IFS= read -r "$1" <&7 +} # confirm "question" [y|n] -> 0 for yes. Without a terminal, or with --yes, the default answer is used. confirm() { @@ -52,7 +62,7 @@ confirm() { [ "$def" = y ]; return fi printf ' %s?%s %s %s ' "$C_PINK" "$C_OFF" "$q" "$hint" >&2 - read -r ans timeout(1) where it exists (GNU coreutils; gtimeout from Homebrew), else just run CMD +with_timeout() { + local s="$1"; shift + if have timeout; then timeout "$s" "$@" + elif have gtimeout; then gtimeout "$s" "$@" + else "$@"; fi +} + +# linked_here DST -> 0 when DST is a symlink into this repository (the component already put it there) +linked_here() { [ -L "$1" ] && case "$(readlink "$1")" in "$DOTFILES_DIR"/*) true ;; *) false ;; esac; } + # version_ge A B -> 0 when A >= B (dotted numbers) version_ge() { [ "$(printf '%s\n%s\n' "$2" "$1" | sort -V | head -n1)" = "$2" ]; } diff --git a/lib/menu.sh b/lib/menu.sh new file mode 100644 index 0000000..5e6fb5e --- /dev/null +++ b/lib/menu.sh @@ -0,0 +1,162 @@ +# shellcheck shell=bash +# The component checklist shared by the Linux and macOS installers. Source lib/common.sh first. +# +# Works with the bash 3.2 that macOS ships: no associative arrays, and no bare "${a[@]}" of a possibly empty +# array (that is an "unbound variable" error under `set -u` before bash 4.4). +# +# The installer sets, before calling menu_choose: +# MENU_TITLE shown at the top, e.g. "macOS (beta)" +# MENU_ITEMS=(...) components that can be installed on this machine, in install order +# MENU_DEFAULTS=(...) the ones checked at the start (minus those already installed) +# MENU_OFF=(...) "name|reason" for components that exist in the repo but cannot be installed here: +# listed separately, never selectable, skipped by "a" and refused by --only +# and defines: +# menu_desc NAME one-line description +# menu_status NAME prints a short status ("installed") when there is nothing left to do, else nothing +# menu_note NAME optional: a warning shown before installing it ("about 1 GB download"), else nothing +# +# Results: menu_choose and menu_from_list fill SELECTED=(...); menu_confirm shows the summary and asks once. + +MENU_TITLE="${MENU_TITLE:-}" +SELECTED=() + +in_list() { # in_list WORD [LIST...] + local w="$1" x; shift + for x in "$@"; do [ "$x" = "$w" ] && return 0; done + return 1 +} + +menu_off_reason() { # menu_off_reason NAME -> prints the reason when NAME cannot be installed here + local e + for e in ${MENU_OFF[@]+"${MENU_OFF[@]}"}; do + [ "${e%%|*}" = "$1" ] && { printf '%s' "${e#*|}"; return 0; } + done + return 1 +} + +# menu_from_list "a,b,c" -> SELECTED, or exits with a clear message for an unknown or unavailable name +menu_from_list() { + local c why list + SELECTED=() + list="$(printf '%s' "$1" | tr ',' ' ')" + for c in $list; do + if why="$(menu_off_reason "$c")"; then die "$c cannot be installed here: $why"; fi + in_list "$c" "${MENU_ITEMS[@]}" || die "unknown component: $c (see --list)" + in_list "$c" ${SELECTED[@]+"${SELECTED[@]}"} || SELECTED+=("$c") + done +} + +menu_list() { # for --list + local c e mark + for c in "${MENU_ITEMS[@]}"; do + mark=" "; in_list "$c" ${MENU_DEFAULTS[@]+"${MENU_DEFAULTS[@]}"} && mark="*" + printf ' %s %-9s %s\n' "$mark" "$c" "$(menu_desc "$c")" + done + printf '\n * = checked by default\n' + if [ "${#MENU_OFF[@]}" -gt 0 ]; then + printf '\n Not available on this system:\n' + for e in "${MENU_OFF[@]}"; do printf ' - %-9s %s\n' "${e%%|*}" "${e#*|}"; done + fi +} + +_MENU_ON=(); _MENU_STATUS=() + +_menu_draw() { + local i c e n="${#MENU_ITEMS[@]}" box st + [ -t 1 ] && printf '\033[H\033[2J' + printf '\n %sdotfiles installer%s %s%s%s\n\n' "$C_BOLD$C_CYAN" "$C_OFF" "$C_DIM" "${MENU_TITLE:+- $MENU_TITLE}" "$C_OFF" + printf ' Choose what to install (type a number to check or uncheck it):\n\n' + for (( i=0; i ' +} + +# menu_choose -> SELECTED. Without a terminal, or with --yes, it takes MENU_DEFAULTS without asking. +menu_choose() { + local i c n="${#MENU_ITEMS[@]}" ans tok msg="" why + SELECTED=() + if [ "${ASSUME_YES:-0}" = 1 ] || ! interactive; then + SELECTED=(${MENU_DEFAULTS[@]+"${MENU_DEFAULTS[@]}"}); return 0 + fi + _MENU_ON=(); _MENU_STATUS=() + for (( i=0; i/dev/null)" + # already installed: nothing to do, so it starts unchecked (check it again to repair or update it) + if in_list "$c" ${MENU_DEFAULTS[@]+"${MENU_DEFAULTS[@]}"} && [ -z "${_MENU_STATUS[$i]}" ]; then _MENU_ON[$i]=1; else _MENU_ON[$i]=0; fi + done + while :; do + _menu_draw "$msg"; msg="" + tty_read ans || ans=q + case "$ans" in + "") break ;; + a|A) for (( i=0; i 0 to go ahead. Lists what will be installed, the heavy parts, and what this system skips. +menu_confirm() { + local c e note off="" + [ "${#SELECTED[@]}" -gt 0 ] || { info "Nothing selected."; exit 0; } + printf '\n %sWill install:%s %s\n' "$C_BOLD" "$C_OFF" "${SELECTED[*]}" + for c in "${SELECTED[@]}"; do + [ -n "$(menu_status "$c" 2>/dev/null)" ] && continue # already there: little or nothing to download + note="$(menu_note "$c" 2>/dev/null)" || note="" + [ -n "$note" ] && printf ' %s!%s %-9s %s\n' "$C_YELLOW" "$C_OFF" "$c" "$note" + done + for e in ${MENU_OFF[@]+"${MENU_OFF[@]}"}; do off="$off ${e%%|*}"; done + [ -n "$off" ] && printf ' %sNot available here, skipped:%s%s\n' "$C_DIM" "$off" "$C_OFF" + printf '\n' + [ "${DRY_RUN:-0}" = 1 ] && return 0 + confirm "Continue?" y +} + +# run_selected -> runs comp_ for each selected component, in MENU_ITEMS order, each in its own subshell so +# one failure does not stop the rest. Fills OK=(...) and FAILED=(...) and prints the result. +run_selected() { + local c rc + OK=(); FAILED=() + for c in "${MENU_ITEMS[@]}"; do + in_list "$c" ${SELECTED[@]+"${SELECTED[@]}"} || continue + # NOT inside `if (...)`: bash ignores `set -e` in a condition context, which would let a failed package install + # carry on and be reported as success. A plain subshell followed by $? keeps errexit active inside it. + ( set -e; "comp_$c" ) + rc=$? + if [ "$rc" -eq 0 ]; then OK+=("$c"); else FAILED+=("$c"); fail "component failed (exit $rc): $c"; fi + done + printf '\n' + [ ${#OK[@]} -gt 0 ] && ok "Done: ${OK[*]}" + if [ ${#FAILED[@]} -gt 0 ]; then + fail "Failed: ${FAILED[*]} (run again with --only $(printf '%s' "${FAILED[*]}" | tr ' ' ',') after fixing the cause)" + fi + return 0 +} diff --git a/lib/parts.sh b/lib/parts.sh new file mode 100644 index 0000000..be10c24 --- /dev/null +++ b/lib/parts.sh @@ -0,0 +1,71 @@ +# shellcheck shell=bash +# Steps that are the same on Linux and macOS once the packages are there. Source lib/common.sh first and set C to +# the repository's config/ directory. bash 3.2 safe (macOS). + +# Oh My Zsh with the two plugins, then the zsh and starship configs from the repo +shell_configs() { + git_clone https://github.com/ohmyzsh/ohmyzsh "$HOME/.oh-my-zsh" + git_clone https://github.com/zsh-users/zsh-autosuggestions "$HOME/.oh-my-zsh/custom/plugins/zsh-autosuggestions" + git_clone https://github.com/zsh-users/zsh-syntax-highlighting "$HOME/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting" + backup_and_link "$C/zsh/zshrc" "$HOME/.zshrc" + backup_and_link "$C/starship/starship.toml" "$HOME/.config/starship.toml" +} + +# The NvChad starter (unless the user already has an nvim config of their own), the repo's plugin spec, then a +# headless first run: plugins, tree-sitter parsers and the Mason LSP servers. Needs nvim, git, node/npm, a C compiler. +nvchad_setup() { + local cfg="$HOME/.config/nvim" marker="$HOME/.config/nvim/.rhmatzeka-dotfiles" + if [ -e "$cfg" ] && [ ! -e "$marker" ]; then + warn "an nvim config already exists at ~/.config/nvim: leaving it untouched." + warn "Add $C/nvim/dotfiles.lua to its lua/plugins/ if you want the extra parsers/LSP." + return 0 + fi + if [ ! -e "$cfg" ]; then + info "cloning the NvChad starter" + if [ "$DRY_RUN" = 1 ]; then info "[dry-run] would clone NvChad/starter" + else git clone -q --depth 1 https://github.com/NvChad/starter "$cfg" && rm -rf "$cfg/.git" && : >"$marker"; fi + fi + [ "$DRY_RUN" = 1 ] || mkdir -p "$cfg/lua/plugins" + backup_and_link "$C/nvim/dotfiles.lua" "$cfg/lua/plugins/dotfiles.lua" + + [ "$DRY_RUN" = 1 ] && { info "[dry-run] would sync plugins, build tree-sitter parsers and install LSP servers"; return 0; } + info "syncing plugins (first run, a minute or two)" + with_timeout 600 nvim --headless "+Lazy! sync" +qa >/dev/null 2>&1 || warn "plugin sync reported a problem; it will retry when you open nvim" + info "building tree-sitter parsers" + with_timeout 600 nvim --headless "+Lazy load nvim-treesitter" "+lua local ok,e=pcall(function() require('nvim-treesitter').install(dofile(vim.fn.stdpath('config')..'/lua/plugins/dotfiles.lua')[1].opts.ensure_installed):wait(540000) end) if not ok then print(e) end" +qa >/dev/null 2>&1 \ + || warn "parser build reported a problem; check :checkhealth nvim-treesitter" + info "installing LSP servers and formatters (Mason)" + with_timeout 900 nvim --headless "+Lazy load mason.nvim" "+MasonInstall $(paste -sd' ' "$C/nvim/mason-packages")" "+qall" >/dev/null 2>&1 \ + || warn "Mason reported a problem; run :Mason inside nvim to see which package failed" + ok "Neovim is ready" +} + +# elinks config; DOWNLOADS = where it should save files +elinks_configs() { + local downloads="$1" + # elinks saves into the current directory by default; point it at the Downloads folder (absolute path, this file is per machine) + if [ "$DRY_RUN" = 1 ]; then info "[dry-run] would write ~/.config/elinks/local.conf (download folder)" + else + mkdir -p "$HOME/.config/elinks" "$downloads" + printf '## written by rhmatzeka/dotfile: where elinks saves downloads\nset document.download.directory = "%s/"\n' "$downloads" >"$HOME/.config/elinks/local.conf" + fi + backup_and_link "$C/elinks/elinks.conf" "$HOME/.config/elinks/elinks.conf" +} + +# rustup from rustup.rs (stable toolchain, shell files untouched: the zsh config puts ~/.cargo/bin on PATH) +rustup_setup() { + if have rustup || [ -x "$HOME/.cargo/bin/rustup" ]; then + ok "rustup already installed" + elif [ "$DRY_RUN" = 1 ]; then info "[dry-run] would run the official rustup installer (rustup.rs)" + else + info "running the official rustup installer (stable toolchain, no changes to your shell files)" + curl -fsSL --proto '=https' --tlsv1.2 https://sh.rustup.rs | sh -s -- -y --no-modify-path --profile default \ + || { fail "rustup installer failed"; return 1; } + fi + if [ "$DRY_RUN" != 1 ]; then + export PATH="$HOME/.cargo/bin:$PATH" + rustup component add rust-analyzer clippy rustfmt >/dev/null 2>&1 || warn "could not add rust-analyzer/clippy/rustfmt" + ok "$(rustc --version 2>/dev/null)" + fi + info "\$HOME/.cargo/bin must be on your PATH (the zsh config from this repo does that)." +} diff --git a/linux/install.sh b/linux/install.sh index 3b25f44..4ab0d0e 100755 --- a/linux/install.sh +++ b/linux/install.sh @@ -17,13 +17,21 @@ export DOTFILES_DIR="${DOTFILES_DIR:-$(cd "$HERE/.." && pwd)}" . "$DOTFILES_DIR/lib/common.sh" # shellcheck source=../lib/pkg.sh . "$DOTFILES_DIR/lib/pkg.sh" +# shellcheck source=../lib/menu.sh +. "$DOTFILES_DIR/lib/menu.sh" +# shellcheck source=../lib/parts.sh +. "$DOTFILES_DIR/lib/parts.sh" pkg_detect || { fail "This Linux distribution is not supported yet (Debian/Ubuntu, Arch, Fedora and openSUSE families are)."; exit 1; } C="$DOTFILES_DIR/config" ARCH="$(uname -m)" COMPONENTS=(base shell tmux nvim terminal browsers apps web rust) -[ "$PKG_FAMILY" = debian ] && COMPONENTS+=(desktop) +MENU_OFF=() +# shellcheck disable=SC2034 # MENU_* are read by lib/menu.sh +if [ "$PKG_FAMILY" = debian ]; then COMPONENTS+=(desktop) +else MENU_OFF=("desktop|Hyprland + Caelestia: only implemented for Debian 13 so far; install Hyprland from your repositories and follow https://github.com/caelestia-dots/caelestia") +fi declare -A DESC=( [base]="Base packages and the JetBrainsMono Nerd Font" [shell]="zsh + Oh My Zsh + starship prompt" @@ -42,13 +50,38 @@ DEFAULTS=(base shell tmux nvim terminal) usage() { sed -n '2,12p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; } -list_components() { - local c mark - for c in "${COMPONENTS[@]}"; do - mark=" "; for d in "${DEFAULTS[@]}"; do [ "$c" = "$d" ] && mark="*"; done - printf ' %s %-9s %s\n' "$mark" "$c" "${DESC[$c]}" - done - printf '\n * = selected by default\n' +# for lib/menu.sh +# shellcheck disable=SC2034 +MENU_TITLE="Linux / $PKG_FAMILY" +# shellcheck disable=SC2034 +MENU_ITEMS=("${COMPONENTS[@]}") +# shellcheck disable=SC2034 +MENU_DEFAULTS=("${DEFAULTS[@]}") +menu_desc() { printf '%s' "${DESC[$1]}"; } +menu_status() { + case "$1" in + base) fc-list 2>/dev/null | grep -qi "JetBrainsMono Nerd" && have rg && have fzf && echo installed ;; + shell) linked_here "$HOME/.zshrc" && [ -d "$HOME/.oh-my-zsh" ] && echo installed ;; + tmux) linked_here "$HOME/.config/tmux/tmux.conf" && have tmux && echo installed ;; + nvim) linked_here "$HOME/.config/nvim/lua/plugins/dotfiles.lua" && have nvim && echo installed ;; + terminal) linked_here "$HOME/.config/ghostty/config.ghostty" && echo installed ;; + browsers) have elinks && have w3m && echo installed ;; + apps) linked_here "$HOME/.local/bin/m365" && echo installed ;; + web) have php && { have mariadb || have mysql; } && echo installed ;; + rust) { have rustup || [ -x "$HOME/.cargo/bin/rustup" ]; } && echo installed ;; + desktop) [ -e "$HOME/.config/hypr/.rhmatzeka-dotfiles" ] && have qs && echo installed ;; + esac + return 0 +} +menu_note() { + case "$1" in + nvim) echo "about 500 MB (Node.js, parsers and LSP servers), 5-15 minutes" ;; + apps) echo "about 600 MB (Chromium if you have no Chrome/Brave/Edge, VLC, GIMP)" ;; + web) echo "about 300 MB; services stay off unless you say yes" ;; + rust) echo "about 1 GB (toolchain in ~/.rustup)" ;; + desktop) echo "1-2 hours: builds Qt 6.11 from source, about 5 GB of disk while building" ;; + esac + return 0 } # ------------------------------------------------------------ arguments ---- @@ -60,7 +93,7 @@ while [ $# -gt 0 ]; do --all) ALL=1 ;; --only) shift; ONLY="${1:-}" ;; --only=*) ONLY="${1#--only=}" ;; - --list) list_components; exit 0 ;; + --list) menu_list; exit 0 ;; -h|--help) usage; exit 0 ;; *) die "unknown option: $1 (try --help)" ;; esac @@ -77,35 +110,10 @@ preflight() { } select_components() { - local c ans n + # shellcheck disable=SC2034 # read by run_selected (lib/menu.sh) if [ "$ALL" = 1 ]; then SELECTED=("${COMPONENTS[@]}"); return; fi - if [ -n "$ONLY" ]; then - IFS=',' read -ra SELECTED <<<"$ONLY" - for c in "${SELECTED[@]}"; do - if [ "$c" = desktop ] && [ "$PKG_FAMILY" != debian ]; then - die "the desktop component (Hyprland + Caelestia) is only implemented for Debian 13 so far; on Arch/Fedora/openSUSE install Hyprland from your repositories and follow https://github.com/caelestia-dots/caelestia" - fi - [[ " ${COMPONENTS[*]} " == *" $c "* ]] || die "unknown component: $c (see --list)" - done - return - fi - if [ "$ASSUME_YES" = 1 ] || ! interactive; then SELECTED=("${DEFAULTS[@]}"); return; fi - printf '\n Components:\n\n' - n=1; for c in "${COMPONENTS[@]}"; do - local mark=" "; for d in "${DEFAULTS[@]}"; do [ "$c" = "$d" ] && mark="*"; done - printf ' %d. [%s] %-9s %s\n' "$n" "$mark" "$c" "${DESC[$c]}"; n=$((n+1)) - done - printf '\n Pick numbers (e.g. "1 2 4"), "a" for all, Enter for the defaults (*), "q" to quit: ' >&2 - read -r ans /dev/null || return 1; fi fi - git_clone https://github.com/ohmyzsh/ohmyzsh "$HOME/.oh-my-zsh" - git_clone https://github.com/zsh-users/zsh-autosuggestions "$HOME/.oh-my-zsh/custom/plugins/zsh-autosuggestions" - git_clone https://github.com/zsh-users/zsh-syntax-highlighting "$HOME/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting" - backup_and_link "$C/zsh/zshrc" "$HOME/.zshrc" - backup_and_link "$C/starship/starship.toml" "$HOME/.config/starship.toml" + shell_configs if [ "$(basename "${SHELL:-}")" != zsh ] && confirm "Make zsh your default login shell?" n; then have chsh || pkg_optional chsh run chsh -s "$(command -v zsh)" "$USER" @@ -251,31 +255,7 @@ comp_nvim() { fi else ok "Neovim $v"; fi - # the NvChad starter, unless the user already has an nvim config of their own - local cfg="$HOME/.config/nvim" marker="$HOME/.config/nvim/.rhmatzeka-dotfiles" - if [ -e "$cfg" ] && [ ! -e "$marker" ]; then - warn "an nvim config already exists at ~/.config/nvim: leaving it untouched." - warn "Add $C/nvim/dotfiles.lua to its lua/plugins/ if you want the extra parsers/LSP." - return 0 - fi - if [ ! -e "$cfg" ]; then - info "cloning the NvChad starter" - if [ "$DRY_RUN" = 1 ]; then info "[dry-run] would clone NvChad/starter" - else git clone -q --depth 1 https://github.com/NvChad/starter "$cfg" && rm -rf "$cfg/.git" && : >"$marker"; fi - fi - [ "$DRY_RUN" = 1 ] || mkdir -p "$cfg/lua/plugins" - backup_and_link "$C/nvim/dotfiles.lua" "$cfg/lua/plugins/dotfiles.lua" - - [ "$DRY_RUN" = 1 ] && { info "[dry-run] would sync plugins, build tree-sitter parsers and install LSP servers"; return 0; } - info "syncing plugins (first run, a minute or two)" - timeout 600 nvim --headless "+Lazy! sync" +qa >/dev/null 2>&1 || warn "plugin sync reported a problem; it will retry when you open nvim" - info "building tree-sitter parsers" - timeout 600 nvim --headless "+Lazy load nvim-treesitter" "+lua local ok,e=pcall(function() require('nvim-treesitter').install(dofile(vim.fn.stdpath('config')..'/lua/plugins/dotfiles.lua')[1].opts.ensure_installed):wait(540000) end) if not ok then print(e) end" +qa >/dev/null 2>&1 \ - || warn "parser build reported a problem; check :checkhealth nvim-treesitter" - info "installing LSP servers and formatters (Mason)" - timeout 900 nvim --headless "+Lazy load mason.nvim" "+MasonInstall $(paste -sd' ' "$C/nvim/mason-packages")" "+qall" >/dev/null 2>&1 \ - || warn "Mason reported a problem; run :Mason inside nvim to see which package failed" - ok "Neovim is ready" + nvchad_setup } comp_terminal() { @@ -292,13 +272,7 @@ comp_terminal() { comp_browsers() { step "Terminal browsers" pkg_need elinks w3m - # elinks saves into the current directory by default; point it at the Downloads folder (absolute path, this file is per machine) - if [ "$DRY_RUN" = 1 ]; then info "[dry-run] would write ~/.config/elinks/local.conf (download folder)" - else - mkdir -p "$HOME/.config/elinks" "$(downloads_dir)" - printf '## written by rhmatzeka/dotfile: where elinks saves downloads\nset document.download.directory = "%s/"\n' "$(downloads_dir)" >"$HOME/.config/elinks/local.conf" - fi - backup_and_link "$C/elinks/elinks.conf" "$HOME/.config/elinks/elinks.conf" + elinks_configs "$(downloads_dir)" pkg_optional firefox if ! pkg_have firefox; then warn "Firefox is unavailable here, so Browsh is skipped (elinks and w3m still work)."; return 0; fi local t; t="$(arch_tag amd64 arm64)" || { fail "unsupported CPU: $ARCH"; return 1; } @@ -368,20 +342,7 @@ comp_web() { comp_rust() { step "Rust (rustup)" pkg_need curl ca-certificates buildtools - if have rustup || [ -x "$HOME/.cargo/bin/rustup" ]; then - ok "rustup already installed" - elif [ "$DRY_RUN" = 1 ]; then info "[dry-run] would run the official rustup installer (rustup.rs)" - else - info "running the official rustup installer (stable toolchain, no changes to your shell files)" - curl -fsSL --proto '=https' --tlsv1.2 https://sh.rustup.rs | sh -s -- -y --no-modify-path --profile default \ - || { fail "rustup installer failed"; return 1; } - fi - if [ "$DRY_RUN" != 1 ]; then - export PATH="$HOME/.cargo/bin:$PATH" - rustup component add rust-analyzer clippy rustfmt >/dev/null 2>&1 || warn "could not add rust-analyzer/clippy/rustfmt" - ok "$(rustc --version 2>/dev/null)" - fi - info "\$HOME/.cargo/bin must be on your PATH (the zsh config from this repo does that)." + rustup_setup } comp_desktop() { @@ -392,24 +353,9 @@ comp_desktop() { # ----------------------------------------------------------------- main ---- preflight select_components -[ "${#SELECTED[@]}" -gt 0 ] || { info "Nothing selected."; exit 0; } -printf '\n Installing: %s\n' "${SELECTED[*]}" -[ "$DRY_RUN" = 1 ] || confirm "Continue?" y || { info "Cancelled."; exit 0; } - -# keep components in canonical order, run each in its own subshell so one failure does not stop the rest -OK=(); FAILED=() -for c in "${COMPONENTS[@]}"; do - [[ " ${SELECTED[*]} " == *" $c "* ]] || continue - # NOT inside `if (...)`: bash ignores `set -e` in a condition context, which would let a failed apt run - # carry on and be reported as success. A plain subshell followed by $? keeps errexit active inside it. - ( set -e; "comp_$c" ) - rc=$? - if [ "$rc" -eq 0 ]; then OK+=("$c"); else FAILED+=("$c"); fail "component failed (exit $rc): $c"; fi -done +menu_confirm || { info "Cancelled."; exit 0; } -printf '\n' -[ ${#OK[@]} -gt 0 ] && ok "Done: ${OK[*]}" -[ ${#FAILED[@]} -gt 0 ] && fail "Failed: ${FAILED[*]} (run again with --only ${FAILED[*]// /,} after fixing the cause)" +run_selected if [ "$DRY_RUN" != 1 ] && [ ${#OK[@]} -gt 0 ]; then [[ " ${OK[*]} " == *" shell "* ]] && info "Open a new terminal, or run: exec zsh" [[ " ${OK[*]} " == *" desktop "* ]] && info "Log out and choose the Hyprland session." diff --git a/linux/uninstall.sh b/linux/uninstall.sh index bc01f35..9a1b029 100755 --- a/linux/uninstall.sh +++ b/linux/uninstall.sh @@ -43,7 +43,7 @@ while IFS=$'\t' read -r dst src backup; do else warn "left alone (changed since install): ${dst/#$HOME/~}"; skipped=$((skipped+1)) fi -done < <(tac "$MANIFEST") +done < <(awk '{ l[NR] = $0 } END { for (i = NR; i > 0; i--) print l[i] }' "$MANIFEST") # tac is not on macOS if [ "$DRY_RUN" != 1 ]; then rm -f "$MANIFEST"; rmdir "$STATE_DIR" 2>/dev/null || true; fi printf '\n'; ok "$removed link(s) removed, $restored original file(s) restored, $skipped skipped." @@ -60,3 +60,9 @@ cat <), the font in ~/Library/Fonts, + Homebrew itself (see https://github.com/homebrew/install#uninstall-homebrew) +MSG +fi diff --git a/macos/install.sh b/macos/install.sh new file mode 100755 index 0000000..9a52d54 --- /dev/null +++ b/macos/install.sh @@ -0,0 +1,260 @@ +#!/usr/bin/env bash +# macOS installer (beta): the terminal setup and the developer tools, split into components you pick from a menu. +# +# ./install.sh interactive menu +# ./install.sh --all every component that works on macOS +# ./install.sh --only shell,nvim +# ./install.sh --dry-run show what would happen, change nothing +# ./install.sh --yes no questions; answer each with its default +# ./install.sh --list list the components +# +# Normally started through the top-level install.sh (which also fetches the repository). +# Written for the bash 3.2 that ships with macOS: no associative arrays, no bare "${a[@]}" of an empty array. +# Packages come from Homebrew, which is installed first when it is missing. The Linux-only parts (the Hyprland +# desktop, the Microsoft 365 web-app launchers) are listed in the menu but cannot be selected. +set -uo pipefail + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +export DOTFILES_DIR="${DOTFILES_DIR:-$(cd "$HERE/.." && pwd)}" +# shellcheck source=../lib/common.sh +. "$DOTFILES_DIR/lib/common.sh" +# shellcheck source=../lib/menu.sh +. "$DOTFILES_DIR/lib/menu.sh" +# shellcheck source=../lib/parts.sh +. "$DOTFILES_DIR/lib/parts.sh" + +C="$DOTFILES_DIR/config" +ARCH="$(uname -m)" + +# shellcheck disable=SC2034 # MENU_* are read by lib/menu.sh +MENU_TITLE="macOS (beta)" +MENU_ITEMS=(base shell tmux nvim terminal browsers web rust) +# shellcheck disable=SC2034 +MENU_DEFAULTS=(base shell tmux nvim terminal) +# shellcheck disable=SC2034 +MENU_OFF=( + "desktop|Hyprland + Caelestia desktop (Linux only)" + "apps|Microsoft 365 web-app launchers, Thunar menu (Linux only; on a Mac install Office from the App Store)" +) + +menu_desc() { + case "$1" in + base) echo "Homebrew, git, ripgrep, fzf, eza, zoxide and the JetBrainsMono Nerd Font" ;; + shell) echo "zsh + Oh My Zsh + starship prompt" ;; + tmux) echo "tmux with a cyan/Dracula theme" ;; + nvim) echo "Neovim (NvChad) with tree-sitter parsers and LSP servers" ;; + terminal) echo "Ghostty + its config (Vim-style keys, dark theme)" ;; + browsers) echo "Terminal browser: w3m (elinks and Browsh are Linux only: Homebrew has no elinks)" ;; + web) echo "Web development: PHP, Composer, MariaDB, phpMyAdmin (services stay off unless you say yes)" ;; + rust) echo "Rust toolchain via rustup (rustc, cargo, clippy, rustfmt, rust-analyzer)" ;; + esac +} + +menu_status() { + case "$1" in + base) find_brew && have rg && have fzf && font_present && echo installed ;; + shell) linked_here "$HOME/.zshrc" && [ -d "$HOME/.oh-my-zsh" ] && echo installed ;; + tmux) linked_here "$HOME/.config/tmux/tmux.conf" && have tmux && echo installed ;; + nvim) linked_here "$HOME/.config/nvim/lua/plugins/dotfiles.lua" && have nvim && echo installed ;; + terminal) linked_here "$HOME/.config/ghostty/config.ghostty" && ghostty_app >/dev/null && echo installed ;; + browsers) have w3m && echo installed ;; + web) have php && { have mariadb || have mysql; } && echo installed ;; + rust) { have rustup || [ -x "$HOME/.cargo/bin/rustup" ]; } && echo installed ;; + esac + return 0 +} + +menu_note() { + case "$1" in + nvim) echo "about 500 MB (Node.js, parsers and LSP servers), 5-15 minutes" ;; + web) echo "about 400 MB; MariaDB stays off unless you say yes" ;; + rust) echo "about 1 GB (toolchain in ~/.rustup)" ;; + esac + return 0 +} + +usage() { sed -n '2,12p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; } + +# ------------------------------------------------------------ arguments ---- +ALL=0; ONLY="" +while [ $# -gt 0 ]; do + case "$1" in + -y|--yes) ASSUME_YES=1 ;; + -n|--dry-run) DRY_RUN=1 ;; + --all) ALL=1 ;; + --only) shift; ONLY="${1:-}" ;; + --only=*) ONLY="${1#--only=}" ;; + --list) menu_list; exit 0 ;; + -h|--help) usage; exit 0 ;; + *) die "unknown option: $1 (try --help)" ;; + esac + shift +done +export DRY_RUN ASSUME_YES + +# ------------------------------------------------------------- Homebrew ---- +BREW="" +find_brew() { + local b + for b in /opt/homebrew/bin/brew /usr/local/bin/brew; do [ -x "$b" ] && { BREW="$b"; return 0; }; done + have brew && { BREW="$(command -v brew)"; return 0; } + return 1 +} + +ensure_brew() { + if find_brew; then eval "$("$BREW" shellenv)"; return 0; fi + if [ "$DRY_RUN" = 1 ]; then info "[dry-run] would install Homebrew (brew.sh)"; BREW=brew; return 0; fi + info "installing Homebrew from brew.sh (it asks for your password, and installs the Xcode Command Line Tools if needed)" + local script; script="$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" \ + || { fail "could not download the Homebrew installer"; return 1; } + if interactive && [ "$ASSUME_YES" != 1 ]; then /bin/bash -c "$script" <"$DOTFILES_TTY" + else NONINTERACTIVE=1 /bin/bash -c "$script" /dev/null 2>&1; } +brew_has_cask() { [ -n "$BREW" ] && "$BREW" list --cask "$1" >/dev/null 2>&1; } + +# brew_need formula... install what is missing (fails if something cannot be installed) +brew_need() { + local missing=() f + ensure_brew || return 1 + for f in "$@"; do brew_has "$f" || missing+=("$f"); done + if [ ${#missing[@]} -eq 0 ]; then ok "already installed: $*"; return 0; fi + info "brew install: ${missing[*]}" + run "$BREW" install "${missing[@]}" +} + +# brew_optional formula... try each one; warn instead of failing +brew_optional() { + local f + ensure_brew || return 1 + for f in "$@"; do + brew_has "$f" && continue + if [ "$DRY_RUN" = 1 ]; then info "[dry-run] would try: brew install $f"; continue; fi + "$BREW" install "$f" >/dev/null 2>&1 && ok "installed $f" || warn "not available or could not install, skipped: $f" + done + return 0 +} + +# cask_need cask (apps and fonts) +cask_need() { + ensure_brew || return 1 + if brew_has_cask "$1"; then ok "already installed: $1"; return 0; fi + info "brew install --cask $1" + run "$BREW" install --cask "$1" +} + +font_present() { ls "$HOME"/Library/Fonts/JetBrainsMonoNerd* /Library/Fonts/JetBrainsMonoNerd* >/dev/null 2>&1; } +ghostty_app() { + local a + for a in /Applications/Ghostty.app "$HOME/Applications/Ghostty.app"; do [ -d "$a" ] && { printf '%s' "$a"; return 0; }; done + return 1 +} + +# ------------------------------------------------------------ preflight ---- +preflight() { + [ "$(uname -s)" = Darwin ] || die "This installer is for macOS (use linux/install.sh on Linux)." + [ "$(id -u)" -ne 0 ] || die "Run this as your normal user, not root: Homebrew refuses to run as root." + case "$ARCH" in arm64|x86_64) ;; *) die "unsupported CPU: $ARCH" ;; esac + [ "$DRY_RUN" = 1 ] && info "DRY RUN: nothing will be changed." + ensure_local_bin_in_path +} + +# ----------------------------------------------------------- components ---- +comp_base() { + step "Base: Homebrew, command-line tools, Nerd Font" + ensure_brew || return 1 + brew_need git curl wget jq fzf ripgrep + brew_optional eza zoxide fastfetch btop htop + if font_present; then ok "font already present: JetBrainsMono Nerd Font" + else cask_need font-jetbrains-mono-nerd-font; fi +} + +comp_shell() { + step "Shell: zsh + Oh My Zsh + starship" + brew_need starship git + shell_configs + # zsh has been the macOS default since Catalina; offer it only if this account was switched to something else + if [ "$(basename "${SHELL:-}")" != zsh ] && confirm "Make zsh your default login shell?" n; then + run chsh -s /bin/zsh + fi +} + +comp_tmux() { + step "tmux" + brew_need tmux + backup_and_link "$C/tmux/tmux.conf" "$HOME/.config/tmux/tmux.conf" +} + +comp_nvim() { + step "Neovim (NvChad)" + brew_need neovim node ripgrep git + # tree-sitter CLI: nvim-treesitter builds the parsers with it (Homebrew calls the CLI formula tree-sitter-cli) + brew_optional tree-sitter-cli + if ! have tree-sitter && [ ! -x "$HOME/.local/bin/tree-sitter" ]; then + local t=x64; [ "$ARCH" = arm64 ] && t=arm64 + info "installing the tree-sitter CLI into ~/.local/bin" + if [ "$DRY_RUN" = 1 ]; then info "[dry-run] would download tree-sitter-macos-$t.gz" + else + local tmp; tmp="$(mktemp)" + fetch "https://github.com/tree-sitter/tree-sitter/releases/latest/download/tree-sitter-macos-$t.gz" "$tmp" || return 1 + mkdir -p "$HOME/.local/bin" && gunzip -c "$tmp" >"$HOME/.local/bin/tree-sitter" && chmod +x "$HOME/.local/bin/tree-sitter" && rm -f "$tmp" + fi + fi + nvchad_setup +} + +comp_terminal() { + step "Terminal: Ghostty" + # Ghostty on macOS reads ~/.config/ghostty/config.ghostty as well as its ~/Library location + backup_and_link "$C/ghostty/config.ghostty" "$HOME/.config/ghostty/config.ghostty" + if ghostty_app >/dev/null; then ok "Ghostty already installed: $(ghostty_app)" + else cask_need ghostty; fi + info "Open Ghostty from Launchpad or Spotlight; it starts zsh with this setup." +} + +comp_browsers() { + step "Terminal browser: w3m" + brew_need w3m + info "elinks and Browsh are set up on Linux only (Homebrew has no elinks package)." + info "use it with: w3m https://example.com (q quits)" +} + +comp_web() { + step "Web development: PHP, Composer, MariaDB, phpMyAdmin" + brew_need php composer mariadb + brew_optional phpmyadmin + if [ "$DRY_RUN" = 1 ]; then info "[dry-run] would offer to start MariaDB" + elif confirm "Start MariaDB now and at every login (brew services)?" n; then + "$BREW" services start mariadb || warn "could not start MariaDB; start it with: brew services start mariadb" + else + info "MariaDB was not started. Start it with: brew services start mariadb" + fi + info "MariaDB lets your macOS user in without a password (run: mariadb). No password was set anywhere." + [ -n "$BREW" ] && info "phpMyAdmin: php -S localhost:8080 -t \"$("$BREW" --prefix 2>/dev/null)/share/phpmyadmin\" then open http://localhost:8080" + return 0 +} + +comp_rust() { + step "Rust (rustup)" + rustup_setup +} + +# ----------------------------------------------------------------- main ---- +preflight +# shellcheck disable=SC2034 # read by run_selected (lib/menu.sh) +if [ "$ALL" = 1 ]; then SELECTED=("${MENU_ITEMS[@]}") +elif [ -n "$ONLY" ]; then menu_from_list "$ONLY" +else menu_choose; fi +[ "${#SELECTED[@]}" -gt 0 ] && ! find_brew && info "Homebrew is not installed yet: it is installed first (brew.sh, asks for your password once)." +menu_confirm || { info "Cancelled."; exit 0; } + +run_selected +if [ "$DRY_RUN" != 1 ] && [ ${#OK[@]} -gt 0 ]; then + in_list shell "${OK[@]}" && info "Open a new terminal window (or run: exec zsh) to load the new shell." + [ -d "$BACKUP_ROOT/$STAMP" ] && info "Files that were replaced are in ${BACKUP_ROOT/#$HOME/~}/$STAMP" +fi +[ ${#FAILED[@]} -eq 0 ] diff --git a/macos/uninstall.sh b/macos/uninstall.sh new file mode 100755 index 0000000..b5ece2a --- /dev/null +++ b/macos/uninstall.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# Undo what install.sh linked on macOS. The undo logic is the same as on Linux (it only reads the record of links +# the installer made), so this hands over to linux/uninstall.sh, which is kept bash 3.2 compatible for that reason. +# +# ./uninstall.sh interactive +# ./uninstall.sh --dry-run show what would happen +# ./uninstall.sh --yes no questions +set -uo pipefail +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +export DOTFILES_DIR="${DOTFILES_DIR:-$(cd "$HERE/.." && pwd)}" +exec "${BASH:-bash}" "$DOTFILES_DIR/linux/uninstall.sh" "$@" diff --git a/tests/menu.sh b/tests/menu.sh new file mode 100755 index 0000000..dfb2cf9 --- /dev/null +++ b/tests/menu.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# Drive the component checklist with scripted answers (DOTFILES_TTY = a file of answers) in dry-run mode and check +# what it picks. Runs with whatever bash starts it: CI runs it with /bin/bash 3.2 on macOS. +# +# tests/menu.sh +set -uo pipefail +REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +TMP="$(mktemp -d)"; trap 'rm -rf "$TMP"' EXIT +export HOME="$TMP/home" NO_COLOR=1; mkdir -p "$HOME" +fails=0 + +pick() { # pick "answers" "expected Will install line" [extra text that must appear] + printf '%b' "$1" >"$TMP/answers" + out="$(DOTFILES_TTY="$TMP/answers" "${BASH:-bash}" "$REPO/install.sh" --dry-run 2>&1)" + if printf '%s\n' "$out" | grep -qF "Will install: $2" && { [ -z "${3:-}" ] || printf '%s\n' "$out" | grep -qF "$3"; }; then + echo "ok answers=$(printf '%s' "$1" | tr '\n' ' ')-> $2" + else + echo "FAIL answers=$(printf '%s' "$1" | tr '\n' ' ')expected: $2 ${3:+/ $3}"; printf '%s\n' "$out" | tail -25; fails=$((fails+1)) + fi +} + +pick 'n\n2\n\n' 'shell' # none, then check #2 +pick 'n\n2 3,4\n\n' 'shell tmux nvim' # several numbers on one line, commas or spaces +pick 'n\ntmux\n\n' 'tmux' # by name +pick 'n\n2\n2\n3\n\n' 'tmux' # toggling twice unchecks +pick 'n\n99\n3\n\n' 'tmux' 'not a valid choice: 99' +if [ "$(uname -s)" = Darwin ]; then + pick 'n\ndesktop\n2\n\n' 'shell' 'desktop is not available here' + pick 'a\n\n' 'base shell tmux nvim terminal browsers web rust' 'skipped: desktop apps' + "${BASH:-bash}" "$REPO/install.sh" --dry-run --only desktop >/dev/null 2>&1 && { echo "FAIL --only desktop was accepted on macOS"; fails=$((fails+1)); } || echo "ok --only desktop refused on macOS" +fi +printf 'q\n' >"$TMP/answers" +DOTFILES_TTY="$TMP/answers" "${BASH:-bash}" "$REPO/install.sh" --dry-run 2>&1 | grep -q "Nothing installed" && echo "ok q quits" || { echo "FAIL q"; fails=$((fails+1)); } +[ "$fails" -eq 0 ] && echo "all menu checks passed" || { echo "$fails menu check(s) failed"; exit 1; }