twpayne / chezmoi

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

Having issues with getting values from `chezmoi` in templates #1108

Closed triallax closed 3 years ago

triallax commented 3 years ago

What exactly are you trying to do?

Trying to write a template for a configuration file which has different locations on different systems. I have a file in .chezmoitemplates; let's call it file.toml. Here are the file's contents (I've removed all unnecessary details):

[foo]
bar = "{{ chezmoi.homeDir }}"

I then have the actual config file with the following content:

{{- template "file.toml" }}

The issue is that {{ chezmoi.homeDir }} doesn't work, giving me the following error:

chezmoi: template: file.toml:2:18: executing "file.toml" at <.chezmoi.homeDir>: nil data; no entry for key "chezmoi"

What have you tried so far?

Running chezmoi execute-template "{{ .chezmoi.homeDir }}" works just fine.

Where else have you checked for solutions?

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

$ chezmoi apply --verbose
chezmoi: template: file.toml:2:18: executing "file.toml" at <.chezmoi.homeDir>: nil data; no entry for key "chezmoi"

Output of chezmoi doctor

$ chezmoi doctor
RESULT    CHECK                MESSAGE
ok        version              v2.0.4, commit 9cc8184cc466a87c44006218e02365046e5d5f8b, built at 2021-03-24T18:55:30Z, built by goreleaser
ok        os-arch              linux/amd64
ok        config-file          /home/mhmdanas/.config/chezmoi/chezmoi.toml is a file
ok        source-dir           /home/mhmdanas/.local/share/chezmoi is a directory
ok        suspicious-entries   no suspicious entries found in /home/mhmdanas/.local/share/chezmoi
ok        dest-dir             /home/mhmdanas is a directory
ok        shell                found /home/mhmdanas/.cargo/bin/nu
ok        editor               found /usr/bin/nano
ok        git-cli              found /usr/bin/git, version 2.30.2
warning   merge-cli            vimdiff not found in $PATH
info      age-cli              age not found in $PATH
ok        gnupg-cli            found /usr/bin/gpg, version 2.2.25
info      1password-cli        op not found in $PATH
info      bitwarden-cli        bw not found in $PATH
info      gopass-cli           gopass not found in $PATH
info      keepassxc-cli        keepassxc-cli not found in $PATH
info      keepassxc-db         not set
info      lastpass-cli         lpass not found in $PATH
info      pass-cli             pass not found in $PATH
info      vault-cli            vault not found in $PATH
info      secret-cli           not set

Additional context

None.

zb140 commented 3 years ago

When a template gets executed with {{ template "name" }}, it will be executed with nil data unless you explicitly provide some other value. (See here in the docs for Go's text/template feature for more details).

You should be able to fix it by explicitly passing . in instead:

{{- template "file.toml" . }}

I haven't actually tested this myself because when I tried I got some very weird errors trying to add a new template in .chezmoitemplates. (I'll file a separate issue about that if I can't track it down). But I'm pretty sure it'll work 😄

triallax commented 3 years ago

Thanks for the help! I'm away from the machine with the error right now, so I'll be keeping this open till I confirm that everything works. I was following https://www.chezmoi.io/docs/how-to/#handle-different-file-locations-on-different-systems-with-the-same-contents, which does not include the explicit passing of . you noted. Maybe I'll open a PR to update that part.

triallax commented 3 years ago

@zb140 that seems to have done the trick. Thanks again!