twpayne / chezmoi

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

Unable to get templates working #3163

Closed rioka closed 1 year ago

rioka commented 1 year ago

What exactly are you trying to do?

Just started playing with chezmoi after reading a few blogs, and wanted to understand how it works when used with multiple PCs with different OSes; one feature that caught my eye is the possiblity to use templates, so I tried use them to manage .bashrc.

What have you tried so far?

Working on a Windows 10 machine, I installed chezmoi (using scoop), run chezmoi init, then added .bashrc as a template; so far, so good.

Then, I created dot_bashrc_linux and dot_bashrc_windows

Mostly copied snippets from Manage machine-to-machine differences

When I run chezmoi diff I get this error

chezmoi: template: dot_bashrc.tmpl:2:5: executing "dot_bashrc.tmpl" at <include ".bashrc_windows">: error calling include: open C:/Users/********/.local/share/chezmoi/.bashrc_windows: The system cannot find the file specified.

If I replace {{ include ".bashrc_windows" }} with {{ include "dot_bashrc_windows" }}, I do not get the error any more, but apparently it is detecting .bashrc_windows as a new file to add to my home directory (and it's eventually added if I run chezmoi apply, unless I add --interactive and decide to skip it)

Where else have you checked for solutions?

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

$ chezmoi --verbose diff
diff --git a/.bashrc b/.bashrc
index 8765918d4b6c360fdbef38b50d8eb1c7e7104f9a..e17ab6b4dfd2fcd0c299afcaff5a95894d0accb1 100666
--- a/.bashrc
+++ b/.bashrc
@@ -1,19 +1,3 @@
-eval "$(oh-my-posh init bash)"

-# aliases
-alias ls='ls -la --color'
-
-# git aliases
-alias gs='git status'
... (removed)
+# windows .bashrc contents

-# general
... (removed)
diff --git a/.bashrc_windows b/.bashrc_windows
new file mode 100666
index 0000000000000000000000000000000000000000..dcacd3277c16a809d48bac9f45b62c28d6c9f89e
--- /dev/null
+++ b/.bashrc_windows
@@ -0,0 +1 @@
+# windows .bashrc contents

Output of chezmoi doctor

```console $ chezmoi doctor RESULT CHECK MESSAGE ok version v2.35.2, commit 44814515eb8034b4d153569db86e58e278148e05, built at 2023-07-19T09:14:31Z, built by goreleaser warning latest-version v2.37.0 ok os-arch windows/amd64 ok systeminfo Microsoft Windows 10 Enterprise (10.0.19045 N/A Build 19045) ok go-version go1.20.6 (gc) ok executable ~/scoop/apps/chezmoi/current/chezmoi.exe 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 cd-command found C:/WINDOWS/system32/cmd.exe ok cd-args 'C:\\WINDOWS\\system32\\cmd.exe' info diff-command not set ok edit-command found C:/WINDOWS/system32/notepad.exe ok edit-args 'C:\\WINDOWS\\system32\\notepad.exe' ok git-command found C:/Program Files/Git/cmd/git.exe, version 2.41.0 warning merge-command vimdiff not found in $PATH ok shell-command found C:/WINDOWS/system32/cmd.exe ok shell-args 'C:\\WINDOWS\\system32\\cmd.exe' info age-command age not found in $PATH ok gpg-command found ~/scoop/apps/gpg/current/bin/gpg.exe, version 2.4.3 info pinentry-command not set info 1password-command op not found in $PATH info bitwarden-command bw not found in $PATH info dashlane-command dcli 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

A few things are happening here.

My understanding is that the files are at C:/Users/********/.local/share/chezmoi/dot_bashrc_{linux,windows}.

https://www.chezmoi.io/reference/templates/functions/include/

Relative paths are interpreted relative to the source directory.

Replacing .bashrc_windows with dot_bashrc_windows in the include will correctly include the contents, but this brings its own issue as you have discovered.

Instead, consider one of the following approaches:

  1. Combine the linux and windows contents directly in dot_bashrc.tmpl, using conditionals as needed.
  2. Move dot_bashrc_{linux,windows} to .chezmoitemplates/bashrc/{linux,windows} (.chezmoitemplates doc) and change {{ include ".bashrc_{linux,windows}" }} to {{ template "bashrc/{linux,windows}" }} in dot_bashrc.tmpl.
rioka commented 1 year ago

@bradenhilton thanks mate, it works. 🎆

I think the snippet at https://www.chezmoi.io/user-guide/manage-machine-to-machine-differences/#without-using-symlinks should be updated then, because as it is now, unless I'm missing something, it does not work.

Anyway, I'm glad I can proceed with my experiments... really like this tool

twpayne commented 1 year ago

I think the snippet at https://www.chezmoi.io/user-guide/manage-machine-to-machine-differences/#without-using-symlinks should be updated then, because as it is now, unless I'm missing something, it does not work.

What exactly does not work? Note that the subsection you link to is written assuming that you've read the section that it is part of.

rioka commented 1 year ago

@twpayne

So I guess I'm missing something... what I was trying to do is based on section "Use completely different dotfiles on different machines", simply (or at least this is what it looks to me) replacing "darwin" with "windows"; so I created files dot_bashrc_windows and dot_bashrc_linux, but I did not want to use symlinks... few lines below, I read this statement

The same thing can be achieved using the include function

And there's the snippet which did not work...

The changes suggested here fixed my problem (and I'm more that happy with them) but I still can't figure out what I did wrong the first time, or what part I missed from the linked page.

It's just curiosity: when something does not work as expected (or I cannot make it work) I just want to find out what I did wrong.

I totally agree I am likely missing something trivial, just want to figure out what it is.

🙇