twpayne / chezmoi

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

Chezmoi template is not able to get variables #2712

Closed cregev closed 1 year ago

cregev commented 1 year ago

What exactly are you trying to do?

I am running Chezmoi and I am trying to get variables from promptString into data in order to be able to use them in all other templates with *.tmpl file.

~ chezmoi data

{
  "chezmoi": {
    "arch": "arm64",
    "args": [
      "/opt/homebrew/bin/chezmoi",
      "data"
    ],
    "cacheDir": "/Users/costyaregev/.cache/chezmoi",
    "configFile": "/Users/costyaregev/.config/chezmoi/chezmoi.toml",
    "executable": "/opt/homebrew/bin/chezmoi",
    "fqdnHostname": "macOS-Costya-Regev",
    "gid": "20",
    "group": "staff",
    "homeDir": "/Users/costyaregev",
    "hostname": "macOS-Costya-Regev",
    "kernel": {},
    "os": "darwin",
    "osRelease": {},
    "sourceDir": "/Users/costyaregev/.local/share/chezmoi/home",
    "uid": "501",
    "username": "costyaregev",
    "version": {
      "builtBy": "Homebrew",
      "commit": "fec55002b83b3688d13988055226aa716b0c7c14",
      "date": "2023-01-19T20:40:03Z",
      "version": "2.29.3"
    },
    "windowsVersion": {},
    "workingTree": "/Users/costyaregev/.local/share/chezmoi"
  }
}

What have you tried so far?

First, I have tried to pass only 2 variables in my file .chezmoi.yaml.tmpl:

{{ $full_name := promptString "Full Name" }}
{{ $tweak_mac := promptBool "Do you want to tweak you MAC??" }}

data:
    full_name: {{ $full_name | quote }}
    tweak_mac: {{ $tweak_mac }}

When I have the following template private_config.tmpl:

Host *
  StrictHostKeyChecking no
  GSSAPIAuthentication yes
  UseRoaming no
  User local_"{{ .full_name }}"

The output of the following command:

~ chezmoi execute-template --promptString full_name="costya" --promptBool tweak_mac="true" < dot_ssh/private_config.tmpl --verbose --debug 
2023-01-27T09:25:45+02:00 INF persistentPreRunRootE args=["chezmoi","execute-template","--promptString","full_name=costya","--promptBool","tweak_mac=true","--verbose","--debug"] goVersion=go1.19.5 version={"builtBy":"Homebrew","commit":"fec55002b83b3688d13988055226aa716b0c7c14","date":"2023-01-19T20:40:03Z","version":"2.29.3"}
2023-01-27T09:25:45+02:00 INF Stat component=system name=/Users/costyaregev/.local/share/chezmoi/.git
2023-01-27T09:25:45+02:00 INF chezmoi.OSRelease error="file does not exist"
2023-01-27T09:25:45+02:00 INF ReadFile component=system data=home name=/Users/costyaregev/.local/share/chezmoi/.chezmoiroot size=4
2023-01-27T09:25:45+02:00 INF ReadFile component=system data=2.29.3 name=/Users/costyaregev/.local/share/chezmoi/.chezmoiversion size=6
2023-01-27T09:25:45+02:00 INF Stat component=system name=/Users/costyaregev/.local/share/chezmoi/home
2023-01-27T09:25:45+02:00 INF Stat component=system name=/Users/costyaregev/.local/share/chezmoi/home
2023-01-27T09:25:45+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home
2023-01-27T09:25:45+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/.chezmoiscripts
2023-01-27T09:25:45+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/.chezmoiscripts/darwin
2023-01-27T09:25:45+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/dot_k9s
2023-01-27T09:25:45+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/dot_ssh
2023-01-27T09:25:45+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/dot_vim
2023-01-27T09:25:45+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/dot_vim/autoload
2023-01-27T09:25:45+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/dot_vim/bundle
2023-01-27T09:25:45+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/private_Library
2023-01-27T09:25:45+02:00 INF ReadDir component=system name="/Users/costyaregev/.local/share/chezmoi/home/private_Library/private_Application Support"
2023-01-27T09:25:45+02:00 INF ReadDir component=system name="/Users/costyaregev/.local/share/chezmoi/home/private_Library/private_Application Support/private_Code"
2023-01-27T09:25:45+02:00 INF ReadDir component=system name="/Users/costyaregev/.local/share/chezmoi/home/private_Library/private_Application Support/private_Code/User"
chezmoi: template: stdin:5:17: executing "stdin" at <.full_name>: map has no entry for key "full_name"

Where else have you checked for solutions?

I have hard coded 2 values .chezmoi.yaml.tmpl:

data:
    full_name: "costya"
    tweak_mac: true

When I have the following template private_config.tmpl:

Host *
  StrictHostKeyChecking no
  GSSAPIAuthentication yes
  UseRoaming no
  User local_"{{ .full_name }}"

