chezmoi apply installs packages via tmpl script. tmpl script is a loop of the pkg list.
is it possible to omit already install pkgs with template syntax of chezmoi?
Assistant
Yes, you can use chezmoi's template functions to check if packages are already installed and skip them. Here are a few approaches:
Using the output function with package manager queries
For apt-based systems:
go-template
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
{{- range .packages }}
{{- $installed := output "dpkg" "-l" . | regexMatch (printf "^ii\\s+%s\\s+" .) }}
{{- if not $installed }}
apt install -y {{ . }}
{{- end }}
{{- end }}
For Homebrew:
go-template
{{- range .packages }}
{{- $installed := output "brew" "list" "--formula" | regexMatch . }}
{{- if not $installed }}
brew install {{ . }}
{{- end }}
{{- end }}
Using lookPath for command-based checks
If you're installing tools that provide commands:
go-template
{{- range .packages }}
{{- if not (lookPath .command) }}
# Install {{ .name }}
your-package-manager install {{ .name }}
{{- end }}
{{- end }}
More robust approach with error handling
go-template
{{- range .packages }}
{{- $checkCmd := list "dpkg" "-s" . }}
{{- $installed := false }}
{{- with output $checkCmd | regexMatch "Status: install ok installed" }}
{{- $installed = true }}
{{- end }}
{{- if not $installed }}
apt install -y {{ . }}
{{- end }}
{{- end }}
The key is using chezmoi's output function to run shell commands that check installation status, then using Go template conditionals to control whether installation commands are included in the generated script.
User
and for void linux?
and a check with hash?
Assistant
For Void Linux and using hash to check commands:
Void Linux with xbps
go-template
{{- range .packages }}
{{- $installed := output "xbps-query" "-l" | regexMatch (printf "^ii %s-[0-9]" .) }}
{{- if not $installed }}
xbps-install -y {{ . }}
{{- end }}
{{- end }}
Or more precisely checking installation status:
go-template
{{- range .packages }}
{{- $checkResult := output "sh" "-c" (printf "xbps-query %s >/dev/null 2>&1 && echo installed || echo not-installed" .) }}
{{- if eq $checkResult "not-installed" }}
xbps-install -y {{ . }}
{{- end }}
{{- end }}
Using hash to check for commands
go-template
{{- range .packages }}
{{- $hasCommand := output "sh" "-c" (printf "hash %s 2>/dev/null && echo found || echo not-found" .command) }}
{{- if eq $hasCommand "not-found" }}
xbps-install -y {{ .package }}
{{- end }}
{{- end }}
Combined approach for Void Linux
go-template
{{- range .tools }}
{{- $installed := false }}
{{- if .command }}
{{- $hasCommand := output "sh" "-c" (printf "hash %s 2>/dev/null && echo true || echo false" .command) }}
{{- if eq $hasCommand "true" }}
{{- $installed = true }}
{{- end }}
{{- else }}
{{- $pkgCheck := output "sh" "-c" (printf "xbps-query %s >/dev/null 2>&1 && echo true || echo false" .package) }}
{{- if eq $pkgCheck "true" }}
{{- $installed = true }}
{{- end }}
{{- end }}
{{- if not $installed }}
xbps-install -y {{ .package }}
{{- end }}
{{- end }}
With a data structure like:
yaml
tools:
- package: "git"
command: "git"
- package: "nodejs"
command: "node"
- package: "some-lib"
# no command field for libraries
The hash approach is faster since it's a shell builtin and doesn't spawn external processes like which or command -v.
User
can you use xbps -m instead?
Assistant
Yes, you can use xbps-query -m to check against manually installed packages:
Using xbps-query -m (manually installed packages only)
go-template
{{- range .packages }}
{{- $manuallyInstalled := output "xbps-query" "-m" | regexMatch (printf "^%s-[0-9]" .) }}
{{- if not $manuallyInstalled }}
xbps-install -y {{ . }}
{{- end }}
{{- end }}
{{- range .tools }}
{{- $shouldInstall := true }}
{{- if .command }}
{{- $hasCommand := output "sh" "-c" (printf "hash %s 2>/dev/null && echo true || echo false" .command) }}
{{- if eq $hasCommand "true" }}
{{- $shouldInstall = false }}
{{- end }}
{{- end }}
{{- if $shouldInstall }}
{{- $manuallyInstalled := output "sh" "-c" (printf "xbps-query -m %s >/dev/null 2>&1 && echo true || echo false" .package) }}
{{- if eq $manuallyInstalled "false" }}
xbps-install -y {{ .package }}
{{- end }}
{{- end }}
{{- end }}
Note that xbps-query -m only shows manually installed packages (not dependencies), so this approach will only skip packages that were explicitly installed by the user, not those installed as dependencies. This might be what you want if you want to ensure your essential tools are marked as manually installed.