diff --git a/AGENTS.md b/AGENTS.md index ce90367..04ad5bf 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -43,6 +43,20 @@ the `setup-config` TUI. - Homebrew already follows this pattern with `tasks/homebrew_list.yml` (`installed_brew_formulae`, `installed_brew_casks`, `brew_taps`). +### Package manager precedence +- Prefer Homebrew whenever `brew` is available, even on Linux where + `ansible_facts.pkg_mgr` reports `apt`. `tasks/package_manager.yml` sets + `brew_available`; gate brew tasks on `brew_available | bool`, never on + `pkg_mgr == "homebrew"`. +- Group dispatch lives in `tasks/package_group.yml`: Homebrew formulae run + first, apt supplies only packages Homebrew cannot, and casks run only on + macOS. +- `brew_apt_equivalents` in `default.config.yml` maps apt names to brew + formulae that use a different name. Same-name packages are covered + automatically from `brew formulae`. +- Set `prefer_homebrew: false` to force apt on a Linux machine that has + Homebrew installed. + ### Blocks for repeated conditions - When several tasks share the same conditional check, wrap them in a `block:` with the `when:` at the block level so evaluation happens once. diff --git a/README.md b/README.md index 41e4688..ed763d0 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,10 @@ the arrow keys (or `h`/`j`/`k`/`l`) to move between categories and options, `Enter` to toggle or edit a value, `r` to reset an option, `a` to apply the playbook, and `q` to quit. Changes are written to `~/.setup-options`. +`prefer_homebrew` (default on) makes the playbook use Homebrew formulae +ahead of apt when both are available; apt is used only for packages +Homebrew does not provide. + The tool is a standalone Python script with its dependencies declared inline (PEP 723). It runs under [uv](https://docs.astral.sh/uv/), which the playbook installs as a prerequisite. diff --git a/default.config.yml b/default.config.yml index f2f1d1c..e317ec5 100644 --- a/default.config.yml +++ b/default.config.yml @@ -16,6 +16,17 @@ agent_python_packages: - pyyaml - jinja2 +# Prefer Homebrew formulae over apt when both are available (Linuxbrew). +# Ignored on macOS, where Homebrew is the only supported package manager. +prefer_homebrew: true + +# Apt package names that a Homebrew formula also provides under a different +# name. Used to avoid installing both when Homebrew is preferred. +brew_apt_equivalents: + nodejs: node + npm: node + kubectl: kubernetes-cli + vscode: false install_apple_office: false diff --git a/docs/superpowers/plans/2026-10-25-package-manager-precedence.md b/docs/superpowers/plans/2026-10-25-package-manager-precedence.md new file mode 100644 index 0000000..d92e835 --- /dev/null +++ b/docs/superpowers/plans/2026-10-25-package-manager-precedence.md @@ -0,0 +1,853 @@ +# Package Manager Precedence Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Prefer Homebrew formulae on machines that have both brew and apt, use apt only for packages brew cannot supply, and never install casks outside macOS. + +**Architecture:** A new detection task sets `brew_available` independently of `ansible_facts.pkg_mgr`. A side-effect-free resolver computes the brew formula list and the apt fallback list for each group, then a dispatcher imports the existing Homebrew and apt task files. Group blocks in `main.yml` stop branching on `pkg_mgr` for brew. + +**Tech Stack:** Ansible (YAML playbooks/task files), Jinja2 expressions, Community General Homebrew modules, Python stdlib `unittest` for the config tool. + +**Spec:** `docs/superpowers/specs/2026-10-25-package-manager-precedence-design.md` + +## Global Constraints + +- Use `brew_available | bool` in every condition. `set_fact` may store the value as a bool here, but the `| bool` guard prevents the string `"False"` from being truthy on other Ansible versions. +- Casks run only when `ansible_facts.system == "Darwin"`. +- Install (`enabled: true`) filters apt packages covered by brew. Removal (`enabled: false`) passes the full apt list. +- Keep the three-valued group semantics: `true` install, `false` remove, `ignore` skip. +- Do not change official-installer tasks (`uv`, `opencode`, `omp`, `herdr`, `rustup`) or `bootstrap.sh`. +- Keep existing task-file structure; do not introduce roles. +- Local test commands use `-i localhost, -c local`. +- Match the repo's plain imperative commit style. + +## Review Focus + +- **Casks must never run on Linux.** `tasks/homebrew_packages.yml` must include the Darwin guard on the cask task. Pinned by a grep assertion in Task 3. +- **`brew_available` truthiness.** A string `"False"` is truthy in Ansible. Every consumer must use `| bool`. Pinned by `tests/test_brew_detection.yml` and a grep in Task 2. +- **Removal must not filter the apt list.** `enabled: false` must pass every apt package so packages installed by either manager are removed. Pinned by the removal scenarios in Task 4. +- **Renamed equivalents must not double-install.** `nodejs`/`npm` must not follow `node`, and `kubectl` must not follow `kubernetes-cli`. Pinned by the kube and AI scenarios in Task 4. +- **Unattended cron runs must find Linuxbrew.** Without the Linuxbrew directories in the cron `PATH`, `brew_available` is false and the old apt behavior returns. Pinned by a grep assertion in Task 2. + +--- + +### Task 1: Add the preference defaults and TUI toggle + +**Files:** +- Modify: `default.config.yml` +- Modify: `setup-config-menu.json` +- Modify: `tests/test_setup_config.py:71-75` + +**Interfaces:** +- Produces: `prefer_homebrew` (bool, default `true`) and `brew_apt_equivalents` (mapping apt name → brew formula) available to every playbook task. +- Produces: a `prefer_homebrew` menu item in the `system` category. + +- [ ] **Step 1: Write the failing test** + +In `tests/test_setup_config.py`, change the system item count from 7 to 8 and add: + +```python + def test_prefer_homebrew_toggle(self): + items = {i.key: i for c in self.menu.categories for i in c.items} + self.assertIn("prefer_homebrew", items) + self.assertEqual(items["prefer_homebrew"].kind, "bool") + self.assertIs(self.defaults["prefer_homebrew"], True) +``` + +- [ ] **Step 2: Run the test to verify it fails** + +Run: `python3 -m unittest tests.test_setup_config.MenuTests -v` + +Expected: FAIL — count is 7, and `prefer_homebrew` is missing from the menu. + +- [ ] **Step 3: Add the defaults** + +In `default.config.yml`, after the `agent_python_packages` block and before `vscode`, add: + +```yaml +# Prefer Homebrew formulae over apt when both are available (Linuxbrew). +# Ignored on macOS, where Homebrew is the only supported package manager. +prefer_homebrew: true + +# Apt package names that a Homebrew formula also provides under a different +# name. Used to avoid installing both when Homebrew is preferred. +brew_apt_equivalents: + nodejs: node + npm: node + kubectl: kubernetes-cli +``` + +- [ ] **Step 4: Add the TUI menu item** + +In `setup-config-menu.json`, in the `system` category, insert after the `oh_my_zsh` item: + +```json + { + "key": "prefer_homebrew", + "label": "Prefer Homebrew over apt", + "type": "bool" + }, +``` + +- [ ] **Step 5: Run the tests to verify they pass** + +Run: `python3 -m unittest tests.test_setup_config -v` + +Expected: PASS, including `test_categories_and_counts` and `test_prefer_homebrew_toggle`. + +- [ ] **Step 6: Commit** + +```bash +git add default.config.yml setup-config-menu.json tests/test_setup_config.py docs/superpowers/specs/2026-10-25-package-manager-precedence-design.md docs/superpowers/plans/2026-10-25-package-manager-precedence.md +git commit -m "Add Homebrew preference settings and plan" +``` + +--- + +### Task 2: Detect Homebrew independently of pkg_mgr + +**Files:** +- Create: `tasks/package_manager.yml` +- Create: `tests/test_brew_detection.yml` +- Modify: `main.yml:21-50` +- Modify: `tasks/homebrew_list.yml` +- Modify: `tasks/ansible-pull-cron.yml:7-11` + +**Interfaces:** +- Produces: `brew_bin` (string path), `brew_bin_dir` (string), `brew_available` (bool-ish; always consumed with `| bool`). +- Produces: `installed_brew_formulae`, `installed_brew_casks`, `brew_taps`, `available_brew_formulae`, each with a `stdout_lines` list, even when brew is unavailable. + +- [ ] **Step 1: Write the failing detection test** + +Create `tests/test_brew_detection.yml`: + +```yaml +- name: Homebrew detection + hosts: localhost + gather_facts: true + tasks: + - name: Detect with Homebrew preferred + ansible.builtin.import_tasks: ../tasks/package_manager.yml + vars: + prefer_homebrew: true + + - name: Assert detection matches brew presence + ansible.builtin.assert: + that: + - (brew_available | bool) == (brew_bin | length > 0) + + - name: Detect with Homebrew not preferred + ansible.builtin.import_tasks: ../tasks/package_manager.yml + vars: + prefer_homebrew: false + + - name: Assert preference only disables brew where apt is an alternative + ansible.builtin.assert: + that: + - (brew_available | bool) == ((brew_bin | length > 0) and ansible_facts.pkg_mgr == "homebrew") +``` + +- [ ] **Step 2: Run the test to verify it fails** + +Run: `ansible-playbook -i localhost, -c local tests/test_brew_detection.yml` + +Expected: FAIL — `../tasks/package_manager.yml` does not exist. + +- [ ] **Step 3: Create the detection task file** + +Create `tasks/package_manager.yml`: + +```yaml +# Resolve whether Homebrew should win over apt. On Linux, Ansible reports +# pkg_mgr == "apt" even when Linuxbrew is installed, so brew detection must be +# independent of pkg_mgr. +- name: Locate Homebrew + ansible.builtin.command: + cmd: which brew + environment: + PATH: >- + {{ lookup('env', 'HOME') }}/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/bin:/opt/homebrew/bin:/usr/local/bin:{{ ansible_facts.env.PATH | default('/usr/bin:/bin') }} + register: brew_lookup + changed_when: false + failed_when: false + +- name: Record Homebrew availability + ansible.builtin.set_fact: + brew_bin: "{{ brew_lookup.stdout | trim }}" + brew_bin_dir: "{{ (brew_lookup.stdout | trim) | dirname }}" + brew_available: >- + {{ (brew_lookup.rc | default(1)) == 0 + and (brew_lookup.stdout | trim | length > 0) + and ((prefer_homebrew | default(true) | bool) or ansible_facts.pkg_mgr == "homebrew") }} + +# Keep every later expression free of undefined-variable handling. +- name: Initialise empty Homebrew package lists + ansible.builtin.set_fact: + installed_brew_formulae: {stdout_lines: []} + installed_brew_casks: {stdout_lines: []} + brew_taps: {stdout_lines: []} + available_brew_formulae: {stdout_lines: []} + when: not (brew_available | bool) +``` + +- [ ] **Step 4: Run the detection test to verify it passes** + +Run: `ansible-playbook -i localhost, -c local tests/test_brew_detection.yml` + +Expected: PASS on this machine (brew is at `/home/linuxbrew/.linuxbrew/bin/brew`) and on machines without brew. + +- [ ] **Step 5: Wire detection into the play and gate the snapshots on it** + +In `main.yml`, add this import immediately after the "Include local configuration" task and before "Ensure uv is installed": + +```yaml + - name: Resolve package manager preference + ansible.builtin.import_tasks: tasks/package_manager.yml +``` + +In the "Get list of installed packages" block, change the Homebrew condition from `ansible_facts.pkg_mgr == "homebrew"` to `brew_available | bool`: + +```yaml + - name: Get installed homebrew packages + ansible.builtin.import_tasks: tasks/homebrew_list.yml + when: brew_available | bool +``` + +In the "Update package managers" block, change the Homebrew update condition and pass the detected path: + +```yaml + - name: Update homebrew packages + community.general.homebrew: + update_homebrew: true + path: "{{ brew_bin_dir }}" + when: brew_available | bool +``` + +- [ ] **Step 6: Add the available-formulae snapshot** + +Replace `tasks/homebrew_list.yml` with: + +```yaml +- name: Get list of installed formulae + ansible.builtin.command: "{{ brew_bin }} list --formulae -1" + register: installed_brew_formulae + changed_when: false + +- name: Get list of installed casks + ansible.builtin.command: "{{ brew_bin }} list --casks -1" + register: installed_brew_casks + changed_when: false + +- name: Get list of taps + ansible.builtin.command: "{{ brew_bin }} tap" + register: brew_taps + changed_when: false + +# Used to promote apt package names that have a same-named brew formula. +- name: Get list of available formulae + ansible.builtin.command: "{{ brew_bin }} formulae" + register: available_brew_formulae + changed_when: false + failed_when: false +``` + +- [ ] **Step 7: Add Linuxbrew to the cron PATH** + +In `tasks/ansible-pull-cron.yml`, replace the "Setup cron PATH" job value with: + +```yaml + job: "/opt/homebrew/bin:/opt/homebrew/sbin:/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin" +``` + +- [ ] **Step 8: Verify syntax, lint, and the bool guard** + +Run: + +```bash +ansible-playbook --syntax-check -i localhost, -c local main.yml +ansible-lint tasks/package_manager.yml tasks/homebrew_list.yml tasks/ansible-pull-cron.yml main.yml +grep -n 'when: brew_available' main.yml +``` + +Expected: syntax and lint clean; every `brew_available` condition in `main.yml` shows `| bool`. + +- [ ] **Step 9: Commit** + +```bash +git add tasks/package_manager.yml tests/test_brew_detection.yml main.yml tasks/homebrew_list.yml tasks/ansible-pull-cron.yml +git commit -m "Detect Homebrew independently of apt" +``` + +--- + +### Task 3: Make Homebrew and apt task files precedence-aware + +**Files:** +- Modify: `tasks/homebrew_packages.yml` +- Modify: `tasks/apt_packages.yml:1-9` + +**Interfaces:** +- Consumes: `brew_bin_dir` from Task 2. +- Produces: Homebrew modules search the detected brew root; the cask task is macOS-only. + +- [ ] **Step 1: Add the Darwin guard and brew path** + +In `tasks/homebrew_packages.yml`, add `path: "{{ brew_bin_dir }}"` to the tap, formula, and cask modules. Add `- ansible_facts.system == "Darwin"` as the first condition on the cask task. The three task headers become: + +```yaml +- name: Homebrew taps for {{ groupname }} + community.general.homebrew_tap: + name: "{{ taps }}" + path: "{{ brew_bin_dir }}" + state: '{{ enabled | ternary("present", "absent") }}' +``` + +```yaml +- name: Homebrew formulae for {{ groupname }} + community.general.homebrew: + name: "{{ formulae }}" + path: "{{ brew_bin_dir }}" + state: '{{ enabled | ternary("latest", "absent") }}' +``` + +```yaml +- name: Homebrew casks for {{ groupname }} + community.general.homebrew_cask: + name: "{{ casks }}" + path: "{{ brew_bin_dir }}" + state: '{{ enabled | ternary("present", "absent") }}' + sudo_password: "{{ ansible_become_password | default() }}" + when: + - ansible_facts.system == "Darwin" + - casks | length > 0 + - casks | difference(installed_brew_casks.stdout_lines) | length > 0 or + (casks | intersect(installed_brew_casks.stdout_lines) | length > 0 and not enabled) + - enabled != "ignore" +``` + +- [ ] **Step 2: Update the apt task-file comment** + +Replace the header comment in `tasks/apt_packages.yml` with: + +```yaml +# Apt (Debian/Ubuntu) fallback for a package group. +# +# Groups run Homebrew first (tasks/package_group.yml); when the group is +# enabled, `packages` already excludes anything Homebrew provides. On removal +# (enabled false) the full apt list is passed so packages installed by either +# manager are cleaned up. +``` + +Leave the task logic unchanged. + +- [ ] **Step 3: Verify the Darwin guard and syntax** + +Run: + +```bash +grep -A8 'name: Homebrew casks' tasks/homebrew_packages.yml | grep 'Darwin' +ansible-playbook --syntax-check -i localhost, -c local main.yml +ansible-lint tasks/homebrew_packages.yml tasks/apt_packages.yml +``` + +Expected: the grep prints the Darwin condition; syntax and lint clean. + +- [ ] **Step 4: Commit** + +```bash +git add tasks/homebrew_packages.yml tasks/apt_packages.yml +git commit -m "Gate casks to macOS and pass the brew path" +``` + +--- + +### Task 4: Add the resolver, dispatcher, and migrate the groups + +**Files:** +- Create: `tasks/package_resolve.yml` +- Create: `tasks/package_group.yml` +- Create: `tests/test_package_precedence.yml` +- Modify: `main.yml:78-343` (the ten group blocks) +- Modify: `tasks/ai-dev-tools.yml:5-22` + +**Interfaces:** +- Consumes: `brew_available`, `available_brew_formulae`, `installed_brew_formulae` (Task 2); `brew_apt_equivalents` (Task 1). +- Produces: `_group_formulae`, `_group_brew_present`, `_group_apt_covered`, `_group_apt_packages`. +- Produces: `tasks/package_group.yml` accepting `enabled`, `groupname`, `formulae`, `casks`, `apt_packages`, and optional `taps`. + +- [ ] **Step 1: Write the failing resolution test** + +Create `tests/test_package_precedence.yml`: + +```yaml +- name: Package precedence resolution + hosts: localhost + gather_facts: false + vars_files: + - ../default.config.yml + vars: + available_brew_formulae: + stdout_lines: + - handbrake + - kubectx + - kubernetes-cli + - node + - syncthing + - zstd + installed_brew_formulae: + stdout_lines: [] + tasks: + - name: Same-name apt package with a brew formula + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: true + groupname: media + formulae: "{{ media_formulae }}" + apt_packages: "{{ media_apt_packages }}" + - name: Media resolves to brew + ansible.builtin.assert: + that: + - _group_formulae == ['handbrake'] + - _group_apt_packages == [] + + - name: Sync group with a brew formula and an apt-only package + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: true + groupname: sync + formulae: "{{ sync_formulae }}" + apt_packages: "{{ sync_apt_packages }}" + - name: Sync resolves to brew plus one apt gap + ansible.builtin.assert: + that: + - _group_formulae | sort == ['syncthing'] + - _group_apt_packages == ['nextcloud-desktop'] + + - name: Renamed kube equivalent + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: true + groupname: kube + formulae: "{{ kube_formulae }}" + apt_packages: "{{ kube_apt_packages }}" + - name: Kube resolves without duplicate kubectl + ansible.builtin.assert: + that: + - _group_formulae | sort == ['argocd', 'kubectx', 'kubernetes-cli'] + - _group_apt_packages == [] + + - name: Renamed node equivalents + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: true + groupname: ai-dev-tools + formulae: "{{ ai_dev_tools_formulae }}" + apt_packages: "{{ ai_dev_tools_apt_packages }}" + - name: AI tools resolve without nodejs or npm + ansible.builtin.assert: + that: + - _group_apt_packages == [] + + - name: No brew formula exists + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: true + groupname: dev-gui + formulae: "{{ dev_gui_formulae }}" + apt_packages: "{{ dev_gui_apt_packages }}" + - name: Dev GUI falls back to apt + ansible.builtin.assert: + that: + - _group_formulae == [] + - _group_apt_packages == ['fonts-firacode'] + + - name: Removal keeps the full apt list + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: false + groupname: sync + formulae: "{{ sync_formulae }}" + apt_packages: "{{ sync_apt_packages }}" + - name: Removal list is unfiltered + ansible.builtin.assert: + that: + - _group_apt_packages | sort == ['nextcloud-desktop', 'syncthing'] + + - name: Brew unavailable + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: true + groupname: sync + formulae: "{{ sync_formulae }}" + apt_packages: "{{ sync_apt_packages }}" + available_brew_formulae: {stdout_lines: []} + installed_brew_formulae: {stdout_lines: []} + - name: Apt runs everything when brew is unavailable + ansible.builtin.assert: + that: + - _group_formulae == [] + - _group_apt_packages | sort == ['nextcloud-desktop', 'syncthing'] + + - name: Formula already installed by brew + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: true + groupname: installed-check + formulae: [] + apt_packages: [fzf, zstd] + available_brew_formulae: {stdout_lines: []} + installed_brew_formulae: {stdout_lines: [fzf]} + - name: Installed brew formula shadows apt + ansible.builtin.assert: + that: + - _group_apt_packages == ['zstd'] +``` + +- [ ] **Step 2: Run the test to verify it fails** + +Run: `ansible-playbook -i localhost, -c local tests/test_package_precedence.yml` + +Expected: FAIL — `../tasks/package_resolve.yml` does not exist. + +- [ ] **Step 3: Create the resolver** + +Create `tasks/package_resolve.yml`: + +```yaml +# Compute the effective Homebrew and apt package lists for a group. +# Side-effect free so tests/test_package_precedence.yml can exercise it. +- name: Resolve effective package lists for {{ groupname }} + ansible.builtin.set_fact: + _group_formulae: >- + {{ (formulae | default([]) + + (apt_packages | default([]) | intersect(available_brew_formulae.stdout_lines | default([])))) + | unique }} + +- name: Resolve Homebrew coverage for {{ groupname }} + ansible.builtin.set_fact: + _group_brew_present: >- + {{ (_group_formulae + (installed_brew_formulae.stdout_lines | default([]))) | unique }} + +- name: Resolve aliases covered by Homebrew for {{ groupname }} + ansible.builtin.set_fact: + _group_apt_covered: >- + {{ brew_apt_equivalents | default({}) | dict2items + | selectattr('value', 'in', _group_brew_present) + | map(attribute='key') | list }} + +- name: Resolve apt fallback packages for {{ groupname }} + ansible.builtin.set_fact: + _group_apt_packages: >- + {{ ((apt_packages | default([])) | difference(_group_apt_covered) | difference(_group_brew_present)) + if enabled == true else (apt_packages | default([])) }} +``` + +- [ ] **Step 4: Run the test to verify it passes** + +Run: `ansible-playbook -i localhost, -c local tests/test_package_precedence.yml` + +Expected: PASS, all eight scenarios. + +- [ ] **Step 5: Create the dispatcher** + +Create `tasks/package_group.yml`: + +```yaml +# Manage one package group with Homebrew-first precedence: +# formulae -> apt fallback -> casks (macOS only). +# +# Vars: enabled, groupname, formulae, casks, apt_packages, taps (optional). +- name: Resolve package lists for {{ groupname }} + ansible.builtin.import_tasks: tasks/package_resolve.yml + +- name: Manage Homebrew packages for {{ groupname }} + ansible.builtin.import_tasks: tasks/homebrew_packages.yml + vars: + enabled: "{{ enabled }}" + groupname: "{{ groupname }}" + taps: "{{ taps | default([]) }}" + formulae: "{{ _group_formulae }}" + casks: "{{ casks | default([]) }}" + when: brew_available | bool + +- name: Manage apt fallback packages for {{ groupname }} + ansible.builtin.import_tasks: tasks/apt_packages.yml + vars: + enabled: "{{ enabled }}" + groupname: "{{ groupname }}" + packages: "{{ _group_apt_packages }}" + when: ansible_facts.pkg_mgr == "apt" +``` + +- [ ] **Step 6: Replace each group's two imports with one dispatcher import** + +Apply this pattern to every group block in `main.yml`: + +```yaml + - name: Manage + block: + - name: Manage packages + ansible.builtin.import_tasks: tasks/package_group.yml + vars: + enabled: "{{ | default(false) }}" + groupname: + formulae: "{{ }}" + casks: "{{ }}" + apt_packages: "{{ }}" + rescue: + - name: Warn about install failure + ansible.builtin.debug: + msg: "Failed to manage , continuing: {{ ansible_failed_result | default('unknown error') }}" +``` + +Use these exact values: + +| Label | `enabled` | `groupname` | `formulae` | `casks` | `apt_packages` | extra | +|---|---|---|---|---|---|---| +| sync tools | `install_sync_tools` | `sync` | `sync_formulae` | `sync_casks` | `sync_apt_packages` | | +| misc apps | `install_misc_apps` | `misc` | `misc_formulae` | `misc_casks` | `misc_apt_packages` | | +| entertainment apps | `install_entertainment_apps` | `entertainment` | `entertainment_formulae` | `entertainment_casks` | `entertainment_apt_packages` | | +| media tools | `install_media_tools` | `media` | `media_formulae` | `media_casks` | `media_apt_packages` | | +| kube tools | `install_kube_apps` | `kube` | `kube_formulae` | `kube_casks` | `kube_apt_packages` | `taps: "{{ kube_taps }}"` | +| dev cli tools | `install_dev_cli_tools` | `dev-cli` | `dev_cli_formulae` | `dev_cli_casks` | `dev_cli_apt_packages` | keep the rustup import after the dispatcher | +| dev gui tools | `install_dev_gui_tools` | `dev-gui` | `dev_gui_formulae` | `dev_gui_casks` | `dev_gui_apt_packages` | | +| common cli tools | `install_common_cli_tools` | `common cli` | `common_cli_formulae` | `common_cli_casks` | `common_cli_apt_packages` | | +| aws tools | `install_aws_tools` | `aws` | `aws_formulae` | `aws_casks` | `aws_apt_packages` | | +| other packages | `install_other_apps` | `other` | `install_other_formulae` | `install_other_casks` | `install_other_apt_packages` | default `true`, not `false` | + +Do not add a `taps` var to groups that do not have one. + +- [ ] **Step 7: Migrate the AI group** + +In `tasks/ai-dev-tools.yml`, replace the two imports at the top ("Homebrew packages for ai dev tools" and "Apt packages for ai dev tools") with: + +```yaml +- name: Manage ai dev tools packages + ansible.builtin.import_tasks: tasks/package_group.yml + vars: + enabled: "{{ install_ai_dev_tools | default(false) }}" + groupname: ai-dev-tools + formulae: "{{ ai_dev_tools_formulae }}" + casks: "{{ ai_dev_tools_casks }}" + apt_packages: "{{ ai_dev_tools_apt_packages }}" +``` + +Leave the remaining install/config/remove tasks unchanged. + +- [ ] **Step 8: Verify the migration** + +Run: + +```bash +ansible-playbook -i localhost, -c local tests/test_package_precedence.yml +ansible-playbook --syntax-check -i localhost, -c local main.yml +ansible-lint tasks/package_resolve.yml tasks/package_group.yml tasks/ai-dev-tools.yml main.yml +grep -c 'tasks/package_group.yml' main.yml +grep -c 'tasks/package_group.yml' tasks/ai-dev-tools.yml +ansible-playbook -i localhost, -c local main.yml --list-tasks | grep -E 'Manage (sync tools|kube tools|ai dev tools|other) packages' +``` + +Expected: resolution test PASS; syntax and lint clean; `main.yml` has 10 dispatcher imports; `ai-dev-tools.yml` has 1; each spot-checked group task appears once in `--list-tasks`. + +- [ ] **Step 9: Commit** + +```bash +git add tasks/package_resolve.yml tasks/package_group.yml tests/test_package_precedence.yml main.yml tasks/ai-dev-tools.yml +git commit -m "Prefer Homebrew packages with apt fallback" +``` + +--- + +### Task 5: Apply the same preference to git and krew + +**Files:** +- Modify: `tasks/git.yml` +- Modify: `tasks/krew.yml` + +**Interfaces:** +- Consumes: `brew_available`, `brew_bin_dir`, `installed_brew_formulae` (Task 2). + +- [ ] **Step 1: Update git** + +Replace `tasks/git.yml` with: + +```yaml +# Ensure git is always installed. git is a hard dependency of ansible-pull +# (and of many other tasks), so it must not be tied to an optional group. +# +# Homebrew wins whenever it is available. apt is used only when brew is not +# preferred/available, and then only with a sudo password and when dpkg does +# not already have git. +- name: Ensure git is installed (Homebrew) + when: + - brew_available | bool + - "'git' not in (installed_brew_formulae.stdout_lines | default([]))" + block: + - name: Ensure git is installed (Homebrew) + community.general.homebrew: + name: git + state: present + path: "{{ brew_bin_dir }}" + +- name: Ensure git is installed (apt) + when: + - ansible_facts.pkg_mgr == "apt" + - not (brew_available | bool) + - ansible_become_password is defined + - "'git' not in (installed_apt_packages.stdout_lines | default([]))" + block: + - name: Ensure git is installed (apt) + ansible.builtin.apt: + name: git + state: present + become: true +``` + +- [ ] **Step 2: Update krew** + +In `tasks/krew.yml`, change the header comment and conditions: + +```yaml +# Install krew itself. All krew setup lives here: +# - Homebrew systems: manage the `krew` formula via Homebrew. +# - Systems without Homebrew: follow the manual instructions at +# https://krew.sigs.k8s.io/docs/user-guide/setup/install/. + +- name: Manage krew via Homebrew + ansible.builtin.import_tasks: tasks/homebrew_packages.yml + vars: + enabled: "{{ install_kube_apps | default(false) }}" + groupname: krew + taps: [] + formulae: + - krew + casks: [] + when: + - brew_available | bool + +- name: Ensure git is installed (krew prerequisite, no Homebrew) + when: + - install_kube_apps | default(false) == true + - not (brew_available | bool) + - ansible_become_password is defined + - "'git' not in (installed_apt_packages.stdout_lines | default([]))" + block: + - name: Ensure git is installed (krew prerequisite, no Homebrew) + ansible.builtin.apt: + name: git + state: present + become: true + +- name: Install krew via manual instructions (no Homebrew) + ansible.builtin.shell: | + set -e + cd "$(mktemp -d)" + OS="$(uname | tr '[:upper:]' '[:lower:]')" + ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" + KREW="krew-${OS}_${ARCH}" + curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" + tar zxvf "${KREW}.tar.gz" + ./"${KREW}" install krew + args: + executable: /bin/bash + changed_when: false + when: + - install_kube_apps | default(false) == true + - not (brew_available | bool) +``` + +- [ ] **Step 3: Verify** + +Run: + +```bash +ansible-playbook --syntax-check -i localhost, -c local main.yml +ansible-lint tasks/git.yml tasks/krew.yml +grep -n 'brew_available' tasks/git.yml tasks/krew.yml +``` + +Expected: syntax and lint clean; every brew gate uses `| bool`, and every apt/manual gate uses `not (brew_available | bool)`. + +- [ ] **Step 4: Commit** + +```bash +git add tasks/git.yml tasks/krew.yml +git commit -m "Prefer Homebrew for git and krew" +``` + +--- + +### Task 6: Document the precedence and run the full verification + +**Files:** +- Modify: `AGENTS.md` +- Modify: `README.md` + +- [ ] **Step 1: Add the convention to AGENTS.md** + +Add this section under the existing conventions (after the apt-package-presence section): + +```markdown +### Package manager precedence +- Prefer Homebrew whenever `brew` is available, even on Linux where + `ansible_facts.pkg_mgr` reports `apt`. `tasks/package_manager.yml` sets + `brew_available`; gate brew tasks on `brew_available | bool`, never on + `pkg_mgr == "homebrew"`. +- Group dispatch lives in `tasks/package_group.yml`: Homebrew formulae run + first, apt supplies only packages Homebrew cannot, and casks run only on + macOS. +- `brew_apt_equivalents` in `default.config.yml` maps apt names to brew + formulae that use a different name. Same-name packages are covered + automatically from `brew formulae`. +- Set `prefer_homebrew: false` to force apt on a Linux machine that has + Homebrew installed. +``` + +- [ ] **Step 2: Mention the toggle in README.md** + +After the `setup-config` paragraph, add: + +```markdown +`prefer_homebrew` (default on) makes the playbook use Homebrew formulae +ahead of apt when both are available; apt is used only for packages +Homebrew does not provide. +``` + +- [ ] **Step 3: Run the full verification suite** + +Run: + +```bash +ansible-playbook --syntax-check -i localhost, -c local main.yml +ansible-lint main.yml tasks/package_manager.yml tasks/package_resolve.yml tasks/package_group.yml tasks/homebrew_packages.yml tasks/homebrew_list.yml tasks/apt_packages.yml tasks/git.yml tasks/krew.yml tasks/ai-dev-tools.yml tasks/ansible-pull-cron.yml +python3 -m unittest tests.test_setup_config +ansible-playbook -i localhost, -c local tests/test_brew_detection.yml +ansible-playbook -i localhost, -c local tests/test_package_precedence.yml +``` + +Expected: all commands exit 0; both resolution test plays report `failed=0`. + +- [ ] **Step 4: Confirm the old dispatch is gone** + +Run: + +```bash +grep -n 'pkg_mgr == "homebrew"' main.yml tasks/ai-dev-tools.yml tasks/git.yml tasks/krew.yml tasks/homebrew_list.yml +``` + +Expected: no output. (The only `pkg_mgr` checks left are `== "apt"` for the fallback and the macOS-only App Store path.) + +- [ ] **Step 5: Commit** + +```bash +git add AGENTS.md README.md +git commit -m "Document Homebrew package precedence" +``` + +--- + +## Self-Review Notes + +- Spec coverage: detection (§1), snapshot (§2), resolution/dispatch (§3), cask guard (§4), group wiring (§5), updates (§6), cron (§7), TUI (§8), docs (§9). Every section maps to a task. +- Test coverage: `tests/test_brew_detection.yml` covers detection and `prefer_homebrew`; `tests/test_package_precedence.yml` covers same-name promotion, renamed aliases, apt gaps, removal, brew-unavailable, and already-installed formulae. The cask guard is pinned by the Step 3 grep in Task 3. +- Naming is consistent across tasks: `brew_available`, `brew_bin`, `brew_bin_dir`, `_group_formulae`, `_group_brew_present`, `_group_apt_covered`, `_group_apt_packages`. diff --git a/docs/superpowers/specs/2026-10-25-package-manager-precedence-design.md b/docs/superpowers/specs/2026-10-25-package-manager-precedence-design.md new file mode 100644 index 0000000..fb225e2 --- /dev/null +++ b/docs/superpowers/specs/2026-10-25-package-manager-precedence-design.md @@ -0,0 +1,284 @@ +# Package Manager Precedence — Design Spec + +Date: 2026-10-25 +Status: proposed, pending approval + +## Purpose + +On Linux machines that have both Homebrew (Linuxbrew) and apt, the playbook +currently treats the machine as apt-only. Ansible reports +`ansible_facts.pkg_mgr == "apt"` even when brew is installed, so every +package group runs its apt path and skips its Homebrew path. Homebrew +packages are generally newer, and the user wants brew to win whenever it is +available, with apt used only for packages brew cannot supply. + +Desired precedence for a package group: + +1. Homebrew formula (when Homebrew is available and the package has one) +2. apt (when apt is available and the package has no Homebrew formula) +3. Homebrew cask (macOS only; skipped entirely on non-macOS) + +## Background / current state + +- `main.yml` dispatches each package group on + `ansible_facts.pkg_mgr == "homebrew"` or `== "apt"`. On Linux+brew only + the apt branch runs. +- Group configuration keeps parallel lists: `*_formulae`, `*_casks`, and + `*_apt_packages`. They are not strict equivalents: + - `kube_formulae` contains `kubernetes-cli`; `kube_apt_packages` contains + `kubectl`. + - `ai_dev_tools_formulae` contains `node`; `ai_dev_tools_apt_packages` + contains `nodejs` and `npm`. + - `sync_casks`/`media_casks`/`misc_casks` are macOS GUI apps with apt + equivalents in `*_apt_packages`. +- Casks are not installable on Linux, so they must never run there. +- `git.yml` and `krew.yml` also branch on `pkg_mgr`; `krew.yml` falls back + to a manual install whenever it does not see Homebrew. +- The ansible-pull cron `PATH` does not include + `/home/linuxbrew/.linuxbrew/bin`, so even a brew-aware playbook would miss + brew during unattended runs. +- `community.general.homebrew` and `homebrew_tap` already default to + searching `/home/linuxbrew/.linuxbrew/bin`; `homebrew_cask` does not, but + casks will be macOS-only. + +## Goals + +- Detect Homebrew independently of `ansible_facts.pkg_mgr`. +- Prefer Homebrew formulae on any machine where brew is available. +- Promote apt-listed packages to Homebrew automatically when a same-named + formula exists, so groups with empty `*_formulae` lists still get brew + versions. +- Cover the few renamed equivalents with a small explicit mapping. +- Use apt only for packages not covered by Homebrew. +- Never run casks on non-macOS. +- Keep the three-valued group semantics (`true` install, `false` remove, + `ignore` skip) and unattended sudo gating. +- Make the preference overridable with `prefer_homebrew: true|false`. + +## Non-goals + +- No changes to official-installer paths: `uv`, `opencode`, `omp`, `herdr`, + `rustup`, and `bootstrap.sh`. +- No per-OS package-list split (no `*_linux_formulae`). +- No removal of apt packages that were installed outside the playbook. +- No automatic mapping of every possible apt/brew rename; only the + configured equivalents. +- No TUI list editor for the equivalent map. + +## Approaches considered + +### A. Prefer brew, auto-promote apt entries, apt fills gaps (chosen) + +Detect brew; install `*_formulae` plus apt entries that have a same-named +brew formula; skip apt for anything already covered by brew (same name, +installed formula, or explicit alias); run casks only on macOS. + +- Pros: delivers brew freshness without editing every group's formula list; + apt remains the safety net for Linux-only packages; one alias map. +- Cons: promotion is implicit, and one extra `brew formulae` query. + +### B. Same precedence, explicit lists only + +Brew installs only `*_formulae`; apt handles the rest, with the alias map. + +- Pros: fully predictable; no implicit manager changes. +- Cons: groups with empty `*_formulae` (sync, media, misc) keep using apt; + populating them can install both a CLI formula and a macOS cask. + +### C. Literal formulae → apt → casks with no coverage logic + +Run each list through its native manager in order. + +- Rejected: installs both sides (`kubectx` twice, `node` plus `nodejs`), + still runs apt for everything, and so does not actually prefer brew. + +## Design + +### 1. Homebrew detection and preference (`tasks/package_manager.yml`) + +New pre-task file, imported after `~/.setup-options` is loaded and before +package lists are gathered: + +- Locate brew with `which brew`, searching the normal PATH plus + `~/.linuxbrew/bin`, `/home/linuxbrew/.linuxbrew/bin`, `/opt/homebrew/bin`, + and `/usr/local/bin`. `failed_when: false`. +- Set facts: + - `brew_bin` — resolved absolute path (empty when absent) + - `brew_bin_dir` — `dirname` of `brew_bin` + - `brew_available` — brew found AND + (`prefer_homebrew` is true OR `ansible_facts.pkg_mgr == "homebrew"`). + The second clause keeps macOS working when the preference is off, since + apt is not an alternative there. +- When brew is unavailable, initialise `installed_brew_formulae`, + `installed_brew_casks`, `brew_taps`, and `available_brew_formulae` to + `{stdout_lines: []}` so later expressions never touch an undefined var. + +`default.config.yml` gains: + +```yaml +prefer_homebrew: true +``` + +### 2. Package-list snapshot (`tasks/homebrew_list.yml`) + +- Gate the file on `brew_available` in `main.yml` instead of + `pkg_mgr == "homebrew"`. +- Invoke `{{ brew_bin }}` explicitly so cron runs do not depend on PATH. +- Add `available_brew_formulae` via `{{ brew_bin }} formulae` + (`changed_when: false`, `failed_when: false`), used for auto-promotion. + +### 3. Effective list resolution (`tasks/package_resolve.yml`) + +`tasks/package_resolve.yml` computes the effective lists only, so it can be +exercised by a side-effect-free test playbook. `tasks/package_group.yml` is +the dispatcher: it imports the resolver once per group with +`enabled`, `groupname`, `formulae`, `casks`, `apt_packages`, and optional +`taps`, then imports the Homebrew and apt task files. + +The resolver computes (via `set_fact`, one step each): + +```text +_group_formulae = unique(formulae + (apt_packages ∩ available_brew_formulae)) +_group_brew_present = unique(_group_formulae + installed_brew_formulae) +_group_apt_covered = keys of brew_apt_equivalents whose value ∈ _group_brew_present +_group_apt_packages = + enabled == true -> apt_packages - _group_brew_present - _group_apt_covered + enabled != true -> apt_packages # removal keeps the full list +``` + +`tasks/package_group.yml` then imports, in order: + +1. `tasks/homebrew_packages.yml` when `brew_available`, passing + `formulae: _group_formulae`, `casks`, `taps`, and the unchanged + `enabled`/`groupname`. +2. `tasks/apt_packages.yml` when `pkg_mgr == "apt"`, passing + `packages: _group_apt_packages`. + +`brew_apt_equivalents` lives in `default.config.yml`: + +```yaml +brew_apt_equivalents: + nodejs: node + npm: node + kubectl: kubernetes-cli +``` + +The map is keyed by apt name and valued by brew formula. It covers the +cases where the names differ; same-name packages are covered by the +intersection with `available_brew_formulae` or the installed formula list. + +Removal (`enabled: false`) keeps the full apt list so packages installed by +either manager are removed. Auto-promoted names are also removed via +Homebrew, which is a no-op when brew never installed them. + +### 4. Homebrew task file (`tasks/homebrew_packages.yml`) + +- Add `path: "{{ brew_bin_dir }}"` to the tap, formula, and cask modules so + non-standard brew roots work. +- Gate the cask task on `ansible_facts.system == "Darwin"` as well as its + existing conditions. This is the "skip casks on non-mac" decision. +- Keep formulae before casks. On Linux apt runs after this file; on macOS + apt never runs, so the effective order is still formulae → apt (n/a) → + casks. + +### 5. Group wiring (`main.yml`, `tasks/ai-dev-tools.yml`) + +Replace each pair of Homebrew/apt imports with one +`tasks/package_group.yml` import. Group blocks keep their existing +`rescue` warnings and `install_*` defaults. The kube group also passes +`taps`. `tasks/ai-dev-tools.yml` uses the same dispatcher for its package +lists before its installer/config tasks. + +`tasks/krew.yml` changes from `pkg_mgr == "homebrew"` to `brew_available` +for the brew formula, and from `!= "homebrew"` to `not brew_available` for +the apt prerequisite and manual install. + +`tasks/git.yml`: + +- Install via Homebrew when `brew_available` and git is absent from the + brew list. +- Install via apt only when `pkg_mgr == "apt"`, `not brew_available`, a + become password is available, and git is absent from dpkg. + +### 6. Package-manager updates (`main.yml`) + +- Homebrew update task runs when `brew_available`, with + `path: "{{ brew_bin_dir }}"`. +- apt cache update is unchanged (needed for fallback packages on Linux+brew). + +### 7. Cron PATH (`tasks/ansible-pull-cron.yml`) + +Add `/home/linuxbrew/.linuxbrew/bin` and +`/home/linuxbrew/.linuxbrew/sbin` to the cron `PATH` so unattended runs +detect brew. Keep the existing macOS entries. + +### 8. Configuration UI + +Add `prefer_homebrew` as a boolean under "System & shell" in +`setup-config-menu.json` (label "Prefer Homebrew over apt"), so it can be +changed per machine. Update the menu item count test from 7 to 8. + +### 9. Documentation + +Update `AGENTS.md` with the precedence and alias-map conventions, and +mention `prefer_homebrew` in the README/TUI description. + +## Files touched + +| File | Change | +|---|---| +| `default.config.yml` | `prefer_homebrew`, `brew_apt_equivalents` | +| `main.yml` | detection import, brew list gating, group dispatcher, update gating | +| `tasks/package_manager.yml` | new: brew detection and initialization | +| `tasks/package_resolve.yml` | new: effective-list resolution (side-effect free) | +| `tasks/package_group.yml` | new: imports the resolver, Homebrew, and apt tasks | +| `tests/test_brew_detection.yml` | new: portable assertions for detection/preference | +| `tests/test_package_precedence.yml` | new: assertions for resolution scenarios | +| `tasks/homebrew_list.yml` | absolute brew, available formulae list | +| `tasks/homebrew_packages.yml` | `path`, macOS-only casks | +| `tasks/apt_packages.yml` | comment/fallback wording only | +| `tasks/git.yml` | brew-first, apt only without brew | +| `tasks/krew.yml` | brew-first, manual only without brew | +| `tasks/ai-dev-tools.yml` | use package dispatcher | +| `tasks/ansible-pull-cron.yml` | Linuxbrew in cron PATH | +| `setup-config-menu.json` | `prefer_homebrew` toggle | +| `tests/test_setup_config.py` | system item count | +| `AGENTS.md`, `README.md` | conventions and toggle | + +## Risks and edge cases + +- `brew formulae` is a large list (~17k on this machine) and may warn about + untrusted taps on stderr. It is read once, errors are ignored, and stdout + is the only input. +- Auto-promotion may move a package from apt to brew without a config edit. + The alias map and curated group lists limit surprises; set + `prefer_homebrew: false` to force apt. +- Some macOS GUI casks have Linux apt equivalents (`vlc`, `nextcloud`). On + Linux the cask is skipped and apt supplies the app; on macOS apt is not + used and the cask supplies it. +- On Linux+brew, `git` and `krew` must not be installed twice. Their apt and + manual branches are gated off whenever brew is available. +- Unattended runs still cannot install fallback apt packages without a + become password; brew packages continue to install. + +## Verification + +- `ansible-playbook --syntax-check -i localhost, main.yml` +- `ansible-lint main.yml tasks/package_manager.yml tasks/package_group.yml + tasks/homebrew_packages.yml tasks/homebrew_list.yml tasks/git.yml + tasks/krew.yml tasks/ai-dev-tools.yml tasks/apt_packages.yml` +- `python3 -m unittest tests.test_setup_config` +- `ansible-playbook -i localhost, -c local tests/test_brew_detection.yml` + asserts detection consistency with and without `prefer_homebrew`. +- `ansible-playbook -i localhost, -c local tests/test_package_precedence.yml` + asserts the resolution scenarios below without installing anything: + - A test play that imports the resolution logic for representative + groups, expecting: + - sync: brew `syncthing`, apt `nextcloud-desktop` + - kube: brew `kubernetes-cli`/`kubectx`, no apt fallback + - ai: brew `fzf`/`node`, no apt `fzf`/`nodejs`/`npm` + - dev-gui: no brew formula, apt `fonts-firacode` + - media: brew `handbrake` + - `prefer_homebrew: false` on Linux: apt lists only +- `ansible-playbook main.yml --list-tasks` to confirm the group dispatches + are present exactly once. diff --git a/main.yml b/main.yml index 82d16b7..875fc6e 100644 --- a/main.yml +++ b/main.yml @@ -18,6 +18,9 @@ file: "{{ lookup('env', 'HOME') }}/.setup-options" when: local_options_file.stat.exists + - name: Resolve package manager preference + ansible.builtin.import_tasks: tasks/package_manager.yml + - name: Ensure uv is installed ansible.builtin.import_tasks: tasks/uv.yml @@ -25,7 +28,7 @@ block: - name: Get installed homebrew packages ansible.builtin.import_tasks: tasks/homebrew_list.yml - when: ansible_facts.pkg_mgr == "homebrew" + when: brew_available | bool - name: Get installed apt packages ansible.builtin.import_tasks: tasks/apt_list.yml @@ -39,7 +42,8 @@ - name: Update homebrew packages community.general.homebrew: update_homebrew: true - when: ansible_facts.pkg_mgr == "homebrew" + path: "{{ brew_bin_dir }}" + when: brew_available | bool - name: Update apt cache ansible.builtin.apt: @@ -78,23 +82,13 @@ - name: Manage sync tools block: - name: Manage sync tools packages - ansible.builtin.import_tasks: tasks/homebrew_packages.yml + ansible.builtin.import_tasks: tasks/package_group.yml vars: enabled: "{{ install_sync_tools | default(false) }}" groupname: sync formulae: "{{ sync_formulae }}" casks: "{{ sync_casks }}" - when: - - ansible_facts.pkg_mgr == "homebrew" - - - name: Manage sync tools packages (apt) - ansible.builtin.import_tasks: tasks/apt_packages.yml - vars: - enabled: "{{ install_sync_tools | default(false) }}" - groupname: sync - packages: "{{ sync_apt_packages }}" - when: - - ansible_facts.pkg_mgr == "apt" + apt_packages: "{{ sync_apt_packages }}" rescue: - name: Warn about sync tools install failure ansible.builtin.debug: @@ -103,23 +97,13 @@ - name: Manage misc apps block: - name: Manage misc apps packages - ansible.builtin.import_tasks: tasks/homebrew_packages.yml + ansible.builtin.import_tasks: tasks/package_group.yml vars: enabled: "{{ install_misc_apps | default(false) }}" groupname: misc formulae: "{{ misc_formulae }}" casks: "{{ misc_casks }}" - when: - - ansible_facts.pkg_mgr == "homebrew" - - - name: Manage misc apps packages (apt) - ansible.builtin.import_tasks: tasks/apt_packages.yml - vars: - enabled: "{{ install_misc_apps | default(false) }}" - groupname: misc - packages: "{{ misc_apt_packages }}" - when: - - ansible_facts.pkg_mgr == "apt" + apt_packages: "{{ misc_apt_packages }}" rescue: - name: Warn about misc apps install failure ansible.builtin.debug: @@ -128,23 +112,13 @@ - name: Manage entertainment apps block: - name: Manage entertainment apps packages - ansible.builtin.import_tasks: tasks/homebrew_packages.yml + ansible.builtin.import_tasks: tasks/package_group.yml vars: enabled: "{{ install_entertainment_apps | default(false) }}" groupname: entertainment formulae: "{{ entertainment_formulae }}" casks: "{{ entertainment_casks }}" - when: - - ansible_facts.pkg_mgr == "homebrew" - - - name: Manage entertainment apps packages (apt) - ansible.builtin.import_tasks: tasks/apt_packages.yml - vars: - enabled: "{{ install_entertainment_apps | default(false) }}" - groupname: entertainment - packages: "{{ entertainment_apt_packages }}" - when: - - ansible_facts.pkg_mgr == "apt" + apt_packages: "{{ entertainment_apt_packages }}" rescue: - name: Warn about entertainment apps install failure ansible.builtin.debug: @@ -153,23 +127,13 @@ - name: Manage media tools block: - name: Manage media tools packages - ansible.builtin.import_tasks: tasks/homebrew_packages.yml + ansible.builtin.import_tasks: tasks/package_group.yml vars: enabled: "{{ install_media_tools | default(false) }}" groupname: media formulae: "{{ media_formulae }}" casks: "{{ media_casks }}" - when: - - ansible_facts.pkg_mgr == "homebrew" - - - name: Manage media tools packages (apt) - ansible.builtin.import_tasks: tasks/apt_packages.yml - vars: - enabled: "{{ install_media_tools | default(false) }}" - groupname: media - packages: "{{ media_apt_packages }}" - when: - - ansible_facts.pkg_mgr == "apt" + apt_packages: "{{ media_apt_packages }}" rescue: - name: Warn about media tools install failure ansible.builtin.debug: @@ -178,24 +142,14 @@ - name: Manage kube tools block: - name: Manage kube tools packages - ansible.builtin.import_tasks: tasks/homebrew_packages.yml + ansible.builtin.import_tasks: tasks/package_group.yml vars: enabled: "{{ install_kube_apps | default(false) }}" groupname: kube taps: "{{ kube_taps }}" formulae: "{{ kube_formulae }}" casks: "{{ kube_casks }}" - when: - - ansible_facts.pkg_mgr == "homebrew" - - - name: Manage kube tools packages (apt) - ansible.builtin.import_tasks: tasks/apt_packages.yml - vars: - enabled: "{{ install_kube_apps | default(false) }}" - groupname: kube - packages: "{{ kube_apt_packages }}" - when: - - ansible_facts.pkg_mgr == "apt" + apt_packages: "{{ kube_apt_packages }}" rescue: - name: Warn about kube tools install failure ansible.builtin.debug: @@ -204,23 +158,13 @@ - name: Manage dev cli tools block: - name: Manage dev cli tools packages - ansible.builtin.import_tasks: tasks/homebrew_packages.yml + ansible.builtin.import_tasks: tasks/package_group.yml vars: enabled: "{{ install_dev_cli_tools | default(false) }}" groupname: dev-cli formulae: "{{ dev_cli_formulae }}" casks: "{{ dev_cli_casks }}" - when: - - ansible_facts.pkg_mgr == "homebrew" - - - name: Manage dev cli tools packages (apt) - ansible.builtin.import_tasks: tasks/apt_packages.yml - vars: - enabled: "{{ install_dev_cli_tools | default(false) }}" - groupname: dev-cli - packages: "{{ dev_cli_apt_packages }}" - when: - - ansible_facts.pkg_mgr == "apt" + apt_packages: "{{ dev_cli_apt_packages }}" - name: Manage rustup toolchain ansible.builtin.import_tasks: tasks/rustup.yml @@ -232,23 +176,13 @@ - name: Manage dev gui tools block: - name: Manage dev gui tools packages - ansible.builtin.import_tasks: tasks/homebrew_packages.yml + ansible.builtin.import_tasks: tasks/package_group.yml vars: enabled: "{{ install_dev_gui_tools | default(false) }}" groupname: dev-gui - formulae: [] + formulae: "{{ dev_gui_formulae }}" casks: "{{ dev_gui_casks }}" - when: - - ansible_facts.pkg_mgr == "homebrew" - - - name: Manage dev gui tools packages (apt) - ansible.builtin.import_tasks: tasks/apt_packages.yml - vars: - enabled: "{{ install_dev_gui_tools | default(false) }}" - groupname: dev-gui - packages: "{{ dev_gui_apt_packages }}" - when: - - ansible_facts.pkg_mgr == "apt" + apt_packages: "{{ dev_gui_apt_packages }}" rescue: - name: Warn about dev gui tools install failure ansible.builtin.debug: @@ -270,23 +204,13 @@ - name: Manage common cli tools block: - name: Manage common cli tools packages - ansible.builtin.import_tasks: tasks/homebrew_packages.yml + ansible.builtin.import_tasks: tasks/package_group.yml vars: enabled: "{{ install_common_cli_tools | default(false) }}" groupname: common cli formulae: "{{ common_cli_formulae }}" casks: "{{ common_cli_casks }}" - when: - - ansible_facts.pkg_mgr == "homebrew" - - - name: Manage common cli tools packages (apt) - ansible.builtin.import_tasks: tasks/apt_packages.yml - vars: - enabled: "{{ install_common_cli_tools | default(false) }}" - groupname: common cli - packages: "{{ common_cli_apt_packages }}" - when: - - ansible_facts.pkg_mgr == "apt" + apt_packages: "{{ common_cli_apt_packages }}" rescue: - name: Warn about common cli tools install failure ansible.builtin.debug: @@ -295,23 +219,13 @@ - name: Manage aws tools block: - name: Manage aws tools packages - ansible.builtin.import_tasks: tasks/homebrew_packages.yml + ansible.builtin.import_tasks: tasks/package_group.yml vars: enabled: "{{ install_aws_tools | default(false) }}" groupname: aws formulae: "{{ aws_formulae }}" casks: "{{ aws_casks }}" - when: - - ansible_facts.pkg_mgr == "homebrew" - - - name: Manage aws tools packages (apt) - ansible.builtin.import_tasks: tasks/apt_packages.yml - vars: - enabled: "{{ install_aws_tools | default(false) }}" - groupname: aws - packages: "{{ aws_apt_packages }}" - when: - - ansible_facts.pkg_mgr == "apt" + apt_packages: "{{ aws_apt_packages }}" rescue: - name: Warn about aws tools install failure ansible.builtin.debug: diff --git a/setup-config-menu.json b/setup-config-menu.json index 63d28e9..8b1e8d8 100644 --- a/setup-config-menu.json +++ b/setup-config-menu.json @@ -156,6 +156,11 @@ "label": "Oh My Zsh + powerlevel10k", "type": "bool" }, + { + "key": "prefer_homebrew", + "label": "Prefer Homebrew over apt", + "type": "bool" + }, { "key": "agent_python", "label": "Agent Python venv", diff --git a/tasks/ai-dev-tools.yml b/tasks/ai-dev-tools.yml index 090d603..9a65896 100644 --- a/tasks/ai-dev-tools.yml +++ b/tasks/ai-dev-tools.yml @@ -2,24 +2,14 @@ # true - install and manage everything below # false - remove what this group installed (brew handled by homebrew_packages) # ignore - skip the group entirely, leave everything as-is -- name: Homebrew packages for ai dev tools - ansible.builtin.import_tasks: tasks/homebrew_packages.yml +- name: Manage ai dev tools packages + ansible.builtin.import_tasks: tasks/package_group.yml vars: enabled: "{{ install_ai_dev_tools | default(false) }}" groupname: ai-dev-tools formulae: "{{ ai_dev_tools_formulae }}" casks: "{{ ai_dev_tools_casks }}" - when: - - ansible_facts.pkg_mgr == "homebrew" - -- name: Apt packages for ai dev tools - ansible.builtin.import_tasks: tasks/apt_packages.yml - vars: - enabled: "{{ install_ai_dev_tools | default(false) }}" - groupname: ai-dev-tools - packages: "{{ ai_dev_tools_apt_packages }}" - when: - - ansible_facts.pkg_mgr == "apt" + apt_packages: "{{ ai_dev_tools_apt_packages }}" # --- true: install and manage --- diff --git a/tasks/ansible-pull-cron.yml b/tasks/ansible-pull-cron.yml index 41654bb..fc88f67 100644 --- a/tasks/ansible-pull-cron.yml +++ b/tasks/ansible-pull-cron.yml @@ -8,7 +8,21 @@ ansible.builtin.cron: name: PATH env: true - job: "/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin" + job: >- + {{ + [ + '/opt/homebrew/bin', + '/opt/homebrew/sbin', + '/home/linuxbrew/.linuxbrew/bin', + '/home/linuxbrew/.linuxbrew/sbin', + '/usr/local/bin', + '/usr/local/sbin', + '/usr/bin', + '/usr/sbin', + '/bin', + '/sbin', + ] | join(':') + }} # The script itself decides full re-apply vs quiet sync from the age of # ~/.ansible.last-full-apply, so no flags are needed at the cron level. diff --git a/tasks/apt_list.yml b/tasks/apt_list.yml index cda821c..5697658 100644 --- a/tasks/apt_list.yml +++ b/tasks/apt_list.yml @@ -6,3 +6,4 @@ register: installed_apt_packages changed_when: false failed_when: false + check_mode: false diff --git a/tasks/apt_packages.yml b/tasks/apt_packages.yml index d8e2d24..8c0dd8a 100644 --- a/tasks/apt_packages.yml +++ b/tasks/apt_packages.yml @@ -1,12 +1,9 @@ -# Apt (Debian/Ubuntu) package management for a group. +# Apt (Debian/Ubuntu) fallback for a package group. # -# Unlike the Homebrew path (formulae + casks), apt uses a single list of -# package names provided via `packages`. -# -# The apt module needs root (become), which is only available when a become -# password is supplied; unattended runs therefore skip these tasks. We also -# consult the dpkg snapshot gathered once in main.yml so apt is only invoked -# when something actually needs to change. +# Groups run Homebrew first (tasks/package_group.yml); when the group is +# enabled, `packages` already excludes anything Homebrew provides. On removal +# (enabled false) the full apt list is passed so packages installed by either +# manager are cleaned up. - name: Apt packages for {{ groupname }} when: - enabled != "ignore" diff --git a/tasks/git.yml b/tasks/git.yml index e2e55a6..46aa920 100644 --- a/tasks/git.yml +++ b/tasks/git.yml @@ -1,23 +1,24 @@ # Ensure git is always installed. git is a hard dependency of ansible-pull # (and of many other tasks), so it must not be tied to an optional group. # -# On apt systems installation requires root (become). Such tasks cannot run -# unattended without a sudo password, so they are gated on -# ansible_become_password being defined. We also skip the install when git is -# already present (per the dpkg snapshot gathered once in main.yml). +# Homebrew wins whenever it is available. apt is used only when brew is not +# preferred/available, and then only with a sudo password and when dpkg does +# not already have git. - name: Ensure git is installed (Homebrew) when: - - ansible_facts.pkg_mgr == "homebrew" + - brew_available | bool - "'git' not in (installed_brew_formulae.stdout_lines | default([]))" block: - name: Ensure git is installed (Homebrew) community.general.homebrew: name: git state: present + path: "{{ brew_bin_dir }}" - name: Ensure git is installed (apt) when: - ansible_facts.pkg_mgr == "apt" + - not (brew_available | bool) - ansible_become_password is defined - "'git' not in (installed_apt_packages.stdout_lines | default([]))" block: diff --git a/tasks/homebrew_list.yml b/tasks/homebrew_list.yml index 4f06d8b..26c2be3 100644 --- a/tasks/homebrew_list.yml +++ b/tasks/homebrew_list.yml @@ -1,14 +1,25 @@ - name: Get list of installed formulae - ansible.builtin.command: brew list --formulae -1 + ansible.builtin.command: "{{ brew_bin }} list --formulae -1" register: installed_brew_formulae changed_when: false + check_mode: false - name: Get list of installed casks - ansible.builtin.command: brew list --casks -1 + ansible.builtin.command: "{{ brew_bin }} list --casks -1" register: installed_brew_casks changed_when: false + check_mode: false - name: Get list of taps - ansible.builtin.command: brew tap + ansible.builtin.command: "{{ brew_bin }} tap" register: brew_taps changed_when: false + check_mode: false + +# Used to promote apt package names that have a same-named brew formula. +- name: Get list of available formulae + ansible.builtin.command: "{{ brew_bin }} formulae" + register: available_brew_formulae + changed_when: false + check_mode: false + failed_when: false diff --git a/tasks/homebrew_packages.yml b/tasks/homebrew_packages.yml index a15b190..fa4eb84 100644 --- a/tasks/homebrew_packages.yml +++ b/tasks/homebrew_packages.yml @@ -1,6 +1,7 @@ - name: Homebrew taps for {{ groupname }} community.general.homebrew_tap: name: "{{ taps }}" + path: "{{ brew_bin_dir }}" state: '{{ enabled | ternary("present", "absent") }}' when: - taps is defined @@ -12,6 +13,7 @@ - name: Homebrew formulae for {{ groupname }} community.general.homebrew: name: "{{ formulae }}" + path: "{{ brew_bin_dir }}" state: '{{ enabled | ternary("latest", "absent") }}' when: - formulae | length > 0 @@ -22,9 +24,11 @@ - name: Homebrew casks for {{ groupname }} community.general.homebrew_cask: name: "{{ casks }}" + path: "{{ brew_bin_dir }}" state: '{{ enabled | ternary("present", "absent") }}' sudo_password: "{{ ansible_become_password | default() }}" when: + - ansible_facts.system == "Darwin" - casks | length > 0 - casks | difference(installed_brew_casks.stdout_lines) | length > 0 or (casks | intersect(installed_brew_casks.stdout_lines) | length > 0 and not enabled) diff --git a/tasks/krew.yml b/tasks/krew.yml index b9e60ee..1d4d7d4 100644 --- a/tasks/krew.yml +++ b/tasks/krew.yml @@ -1,6 +1,6 @@ # Install krew itself. All krew setup lives here: # - Homebrew systems: manage the `krew` formula via Homebrew. -# - Other systems: follow the manual instructions at +# - Systems without Homebrew: follow the manual instructions at # https://krew.sigs.k8s.io/docs/user-guide/setup/install/. - name: Manage krew via Homebrew @@ -13,16 +13,16 @@ - krew casks: [] when: - - ansible_facts.pkg_mgr == "homebrew" + - brew_available | bool -- name: Ensure git is installed (krew prerequisite, non-Homebrew) +- name: Ensure git is installed (krew prerequisite, no Homebrew) when: - install_kube_apps | default(false) == true - - ansible_facts.pkg_mgr != "homebrew" + - not (brew_available | bool) - ansible_become_password is defined - "'git' not in (installed_apt_packages.stdout_lines | default([]))" block: - - name: Ensure git is installed (krew prerequisite, non-Homebrew) + - name: Ensure git is installed (krew prerequisite, no Homebrew) ansible.builtin.apt: name: git state: present @@ -43,4 +43,4 @@ changed_when: false when: - install_kube_apps | default(false) == true - - ansible_facts.pkg_mgr != "homebrew" + - not (brew_available | bool) diff --git a/tasks/package_group.yml b/tasks/package_group.yml new file mode 100644 index 0000000..d59c48c --- /dev/null +++ b/tasks/package_group.yml @@ -0,0 +1,21 @@ +# Manage one package group with Homebrew-first precedence: +# formulae -> apt fallback -> casks (macOS only). +# +# Vars: enabled, groupname, formulae, casks, apt_packages, taps (optional). +- name: Resolve package lists for {{ groupname }} + ansible.builtin.import_tasks: tasks/package_resolve.yml + +# Note: enabled, groupname, casks and taps are inherited in scope by the +# imported files; restating them here with the same name would cause a +# recursive-loop templating error at runtime. Only renames are declared. +- name: Manage Homebrew packages for {{ groupname }} + ansible.builtin.import_tasks: tasks/homebrew_packages.yml + vars: + formulae: "{{ _group_formulae }}" + when: brew_available | bool + +- name: Manage apt fallback packages for {{ groupname }} + ansible.builtin.import_tasks: tasks/apt_packages.yml + vars: + packages: "{{ _group_apt_packages }}" + when: ansible_facts.pkg_mgr == "apt" diff --git a/tasks/package_manager.yml b/tasks/package_manager.yml new file mode 100644 index 0000000..9f1b5c1 --- /dev/null +++ b/tasks/package_manager.yml @@ -0,0 +1,39 @@ +# Resolve whether Homebrew should win over apt. On Linux, Ansible reports +# pkg_mgr == "apt" even when Linuxbrew is installed, so brew detection must be +# independent of pkg_mgr. +- name: Locate Homebrew + ansible.builtin.command: + cmd: which brew + check_mode: false + environment: + PATH: >- + {{ + [ + lookup('env', 'HOME') ~ '/.linuxbrew/bin', + '/home/linuxbrew/.linuxbrew/bin', + '/opt/homebrew/bin', + '/usr/local/bin', + ansible_facts.env.PATH | default('/usr/bin:/bin'), + ] | join(':') + }} + register: brew_lookup + changed_when: false + failed_when: false + +- name: Record Homebrew availability + ansible.builtin.set_fact: + brew_bin: "{{ brew_lookup.stdout | trim }}" + brew_bin_dir: "{{ (brew_lookup.stdout | trim) | dirname }}" + brew_available: >- + {{ (brew_lookup.rc | default(1)) == 0 + and (brew_lookup.stdout | trim | length > 0) + and ((prefer_homebrew | default(true) | bool) or ansible_facts.pkg_mgr == "homebrew") }} + +# Keep every later expression free of undefined-variable handling. +- name: Initialise empty Homebrew package lists + ansible.builtin.set_fact: + installed_brew_formulae: {stdout_lines: []} + installed_brew_casks: {stdout_lines: []} + brew_taps: {stdout_lines: []} + available_brew_formulae: {stdout_lines: []} + when: not (brew_available | bool) diff --git a/tasks/package_resolve.yml b/tasks/package_resolve.yml new file mode 100644 index 0000000..080535d --- /dev/null +++ b/tasks/package_resolve.yml @@ -0,0 +1,26 @@ +# Compute the effective Homebrew and apt package lists for a group. +# Side-effect free so tests/test_package_precedence.yml can exercise it. +- name: Resolve effective package lists for {{ groupname }} + ansible.builtin.set_fact: + _group_formulae: >- + {{ (formulae | default([]) + + (apt_packages | default([]) | intersect(available_brew_formulae.stdout_lines | default([])))) + | unique }} + +- name: Resolve Homebrew coverage for {{ groupname }} + ansible.builtin.set_fact: + _group_brew_present: >- + {{ (_group_formulae + (installed_brew_formulae.stdout_lines | default([]))) | unique }} + +- name: Resolve aliases covered by Homebrew for {{ groupname }} + ansible.builtin.set_fact: + _group_apt_covered: >- + {{ brew_apt_equivalents | default({}) | dict2items + | selectattr('value', 'in', _group_brew_present) + | map(attribute='key') | list }} + +- name: Resolve apt fallback packages for {{ groupname }} + ansible.builtin.set_fact: + _group_apt_packages: >- + {{ ((apt_packages | default([])) | difference(_group_apt_covered) | difference(_group_brew_present)) + if enabled == true else (apt_packages | default([])) }} diff --git a/tests/test_brew_detection.yml b/tests/test_brew_detection.yml new file mode 100644 index 0000000..95b2a9a --- /dev/null +++ b/tests/test_brew_detection.yml @@ -0,0 +1,23 @@ +- name: Homebrew detection + hosts: localhost + gather_facts: true + tasks: + - name: Detect with Homebrew preferred + ansible.builtin.import_tasks: ../tasks/package_manager.yml + vars: + prefer_homebrew: true + + - name: Assert detection matches brew presence + ansible.builtin.assert: + that: + - (brew_available | bool) == (brew_bin | length > 0) + + - name: Detect with Homebrew not preferred + ansible.builtin.import_tasks: ../tasks/package_manager.yml + vars: + prefer_homebrew: false + + - name: Assert preference only disables brew where apt is an alternative + ansible.builtin.assert: + that: + - (brew_available | bool) == ((brew_bin | length > 0) and ansible_facts.pkg_mgr == "homebrew") diff --git a/tests/test_package_precedence.yml b/tests/test_package_precedence.yml new file mode 100644 index 0000000..f7d819d --- /dev/null +++ b/tests/test_package_precedence.yml @@ -0,0 +1,121 @@ +- name: Package precedence resolution + hosts: localhost + gather_facts: false + vars_files: + - ../default.config.yml + vars: + available_brew_formulae: + stdout_lines: + - handbrake + - kubectx + - kubernetes-cli + - node + - syncthing + - zstd + installed_brew_formulae: + stdout_lines: [] + tasks: + - name: Same-name apt package with a brew formula + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: true + groupname: media + formulae: "{{ media_formulae }}" + apt_packages: "{{ media_apt_packages }}" + - name: Media resolves to brew + ansible.builtin.assert: + that: + - _group_formulae == ['handbrake'] + - _group_apt_packages == [] + + - name: Sync group with a brew formula and an apt-only package + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: true + groupname: sync + formulae: "{{ sync_formulae }}" + apt_packages: "{{ sync_apt_packages }}" + - name: Sync resolves to brew plus one apt gap + ansible.builtin.assert: + that: + - _group_formulae | sort == ['syncthing'] + - _group_apt_packages == ['nextcloud-desktop'] + + - name: Renamed kube equivalent + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: true + groupname: kube + formulae: "{{ kube_formulae }}" + apt_packages: "{{ kube_apt_packages }}" + - name: Kube resolves without duplicate kubectl + ansible.builtin.assert: + that: + - _group_formulae | sort == ['argocd', 'kubectx', 'kubernetes-cli'] + - _group_apt_packages == [] + + - name: Renamed node equivalents + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: true + groupname: ai-dev-tools + formulae: "{{ ai_dev_tools_formulae }}" + apt_packages: "{{ ai_dev_tools_apt_packages }}" + - name: AI tools resolve without nodejs or npm + ansible.builtin.assert: + that: + - _group_apt_packages == [] + + - name: No brew formula exists + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: true + groupname: dev-gui + formulae: "{{ dev_gui_formulae }}" + apt_packages: "{{ dev_gui_apt_packages }}" + - name: Dev GUI falls back to apt + ansible.builtin.assert: + that: + - _group_formulae == [] + - _group_apt_packages == ['fonts-firacode'] + + - name: Removal keeps the full apt list + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: false + groupname: sync + formulae: "{{ sync_formulae }}" + apt_packages: "{{ sync_apt_packages }}" + - name: Removal list is unfiltered + ansible.builtin.assert: + that: + - _group_apt_packages | sort == ['nextcloud-desktop', 'syncthing'] + + - name: Brew unavailable + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: true + groupname: sync + formulae: "{{ sync_formulae }}" + apt_packages: "{{ sync_apt_packages }}" + available_brew_formulae: {stdout_lines: []} + installed_brew_formulae: {stdout_lines: []} + - name: Apt runs everything when brew is unavailable + ansible.builtin.assert: + that: + - _group_formulae == [] + - _group_apt_packages | sort == ['nextcloud-desktop', 'syncthing'] + + - name: Formula already installed by brew + ansible.builtin.import_tasks: ../tasks/package_resolve.yml + vars: + enabled: true + groupname: installed-check + formulae: [] + apt_packages: [fzf, zstd] + available_brew_formulae: {stdout_lines: []} + installed_brew_formulae: {stdout_lines: [fzf]} + - name: Installed brew formula shadows apt + ansible.builtin.assert: + that: + - _group_apt_packages == ['zstd'] diff --git a/tests/test_setup_config.py b/tests/test_setup_config.py index 918f5bc..4dfb8ea 100644 --- a/tests/test_setup_config.py +++ b/tests/test_setup_config.py @@ -71,9 +71,15 @@ def setUp(self): def test_categories_and_counts(self): self.assertEqual([c.id for c in self.menu.categories], ["groups", "system", "macos"]) self.assertEqual(len(self.menu.categories[0].items), 11) - self.assertEqual(len(self.menu.categories[1].items), 7) + self.assertEqual(len(self.menu.categories[1].items), 8) self.assertEqual(len(self.menu.categories[2].items), 4) + def test_prefer_homebrew_toggle(self): + items = {i.key: i for c in self.menu.categories for i in c.items} + self.assertIn("prefer_homebrew", items) + self.assertEqual(items["prefer_homebrew"].kind, "bool") + self.assertIs(self.defaults["prefer_homebrew"], True) + def test_package_keys_cover_read_only_lists(self): self.assertIn("agent_python_packages", self.menu.package_keys) self.assertIn("install_mas_apps", self.menu.package_keys)