-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_onchange_10-install-packages-homebrew.sh.tmpl
More file actions
87 lines (78 loc) · 3.08 KB
/
run_onchange_10-install-packages-homebrew.sh.tmpl
File metadata and controls
87 lines (78 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env bash
# Chezmoi run_onchange script: Install Homebrew formulas
#
# Cascade order: brew → brew_cask → pacman → apt → dnf → rpm_ostree → flatpak → appimage
# Only includes formula (non-cask) packages assigned to brew by the cascade.
#
# packages.yaml hash: {{ include ".chezmoidata/packages.yaml" | sha256sum }}
# profiles.yaml hash: {{ include ".chezmoidata/profiles.yaml" | sha256sum }}
{{ $p := index .profiles .profile -}}
{{ if not (and (hasKey $p "brew") $p.brew) -}}
# Profile '{{ .profile }}' does not use Homebrew — nothing to do.
exit 0
{{ else -}}
set -euo pipefail
# shellcheck source=/dev/null
source "${CHEZMOI_SOURCE_DIR:-$(chezmoi source-path)}/scripts/lib/common.sh"
log "Using profile '{{ .profile }}' — installing Homebrew formulas"
# Add brew to PATH (installs Homebrew on Linux if not present)
ensure_brew_in_path
require_cmd brew
# =============================================================================
# Install formulas via brew bundle
# =============================================================================
log "Running brew bundle (formulas)..."
export HOMEBREW_NO_AUTO_UPDATE=1
BREWFILE_CONTENT=$(cat <<'BREWFILE'
{{- range $key, $pkg := .packages }}
{{- if hasKey $pkg "brew_tap" }}
{{- if or (not (hasKey $pkg "os")) (eq (default "" $pkg.os) $.chezmoi.os) }}
tap "{{ $pkg.brew_tap }}"
{{- end }}
{{- end }}
{{- end }}
{{- range $key, $pkg := .packages }}
{{- template "cascade-filter" (dict "root" $ "pkg" $pkg "key" $key "manager" "brew" "fmt" "brew \"%s\" # %s") }}
{{- end }}
{{ if eq .chezmoi.os "darwin" -}}
brew "mas"
{{- range $key, $pkg := .packages }}
{{- $mas_tags_ok := false -}}
{{- $mas_profile_tags := splitList ", " $.tags -}}
{{- range $pkg.tags -}}
{{- if has . $mas_profile_tags -}}{{- $mas_tags_ok = true -}}{{- end -}}
{{- end -}}
{{- if and $mas_tags_ok (hasKey $pkg "mas") }}
mas "{{ $pkg.desc }}", id: {{ $pkg.mas }}
{{- end }}
{{- end }}
{{ end -}}
BREWFILE
)
# Run brew bundle with retries (fresh Homebrew installs often have linking failures
# on first pass that resolve after an explicit relink)
MAX_RETRIES=3
for attempt in $(seq 1 "$MAX_RETRIES"); do
if brew bundle --file=- <<<"$BREWFILE_CONTENT"; then
break
fi
if [ "$attempt" -eq "$MAX_RETRIES" ]; then
log "brew bundle still failing after $MAX_RETRIES attempts"
exit 1
fi
log "Some packages failed (attempt $attempt/$MAX_RETRIES), relinking and retrying..."
brew list --formula | xargs -r brew link --overwrite 2>/dev/null || true
done
log "Homebrew formula installation complete!"
# =============================================================================
# Disable vendor fish conf.d files we handle ourselves with caching
# (mise uses MISE_FISH_AUTO_ACTIVATE=0 env var; direnv has no disable mechanism)
# =============================================================================
vendor_dir="$(brew --prefix)/share/fish/vendor_conf.d"
for f in direnv.fish; do
if [ -s "$vendor_dir/$f" ]; then
log "Emptying vendor conf.d/$f (handled by dotfiles with caching)"
: > "$vendor_dir/$f"
fi
done
{{ end -}}