twpayne / chezmoi

Manage your dotfiles across multiple diverse machines, securely.
https://www.chezmoi.io/
MIT License
13.39k stars 493 forks source link

Default values for data in chezmoi.toml #2978

Closed klnusbaum closed 1 year ago

klnusbaum commented 1 year ago

What exactly are you trying to do?

I have the following template for my kitt.conf

font_family      FuraMono Nerd Font Mono
bold_font        auto
italic_font      auto
bold_italic_font auto
font_size        {{ if .termFontSize -}} {{ .termFontSize }} {{- else -}} 14.0 {{- end }}

The idea is that if a ~/.config/chezmoi/chezmoi.toml exists and .termFontSize is set, use that value. Otherwise, I'd like the font size to default to 14.0.

What have you tried so far?

Trying to use the above template with no ~/.config/chezmoi/chezmoi.toml file present results in the following error when I run chezmoi apply

$ chezmoi apply
chezmoi: template: dot_config/kitty/kitty.conf.tmpl:5:23: executing "dot_config/kitty/kitty.conf.tmpl" at <.termFontSize>: map has no entry for key "termFontSize"

I also tried reading https://www.chezmoi.io/user-guide/manage-machine-to-machine-differences/#use-templates but that doesn't seem to have any information on how to specify default values.

Where else have you checked for solutions?

Output of any commands you've tried with --verbose flag

$ chezmoi --verbose apply
chezmoi: template: dot_config/kitty/kitty.conf.tmpl:5:23: executing "dot_config/kitty/kitty.conf.tmpl" at <.termFontSize>: map has no entry for key "termFontSize"

Output of chezmoi doctor

```console 2023-05-08 12:14:48 PDT kurtis@pop-os # chezmoi doctor RESULT CHECK MESSAGE ok version v2.27.2, commit 882d0808feb1fc8112b411ed2216f31306656861, built at 2022-11-24T23:41:28Z, built by goreleaser warning latest-version v2.33.4 ok os-arch linux/amd64 (Pop!_OS 22.04 LTS) ok uname Linux pop-os 6.2.6-76060206-generic #202303130630~1681329778~22.04~d824cd4 SMP PREEMPT_DYNAMIC Wed A x86_64 x86_64 x86_64 GNU/Linux ok go-version go1.19.3 (gc) ok executable /usr/bin/chezmoi ok upgrade-method sudo-upgrade-package ok config-file no config file found warning source-dir ~/.local/share/chezmoi is a git working tree (dirty) ok suspicious-entries no suspicious entries warning working-tree ~/.local/share/chezmoi is a git working tree (dirty) ok dest-dir ~ is a directory ok umask 002 ok cd-command found /bin/zsh ok cd-args /bin/zsh info diff-command not set ok edit-command found /usr/local/bin/nvim ok edit-args /usr/local/bin/nvim ok git-command found /usr/bin/git, version 2.34.1 warning merge-command vimdiff not found in $PATH ok shell-command found /bin/zsh ok shell-args /bin/zsh info age-command age not found in $PATH ok gpg-command found /usr/bin/gpg, version 2.2.27 info pinentry-command not set info 1password-command op not found in $PATH info bitwarden-command bw not found in $PATH info gopass-command gopass not found in $PATH info keepassxc-command keepassxc-cli not found in $PATH info keepassxc-db not set info keeper-command keeper not found in $PATH info lastpass-command lpass not found in $PATH info pass-command pass not found in $PATH info passhole-command ph not found in $PATH info vault-command vault not found in $PATH info secret-command not set ```
klnusbaum commented 1 year ago

There may be a different way to try to accomplish what I'm doing. Just curious as to how one might approach this.

halostatue commented 1 year ago

There are several options for this:

klnusbaum commented 1 year ago

Thanks, that did the trick!

I ended up going with:

font_size        {{ index . "termFontSize" | default "14.0" }}