diff --git a/.chezmoiroot b/.chezmoiroot new file mode 100644 index 00000000..5e72f75c --- /dev/null +++ b/.chezmoiroot @@ -0,0 +1 @@ +home diff --git a/CLAUDE.md b/CLAUDE.md index f8eabcc8..c13831c8 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -53,6 +53,8 @@ just add-machine NAME # Onboard + bootstrap an already-reachable machine via S - `kube` fetches `~/.kube/config` + `~/.talos/config` from Bitwarden — desktops only. - `ssh_keys` round-trips per-machine ed25519 keys through Bitwarden, building cross-machine `authorized_keys`. +After the playbook, `just apply*` runs bash **units** (`units/`, via mise through `scripts/run-units.sh`; see [`docs/src/units.md`](docs/src/units.md)): `omp`, `chezmoi`, `atuin`, `shell-ai`. `home/` is the chezmoi source for shell, git, neovim and atuin config; `ssh_keys` remains an Ansible role. + Roles are conditional on inventory group (`desktop`, `server`, `vps`, `test_fleet`, `termux_hosts`; `llm` is dormant/commented out) and on `skip_*` flags / `machine_profile` in `host_vars/`. See [`docs/src/roles/`](docs/src/roles/) for the full reference. ## Kubeconfig + Talos secrets diff --git a/CONTEXT.md b/CONTEXT.md index 872da52b..3ff8f72c 100644 --- a/CONTEXT.md +++ b/CONTEXT.md @@ -20,7 +20,8 @@ ## Playbook -- **Role** — an Ansible role that configures one concern (e.g. `shell_dotfiles`, `tailscale`, `proxy`). The interface is `tasks/main.yml`; the implementation is templates, files, and handlers. +- **Role** — an Ansible role that configures one concern (e.g. `tailscale`, `proxy`, `ssh_keys`). The interface is `tasks/main.yml`; the implementation is templates, files, and handlers. +- **Unit** — an idempotent bash script in `units/` run in parallel by mise after the playbook (`omp`, `chezmoi`, `atuin`, `shell-ai`). A failing step does not stop the rest of the unit. See `docs/src/units.md`. - **Phase** — a logical grouping of roles in `site.yml` (Phase 1: system + packages, Phase 2: secrets + auth, Phase 3: desktop, Phase 4: services). Phases are documented in comments, not enforced by the playbook. - **Tag** — an Ansible tag applied to roles so `just apply-tags ` can target a subset. diff --git a/Justfile b/Justfile index f10d5aaa..7e78944c 100644 --- a/Justfile +++ b/Justfile @@ -513,7 +513,7 @@ doctor: echo "→ Machine identity" if [ -f /etc/dotfiles-machine ]; then pass "machine: $(cat /etc/dotfiles-machine)"; else warn "/etc/dotfiles-machine missing — run bootstrap"; fi echo "→ Core binaries" - for bin in brew bw gh tailscale ansible-playbook just mise; do + for bin in brew bw gh tailscale ansible-playbook just mise chezmoi; do if command -v "$bin" >/dev/null 2>&1; then pass "$bin"; else warn "$bin missing"; fi done echo "→ Tailscale" diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 9f95290f..a7ea7562 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -61,13 +61,13 @@ ## Dotfiles - [shell_fonts](roles/shell_fonts.md) -- [shell_dotfiles](roles/shell_dotfiles.md) -- [shell_atuin](roles/shell_atuin.md) -- [shell_ai](roles/shell_ai.md) -- [omp](roles/omp.md) +- [shell_dotfiles (chezmoi)](roles/shell_dotfiles.md) +- [shell_atuin (chezmoi + unit)](roles/shell_atuin.md) +- [shell_ai (unit)](roles/shell_ai.md) +- [omp (unit)](roles/omp.md) - [hive_ops](roles/hive_ops.md) -- [git](roles/git.md) -- [neovim](roles/neovim.md) +- [git (chezmoi)](roles/git.md) +- [neovim (chezmoi)](roles/neovim.md) ## Secrets & Auth diff --git a/docs/src/architecture.md b/docs/src/architecture.md index 8bb6be1e..789bbeaa 100644 --- a/docs/src/architecture.md +++ b/docs/src/architecture.md @@ -63,13 +63,8 @@ Every role in `site.yml` carries one or more tags. Tags fall into two kinds: **p | System + packages | `termux_packages` | `system`, `packages`, `termux` | | System + packages | `bitwarden` | `secrets`, `bitwarden` | | System + packages | `shell_fonts` | `dotfiles`, `shell`, `fonts` | -| System + packages | `shell_dotfiles` | `dotfiles`, `shell`, `dotfiles` | | System + packages | `omp` unit | `dotfiles`, `omp` | | System + packages | `hive_ops` | `hive`, `hive_ops` | -| System + packages | `git` | `dotfiles`, `git` | -| System + packages | `neovim` | `dotfiles`, `neovim`, `editor` | -| Secrets + auth | `shell_atuin` | `dotfiles`, `shell`, `atuin`, `secrets` | -| Secrets + auth | `shell_ai` | `dotfiles`, `shell`, `ai`, `secrets` | | Secrets + auth | `ssh_keys` | `secrets`, `ssh`, `ssh_keys` | | Secrets + auth | `ssh_mesh` | `ssh`, `mesh_check` | | Secrets + auth | `github` | `secrets`, `github` | @@ -91,9 +86,11 @@ Every role in `site.yml` carries one or more tags. Tags fall into two kinds: **p | Services | `server_hardening` | `system`, `hardening`, `security` | | Services | `fleet_facts` | `facts`, `fleet_facts` | +Shell, git, neovim and atuin config, omp, and the shell/atuin secrets are not roles: they are [units](units.md) (`chezmoi`, `omp`, `atuin`, `shell-ai`) that `just apply*` runs after the playbook. The old tags `dotfiles`, `shell`, `git`, `neovim`, `editor`, `atuin`, `ai`, `secrets`, `omp` still select them through `scripts/run-units.sh`. + Common phase-wide selections: -- `--tags secrets` — every BW-backed role only (`bitwarden`, `shell_atuin`, `shell_ai`, `ssh_keys`, `github`, `tailscale`, `kube`, `forgejo_registry`) +- `--tags secrets` — every BW-backed role only (`bitwarden`, `ssh_keys`, `github`, `tailscale`, `kube`, `forgejo_registry`) - `--tags desktop` — every desktop-app role (`flatpak`, `bluefin_common`, `gnome`, `zen_browser`, `pipewire_audio`) - `--tags services` — every long-running service role (`syncthing`, `systemd`, `bst_dashboard`, `kirocrew`, `proxy`, `tailscale_cert`) - `--skip-tags secrets` — everything except BW-backed roles. `just apply-nosecrets`, bootstrap's first phase and `phone-apply.service` also pass `--no-secrets` to units. diff --git a/docs/src/bitwarden.md b/docs/src/bitwarden.md index 22c34724..f367cd9c 100644 --- a/docs/src/bitwarden.md +++ b/docs/src/bitwarden.md @@ -44,10 +44,10 @@ Every item a role or script reads (grep `bw get` / `bw list` under `roles/` and | `talosconfig-aws-migration` | Secure Note | `kube` role — AWS cluster Talos config | | `accounts.firefox.com` | Login + TOTP | `browser_fxa` role — Firefox Account credentials | | `github-token` | Login (password) | `github` role — PAT for `gh auth login` | -| `atuin.sh` | Login + custom field `key` | `shell_atuin` role — atuin sync login + mnemonic | -| `deepseek-api-key` | Login (password) | `shell_ai` role — pi `auth.json` | -| `forgejo` | Secure Note (`API Token:` line) | `shell_ai` role — `FORGEJO_TOKEN` | -| `tavily-api-key` | Login (password) | `shell_ai` role — `TAVILY_API_KEY` | +| `atuin.sh` | Login + custom field `key` | `atuin` unit — atuin sync login + mnemonic | +| `deepseek-api-key` | Login (password) | `shell-ai` unit — pi `auth.json` | +| `forgejo` | Secure Note (`API Token:` line) | `shell-ai` unit — `FORGEJO_TOKEN` | +| `tavily-api-key` | Login (password) | `shell-ai` unit — `TAVILY_API_KEY` | | `forgejo.manatee-basking.ts.net` | Login | `forgejo_registry` role — podman registry login | | `bw-api-key` | Login | `scripts/bw-resolve.sh` — `bw login --apikey` on a remote | | `James Bitwarden` | Login | `scripts/bw-resolve.sh` — non-interactive unlock on a remote | diff --git a/docs/src/index.md b/docs/src/index.md index bafdd4a9..68bae622 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -65,9 +65,9 @@ The `llm` group is dormant (commented out in `inventory.yml`). | Phase | Roles | Main tags | |-------|-------|-----------| -| 1 — System + packages + dotfiles | sudo, sshd, apk_packages, homebrew, termux_packages, bitwarden, shell_fonts, shell_dotfiles, hive_ops, git, neovim | `system`, `packages`, `dotfiles` | -| 2 — Secrets + auth | shell_atuin, shell_ai, ssh_keys, ssh_mesh, github, tailscale, kube, forgejo_registry | `secrets` | +| 1 — System + packages + dotfiles | sudo, sshd, apk_packages, homebrew, termux_packages, bitwarden, shell_fonts, hive_ops | `system`, `packages`, `dotfiles` | +| 2 — Secrets + auth | ssh_keys, ssh_mesh, github, tailscale, kube, forgejo_registry | `secrets` | | 3 — Desktop apps | flatpak, bluefin_common, gnome, zen_browser (+ browser_fxa), pipewire_audio | `desktop` | | 4 — Services | syncthing, systemd, bst_dashboard, kirocrew, proxy, tailscale_cert, server_hardening | `services` | -The [`omp` unit](units.md) runs after Ansible, and can also use Bitwarden for its broker token. `just apply-nosecrets` skips the secret-tagged roles and passes `--no-secrets` to units. +After the playbook, `just apply*` runs the [bash units](units.md) (`chezmoi`, `omp`, `atuin`, `shell-ai`) in parallel through mise. The `omp` unit also uses Bitwarden for its broker token. `just apply-nosecrets` skips secret-tagged roles and passes `--no-secrets` to units. diff --git a/docs/src/roles/git.md b/docs/src/roles/git.md index e49dd7ab..6b070e0f 100644 --- a/docs/src/roles/git.md +++ b/docs/src/roles/git.md @@ -1,20 +1,20 @@ # git +**Unit:** `chezmoi` (`units/chezmoi.sh`; see [Units](../units.md)) **Tags:** `dotfiles`, `git` **Secrets needed:** No -**Runs on:** All machines +**Runs on:** All machines with `chezmoi` installed -Deploys Git configuration with SSH commit signing. +Git configuration with SSH commit signing, as chezmoi-managed files under `home/`: -## What It Does - -1. Deploys `~/.gitconfig` with name, email, and signing preferences -2. Configures SSH commit signing using `~/.ssh/id_ed25519` -3. Deploys `~/.ssh/allowed_signers` for signature verification -4. Deploys GitHub CLI config at `~/.config/gh/config.yml` +| Target | Source | +|---|---| +| `~/.gitconfig` | `dot_gitconfig.tmpl` (data: `user_name`, `user_email`, `.chezmoi.homeDir`) | +| `~/.gitignore_global`, `~/.gitmessage` | `dot_gitignore_global`, `dot_gitmessage` | +| `~/.config/gh/config.yml` | `dot_config/gh/config.yml` | ## Key Settings - **Signing:** SSH (`ssh-ed25519` key) for all commits -- **Allowed signers:** All fleet machine keys from Bitwarden +- **Allowed signers:** `~/.ssh/allowed_signers` is written by the `ssh_keys` role from the fleet machine keys in Bitwarden - **Delta:** Git diff viewer (`git-delta`) for side-by-side diffs diff --git a/docs/src/roles/neovim.md b/docs/src/roles/neovim.md index 6c732d02..27393903 100644 --- a/docs/src/roles/neovim.md +++ b/docs/src/roles/neovim.md @@ -1,16 +1,11 @@ # neovim +**Unit:** `chezmoi` (`units/chezmoi.sh`; see [Units](../units.md)) **Tags:** `dotfiles`, `neovim`, `editor` **Secrets needed:** No -**Runs on:** All machines +**Runs on:** All machines with `chezmoi` installed -Deploys [Neovim](https://neovim.io/) configuration. - -## What It Does - -1. Creates `~/.config/nvim/` directory structure -2. Deploys init.lua and plugin configuration -3. Installs Lazy.nvim plugin manager and configured plugins +[Neovim](https://neovim.io/) configuration, deployed from `home/dot_config/nvim/` (`init.lua`, `lua/config/`, `lua/plugins/`) by chezmoi. `.chezmoiscripts/run_after_20-nvim-sync.sh` bootstraps lazy.nvim and runs `nvim --headless "+Lazy! sync" +qa` on first apply and when Lua configuration files are added, changed, or removed. It skips synchronization on unrelated applies; failures are best effort and retried on the next apply (300 s timeout, never fails the apply). The last successful fingerprint lives in `~/.cache/dotfiles/nvim-config.cksum`. ## Key Plugins diff --git a/docs/src/roles/omp.md b/docs/src/roles/omp.md index 76552a46..d419408f 100644 --- a/docs/src/roles/omp.md +++ b/docs/src/roles/omp.md @@ -36,7 +36,7 @@ The token is written only when Bitwarden is unlocked; a locked vault or `--no-se To seed the token, run `just seed-omp-broker` on a client with the vault unlocked and SSH access to `ubuntu@100.113.75.88`. It reads the live token over SSH without rotating it and creates or updates the Bitwarden item. `OMP_AUTH_BROKER_SSH` overrides the SSH destination. Then run `just apply-tags omp` on each client and verify with `omp auth-broker status`. If the token is rotated on spindle, seed again and re-apply on the clients. -`DEEPSEEK_API_KEY` and the other provider keys are exposed to the shell by the `shell_ai` role's secret files, not by this unit. +`DEEPSEEK_API_KEY` and the other provider keys are exposed to the shell by the [`shell-ai` unit](shell_ai.md)'s secret files, not by this unit. ## Kiro authentication diff --git a/docs/src/roles/shell_ai.md b/docs/src/roles/shell_ai.md index d771cc3a..08bd2988 100644 --- a/docs/src/roles/shell_ai.md +++ b/docs/src/roles/shell_ai.md @@ -1,22 +1,31 @@ # shell_ai +**Unit:** `shell-ai` (`units/shell-ai.sh`; see [Units](../units.md)) **Tags:** `dotfiles`, `shell`, `ai`, `secrets` -**Secrets needed:** Yes (`deepseek-api-key`, `forgejo`, `tavily-api-key`) +**Secrets needed:** Yes (`deepseek-api-key`, `forgejo`, `tavily-api-key`, `meta-api-key`, `kiro-api-key`) **Runs on:** All machines -Deploys API credentials for the AI coding tools and shell. +Writes API credentials for the AI coding tools and shell. ## What It Does -1. When `bw_unlocked`: - - `bw get password deepseek-api-key` → `DEEPSEEK_API_KEY` in shell secret files - - `bw get notes forgejo` (the `API Token:` line) → `FORGEJO_TOKEN` - - `bw get password tavily-api-key` → `TAVILY_API_KEY` - - Writes these exports to `~/.config/shell/secrets.sh` (bash/zsh) and - `~/.config/fish/conf.d/secrets.fish` (fish), both 0600 -2. Warns if `deepseek-api-key` is missing; Kiro OAuth remains a per-user OMP sign-in +With an unlocked vault (`bw_session`; never prompts): + +- `bw get password deepseek-api-key` → `DEEPSEEK_API_KEY` +- `bw get notes forgejo` (the `API Token:` line) → `FORGEJO_TOKEN` +- `bw get password tavily-api-key` → `TAVILY_API_KEY` +- `bw get notes meta-api-key` → `META_API_KEY` +- `bw get notes kiro-api-key` → `KIRO_API_KEY` + +The values present are written (single-quoted, 0600, via temp file + `mv`) to `~/.config/shell/secrets.sh` (bash/zsh; also exports `OMP_KIRO_CONFIG`) and `~/.config/fish/conf.d/secrets.fish` (fish; ends with a runtime Kiro key refresh from `/tmp/bw_session`). + +## Output lines + +- `SKIPPED: vault locked or --no-secrets` — files left untouched +- `warn: no secrets found in Bitwarden` — files left untouched ## Notes +- Not a chezmoi template on purpose: `bitwarden` template functions abort the whole template when one item is missing, while the unit writes whatever secrets exist. - A locked vault leaves any previously written secret files untouched — nothing is overwritten with empty values -- `TAVILY_API_KEY` is read by the `tavily-search` skill deployed by the [`omp`](omp.md) role +- `TAVILY_API_KEY` is read by the `tavily-search` skill deployed by the [`omp`](omp.md) unit diff --git a/docs/src/roles/shell_atuin.md b/docs/src/roles/shell_atuin.md index a6fef4d9..b3aaf0f0 100644 --- a/docs/src/roles/shell_atuin.md +++ b/docs/src/roles/shell_atuin.md @@ -1,5 +1,6 @@ # shell_atuin +**Units:** `chezmoi` (config file), `atuin` (`units/atuin.sh`; see [Units](../units.md)) **Tags:** `dotfiles`, `shell`, `atuin`, `secrets` **Secrets needed:** Yes, for login (`atuin.sh` from Bitwarden) **Runs on:** All machines @@ -8,18 +9,22 @@ Configures [Atuin](https://atuin.sh/) shell history and logs it in to sync. ## What It Does -1. Writes `~/.config/atuin/config.toml` — sync to `api.atuin.sh` every 5m, - fuzzy search, `filter_mode = "global"` (including the Up-arrow binding) -2. Checks `atuin status`; if already logged in, skips the Bitwarden round-trip -3. Otherwise, when `bw_unlocked`, reads the `atuin.sh` item (username, - password, and a custom field `key` holding the mnemonic) and runs - `atuin login -u … -p … -k …` -4. Runs a best-effort `atuin sync` -5. Imports existing shell history once (`atuin import auto`, sentinel - `~/.local/share/atuin/.history_imported`) +1. chezmoi writes `~/.config/atuin/config.toml` from `home/dot_config/atuin/config.toml` — sync to `api.atuin.sh` every 5m, fuzzy search, `filter_mode = "global"` (including the Up-arrow binding) +2. The `atuin` unit checks `atuin status`; if it shows `[Sync]` the Bitwarden round-trip is skipped +3. Otherwise, with an unlocked vault, it reads the `atuin.sh` item (username, password, custom field `key` holding the mnemonic). Atuin 18.24 or newer receives the password and key through its environment, not command-line arguments. The unit preserves the data directory and its history; a missing key can be written by Atuin during login. If a key already exists, the unit logs in only when `atuin key` matches the vault mnemonic, then runs a best-effort `atuin sync`. +4. Imports existing shell history once (`atuin import auto`, sentinel `~/.local/share/atuin/.history_imported`). If the version or existing key requires manual login, the unit also defers importing until that situation is resolved. + +## Output lines + +- `SKIPPED: atuin not installed` — install it (`core_brews`) +- `PARTIAL: atuin not logged in; vault locked` — `bw unlock`, then `just apply-tags atuin` +- `PARTIAL: Bitwarden item atuin.sh missing or incomplete` — fix the item (needs username, password, field `key`) +- `PARTIAL: atuin 18.24 or newer required` — upgrade Atuin or run `atuin login` interactively; older versions do not support the non-interactive environment credentials +- `PARTIAL: cannot determine the Atuin key path`, `cannot read the existing Atuin key`, or `existing Atuin key differs` — inspect the local data and resolve the key situation before logging in interactively. A vault key in a different representation can also compare unequal. ## Notes -- Skips login with a warning if the `atuin` binary is missing (it's in `core_brews`) -- The mnemonic is passed to `atuin login -k`, never written to `~/.local/share/atuin/key` directly — atuin expects that file to be a binary key +- Existing keys are never deleted or replaced by the unit; it does not reset `~/.local/share/atuin` when the key is missing. That directory is kept mode 0700, including after a prior history import. +- Atuin 18.24 resolves its own configured key path, including the newer `~/.atuin/data` layout; the history-import sentinel remains in `~/.local/share/atuin`. +- Alpine's community `atuin` package may be an older offline-only build; sync requires a sync-enabled Atuin client. Check the package version and features before attempting a manual login. - Don't set the Up-arrow filter to `session`: it looks broken in fresh terminals diff --git a/docs/src/roles/shell_dotfiles.md b/docs/src/roles/shell_dotfiles.md index 12c9eb6f..e86b6a4d 100644 --- a/docs/src/roles/shell_dotfiles.md +++ b/docs/src/roles/shell_dotfiles.md @@ -1,20 +1,31 @@ # shell_dotfiles -**Tags:** `dotfiles`, `shell` +**Unit:** `chezmoi` (`units/chezmoi.sh`; see [Units](../units.md)) +**Tags:** `dotfiles`, `shell` (`just apply-tags shell` runs `unit:chezmoi`) **Secrets needed:** No -**Runs on:** All machines +**Runs on:** All machines with `chezmoi` installed (`core_brews`) -Deploys the shell, prompt, multiplexer and terminal configs. +No longer an Ansible role: the shell, prompt, multiplexer and terminal configs are chezmoi-managed files under `home/` (`.chezmoiroot` points chezmoi there). -## What It Does +## Files (source in `home/`) -1. `~/.config/shell/aliases.sh` (bash/zsh) and `~/.config/fish/conf.d/aliases.fish` — shared aliases, templated -2. `~/.bashrc`, `~/.zshrc`, `~/.zprofile` — templated -3. `~/.config/fish/config.fish`, `~/.tmux.conf`, `~/.inputrc`, `~/.config/starship.toml` — copied as-is -4. Ghostty terminfo: ships `xterm-ghostty.terminfo` and compiles it into - `~/.terminfo` with `tic`, so SSH sessions from Ghostty don't break - ncurses apps. Warns and skips if `tic` isn't installed. -5. `~/.config/ghostty/config` — auto light/dark following GNOME (inert on headless hosts) +| Target | Source | +|---|---| +| `~/.bashrc`, `~/.zshrc`, `~/.inputrc`, `~/.tmux.conf` | `dot_bashrc`, `dot_zshrc`, `dot_inputrc`, `dot_tmux.conf` | +| `~/.zprofile` | `dot_zprofile.tmpl` (flatpak path on desktops) | +| `~/.config/shell/aliases.sh` | `dot_config/shell/aliases.sh.tmpl` | +| `~/.config/fish/conf.d/aliases.fish` | `dot_config/fish/conf.d/aliases.fish.tmpl` | +| `~/.config/fish/config.fish`, `~/.config/starship.toml` | `dot_config/fish/config.fish`, `dot_config/starship.toml` | +| `~/.config/ghostty/config` | `dot_config/ghostty/config` (inert on headless hosts) | +| `~/.config/shell/ghostty.terminfo` | `dot_config/shell/ghostty.terminfo`, compiled into `~/.terminfo` by `.chezmoiscripts/run_after_10-ghostty-terminfo.sh` (skips if `tic` is missing) so SSH from Ghostty keeps ncurses apps working | + +## Template data + +`home/.chezmoi.toml.tmpl` (regenerated by `apply --init`) provides `machine` and `is_desktop` (from `DOTFILES_MACHINE` / `DOTFILES_IS_DESKTOP`, exported by the unit; `is_desktop` is true when the host is in the `desktop` group of `inventory.yml`). `home/.chezmoidata.yaml` provides `user_name`, `user_email` and `alias_groups`, the single source for the bash/zsh and fish aliases. Add a plain alias there, not in the templates. + +## Adding a file + +Put it under `home/` using chezmoi names: `dot_` for a leading dot, `.tmpl` suffix to template it, `private_`/`executable_` for modes. chezmoi never deletes files it does not manage. ## Fleet aliases @@ -28,5 +39,5 @@ In fish these are abbreviations (`abbr`), not aliases. ## Notes -- Shell secrets (`~/.config/shell/secrets.sh`, `~/.config/fish/conf.d/secrets.fish`) are written by [`shell_ai`](shell_ai.md), not here -- Source files: `roles/shell_dotfiles/files/` and `roles/shell_dotfiles/templates/` +- Shell secrets (`~/.config/shell/secrets.sh`, `~/.config/fish/conf.d/secrets.fish`) are written by the [`shell-ai` unit](shell_ai.md), not here. +- One failing file does not stop the rest: the unit runs `chezmoi apply --init --keep-going --force --no-tty`. This overwrites managed files even when locally edited; back up and review existing dotfiles before adoption. See [Units](../units.md#chezmoi). diff --git a/docs/src/units.md b/docs/src/units.md index 7f98e8ea..738720b3 100644 --- a/docs/src/units.md +++ b/docs/src/units.md @@ -39,3 +39,16 @@ Bootstrap runs all units after its first Ansible pass and the `omp,secrets` sele `tests/test_units_registry.py` fails if the three places disagree. `units/`, `mise.toml` and `scripts/run-units.sh` are executed by the fleet timer, so they are intentionally not carved out of CODEOWNERS. + +## chezmoi + +File-based config (shell, git, neovim, atuin config) is managed by [chezmoi](https://www.chezmoi.io/) from `home/` (`.chezmoiroot` contains `home`, so `.chezmoi.toml.tmpl`, `.chezmoidata.yaml` and `.chezmoiscripts/` live there). `unit:chezmoi` runs `chezmoi --source --keep-going --force --no-tty apply --init`: `--keep-going` continues past a failing file or script, `--force` overwrites like the old roles did, `--no-tty` avoids a TTY, and `--init` regenerates the config from `home/.chezmoi.toml.tmpl`. + +**Back up and review existing dotfiles before the first apply.** The current `--force` will overwrite local edits to managed targets without prompting on every apply. First adoption can replace an existing target even without `--force`: chezmoi does not treat a pre-existing, never-written target as a conflict by default. Review the intended changes with `chezmoi --source diff` (after initializing the config with the correct machine data), and keep a private backup of any local files you need. A safer future cutover should detect both first-adoption and later local conflicts and fail closed for unattended runs, with interactive review; merely removing `--force` does not protect initial adoption. + +If chezmoi is absent the unit fails rather than reporting a successful skip. Homebrew hosts can run `just packages`, Termux users can run `pkg install chezmoi`, and Alpine/postmarketOS users can enable the community repository and run `sudo apk add chezmoi`. + +- Data: `machine`, `is_desktop` (true when the host is in the `desktop` group of `inventory.yml`), `user_name`, `user_email`, `alias_groups`. +- Secrets are **not** chezmoi templates: the `bitwarden` template function aborts a whole template on a missing item. The `shell-ai` and `atuin` units handle secrets. +- chezmoi never deletes files it does not manage (no `exact_` directories). +- `ssh_keys` stays an Ansible role (key lifecycle, `authorized_keys` merge). diff --git a/group_vars/all.yml b/group_vars/all.yml index 11fa59d4..c9ce5be9 100644 --- a/group_vars/all.yml +++ b/group_vars/all.yml @@ -83,6 +83,7 @@ core_brews: - direnv - just - mise + - chezmoi - make - shellcheck - shfmt @@ -128,7 +129,6 @@ desktop_brews: - ansible - block-goose-cli - charmbracelet/tap/crush - - chezmoi - cmake - cosign - curl diff --git a/home/.chezmoi.toml.tmpl b/home/.chezmoi.toml.tmpl new file mode 100644 index 00000000..02c862b3 --- /dev/null +++ b/home/.chezmoi.toml.tmpl @@ -0,0 +1,4 @@ +{{- $machine := env "DOTFILES_MACHINE" | default .chezmoi.hostname -}} +[data] + machine = {{ $machine | quote }} + is_desktop = {{ eq (env "DOTFILES_IS_DESKTOP") "1" }} diff --git a/roles/shell_dotfiles/vars/main.yml b/home/.chezmoidata.yaml similarity index 96% rename from roles/shell_dotfiles/vars/main.yml rename to home/.chezmoidata.yaml index 9d89d4f5..c4552e53 100644 --- a/roles/shell_dotfiles/vars/main.yml +++ b/home/.chezmoidata.yaml @@ -1,12 +1,15 @@ --- # Single source of truth for aliases shared across bash/zsh and fish. -# Rendered into aliases.sh.j2 (POSIX `alias`) and aliases.fish.j2 (fish +# Rendered into aliases.sh.tmpl (POSIX `alias`) and aliases.fish.tmpl (fish # `abbr`, or `alias` where fish_kind: alias is set — e.g. vim→nvim, where # an abbreviation's visual text-expansion would be surprising). # # Entries requiring shell-specific syntax (&&/`; and`, ${VAR:-default}, # heredocs, etc.) are NOT here — they're hand-written once per shell # template instead, clearly marked, to avoid subtle translation bugs. +user_name: James Reilly +user_email: jreilly1821@gmail.com + alias_groups: - guard: nvim fish_kind: alias diff --git a/home/.chezmoiscripts/run_after_10-ghostty-terminfo.sh b/home/.chezmoiscripts/run_after_10-ghostty-terminfo.sh new file mode 100755 index 00000000..a36fdba3 --- /dev/null +++ b/home/.chezmoiscripts/run_after_10-ghostty-terminfo.sh @@ -0,0 +1,6 @@ +#!/bin/sh +# Compile the xterm-ghostty terminfo so SSH sessions from Ghostty keep working ncurses apps. +src="$HOME/.config/shell/ghostty.terminfo"; out="$HOME/.terminfo/x/xterm-ghostty" +command -v tic >/dev/null 2>&1 || { echo "tic not installed; skipping xterm-ghostty terminfo"; exit 0; } +[ -f "$out" ] && [ "$out" -nt "$src" ] && exit 0 +tic -x -o "$HOME/.terminfo" "$src" diff --git a/home/.chezmoiscripts/run_after_20-nvim-sync.sh b/home/.chezmoiscripts/run_after_20-nvim-sync.sh new file mode 100755 index 00000000..cf86e7f6 --- /dev/null +++ b/home/.chezmoiscripts/run_after_20-nvim-sync.sh @@ -0,0 +1,17 @@ +#!/bin/sh +# Best effort: retry bootstrap/sync until it succeeds, then only on config changes. +command -v nvim >/dev/null 2>&1 || exit 0 +config="$HOME/.config/nvim" +[ -d "$config" ] || exit 0 +lazy="$HOME/.local/share/nvim/lazy/lazy.nvim" +state="$HOME/.cache/dotfiles/nvim-config.cksum" +# Include names as well as contents so additions, deletions and renames count. +fingerprint=$(find "$config" -type f -name '*.lua' -exec cksum {} \; | LC_ALL=C sort | cksum) +if [ -d "$lazy" ] && [ -f "$state" ] && [ "$(cat "$state")" = "$fingerprint" ]; then + exit 0 +fi +[ -d "$lazy" ] || git clone --filter=blob:none https://github.com/folke/lazy.nvim.git --branch=stable "$lazy" || exit 0 +if timeout 300 nvim --headless "+Lazy! sync" +qa; then + mkdir -p "$(dirname "$state")" && printf '%s\n' "$fingerprint" > "$state" +fi +exit 0 diff --git a/roles/shell_dotfiles/templates/bashrc.j2 b/home/dot_bashrc similarity index 100% rename from roles/shell_dotfiles/templates/bashrc.j2 rename to home/dot_bashrc diff --git a/home/dot_config/atuin/config.toml b/home/dot_config/atuin/config.toml new file mode 100644 index 00000000..62e781a3 --- /dev/null +++ b/home/dot_config/atuin/config.toml @@ -0,0 +1,18 @@ +sync_address = "https://api.atuin.sh" +sync_frequency = "5m" +auto_sync = true +search_mode = "fuzzy" +# ── filter_mode ────────────────────────────────────────────── +# "global" = search all history across every machine & session. +# "host" = only this machine. +# "session" = only this shell session (empty in fresh terminals!). +# ────────────────────────────────────────────────────────────── +# DO NOT set filter_mode_shell_up_key_binding to "session" — +# it makes the Up arrow appear broken in fresh terminals because +# a new shell session has no commands yet. +# ────────────────────────────────────────────────────────────── +filter_mode = "global" +filter_mode_shell_up_key_binding = "global" +style = "compact" +show_preview = true +max_preview_height = 4 diff --git a/roles/shell_dotfiles/templates/aliases.fish.j2 b/home/dot_config/fish/conf.d/aliases.fish.tmpl similarity index 60% rename from roles/shell_dotfiles/templates/aliases.fish.j2 rename to home/dot_config/fish/conf.d/aliases.fish.tmpl index 4bd92743..57a281c2 100644 --- a/roles/shell_dotfiles/templates/aliases.fish.j2 +++ b/home/dot_config/fish/conf.d/aliases.fish.tmpl @@ -1,27 +1,28 @@ -# Fish abbreviations + aliases — managed by Ansible +# Fish abbreviations + aliases — managed by chezmoi # Abbreviations expand visually on space so you see (and learn) the real command. # NOTE: bluefin-cli init handles: eza/ll/ls, bat/cat, ugrep/grep, # zoxide/cd, starship, atuin, fzf — do NOT duplicate those here # -# Most of this file is generated from roles/shell_dotfiles/vars/main.yml, +# Most of this file is generated from home/.chezmoidata.yaml, # the single source shared with the bash/zsh aliases template. To add/change # a plain alias, edit that file, not this one — it'll show up in both # shells. Only shell-specific-syntax aliases are hand-written below. -{% for group in alias_groups %} -{% if group.guard is defined %} -if command -q {{ group.guard }} -{% endif %} -{% for a in group.aliases %} -{% if group.fish_kind is defined and group.fish_kind == 'alias' %} -{{ ' ' if group.guard is defined else '' }}alias {{ a.name }} {{ a.cmd }} -{% else %} -{{ ' ' if group.guard is defined else '' }}abbr --add {{ a.name }} '{{ a.cmd }}' -{% endif %} -{% endfor %} -{% if group.guard is defined %} +{{- range $group := .alias_groups }} +{{- $guarded := hasKey $group "guard" }} +{{- if $guarded }} +if command -q {{ $group.guard }} +{{- end }} +{{- range $a := $group.aliases }} +{{- if and (hasKey $group "fish_kind") (eq $group.fish_kind "alias") }} +{{ if $guarded }} {{ end }}alias {{ $a.name }} {{ $a.cmd }} +{{- else }} +{{ if $guarded }} {{ end }}abbr --add {{ $a.name }} '{{ $a.cmd }}' +{{- end }} +{{- end }} +{{- if $guarded }} end -{% endif %} -{% endfor %} +{{- end }} +{{- end }} # ── Shell-specific aliases (syntax differs from bash/zsh; not in the shared source) ── abbr --add dots 'cd ~/.local/share/dotfiles; and just apply-nosecrets' abbr --add dots-apply 'cd ~/.local/share/dotfiles; and git pull --ff-only; and just apply' diff --git a/roles/shell_dotfiles/files/config.fish b/home/dot_config/fish/config.fish similarity index 100% rename from roles/shell_dotfiles/files/config.fish rename to home/dot_config/fish/config.fish diff --git a/roles/git/files/gh_config.yml b/home/dot_config/gh/config.yml similarity index 100% rename from roles/git/files/gh_config.yml rename to home/dot_config/gh/config.yml diff --git a/roles/shell_dotfiles/files/ghostty.config b/home/dot_config/ghostty/config similarity index 76% rename from roles/shell_dotfiles/files/ghostty.config rename to home/dot_config/ghostty/config index d51bef8d..e8ca9c01 100644 --- a/roles/shell_dotfiles/files/ghostty.config +++ b/home/dot_config/ghostty/config @@ -1,4 +1,4 @@ -# Managed by Ansible (roles/shell_dotfiles). Edits here are overwritten on apply. +# Managed by chezmoi (home/dot_config/ghostty/config). Edits here are overwritten on apply. # # Follow the GNOME light/dark preference automatically. Ghostty watches the # desktop color-scheme and swaps between the two themes live — no restart. diff --git a/roles/neovim/files/nvim/init.lua b/home/dot_config/nvim/init.lua similarity index 100% rename from roles/neovim/files/nvim/init.lua rename to home/dot_config/nvim/init.lua diff --git a/roles/neovim/files/nvim/lua/config/autocmds.lua b/home/dot_config/nvim/lua/config/autocmds.lua similarity index 100% rename from roles/neovim/files/nvim/lua/config/autocmds.lua rename to home/dot_config/nvim/lua/config/autocmds.lua diff --git a/roles/neovim/files/nvim/lua/config/keymaps.lua b/home/dot_config/nvim/lua/config/keymaps.lua similarity index 100% rename from roles/neovim/files/nvim/lua/config/keymaps.lua rename to home/dot_config/nvim/lua/config/keymaps.lua diff --git a/roles/neovim/files/nvim/lua/config/options.lua b/home/dot_config/nvim/lua/config/options.lua similarity index 100% rename from roles/neovim/files/nvim/lua/config/options.lua rename to home/dot_config/nvim/lua/config/options.lua diff --git a/roles/neovim/files/nvim/lua/plugins/coding.lua b/home/dot_config/nvim/lua/plugins/coding.lua similarity index 100% rename from roles/neovim/files/nvim/lua/plugins/coding.lua rename to home/dot_config/nvim/lua/plugins/coding.lua diff --git a/roles/neovim/files/nvim/lua/plugins/colorscheme.lua b/home/dot_config/nvim/lua/plugins/colorscheme.lua similarity index 100% rename from roles/neovim/files/nvim/lua/plugins/colorscheme.lua rename to home/dot_config/nvim/lua/plugins/colorscheme.lua diff --git a/roles/neovim/files/nvim/lua/plugins/editor.lua b/home/dot_config/nvim/lua/plugins/editor.lua similarity index 100% rename from roles/neovim/files/nvim/lua/plugins/editor.lua rename to home/dot_config/nvim/lua/plugins/editor.lua diff --git a/roles/neovim/files/nvim/lua/plugins/lsp.lua b/home/dot_config/nvim/lua/plugins/lsp.lua similarity index 100% rename from roles/neovim/files/nvim/lua/plugins/lsp.lua rename to home/dot_config/nvim/lua/plugins/lsp.lua diff --git a/roles/neovim/files/nvim/lua/plugins/ui.lua b/home/dot_config/nvim/lua/plugins/ui.lua similarity index 100% rename from roles/neovim/files/nvim/lua/plugins/ui.lua rename to home/dot_config/nvim/lua/plugins/ui.lua diff --git a/roles/shell_dotfiles/templates/aliases.sh.j2 b/home/dot_config/shell/aliases.sh.tmpl similarity index 72% rename from roles/shell_dotfiles/templates/aliases.sh.j2 rename to home/dot_config/shell/aliases.sh.tmpl index 03cf2028..34e2b919 100644 --- a/roles/shell_dotfiles/templates/aliases.sh.j2 +++ b/home/dot_config/shell/aliases.sh.tmpl @@ -2,21 +2,22 @@ # NOTE: bluefin-cli init handles: eza/ll/ls, bat/cat, ugrep/grep, # zoxide/cd, starship, atuin, fzf — do NOT duplicate those here # -# Most of this file is generated from roles/shell_dotfiles/vars/main.yml, +# Most of this file is generated from home/.chezmoidata.yaml, # the single source shared with the fish aliases template. To add/change # a plain alias, edit that file, not this one — it'll show up in both # shells. Only shell-specific-syntax aliases are hand-written below. -{% for group in alias_groups %} -{% if group.guard is defined %} -if command -v {{ group.guard }} &>/dev/null; then -{% endif %} -{% for a in group.aliases %} -{{ ' ' if group.guard is defined else '' }}alias {{ a.name }}='{{ a.cmd }}' -{% endfor %} -{% if group.guard is defined %} +{{- range $group := .alias_groups }} +{{- $guarded := hasKey $group "guard" }} +{{- if $guarded }} +if command -v {{ $group.guard }} &>/dev/null; then +{{- end }} +{{- range $a := $group.aliases }} +{{ if $guarded }} {{ end }}alias {{ $a.name }}='{{ $a.cmd }}' +{{- end }} +{{- if $guarded }} fi -{% endif %} -{% endfor %} +{{- end }} +{{- end }} # ── Shell-specific aliases (syntax differs from fish; not in the shared source) ── alias -- -='cd -' alias dots='cd ~/.local/share/dotfiles && just apply-nosecrets' @@ -26,9 +27,9 @@ alias reload='exec $SHELL -l' alias path='tr ":" "\n" <<< "$PATH"' alias myip='curl -s ifconfig.me && echo' alias please='sudo $(fc -ln -1)' -{% if is_desktop | bool %} +{{- if .is_desktop }} alias open='xdg-open' -{% endif %} +{{- end }} # ── Local secrets (not committed to git) ────────────────────────── [ -f ~/.config/shell/secrets.sh ] && source ~/.config/shell/secrets.sh diff --git a/roles/shell_dotfiles/files/xterm-ghostty.terminfo b/home/dot_config/shell/ghostty.terminfo similarity index 100% rename from roles/shell_dotfiles/files/xterm-ghostty.terminfo rename to home/dot_config/shell/ghostty.terminfo diff --git a/roles/shell_dotfiles/files/starship.toml b/home/dot_config/starship.toml similarity index 100% rename from roles/shell_dotfiles/files/starship.toml rename to home/dot_config/starship.toml diff --git a/roles/git/templates/gitconfig.j2 b/home/dot_gitconfig.tmpl similarity index 93% rename from roles/git/templates/gitconfig.j2 rename to home/dot_gitconfig.tmpl index 9ae3ce64..a28480d7 100644 --- a/roles/git/templates/gitconfig.j2 +++ b/home/dot_gitconfig.tmpl @@ -1,6 +1,6 @@ [user] - name = {{ user_name }} - email = {{ user_email }} + name = {{ .user_name }} + email = {{ .user_email }} signingkey = ~/.ssh/id_ed25519.pub [commit] @@ -11,7 +11,7 @@ format = ssh [gpg "ssh"] - allowedSignersFile = {{ ansible_facts['env']['HOME'] }}/.ssh/allowed_signers + allowedSignersFile = {{ .chezmoi.homeDir }}/.ssh/allowed_signers [init] defaultBranch = main diff --git a/roles/git/files/gitignore_global b/home/dot_gitignore_global similarity index 100% rename from roles/git/files/gitignore_global rename to home/dot_gitignore_global diff --git a/roles/git/files/gitmessage b/home/dot_gitmessage similarity index 100% rename from roles/git/files/gitmessage rename to home/dot_gitmessage diff --git a/roles/shell_dotfiles/files/inputrc b/home/dot_inputrc similarity index 100% rename from roles/shell_dotfiles/files/inputrc rename to home/dot_inputrc diff --git a/roles/shell_dotfiles/files/tmux.conf b/home/dot_tmux.conf similarity index 100% rename from roles/shell_dotfiles/files/tmux.conf rename to home/dot_tmux.conf diff --git a/roles/shell_dotfiles/templates/zprofile.j2 b/home/dot_zprofile.tmpl similarity index 84% rename from roles/shell_dotfiles/templates/zprofile.j2 rename to home/dot_zprofile.tmpl index a569c479..04395a0e 100644 --- a/roles/shell_dotfiles/templates/zprofile.j2 +++ b/home/dot_zprofile.tmpl @@ -1,5 +1,5 @@ [ -f /home/linuxbrew/.linuxbrew/bin/brew ] && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" -{% if is_desktop | bool %} +{{ if .is_desktop -}} export PATH="$HOME/.local/share/flatpak/exports/bin:$PATH" -{% endif %} +{{ end -}} [ -f "$HOME/.cargo/env" ] && . "$HOME/.cargo/env" diff --git a/roles/shell_dotfiles/templates/zshrc.j2 b/home/dot_zshrc similarity index 100% rename from roles/shell_dotfiles/templates/zshrc.j2 rename to home/dot_zshrc diff --git a/host_vars/himachal.yml b/host_vars/himachal.yml index aa7df1dd..453fb2fd 100644 --- a/host_vars/himachal.yml +++ b/host_vars/himachal.yml @@ -8,7 +8,6 @@ phone_apply_enabled: false web_services: [] mic_hardware_volume_pct: 75 -# Hive ops (rotation, watchdog, peak windows) now run as CronJobs inside the -# AWS cluster; himachal no longer drives the Hive. hive_ops removes its old -# user timers on the next apply. +# The AWS cluster was terminated; local Hive ops remain disabled. +# The role removes any old local timers on the next apply. hive_ops_enabled: false diff --git a/mise.toml b/mise.toml index cb4032f2..267f10f8 100644 --- a/mise.toml +++ b/mise.toml @@ -5,3 +5,15 @@ depends = ["unit:*"] [tasks."unit:omp"] description = "omp: skills, Kiro plugin, routing, and the auth-broker client" file = "units/omp.sh" + +[tasks."unit:chezmoi"] +description = "chezmoi: shell, git, neovim and atuin config files from home/" +file = "units/chezmoi.sh" + +[tasks."unit:atuin"] +description = "atuin: sync login from Bitwarden and history import" +file = "units/atuin.sh" + +[tasks."unit:shell-ai"] +description = "shell-ai: API secrets from Bitwarden into the shell secret files" +file = "units/shell-ai.sh" diff --git a/roles/git/tasks/main.yml b/roles/git/tasks/main.yml deleted file mode 100644 index 993b7fb3..00000000 --- a/roles/git/tasks/main.yml +++ /dev/null @@ -1,30 +0,0 @@ ---- -- name: Deploy ~/.gitconfig - ansible.builtin.template: - src: gitconfig.j2 - dest: "{{ ansible_facts['env']['HOME'] }}/.gitconfig" - mode: "0644" - -- name: Deploy ~/.gitignore_global - ansible.builtin.copy: - src: gitignore_global - dest: "{{ ansible_facts['env']['HOME'] }}/.gitignore_global" - mode: "0644" - -- name: Deploy ~/.gitmessage commit template - ansible.builtin.copy: - src: gitmessage - dest: "{{ ansible_facts['env']['HOME'] }}/.gitmessage" - mode: "0644" - -- name: Create ~/.config/gh/ directory - ansible.builtin.file: - path: "{{ ansible_facts['env']['HOME'] }}/.config/gh" - state: directory - mode: "0755" - -- name: Deploy ~/.config/gh/config.yml - ansible.builtin.copy: - src: gh_config.yml - dest: "{{ ansible_facts['env']['HOME'] }}/.config/gh/config.yml" - mode: "0644" diff --git a/roles/neovim/tasks/main.yml b/roles/neovim/tasks/main.yml deleted file mode 100644 index d4143602..00000000 --- a/roles/neovim/tasks/main.yml +++ /dev/null @@ -1,66 +0,0 @@ ---- -- name: Ensure nvim config directory exists - ansible.builtin.file: - path: "{{ ansible_facts['env']['HOME'] }}/.config/nvim" - state: directory - mode: "0755" - -- name: Ensure nvim lua config directories exist - ansible.builtin.file: - path: "{{ ansible_facts['env']['HOME'] }}/.config/nvim/{{ item }}" - state: directory - mode: "0755" - loop: - - lua/config - - lua/plugins - -- name: Deploy nvim init.lua - ansible.builtin.copy: - src: nvim/init.lua - dest: "{{ ansible_facts['env']['HOME'] }}/.config/nvim/init.lua" - mode: "0644" - -- name: Deploy nvim config files - ansible.builtin.copy: - src: "nvim/lua/config/{{ item }}" - dest: "{{ ansible_facts['env']['HOME'] }}/.config/nvim/lua/config/{{ item }}" - mode: "0644" - loop: - - options.lua - - keymaps.lua - - autocmds.lua - -- name: Deploy nvim plugin specs - ansible.builtin.copy: - src: "nvim/lua/plugins/{{ item }}" - dest: "{{ ansible_facts['env']['HOME'] }}/.config/nvim/lua/plugins/{{ item }}" - mode: "0644" - loop: - - colorscheme.lua - - editor.lua - - lsp.lua - - ui.lua - - coding.lua - -- name: Bootstrap lazy.nvim (headless install) - ansible.builtin.shell: | - LAZY_DIR="${HOME}/.local/share/nvim/lazy/lazy.nvim" - if [ ! -d "$LAZY_DIR" ]; then - git clone --filter=blob:none https://github.com/folke/lazy.nvim.git \ - --branch=stable "$LAZY_DIR" - fi - changed_when: false - failed_when: false - environment: - PATH: "{{ ansible_env_path }}" - -- name: Install nvim plugins (headless) - ansible.builtin.shell: | - set -o pipefail - nvim --headless "+Lazy! sync" +qa 2>&1 | tail -5 - environment: - PATH: "{{ ansible_env_path }}" - HOME: "{{ ansible_facts['env']['HOME'] }}" - changed_when: false - failed_when: false - timeout: 300 diff --git a/roles/pi/files/skills/tavily-search/SKILL.md b/roles/pi/files/skills/tavily-search/SKILL.md index ad1ba9f1..43fb0ed1 100644 --- a/roles/pi/files/skills/tavily-search/SKILL.md +++ b/roles/pi/files/skills/tavily-search/SKILL.md @@ -20,6 +20,6 @@ Returns: title, url, content snippet for each result. ## API Key -API key: read from `$TAVILY_API_KEY` (provisioned by the `shell_ai` role from Bitwarden item `tavily-api-key`). +API key: read from `$TAVILY_API_KEY` (provisioned by the `shell-ai` unit from Bitwarden item `tavily-api-key`). Endpoint: `https://api.tavily.com/search` diff --git a/roles/shell_ai/tasks/main.yml b/roles/shell_ai/tasks/main.yml deleted file mode 100644 index 9898a173..00000000 --- a/roles/shell_ai/tasks/main.yml +++ /dev/null @@ -1,141 +0,0 @@ ---- -# ── AI Coding Tools configuration ──────────────────────────────── - -# ── Per-host secrets from Bitwarden (DeepSeek + Forgejo) ───────── -# Gated on `bw_unlocked` (set by the bitwarden role) so a locked vault -# leaves any previously-written secret files untouched instead of writing -# empty values. -- name: Fetch and deploy shell secrets from Bitwarden - when: bw_unlocked | default(false) - environment: - BW_SESSION: "{{ bw_session }}" - PATH: "{{ ansible_env_path }}" - block: - - name: Fetch DeepSeek API key from Bitwarden - ansible.builtin.command: bw get password deepseek-api-key - register: deepseek_key - no_log: true - changed_when: false - failed_when: false - - - - name: Fetch Forgejo API token from Bitwarden - ansible.builtin.shell: | - set -euo pipefail - bw get notes forgejo | grep "API Token:" | sed 's/API Token: //' - register: forgejo_token - no_log: true - changed_when: false - failed_when: false - - # The OMP DeepSeek provider consumes this env var; the API key never lands - # in the agent config or this public repository. - - name: Fetch Tavily API key from Bitwarden - ansible.builtin.command: bw get password tavily-api-key - register: tavily_key - no_log: true - changed_when: false - failed_when: false - - # Meta Model API key (Muse Spark) lives in a Bitwarden secure note. - - name: Fetch Meta API key from Bitwarden - ansible.builtin.command: bw get notes meta-api-key - register: meta_key - no_log: true - changed_when: false - failed_when: false - - # OMP's Kiro provider sends this API key directly to Kiro's runtime API. - # It is separate from the OAuth bearer stored by OMP/kiro-cli. - - name: Fetch Kiro API key from Bitwarden - ansible.builtin.command: bw get notes kiro-api-key - register: kiro_key - no_log: true - changed_when: false - failed_when: false - - - name: Write shell secrets file - ansible.builtin.copy: - content: | - # Managed by dotfiles playbook — DO NOT EDIT - export OMP_KIRO_CONFIG="$HOME/.config/omp/kiro.json" - {% if deepseek_key.rc == 0 and deepseek_key.stdout | trim | length > 0 %} - export DEEPSEEK_API_KEY={{ deepseek_key.stdout | trim | quote }} - {% endif %} - {% if forgejo_token.rc == 0 and forgejo_token.stdout | trim | length > 0 %} - export FORGEJO_TOKEN={{ forgejo_token.stdout | trim | quote }} - {% endif %} - {% if tavily_key.rc == 0 and tavily_key.stdout | trim | length > 0 %} - export TAVILY_API_KEY={{ tavily_key.stdout | trim | quote }} - {% endif %} - {% if meta_key.rc == 0 and meta_key.stdout | trim | length > 0 %} - export META_API_KEY={{ meta_key.stdout | trim | quote }} - {% endif %} - {% if kiro_key.rc == 0 and kiro_key.stdout | trim | length > 0 %} - export KIRO_API_KEY={{ kiro_key.stdout | trim | quote }} - {% endif %} - dest: "{{ ansible_facts['env']['HOME'] }}/.config/shell/secrets.sh" - mode: "0600" - no_log: true - when: >- - (deepseek_key.rc == 0 and deepseek_key.stdout | trim | length > 0) - or (forgejo_token.rc == 0 and forgejo_token.stdout | trim | length > 0) - or (tavily_key.rc == 0 and tavily_key.stdout | trim | length > 0) - or (meta_key.rc == 0 and meta_key.stdout | trim | length > 0) - or (kiro_key.rc == 0 and kiro_key.stdout | trim | length > 0) - - # Fish doesn't source secrets.sh; give it the same values via conf.d. - - name: Ensure fish conf.d directory exists - ansible.builtin.file: - path: "{{ ansible_facts['env']['HOME'] }}/.config/fish/conf.d" - state: directory - mode: "0755" - when: >- - (deepseek_key.rc == 0 and deepseek_key.stdout | trim | length > 0) - or (forgejo_token.rc == 0 and forgejo_token.stdout | trim | length > 0) - or (tavily_key.rc == 0 and tavily_key.stdout | trim | length > 0) - or (meta_key.rc == 0 and meta_key.stdout | trim | length > 0) - or (kiro_key.rc == 0 and kiro_key.stdout | trim | length > 0) - - - name: Write fish secrets file - ansible.builtin.copy: - content: | - # Managed by dotfiles playbook — DO NOT EDIT - {% if deepseek_key.rc == 0 and deepseek_key.stdout | trim | length > 0 %} - set -gx DEEPSEEK_API_KEY {{ deepseek_key.stdout | trim | quote }} - {% endif %} - {% if forgejo_token.rc == 0 and forgejo_token.stdout | trim | length > 0 %} - set -gx FORGEJO_TOKEN {{ forgejo_token.stdout | trim | quote }} - {% endif %} - {% if tavily_key.rc == 0 and tavily_key.stdout | trim | length > 0 %} - set -gx TAVILY_API_KEY {{ tavily_key.stdout | trim | quote }} - {% endif %} - {% if meta_key.rc == 0 and meta_key.stdout | trim | length > 0 %} - set -gx META_API_KEY {{ meta_key.stdout | trim | quote }} - {% endif %} - {% if kiro_key.rc == 0 and kiro_key.stdout | trim | length > 0 %} - set -gx KIRO_API_KEY {{ kiro_key.stdout | trim | quote }} - {% endif %} - if command -v bw >/dev/null 2>&1; and test -r /tmp/bw_session - set -gx KIRO_API_KEY (env BW_SESSION=(cat /tmp/bw_session) bw get item 2706eda5-e8ad-43df-9838-b4e00167e95d 2>/dev/null | jq -r .notes) - test "$KIRO_API_KEY" = null; and set -e KIRO_API_KEY - end - dest: "{{ ansible_facts['env']['HOME'] }}/.config/fish/conf.d/secrets.fish" - mode: "0600" - no_log: true - when: >- - (deepseek_key.rc == 0 and deepseek_key.stdout | trim | length > 0) - or (forgejo_token.rc == 0 and forgejo_token.stdout | trim | length > 0) - or (tavily_key.rc == 0 and tavily_key.stdout | trim | length > 0) - or (meta_key.rc == 0 and meta_key.stdout | trim | length > 0) - or (kiro_key.rc == 0 and kiro_key.stdout | trim | length > 0) - -- name: Warn if deepseek-api-key not found in Bitwarden - ansible.builtin.debug: - msg: >- - Bitwarden item 'deepseek-api-key' not found — skipping DEEPSEEK_API_KEY. - OMP/Kiro OAuth can still be configured with `/login kiro`. - when: - - bw_unlocked | default(false) - - (deepseek_key | default({})).rc | default(1) != 0 - or (deepseek_key | default({})).stdout | default('') | trim | length == 0 diff --git a/roles/shell_atuin/tasks/main.yml b/roles/shell_atuin/tasks/main.yml deleted file mode 100644 index d00b611a..00000000 --- a/roles/shell_atuin/tasks/main.yml +++ /dev/null @@ -1,153 +0,0 @@ ---- -- name: Ensure atuin config dir exists - ansible.builtin.file: - path: "{{ ansible_facts['env']['HOME'] }}/.config/atuin" - state: directory - mode: "0755" - -- name: Deploy atuin config - ansible.builtin.copy: - dest: "{{ ansible_facts['env']['HOME'] }}/.config/atuin/config.toml" - mode: "0644" - content: | - sync_address = "https://api.atuin.sh" - sync_frequency = "5m" - auto_sync = true - search_mode = "fuzzy" - # ── filter_mode ────────────────────────────────────────────── - # "global" = search all history across every machine & session. - # "host" = only this machine. - # "session" = only this shell session (empty in fresh terminals!). - # ────────────────────────────────────────────────────────────── - # DO NOT set filter_mode_shell_up_key_binding to "session" — - # it makes the Up arrow appear broken in fresh terminals because - # a new shell session has no commands yet. - # ────────────────────────────────────────────────────────────── - filter_mode = "global" - filter_mode_shell_up_key_binding = "global" - style = "compact" - show_preview = true - max_preview_height = 4 - -# ── Atuin setup from Bitwarden ──────────────────────────────────── -- name: Ensure atuin data dir exists - ansible.builtin.file: - path: "{{ ansible_facts['env']['HOME'] }}/.local/share/atuin" - state: directory - mode: "0700" - -# bw sync is handled by the bitwarden role earlier in the play — skip here. - -# Skip BW round-trip when already logged in — atuin sync status is the source of truth. -- name: Check Atuin sync status - ansible.builtin.command: atuin status - register: atuin_status_cmd - changed_when: false - failed_when: false - environment: - PATH: "{{ ansible_env_path }}" - -- name: Set Atuin logged in fact - ansible.builtin.set_fact: - atuin_logged_in: "{{ atuin_status_cmd.rc == 0 and '[Sync]' in atuin_status_cmd.stdout }}" - -- name: Check if atuin binary is installed - ansible.builtin.shell: command -v atuin - register: atuin_binary - changed_when: false - failed_when: false - -- name: Warn if atuin binary is missing - ansible.builtin.debug: - msg: >- - atuin binary not found — skipping atuin login/sync. - Install atuin (brew install atuin) and re-run. - when: atuin_binary.rc != 0 - -- name: Check if atuin key exists - ansible.builtin.stat: - path: "{{ ansible_facts['env']['HOME'] }}/.local/share/atuin/key" - register: atuin_existing_key - -- name: Fetch atuin credentials from Bitwarden - ansible.builtin.command: bw get item atuin.sh - register: atuin_bw_item - no_log: true - changed_when: false - failed_when: false - when: - - not atuin_logged_in - - bw_unlocked | default(false) - environment: - BW_SESSION: "{{ bw_session | default('') }}" - PATH: "{{ ansible_env_path }}" - -- name: Skip atuin login (already logged in or vault locked) - ansible.builtin.set_fact: - atuin_bw_item: {rc: 1, stdout: ""} - when: atuin_logged_in or not (bw_unlocked | default(false)) - -- name: Warn if atuin.sh not found in Bitwarden - ansible.builtin.debug: - msg: >- - Bitwarden item 'atuin.sh' not found or vault locked — skipping atuin login. - Run 'bw unlock' on this machine and re-run, or log in to atuin manually. - when: - - not atuin_logged_in - - atuin_bw_item.rc != 0 or (atuin_bw_item.stdout | default('')) | length == 0 - -- name: Parse atuin credentials - ansible.builtin.set_fact: - atuin_username: "{{ (atuin_bw_item.stdout | from_json).login.username }}" - atuin_password: "{{ (atuin_bw_item.stdout | from_json).login.password }}" - atuin_key: "{{ (atuin_bw_item.stdout | from_json).fields | selectattr('name', 'equalto', 'key') | map(attribute='value') | first }}" - no_log: true - when: atuin_bw_item.rc == 0 and (atuin_bw_item.stdout | default('')) | length > 0 - -# NOTE: We do NOT write atuin_key (mnemonic) to ~/.local/share/atuin/key directly. -# Atuin expects that file to be a base64-encoded binary key. -# Running 'atuin login -k' with the mnemonic correctly generates the binary key file. -- name: Remove atuin data directory before fresh login - ansible.builtin.file: - path: "{{ ansible_facts['env']['HOME'] }}/.local/share/atuin" - state: absent - when: > - atuin_bw_item.rc == 0 and (atuin_bw_item.stdout | default("")) | length > 0 - and not atuin_existing_key.stat.exists - -- name: Login to atuin sync - ansible.builtin.command: > - atuin login -u "{{ atuin_username }}" -p "{{ atuin_password }}" -k "{{ atuin_key }}" - no_log: true - when: > - atuin_bw_item.rc == 0 and (atuin_bw_item.stdout | default("")) | length > 0 - and not atuin_logged_in - and atuin_binary.rc == 0 - changed_when: true - environment: - PATH: "{{ ansible_env_path }}" - -- name: Sync atuin history (best-effort — key mismatch is non-fatal) - ansible.builtin.command: atuin sync - changed_when: false - failed_when: false - environment: - PATH: "{{ ansible_env_path }}" - when: atuin_bw_item.rc == 0 and (atuin_bw_item.stdout | default("")) | length > 0 - -- name: Check if shell history has already been imported into atuin - ansible.builtin.stat: - path: "{{ ansible_facts['env']['HOME'] }}/.local/share/atuin/.history_imported" - register: atuin_import_sentinel - -- name: Import existing shell history into atuin - ansible.builtin.shell: | - atuin import auto 2>&1 && - touch "{{ ansible_facts['env']['HOME'] }}/.local/share/atuin/.history_imported" - environment: - PATH: "{{ ansible_env_path }}" - when: - - atuin_binary.rc == 0 - - not atuin_import_sentinel.stat.exists - changed_when: false - failed_when: false diff --git a/roles/shell_dotfiles/tasks/main.yml b/roles/shell_dotfiles/tasks/main.yml deleted file mode 100644 index 4efdfec9..00000000 --- a/roles/shell_dotfiles/tasks/main.yml +++ /dev/null @@ -1,136 +0,0 @@ ---- -- name: Create ~/.config/shell/ directory - ansible.builtin.file: - path: "{{ ansible_facts['env']['HOME'] }}/.config/shell" - state: directory - mode: "0755" - -- name: Deploy shared aliases (bash/zsh) - ansible.builtin.template: - src: aliases.sh.j2 - dest: "{{ ansible_facts['env']['HOME'] }}/.config/shell/aliases.sh" - mode: "0644" - -- name: Deploy ~/.bashrc - ansible.builtin.template: - src: bashrc.j2 - dest: "{{ ansible_facts['env']['HOME'] }}/.bashrc" - mode: "0644" - -- name: Deploy ~/.zshrc - ansible.builtin.template: - src: zshrc.j2 - dest: "{{ ansible_facts['env']['HOME'] }}/.zshrc" - mode: "0644" - -- name: Deploy ~/.zprofile - ansible.builtin.template: - src: zprofile.j2 - dest: "{{ ansible_facts['env']['HOME'] }}/.zprofile" - mode: "0644" - -- name: Deploy ~/.tmux.conf - ansible.builtin.copy: - src: tmux.conf - dest: "{{ ansible_facts['env']['HOME'] }}/.tmux.conf" - mode: "0644" - -- name: Deploy ~/.inputrc (readline defaults for bash + readline apps) - ansible.builtin.copy: - src: inputrc - dest: "{{ ansible_facts['env']['HOME'] }}/.inputrc" - mode: "0644" - -- name: Ensure ~/.config/starship directory exists - ansible.builtin.file: - path: "{{ ansible_facts['env']['HOME'] }}/.config" - state: directory - mode: "0755" - -- name: Deploy starship.toml - ansible.builtin.copy: - src: starship.toml - dest: "{{ ansible_facts['env']['HOME'] }}/.config/starship.toml" - mode: "0644" - -- name: Create ~/.config/fish/ directory - ansible.builtin.file: - path: "{{ ansible_facts['env']['HOME'] }}/.config/fish" - state: directory - mode: "0755" - -- name: Create ~/.config/fish/conf.d/ directory - ansible.builtin.file: - path: "{{ ansible_facts['env']['HOME'] }}/.config/fish/conf.d" - state: directory - mode: "0755" - -- name: Deploy ~/.config/fish/config.fish - ansible.builtin.copy: - src: config.fish - dest: "{{ ansible_facts['env']['HOME'] }}/.config/fish/config.fish" - mode: "0644" - -- name: Deploy fish aliases - ansible.builtin.template: - src: aliases.fish.j2 - dest: "{{ ansible_facts['env']['HOME'] }}/.config/fish/conf.d/aliases.fish" - mode: "0644" - -# ── Ghostty terminfo ────────────────────────────────────────────── -# SSH-ing in from a Ghostty terminal exports TERM=xterm-ghostty. Hosts -# without that terminfo entry break ncurses apps ("missing or unsuitable -# terminal: xterm-ghostty" from tmux/vim/etc). Ship the entry and compile -# it into the user's ~/.terminfo so every host understands the term. -- name: Check whether tic (ncurses) is available - ansible.builtin.command: which tic - register: _tic_bin - changed_when: false - failed_when: false - -- name: Deploy ghostty terminfo source - ansible.builtin.copy: - src: xterm-ghostty.terminfo - dest: "{{ ansible_facts['env']['HOME'] }}/.config/shell/ghostty.terminfo" - mode: "0644" - register: _ghostty_ti_src - when: _tic_bin.rc == 0 - -- name: Check whether xterm-ghostty terminfo is already installed - ansible.builtin.command: infocmp -x xterm-ghostty - register: _ghostty_ti_installed - changed_when: false - failed_when: false - when: _tic_bin.rc == 0 - -- name: Compile ghostty terminfo into ~/.terminfo - ansible.builtin.command: > - tic -x -o {{ ansible_facts['env']['HOME'] }}/.terminfo - {{ ansible_facts['env']['HOME'] }}/.config/shell/ghostty.terminfo - when: - - _tic_bin.rc == 0 - - _ghostty_ti_installed.rc != 0 or _ghostty_ti_src.changed - changed_when: true - -- name: Warn if tic is unavailable (ghostty terminfo not installable) - ansible.builtin.debug: - msg: >- - tic (ncurses) is not installed on {{ inventory_hostname }}; the - xterm-ghostty terminfo cannot be compiled. SSH sessions from Ghostty - will need `TERM=xterm-256color` until ncurses is present. - when: _tic_bin.rc != 0 - -# ── Ghostty config (auto light/dark, follows GNOME) ─────────────── -# Inert on headless hosts (Ghostty never launched there); active wherever -# Ghostty runs. Kept alongside the rest of the managed shell config. -- name: Create ~/.config/ghostty/ directory - ansible.builtin.file: - path: "{{ ansible_facts['env']['HOME'] }}/.config/ghostty" - state: directory - mode: "0755" - -- name: Deploy ghostty config - ansible.builtin.copy: - src: ghostty.config - dest: "{{ ansible_facts['env']['HOME'] }}/.config/ghostty/config" - mode: "0644" diff --git a/scripts/omp-login.sh b/scripts/omp-login.sh index 48305b43..6b6ca43d 100755 --- a/scripts/omp-login.sh +++ b/scripts/omp-login.sh @@ -4,7 +4,7 @@ # scripts/omp-login.sh # status + log into anything missing # # - API-key providers (DeepSeek, Tavily, ...) need no omp login: omp reads them -# from env, and roles/shell_ai exports them from Bitwarden into the shell. +# from env, and units/shell-ai.sh exports them from Bitwarden into the shell. # This script only reports whether they resolve. # - OAuth providers (Claude Max, Codex Pro, Antigravity) need a one-time browser # approval. They are stored in ~/.omp/agent/agent.db and refresh on their own, @@ -32,7 +32,7 @@ done if session=$("$SCRIPT_DIR/bw-unlock.sh" 2>/dev/null); then for item in deepseek-api-key tavily-api-key meta-api-key; do if BW_SESSION="$session" bw get item "$item" >/dev/null 2>&1; then - printf ' ✓ %s (in Bitwarden; exported by shell_ai role)\n' "$item" + printf ' ✓ %s (in Bitwarden; exported by shell-ai unit)\n' "$item" else printf ' ⚠ %s not found in Bitwarden\n' "$item" fi diff --git a/scripts/run-units.sh b/scripts/run-units.sh index ac93b734..ad101ec6 100755 --- a/scripts/run-units.sh +++ b/scripts/run-units.sh @@ -3,14 +3,20 @@ # Usage: scripts/run-units.sh [--no-secrets] [--tags a,b] set -uo pipefail -ALL_UNITS="omp" # run order for the sequential fallback; keep in step with mise.toml +ALL_UNITS="chezmoi omp atuin shell-ai" # run order for the sequential fallback; keep in step with mise.toml tasks_for_tag() { case "$1" in # Ansible tag -> units (mirrors the old role tags) - dotfiles|omp) echo omp ;; + dotfiles) echo "chezmoi omp atuin shell-ai" ;; + shell) echo "chezmoi atuin shell-ai" ;; + git|neovim|editor) echo chezmoi ;; + atuin) echo atuin ;; + ai) echo shell-ai ;; + secrets) echo "atuin shell-ai" ;; + omp) echo omp ;; esac; } repo="$(cd "$(dirname "$0")/.." && pwd)" export DOTFILES_DIR="$repo" -PATH=/home/linuxbrew/.linuxbrew/bin:$PATH +PATH="$PATH:/home/linuxbrew/.linuxbrew/bin" tags="" while [ $# -gt 0 ]; do diff --git a/site.yml b/site.yml index 46b332d3..aff4351a 100644 --- a/site.yml +++ b/site.yml @@ -73,20 +73,10 @@ tags: [secrets, bitwarden] - role: shell_fonts tags: [dotfiles, shell, fonts] - - role: shell_dotfiles - tags: [dotfiles, shell, dotfiles] - role: hive_ops tags: [hive, hive_ops] - - role: git - tags: [dotfiles, git] - - role: neovim - tags: [dotfiles, neovim, editor] # Phase 2: Secrets + auth - - role: shell_atuin - tags: [dotfiles, shell, atuin, secrets] - - role: shell_ai - tags: [dotfiles, shell, ai, secrets] - role: ssh_keys tags: [secrets, ssh, ssh_keys] - role: ssh_mesh diff --git a/tests/test_atuin_unit.py b/tests/test_atuin_unit.py new file mode 100644 index 00000000..399a7f4a --- /dev/null +++ b/tests/test_atuin_unit.py @@ -0,0 +1,158 @@ +import shutil +import subprocess +from pathlib import Path + +import pytest + +from conftest import REPO, write_stub + +pytestmark = pytest.mark.skipif(shutil.which("jq") is None, reason="jq not installed") + +ITEM = '{"login":{"username":"U","password":"P"},"fields":[{"name":"key","value":"K"}]}' + + +def stubs(bindir, status_out, status_rc=1, unlock_rc=0, version="18.24.0"): + write_stub( + bindir, + "atuin", + f"""echo "atuin $*" >> "$HOME/calls" +case "$1" in + status) echo '{status_out}'; exit {status_rc} ;; + --version) echo 'atuin {version}'; exit 0 ;; + info) echo "key path: $HOME/.local/share/atuin/key"; exit 0 ;; + key) cat "$HOME/.local/share/atuin/key"; exit $? ;; + login) [ "$*" = 'login -u U' ] && [ "$ATUIN_PASSWORD" = P ] && [ "$ATUIN_ENCRYPTION_KEY" = K ]; exit $? ;; +esac +exit 0 +""", + ) + write_stub( + bindir, + "bw", + f"""echo "bw $*" >> "$HOME/calls" +[ "$1 $2" = "unlock --check" ] && exit {unlock_rc} +[ "$1 $2 $3" = "get item atuin.sh" ] && echo '{ITEM}' +exit 0 +""", + ) + + +def run(env): + env = {**env, "BW_SESSION": "x", "DOTFILES_DIR": str(REPO), "BREW_PREFIX": "/nonexistent"} + r = subprocess.run(["bash", str(REPO / "units/atuin.sh")], env=env, capture_output=True, text=True) + calls = Path(env["HOME"], "calls") + return r, calls.read_text() if calls.exists() else "" + + +def test_logs_in_with_bitwarden_credentials(stub_env): + bindir, env = stub_env + stubs(bindir, "not logged in") + r, calls = run(env) + assert r.returncode == 0, r.stdout + r.stderr + assert "atuin login -u U\n" in calls + assert " -p " not in calls and " -k " not in calls + + +def test_missing_key_login_keeps_imported_history_and_permissions(stub_env): + bindir, env = stub_env + data = Path(env["HOME"], ".local/share/atuin") + data.mkdir(parents=True, mode=0o755) + data.chmod(0o755) + (data / "history.db").write_bytes(b"existing history") + (data / "history.db-wal").write_bytes(b"pending history") + (data / ".history_imported").write_text("already imported") + stubs(bindir, "not logged in") + + r, calls = run(env) + + assert r.returncode == 0, r.stdout + r.stderr + assert "atuin login -u U\n" in calls + assert "atuin import" not in calls + assert (data / "history.db").read_bytes() == b"existing history" + assert (data / "history.db-wal").read_bytes() == b"pending history" + assert (data / ".history_imported").read_text() == "already imported" + assert data.stat().st_mode & 0o777 == 0o700 + + +def test_existing_different_key_is_never_replaced(stub_env): + bindir, env = stub_env + data = Path(env["HOME"], ".local/share/atuin") + data.mkdir(parents=True) + (data / "key").write_text("different key") + (data / "history.db").write_bytes(b"existing history") + stubs(bindir, "not logged in") + + r, calls = run(env) + + assert r.returncode == 0 + assert "existing Atuin key differs" in r.stdout + assert "atuin login" not in calls and "atuin import" not in calls + assert (data / "key").read_text() == "different key" + assert (data / "history.db").read_bytes() == b"existing history" + assert not (data / ".history_imported").exists() + + +def test_existing_matching_key_can_log_in(stub_env): + bindir, env = stub_env + data = Path(env["HOME"], ".local/share/atuin") + data.mkdir(parents=True) + (data / "key").write_text("K") + stubs(bindir, "not logged in") + + r, calls = run(env) + + assert r.returncode == 0, r.stdout + r.stderr + assert "atuin login -u U\n" in calls + assert (data / "key").read_text() == "K" + + +def test_old_version_requests_manual_login(stub_env): + bindir, env = stub_env + stubs(bindir, "not logged in", version="18.23.0") + + r, calls = run(env) + + assert r.returncode == 0 + assert "run atuin login interactively" in r.stdout + assert "atuin login" not in calls and "atuin import" not in calls + + +def test_no_secrets_keeps_history_without_touching_vault(stub_env): + bindir, env = stub_env + data = Path(env["HOME"], ".local/share/atuin") + data.mkdir(parents=True) + (data / "history.db").write_bytes(b"existing history") + (data / ".history_imported").touch() + stubs(bindir, "not logged in") + + r, calls = run({**env, "UNITS_NO_SECRETS": "1"}) + + assert r.returncode == 0 + assert "PARTIAL:" in r.stdout + assert "bw " not in calls and "atuin login" not in calls + assert (data / "history.db").read_bytes() == b"existing history" + + +def test_already_logged_in_never_touches_bitwarden(stub_env): + bindir, env = stub_env + stubs(bindir, "[Sync] ok", status_rc=0) + r, calls = run(env) + assert r.returncode == 0 + assert "login" not in calls and "bw " not in calls + + +def test_locked_vault_is_partial_and_does_not_login(stub_env): + bindir, env = stub_env + stubs(bindir, "not logged in", unlock_rc=1) + r, calls = run(env) + assert r.returncode == 0 + assert "PARTIAL:" in r.stdout + assert "login" not in calls + + +def test_missing_atuin_is_skipped(stub_env): + bindir, env = stub_env + env["PATH"] = f"{bindir}:/usr/bin:/bin" + r, _ = run(env) + assert r.returncode == 0 + assert "SKIPPED:" in r.stdout diff --git a/tests/test_brew_blocked.py b/tests/test_brew_blocked.py index e7e31555..127b5e45 100644 --- a/tests/test_brew_blocked.py +++ b/tests/test_brew_blocked.py @@ -39,7 +39,7 @@ def test_no_install_list_names_a_blocked_package(): def test_no_alias_routes_through_trash(): - groups = yaml.safe_load((REPO / "roles" / "shell_dotfiles" / "vars" / "main.yml").read_text())["alias_groups"] + groups = yaml.safe_load((REPO / "home" / ".chezmoidata.yaml").read_text())["alias_groups"] assert all(g.get("guard") != "trash" for g in groups) cmds = [a["cmd"] for g in groups for a in g.get("aliases", [])] assert not [c for c in cmds if c.split()[0] in {"trash", "trash-put"}] diff --git a/tests/test_chezmoi_data.py b/tests/test_chezmoi_data.py new file mode 100644 index 00000000..a79c4c06 --- /dev/null +++ b/tests/test_chezmoi_data.py @@ -0,0 +1,19 @@ +import yaml + +from conftest import REPO + + +def load(rel): + return yaml.safe_load((REPO / rel).read_text()) + + +def test_identity_matches_ansible_vars(): + # group_vars/all.yml feeds ssh_keys; home/.chezmoidata.yaml feeds ~/.gitconfig. + ans, chez = load("group_vars/all.yml"), load("home/.chezmoidata.yaml") + assert (chez["user_name"], chez["user_email"]) == (ans["user_name"], ans["user_email"]) + + +def test_every_alias_has_name_and_cmd(): + for group in load("home/.chezmoidata.yaml")["alias_groups"]: + for a in group["aliases"]: + assert a.get("name") and a.get("cmd"), a diff --git a/tests/test_chezmoi_unit.py b/tests/test_chezmoi_unit.py new file mode 100644 index 00000000..f5552c03 --- /dev/null +++ b/tests/test_chezmoi_unit.py @@ -0,0 +1,21 @@ +import shutil +import subprocess +from pathlib import Path + +from conftest import REPO + + +def test_missing_chezmoi_fails_with_install_guidance(stub_env): + bindir, env = stub_env + # Keep only the utilities used by the shared library, not the host's chezmoi. + for name in ("cat", "dirname", "hostname"): + Path(bindir, name).symlink_to(shutil.which(name)) + env = {**env, "PATH": str(bindir), "BREW_PREFIX": "/nonexistent", "DOTFILES_DIR": str(REPO)} + + result = subprocess.run( + ["/bin/bash", str(REPO / "units/chezmoi.sh")], env=env, capture_output=True, text=True + ) + + assert result.returncode != 0 + assert "chezmoi not installed" in result.stdout + assert any(hint in result.stdout for hint in ("just packages", "pkg install", "apk add")) diff --git a/tests/test_nvim_sync.py b/tests/test_nvim_sync.py new file mode 100644 index 00000000..1952a3f5 --- /dev/null +++ b/tests/test_nvim_sync.py @@ -0,0 +1,56 @@ +import subprocess +from pathlib import Path + +from conftest import REPO, write_stub + + +SCRIPT = REPO / "home/.chezmoiscripts/run_after_20-nvim-sync.sh" + + +def run(env): + return subprocess.run(["sh", str(SCRIPT)], env=env, capture_output=True, text=True) + + +def calls(home): + log = Path(home, "nvim-sync-calls") + return log.read_text().splitlines() if log.exists() else [] + + +def test_sync_runs_on_bootstrap_and_config_change_only(stub_env): + bindir, env = stub_env + home = Path(env["HOME"]) + config = home / ".config/nvim" + config.mkdir(parents=True) + (config / "init.lua").write_text("return 1\n") + write_stub(bindir, "git", 'echo clone >> "$HOME/nvim-sync-calls"\nmkdir -p "${@: -1}"\n') + write_stub(bindir, "nvim", 'echo sync >> "$HOME/nvim-sync-calls"\n') + + assert run(env).returncode == 0 + assert calls(home) == ["clone", "sync"] + assert run(env).returncode == 0 + assert calls(home) == ["clone", "sync"] + + plugin = config / "lua/plugins/new.lua" + plugin.parent.mkdir(parents=True) + plugin.write_text("return {}\n") + assert run(env).returncode == 0 + assert calls(home) == ["clone", "sync", "sync"] + plugin.unlink() + assert run(env).returncode == 0 + assert calls(home) == ["clone", "sync", "sync", "sync"] + + +def test_failed_sync_is_retried(stub_env): + bindir, env = stub_env + home = Path(env["HOME"]) + config = home / ".config/nvim" + config.mkdir(parents=True) + (config / "init.lua").write_text("return 1\n") + lazy = home / ".local/share/nvim/lazy/lazy.nvim" + lazy.mkdir(parents=True) + write_stub(bindir, "nvim", 'echo sync >> "$HOME/nvim-sync-calls"\nexit "${NVIM_RC:-0}"\n') + + assert run({**env, "NVIM_RC": "1"}).returncode == 0 + assert run(env).returncode == 0 + assert run(env).returncode == 0 + assert calls(home) == ["sync", "sync"] diff --git a/tests/test_shell_ai_unit.py b/tests/test_shell_ai_unit.py new file mode 100644 index 00000000..77464762 --- /dev/null +++ b/tests/test_shell_ai_unit.py @@ -0,0 +1,74 @@ +import subprocess + +import pytest + +from conftest import REPO, write_stub + +NASTY = "pa'ss\"w$ord" + +BW_STUB = """\ +if [ "$1 $2" = "unlock --check" ]; then exit "${STUB_UNLOCK_RC:-0}"; fi +[ "$1" = get ] || exit 1 +case "$2 $3" in + "password deepseek-api-key") printf '%s\\n' "$STUB_DEEPSEEK" ;; + "notes forgejo") printf 'Name: x\\nAPI Token: forge123\\n' ;; + "password tavily-api-key") printf 'tav456\\n' ;; + *) exit 1 ;; +esac +""" + + +@pytest.fixture +def env(stub_env): + bindir, env = stub_env + write_stub(bindir, "bw", BW_STUB) + env.update(BW_SESSION="x", DOTFILES_DIR=str(REPO), STUB_DEEPSEEK=NASTY, BREW_PREFIX="/nonexistent") + return env + + +def run(env, **extra): + return subprocess.run( + ["bash", str(REPO / "units/shell-ai.sh")], env={**env, **extra}, capture_output=True, text=True + ) + + +def home(env): + from pathlib import Path + + return Path(env["HOME"]) + + +def test_secrets_round_trip_and_modes(env): + r = run(env) + assert r.returncode == 0, r.stdout + r.stderr + sh = home(env) / ".config/shell/secrets.sh" + fish = home(env) / ".config/fish/conf.d/secrets.fish" + assert sh.stat().st_mode & 0o777 == 0o600 and fish.stat().st_mode & 0o777 == 0o600 + out = subprocess.run( + ["bash", "-c", f'. {sh}; printf "%s\\n%s\\n%s" "$DEEPSEEK_API_KEY" "$FORGEJO_TOKEN" "$TAVILY_API_KEY"'], + capture_output=True, + text=True, + ).stdout + assert out == f"{NASTY}\nforge123\ntav456" + assert "set -gx FORGEJO_TOKEN 'forge123'" in fish.read_text() + assert "2706eda5-e8ad-43df-9838-b4e00167e95d" in fish.read_text() + assert NASTY not in r.stdout + r.stderr + + +def test_missing_item_still_writes_the_others(env): + r = run(env, STUB_DEEPSEEK="") + assert r.returncode == 0 + text = (home(env) / ".config/shell/secrets.sh").read_text() + assert "DEEPSEEK_API_KEY" not in text + assert "FORGEJO_TOKEN='forge123'" in text + + +@pytest.mark.parametrize("extra", [{"STUB_UNLOCK_RC": "1"}, {"UNITS_NO_SECRETS": "1"}]) +def test_locked_or_nosecrets_leaves_files_untouched(env, extra): + sh = home(env) / ".config/shell/secrets.sh" + sh.parent.mkdir(parents=True) + sh.write_text("previous\n") + r = run(env, **extra) + assert r.returncode == 0 + assert "SKIPPED:" in r.stdout + assert sh.read_text() == "previous\n" diff --git a/units/atuin.sh b/units/atuin.sh new file mode 100755 index 00000000..7b0a67ae --- /dev/null +++ b/units/atuin.sh @@ -0,0 +1,85 @@ +#!/usr/bin/env bash +# atuin unit: log in to atuin sync from Bitwarden and import shell history. +# The config file itself (~/.config/atuin/config.toml) is deployed by chezmoi. +set -uo pipefail +# shellcheck source=units/lib.sh +. "$(dirname "${BASH_SOURCE[0]}")/lib.sh" + +data="$HOME/.local/share/atuin" +allow_import=1 + +if ! command -v atuin >/dev/null 2>&1; then + note "SKIPPED: atuin not installed (brew install atuin)" + exit 0 +fi + +step "atuin data dir" install -d -m 0700 "$data" +[ "$UNIT_FAILED" = 0 ] || finish + +logged_in() { + local out + out="$(atuin status 2>&1)" || return 1 + case "$out" in *"[Sync]"*) return 0 ;; *) return 1 ;; esac +} + +atuin_login() { + local s item u p k version key_path line local_key + s="$(bw_session)" + if [ -z "$s" ]; then + note "PARTIAL: atuin not logged in; vault locked (bw unlock, then just apply-tags atuin)" + return 0 + fi + item="$(BW_SESSION="$s" bw get item atuin.sh 2>/dev/null)" + u="$(printf '%s' "$item" | jq -r '.login.username // empty' 2>/dev/null)" + p="$(printf '%s' "$item" | jq -r '.login.password // empty' 2>/dev/null)" + k="$(printf '%s' "$item" | jq -r '.fields[]? | select(.name=="key") | .value' 2>/dev/null | head -n1)" + if [ -z "$u" ] || [ -z "$p" ] || [ -z "$k" ]; then + note "PARTIAL: Bitwarden item atuin.sh missing or incomplete; atuin login skipped" + return 0 + fi + # Atuin gained non-interactive environment credentials in 18.24. Older + # versions need a manual login; never expose the password or key in argv. + version="$(atuin --version 2>/dev/null)" + if [[ ! "$version" =~ ([0-9]+)\.([0-9]+)\.([0-9]+) ]] || + (( 10#${BASH_REMATCH[1]} < 18 || + (10#${BASH_REMATCH[1]} == 18 && 10#${BASH_REMATCH[2]} < 24) )); then + note "PARTIAL: atuin 18.24 or newer required for unattended login; upgrade or run atuin login interactively" + allow_import=0 + return 0 + fi + + # Let Atuin resolve its configured key path, including the newer data layout. + key_path="" + while IFS= read -r line; do + case "$line" in 'key path: '*) key_path="${line#key path: }" ;; esac + done <<< "$(atuin info 2>/dev/null)" + if [ -z "$key_path" ]; then + note "PARTIAL: cannot determine the Atuin key path; inspect the local data and log in interactively" + allow_import=0 + return 0 + fi + if [ -e "$key_path" ] || [ -L "$key_path" ]; then + local_key="$(atuin key 2>/dev/null)" || { + note "PARTIAL: cannot read the existing Atuin key; inspect it and log in interactively" + allow_import=0 + return 0 + } + if [ "$local_key" != "$k" ]; then + note "PARTIAL: existing Atuin key differs from the vault; inspect it and log in interactively" + allow_import=0 + return 0 + fi + fi + # Keep both the history and any existing key; Atuin writes a missing key. + ATUIN_PASSWORD="$p" ATUIN_ENCRYPTION_KEY="$k" atuin login -u "$u" &1 && touch "$1/.history_imported"' sh "$data" +fi +finish diff --git a/units/chezmoi.sh b/units/chezmoi.sh new file mode 100755 index 00000000..4f5c8d91 --- /dev/null +++ b/units/chezmoi.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# chezmoi unit: apply the file-based dotfiles from home/ (shell, git, neovim, atuin config). +set -uo pipefail +# shellcheck source=units/lib.sh +. "$(dirname "${BASH_SOURCE[0]}")/lib.sh" + +if ! command -v chezmoi >/dev/null 2>&1 && [ "$IS_TERMUX" = 1 ]; then + soft "pkg install chezmoi" pkg install -y chezmoi +fi +if ! command -v chezmoi >/dev/null 2>&1; then + if [ "$IS_TERMUX" = 1 ]; then + note "FAIL: chezmoi not installed; run: pkg install chezmoi" + elif [ "$IS_MUSL" = 1 ]; then + note "FAIL: chezmoi not installed; on Alpine/postmarketOS enable the community repository and run: sudo apk add chezmoi" + else + note "FAIL: chezmoi not installed; run: just packages" + fi + exit 1 +fi + +is_desktop=0 +if command -v yq >/dev/null 2>&1; then + [ "$(yq ".all.children.desktop.hosts | has(\"$MACHINE\")" "$DOTFILES_DIR/inventory.yml" 2>/dev/null)" = true ] && is_desktop=1 +else + note "warn: yq missing, assuming non-desktop" +fi +export DOTFILES_MACHINE="$MACHINE" DOTFILES_IS_DESKTOP="$is_desktop" + +step "chezmoi apply" chezmoi --source "$DOTFILES_DIR" --keep-going --force --no-tty apply --init +finish diff --git a/units/lib.sh b/units/lib.sh old mode 100644 new mode 100755 index ca312c96..6d8a1871 --- a/units/lib.sh +++ b/units/lib.sh @@ -2,6 +2,8 @@ # Shared helpers for units/*.sh. Source this; do not execute it. # Units use `set -uo pipefail` and never `set -e`: a failing step is recorded # and the later independent steps still run. +# Homebrew tools come last so a stub or an earlier install wins; BREW_PREFIX is overridable for tests. +PATH="$PATH:${BREW_PREFIX:-/home/linuxbrew/.linuxbrew}/bin" UNIT_FAILED=0 step() { local d="$1"; shift; if "$@"; then echo "ok $d"; else echo "FAIL $d (exit $?)"; UNIT_FAILED=1; fi; } soft() { local d="$1"; shift; if "$@"; then echo "ok $d"; else echo "warn $d (exit $?, ignored)"; fi; } @@ -23,3 +25,4 @@ bw_session() { [ -n "$s" ] && BW_SESSION="$s" bw unlock --check >/dev/null 2>&1 && printf '%s' "$s" return 0 } +MACHINE="$(cat /etc/dotfiles-machine 2>/dev/null || hostname)" diff --git a/units/omp.sh b/units/omp.sh index b97b34e4..95efa861 100755 --- a/units/omp.sh +++ b/units/omp.sh @@ -4,7 +4,7 @@ set -uo pipefail # shellcheck source=units/lib.sh . "$(dirname "${BASH_SOURCE[0]}")/lib.sh" -PATH="$HOME/.bun/bin:$HOME/.local/bin:/home/linuxbrew/.linuxbrew/bin:$PATH" +PATH="$HOME/.bun/bin:$HOME/.local/bin:$PATH" cfg="$HOME/.omp/agent/config.yml" skills_dest="$HOME/.omp/agent/skills/james" diff --git a/units/omp/omp-route b/units/omp/omp-route index e147f9db..a4c79098 100755 --- a/units/omp/omp-route +++ b/units/omp/omp-route @@ -7,7 +7,7 @@ # own usage-aware fallback and retry.fallbackChains.default (same order) take # over mid-session. # -# omp-route [omp args...] # aliased to `omp` by shell_dotfiles +# omp-route [omp args...] # aliased to `omp` by the chezmoi shell aliases # omp-route --pick # print the chosen model and why, then exit set -uo pipefail diff --git a/units/shell-ai.sh b/units/shell-ai.sh new file mode 100755 index 00000000..87a0949a --- /dev/null +++ b/units/shell-ai.sh @@ -0,0 +1,69 @@ +#!/usr/bin/env bash +# shell-ai unit: per-host API secrets from Bitwarden -> ~/.config/shell/secrets.sh and fish conf.d. +# Not a chezmoi template: `bitwarden` aborts a whole template on one missing item, while this +# unit writes whatever secrets exist and leaves the files alone when the vault is locked. +set -uo pipefail +# shellcheck source=units/lib.sh +. "$(dirname "${BASH_SOURCE[0]}")/lib.sh" + +s="$(bw_session)" +if [ -z "$s" ]; then + note "SKIPPED: vault locked or --no-secrets; secret files left untouched" + exit 0 +fi + +trim() { printf '%s' "$1" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'; } +shq() { printf "'%s'" "${1//\'/\'\\\'\'}"; } +bwget() { BW_SESSION="$s" bw get "$@" 2>/dev/null; } + +deepseek="$(trim "$(bwget password deepseek-api-key)")" +forgejo="$(trim "$(bwget notes forgejo | grep 'API Token:' | sed 's/API Token: //')")" +tavily="$(trim "$(bwget password tavily-api-key)")" +meta="$(trim "$(bwget notes meta-api-key)")" +kiro="$(trim "$(bwget notes kiro-api-key)")" + +if [ -z "$deepseek$forgejo$tavily$meta$kiro" ]; then + note "warn: no secrets found in Bitwarden (deepseek-api-key missing?); files left untouched" + exit 0 +fi + +# write_atomic dest: stdin -> temp file in the same directory (mode 0600) -> mv +write_atomic() { + local dest="$1" tmp + mkdir -p "$(dirname "$dest")" || return 1 + tmp="$(umask 077; mktemp "$dest.XXXXXX")" || return 1 + if cat > "$tmp" && chmod 600 "$tmp" && mv -f "$tmp" "$dest"; then return 0; fi + rm -f "$tmp"; return 1 +} + +secrets_sh() { + echo '# Managed by dotfiles playbook — DO NOT EDIT' + echo 'export OMP_KIRO_CONFIG="$HOME/.config/omp/kiro.json"' + [ -z "$deepseek" ] || echo "export DEEPSEEK_API_KEY=$(shq "$deepseek")" + [ -z "$forgejo" ] || echo "export FORGEJO_TOKEN=$(shq "$forgejo")" + [ -z "$tavily" ] || echo "export TAVILY_API_KEY=$(shq "$tavily")" + [ -z "$meta" ] || echo "export META_API_KEY=$(shq "$meta")" + [ -z "$kiro" ] || echo "export KIRO_API_KEY=$(shq "$kiro")" +} + +secrets_fish() { + echo '# Managed by dotfiles playbook — DO NOT EDIT' + [ -z "$deepseek" ] || echo "set -gx DEEPSEEK_API_KEY $(shq "$deepseek")" + [ -z "$forgejo" ] || echo "set -gx FORGEJO_TOKEN $(shq "$forgejo")" + [ -z "$tavily" ] || echo "set -gx TAVILY_API_KEY $(shq "$tavily")" + [ -z "$meta" ] || echo "set -gx META_API_KEY $(shq "$meta")" + [ -z "$kiro" ] || echo "set -gx KIRO_API_KEY $(shq "$kiro")" + cat <<'FISH' +if command -v bw >/dev/null 2>&1; and test -r /tmp/bw_session + set -gx KIRO_API_KEY (env BW_SESSION=(cat /tmp/bw_session) bw get item 2706eda5-e8ad-43df-9838-b4e00167e95d 2>/dev/null | jq -r .notes) + test "$KIRO_API_KEY" = null; and set -e KIRO_API_KEY +end +FISH +} + +write_sh() { secrets_sh | write_atomic "$HOME/.config/shell/secrets.sh"; } +write_fish() { secrets_fish | write_atomic "$HOME/.config/fish/conf.d/secrets.fish"; } + +step "shell secrets file" write_sh +step "fish secrets file" write_fish +finish