twpayne / chezmoi

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

How can I manage chezmoi configuration with chezmoi? #3405

Closed raimundomartins closed 9 months ago

raimundomartins commented 9 months ago

What exactly are you trying to do?

I'm trying to manage chezmoi's config with chezmoi, but for some reason it's "protected".

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

I can understand .local/share/chezmoi being protected, but not this.

Where else have you checked for solutions?

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

$ chezmoi --verbose $COMMAND

Output of chezmoi doctor

```console $ chezmoi doctor RESULT CHECK MESSAGE ok version v2.42.2, commit v2.42.2, built at 2023-12-05, built by xbps ok latest-version v2.42.2 ok os-arch linux/amd64 (Void) ok uname Linux yamtech 6.5.11_1 #1 SMP PREEMPT_DYNAMIC Fri Nov 10 16:34:34 UTC 2023 x86_64 GNU/Linux ok go-version go1.21.4 (gc) ok executable /usr/bin/chezmoi ok config-file ~/.config/chezmoi/chezmoi.toml, last modified 2023-12-12T13:22:59Z 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/bash ok cd-args /bin/bash info diff-command not set ok edit-command found /usr/bin/vim ok edit-args /usr/bin/vim ok git-command found /usr/bin/git, version 2.42.0 ok merge-command found /usr/bin/vimdiff ok shell-command found /bin/bash ok shell-args /bin/bash info age-command age not found in $PATH ok gpg-command found /usr/bin/gpg, 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 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 ok pass-command found /usr/bin/pass, version 1.7.4 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

I'm still trying out chezmoi, even if quite decided on using it already.

There are no *chezmoi* files in .local/share/chezmoi/ (so there can't be any conflicts with that).

bradenhilton commented 9 months ago

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

https://www.chezmoi.io/user-guide/setup/#re-create-your-config-file

raimundomartins commented 9 months ago

Just to confirm, does that mean that I can only manage chezmoi config by means of a template? I can't just store the plain file?

Which should also mean that the only way to create and edit it is to chezmoi cd and $EDITOR .config.$FORMAT.tmpl?

bradenhilton commented 9 months ago

Just to confirm, does that mean that I can only manage chezmoi config by means of a template? I can't just store the plain file?

You can use a plain file, but you would need to make changes to it directly (under .config) outside of chezmoi to avoid the issue described below.

It used to be possible to manage the config file with chezmoi, but it always resulted in any changes made to the config (inside the source state) being one chezmoi apply behind the rest of the source state. This is strongly discouraged.

Instead, we encourage the usage of a config template, which has its contents evaluated at the same time as the rest of the source state, so you don't need to run chezmoi apply first in order to propagate your config changes to your other dotfiles. There may be a few edge cases I've forgotten about, but I'm mostly right either way.

I think the docs may need a few more tweaks regarding this.

Which should also mean that the only way to create and edit it is to chezmoi cd and $EDITOR .config.$FORMAT.tmpl?

It's not the only way. You can also use chezmoi edit-config or chezmoi edit-config-template. My preferred way is to open my entire source state in my editor.

halostatue commented 9 months ago

You cannot store the chezmoi config within itself, because the chezmoi config is required to know how to apply the changes. See #2854 for more details.

As long as there are no secrets in your .config/chezmoi/chezmoi.$FORMAT, you could copy it to $(chezmoi source-path).chezmoi.$FORMAT.tmpl and then add templating as required. Be sure that there is nothing you don't want in a (possibly public) repo before you commit it, though, and make it configurable that way.

raimundomartins commented 9 months ago

Thank you for the explanation @bradenhilton, but I don't quite follow how it would be one chezmoi apply behind, nor how editing it in a template is any different from a normal config file except in edge cases (as opposed to the "always" you mention). After reading more issues, perhaps I should explain my workflow and my difficulties with the docs as they are:

  1. I setup using chezmoi init so it checks out my empty repo (that's how you start, right?).
  2. I create the chezmoi config
  3. I try to add chezmoi config to chezmoi

The docs only mention how to Create a config file on a new machine automatically (which I had no need to at first and assumes you had a config file in chezmoi) and the one you sent to Re-create your config file which also assumes you had a config in chezmoi already. The bootstrap step of starting with an empty repo is missing.

You cannot store the chezmoi config within itself, because the chezmoi config is required to know how to apply the changes. See #2854 for more details.

That statement is contradictory with reality, at least the way it is written: the configuration is managed by chezmoi, it's just in a different place. And like I said, not allowing the source-path is very logical, but in none of the issue-tree starting from that one I read any argument as to why the same should be done for .config/chezmoi. I don't question that there isn't, I just didn't find it, which means that this part was confusing for me as a new user.

Finally, chezmoi edit-config ~and chezmoi edit-config-template~ don't work as I would expect. The former felt like 100% replaceable with $EDITOR ~/.config/chezmoi/chezmoi.toml except perhaps it checks for the other formats. It doesn't add/update the source-path. ~The latter doesn't apply the config I just setup.~ I see now that this is standard procedure for chezmoi edit as well, but there is an apply flag for edit which doesn't exist for edit-config-template.

In any case, I'm probably wrong somewhere in my reasoning (lack of knowledge and whatnot), but I thought I'd give my feedback on my onboarding experience of chezmoi currently, since I think it's a great project!

Feel free to close this issue.

halostatue commented 9 months ago

When you are first starting with chezmoi, you will have an empty repo. But that is typically an exactly once operation. After that, you will have your machine specific configuration in ~/.config/chezmoi/chezmoi.$FORMAT and your dotfiles and templates &c. in the repo/source directory. Then you want to make it easy to start on a new machine (it’s time to upgrade your computer), so you create your .chemoiz.$FORMAT.tmpl based on your configuration and extract out the pieces that you want to answer (every time or once with fallback to what is in the config now) and then you go to your new computer and you do chezmoi init REPO_URL --apply and it asks you the questions you set up, determines machine configuration, and then applies based on that configuration so that your new computer's home directory is configured the way you want it to be (modulo missing programs).

If the configuration file were managed by chezmoi, you would have two problems:

Chezmoi will be able to answer if your .chezmoi.$FORMAT.tmpl file is newer than your ~/.config/chezmoi/chezmoi.$FORMAT file and will suggest using chezmoi init to update it — but there's no rollback (because the configuration file is not version-controlled).

If you use chezmoi edit-config-template, you would need to run chezmoi init to apply any changes to the configuration file.

I hope that this reduces the confusion you have on this. If you have any specific documentation suggestions that would help clarify this, please raise them or open a pull request as we are always looking to make sure that the documentation is as clear as possible.