Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having issues with getting values from chezmoi in templates #1108

Closed
4 tasks done
triallax opened this issue Mar 28, 2021 · 3 comments
Closed
4 tasks done

Having issues with getting values from chezmoi in templates #1108

triallax opened this issue Mar 28, 2021 · 3 comments
Labels
support Support request

Comments

@triallax
Copy link
Contributor

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.

@triallax triallax added the support Support request label Mar 28, 2021
@zb140
Copy link
Collaborator

zb140 commented Mar 28, 2021

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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

twpayne pushed a commit that referenced this issue Sep 29, 2021
The "different files locations on different systems" guide didn't pass
data to the template file, which can result in errors. See
#1108 for an example of this
problem.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
support Support request
Projects
None yet
Development

No branches or pull requests

2 participants