twpayne / chezmoi

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

Evaluating an init promptString in onepasswordDetailedFields call / working with multiple 1Password environments #3320

Closed julian45 closed 10 months ago

julian45 commented 1 year ago

What exactly are you trying to do?

I have an environment in which my home and work computers have completely different 1Password accounts installed. I'm hoping to adapt my global git config file so that a given ssh key's public key can be put into the config without relying on a constant UUID in every environment. (In other words, I'd like to be able to work using my personal key on one computer, and using my work key on another, while maintaining a complete separation of 1Password accounts.) I'm not sure if this is already something that can be done with chezmoi and I'm just going about it the wrong way in this case, or if this is something new to chezmoi and thus a feature request.

What have you tried so far?

I attempted to set up my dot_gitconfig.tmpl as follows (simplified to focus on the question at hand):

{{ $commitkeyuuid := promptString "insert 1p key item uuid here" -}}

[user]
        name =blah
        email = "blah@blah.com"
        signingkey = {{- (onepasswordDetailsFields $commitkeyuuid).public_key.value }}

In this example, I was anticipating that upon init, I'd be prompted to input the 1Password UUID of the key they want to use, and then my answer could be evaluated within the call to onepasswordDetailedFields. However, when I went to test the template by running chezmoi execute-template --init --promptString commitkeyuuid="[redacted]" < dot_gitconfig.tmpl, I received the following error:

chezmoi: template: stdin:6: undefined variable "$commitkeyuuid"

Where else have you checked for solutions?

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

$ chezmoi --verbose execute-template --init --promptString commitkeyuuid="[redacted]"  < dot_gitconfig.tmpl
chezmoi: template: stdin:6: undefined variable "$commitkeyuuid"

Output of chezmoi doctor

```console $ chezmoi doctor RESULT CHECK MESSAGE ok version v2.40.4, commit 97de3c9738828f6f5c2c282b9e8114142f07edb5, built at 2023-10-29T18:05:59Z, built by goreleaser ok latest-version v2.40.4 ok os-arch linux/amd64 (Rocky Linux 9.2 (Blue Onyx)) ok uname Linux Athena 5.15.90.1-microsoft-standard-WSL2 #1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux ok go-version go1.21.3 (gc) ok executable ~/bin/chezmoi ok upgrade-method replace-executable 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 022 ok cd-command found /usr/bin/zsh ok cd-args /usr/bin/zsh info diff-command not set ok edit-command found /usr/bin/nvim ok edit-args /usr/bin/nvim ok git-command found /usr/bin/git, version 2.39.3 ok merge-command found /usr/bin/vimdiff ok shell-command found /usr/bin/zsh ok shell-args /usr/bin/zsh info age-command age not found in $PATH ok gpg-command found /usr/bin/gpg, version 2.3.3 info pinentry-command not set ok 1password-command found /usr/bin/op, version 2.22.0 info bitwarden-command bw not found in $PATH info bitwarden-secrets-command bws not found in $PATH info dashlane-command dcli not found in $PATH info doppler-command doppler 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 rbw-command rbw not found in $PATH info vault-command vault not found in $PATH info vlt-command vlt not found in $PATH info secret-command not set ```
bradenhilton commented 1 year ago

prompt* functions aren't available outside of a chezmoi config template.

https://www.chezmoi.io/reference/templates/init-functions/

If this wasn't the case, your template would prompt you for a UUID every time the template was executed (chezmoi diff, chezmoi apply etc.).

julian45 commented 1 year ago

I might be missing something, but I don't quite understand how that's not what I'm already trying to do here. All of the prompt* calls are taking place within a config template, as shown in my original comment.

If I'm correctly understanding the init function reference, as well as the reference for chezmoi execute-template, the template I described seems like it should — whether in the context of a chezmoi init, or in a call to chezmoi execute-template call with --init and --promptString commitkeyuuid="[redacted]" — evaluate $commitkeyuuid and pass it to onepasswordDetailsFields for evaluation.

bradenhilton commented 1 year ago

You're using a prompt* template function inside a template for .gitconfig. Those functions can only be used in a template for chezmoi's own config.

See https://www.chezmoi.io/reference/special-files-and-directories/chezmoi-format-tmpl/

julian45 commented 1 year ago

OK, I see now! Thank you for clarifying.

I did not quite understand from documentation that prompt* functions were specifically limited to chezmoi's own config. In the init function reference, your link, and this FAQ answer, the words "config file" and "template" are used, and given that chezmoi is, at its core, a utility for managing configuration files of various kinds, it wasn't clear to me that the limit to .chezmoi.$FORMAT.tmpl was present.

That, along with the prompt* function examples' formatting looking a lot like an excerpt from a .gitconfig file and the non-presence of filenames in those examples, led me to believe that it may have been possible to use the prompt* functions in other templates. Sorry about that! 😅

Would it be reasonable to suggest that language could be added to some of those pages to help make the specificity clear? I would be happy to draft and make the pull request myself if so.

bradenhilton commented 1 year ago

No need to apologize!

Would it be reasonable to suggest that language could be added to some of those pages to help make the specificity clear? I would be happy to draft and make the pull request myself if so.

Sure. Documentation improvements are always welcome and appreciated. I'll assign this issue to you (no rush)...

twpayne commented 10 months ago

Please open a PR when you have a proposed changed to the documentation. In the meantime, I'll close this issue.