twpayne / chezmoi

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

Unable to add `~/.config/chezmoi/chezmoi.toml` to chezmoi #3638

Closed youtux closed 8 months ago

youtux commented 8 months ago

What exactly are you trying to do?

I made a config file so that every edit I make is autocommited and pushed. I would like to have this configuration apply to all the machines I use.

What have you tried so far?

I tried to add the config file to chezmoi, but it complains saying it's protected:

❯ chezmoi add ~/.config/chezmoi/chezmoi.toml
chezmoi: /Users/youtux/.config/chezmoi/chezmoi.toml: cannot add chezmoi file to chezmoi (/Users/youtux/.config/chezmoi/chezmoi.toml is protected)

Where else have you checked for solutions?

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

❯ chezmoi --verbose  add ~/.config/chezmoi/chezmoi.toml
chezmoi: /Users/youtux/.config/chezmoi/chezmoi.toml: cannot add chezmoi file to chezmoi (/Users/youtux/.config/chezmoi/chezmoi.toml is protected)

Output of chezmoi doctor

```console ❯ chezmoi doctor RESULT CHECK MESSAGE ok version v2.47.1, commit 1ce6b2eeb0caf75bd91883e5a968e713a26e7be2, built at 2024-03-03T00:47:52Z, built by Homebrew ok latest-version v2.47.1 ok os-arch darwin/arm64 ok uname Darwin m1pro.local 23.3.0 Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:44 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6000 arm64 ok go-version go1.22.0 (gc) ok executable /opt/homebrew/bin/chezmoi ok upgrade-method brew-upgrade ok config-file ~/.config/chezmoi/chezmoi.toml, last modified 2024-03-09T16:12:29+01:00 ok source-dir ~/.local/share/chezmoi is a git working tree (clean) ok suspicious-entries no suspicious entries ok working-tree ~/.local/share/chezmoi is a git working tree (clean) 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 ~/.local/bin/codew ok edit-args ~/.local/bin/codew ok git-command found /opt/homebrew/bin/git, version 2.44.0 ok merge-command found /opt/homebrew/bin/vimdiff ok shell-command found /bin/zsh ok shell-args /bin/zsh info age-command age not found in $PATH ok gpg-command found /opt/homebrew/bin/gpg, version 2.4.4 info pinentry-command not set ok 1password-command found /opt/homebrew/bin/op, version 2.24.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 ```

Additional context

Add any other context about the problem here.

bradenhilton commented 8 months ago

You need to use .chezmoi.$FORMAT.tmpl. In your case it would be .chezmoi.toml.tmpl.

You can use your existing config by simply moving/renaming it.

Running chezmoi init will execute this template and generate a config.

youtux commented 8 months ago

shouldn't then I be able to run chezmoi add --template ~/.config/chezmoi/chezmoi.toml? It still gives me the same error. If this is an allowed way, why would chezomoi not allow that operation?

bradenhilton commented 8 months ago

That command would create ~/.local/share/chezmoi/dot_config/chezmoi/chezmoi.toml.tmpl which is still incorrect.

We prevent this because allowing this means that any changes you make to your config would always be one chezmoi apply behind the rest of your dotfiles.

You can move ~/.config/chezmoi/chezmoi.toml to ~/.local/share/chezmoi/.chezmoi.toml.tmpl then run chezmoi init.

Every time you change your config template you must also run chezmoi init to regenerate your config.

youtux commented 8 months ago

I wish the docs would describe this specific use case, it was not immediate to me how this works.

For posterity, I solved the issue running the following:

mv ~/.config/chezmoi/chezmoi.toml "$(chezmoi source-path)/.chezmoi.toml.tmpl"
chezmoi apply
chezmoi init
halostatue commented 8 months ago

They do:

If you have some suggestions for improving the documentation to make this easier to find, we will happily accept either pull requests or new issues with those suggestions. But it is in the documentation.

We do request that people search all issues before opening a new one, as a closely related question was asked two days ago (#3634), which has pointers to #3405, #2854, and #3285—the latter two of which have the text from the error in their title and/or body.

youtux commented 8 months ago

Ah sorry, I only used google assuming it would have indexed issues with that string. What I was suggesting was to add this to a more visible place in the docs, like the FAQs, since this seem to be a frequently asked question :) maybe even including this error message for better indexing.

Or, why not allowing that file to be just added to the dotfiles, since one can already workaround it by adding the .chezmoi.toml.tmpl?

halostatue commented 8 months ago

Ah sorry, I only used google assuming it would have indexed issues with that string. What I was suggesting was to add this to a more visible place in the docs, like the FAQs, since this seem to be a frequently asked question :) maybe even including this error message for better indexing.

Or, why not allowing that file to be just added to the dotfiles, since one can already workaround it by adding the .chezmoi.toml.tmpl?

The issues linked above have the reasons why this cannot be resolved, but https://github.com/twpayne/chezmoi/issues/3405#issuecomment-1852470433 (and probably other linked issues) explains why it has to be handled differently.

youtux commented 8 months ago

got it. One last suggestion then: maybe it would be worth to have the error message from chezmoi add ~/.config/chezmoi/chezmoi.toml would link to the page that explains how to add chezmoi config to the tracked dotfiles.

twpayne commented 8 months ago

One last suggestion then: maybe it would be worth to have the error message from chezmoi add ~/.config/chezmoi/chezmoi.toml

Good idea. Implemented in #3644. I've used text instead of a URL (as the URL might change over time), but the text should be a sufficient clue for future users.

twpayne commented 8 months ago

BTW @youtux you should remove this file from your dotfiles repo.

bradenhilton commented 8 months ago

That entire chezmoi directory should be removed.

@twpayne do we not also protect against chezmoi add */.chezmoiignore?

twpayne commented 8 months ago

@twpayne do we not also protect against chezmoi add */.chezmoiignore?

Not at the moment. At the moment, chezmoi protects against adding files in its config (~/.config/chezmoi), state (~/.local/share/chezmoi), and cache (~/.cache/chezmoi) directories. .chezmoiignore files have no effect outside of chezmoi's state.