Output:

~ chezmoi execute-template --promptString full_name="costya" --promptBool tweak_mac="true" < dot_ssh/private_config.tmpl --verbose --debug
2023-01-27T09:27:16+02:00 INF persistentPreRunRootE args=["chezmoi","execute-template","--promptString","full_name=costya","--promptBool","tweak_mac=true","--verbose","--debug"] goVersion=go1.19.5 version={"builtBy":"Homebrew","commit":"fec55002b83b3688d13988055226aa716b0c7c14","date":"2023-01-19T20:40:03Z","version":"2.29.3"}
2023-01-27T09:27:16+02:00 INF Stat component=system name=/Users/costyaregev/.local/share/chezmoi/.git
2023-01-27T09:27:16+02:00 INF chezmoi.OSRelease error="file does not exist"
2023-01-27T09:27:16+02:00 INF ReadFile component=system data=home name=/Users/costyaregev/.local/share/chezmoi/.chezmoiroot size=4
2023-01-27T09:27:16+02:00 INF ReadFile component=system data=2.29.3 name=/Users/costyaregev/.local/share/chezmoi/.chezmoiversion size=6
2023-01-27T09:27:16+02:00 INF Stat component=system name=/Users/costyaregev/.local/share/chezmoi/home
2023-01-27T09:27:16+02:00 INF Stat component=system name=/Users/costyaregev/.local/share/chezmoi/home
2023-01-27T09:27:16+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home
2023-01-27T09:27:16+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/.chezmoiscripts
2023-01-27T09:27:16+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/.chezmoiscripts/darwin
2023-01-27T09:27:16+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/dot_k9s
2023-01-27T09:27:16+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/dot_ssh
2023-01-27T09:27:16+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/dot_vim
2023-01-27T09:27:16+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/dot_vim/autoload
2023-01-27T09:27:16+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/dot_vim/bundle
2023-01-27T09:27:16+02:00 INF ReadDir component=system name=/Users/costyaregev/.local/share/chezmoi/home/private_Library
2023-01-27T09:27:16+02:00 INF ReadDir component=system name="/Users/costyaregev/.local/share/chezmoi/home/private_Library/private_Application Support"
2023-01-27T09:27:16+02:00 INF ReadDir component=system name="/Users/costyaregev/.local/share/chezmoi/home/private_Library/private_Application Support/private_Code"
2023-01-27T09:27:16+02:00 INF ReadDir component=system name="/Users/costyaregev/.local/share/chezmoi/home/private_Library/private_Application Support/private_Code/User"
chezmoi: template: stdin:5:17: executing "stdin" at <.full_name>: map has no entry for key "full_name"

Output of chezmoi doctor

RESULT    CHECK                MESSAGE
ok        version              v2.29.3, commit fec55002b83b3688d13988055226aa716b0c7c14, built at 2023-01-19T20:40:03Z, built by Homebrew
warning   latest-version       v2.29.4
ok        os-arch              darwin/arm64
ok        uname                Darwin macOS-Costya-Regev 21.6.0 Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:35 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T8101 arm64
ok        go-version           go1.19.5 (gc)
ok        executable           /opt/homebrew/bin/chezmoi
ok        upgrade-method       brew-upgrade
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 /bin/zsh
ok        cd-args              /bin/zsh
info      diff-command         not set
ok        edit-command         found /usr/bin/vim
ok        edit-args            /usr/bin/vim
ok        git-command          found /opt/homebrew/bin/git, version 2.39.1
ok        merge-command        found /usr/bin/vimdiff
ok        shell-command        found /bin/zsh
ok        shell-args           /bin/zsh
info      age-command          age not found in $PATH
info      gpg-command          gpg not found in $PATH
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
ok        vault-command        found /usr/local/bin/vault, version 1.9.2
info      secret-command       not set
bradenhilton commented 1 year ago

full_name and tweak_mac don't appear in your chezmoi data. I don't think they are being initialized because I don't think underscores are supported in variable names.

https://www.chezmoi.io/reference/templates/variables:

Variable names must consist of a letter and be followed by zero or more letters and/or digits.

halostatue commented 1 year ago

The documentation should be updated, because _ are considered "letters":

❯ chezmoi execute-template '{{ $_x := .chezmoi.arch }}{{ $_x }}'
arm64

.chezmoi.yaml.tmpl is used to generate ~/.config/chezmoi/chezmoi.yaml and is not itself run during apply, only during init. So if the hardcoded values aren’t in ~/.config/chezmoi/chezmoi.yaml, the error (that the data does not contain .full_name) is correct.

twpayne commented 1 year ago

I think @halostatue has spotted the problem.

You have a config file template, but you don't have a config file. To generate the config file, run:

$ chezmoi init
twpayne commented 1 year ago

Hopefully your question has been answered. Please re-open if needed.