twpayne / chezmoi

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

Using template variables in .chezmoiroot #1720

Closed bdavs3 closed 2 years ago

bdavs3 commented 2 years ago

What exactly are you trying to do?

Hi! Let me first say that I really love this project.

Right now, I'm using a script that populates ~/.config/chezmoi/chezmoi.toml with a template variable, in hopes of changing .chezmoiroot according to a user's preferences. I'm curious as to whether this is currently possible.

What have you tried so far?

Where else have you checked for solutions?

Output of any commands you've tried

Here is my .chezmoiroot:

{{ .team }}

And my ~/.config/chezmoi/chezmoi.toml:

[data]
    shell = 'bash'
    team = 'my-team'

In ~/.local/share/chezmoi I have the following:

my-team
│   dot_bashrc
│   dot_zshrc    
│   .chezmoiignore
.chezmoiroot
.git/
.gitignore
install.sh
README.md

Unfortunately when I run chezmoi diff, I get

my-team/.bashrc
my-team/.zshrc
install.sh
README.md

If I change .chezmoiroot to simply say my-team, I can confirm that things work properly when I run chezmoi diff:

.bashrc
.zshrc

Let me know if anyone has input!

felipecrs commented 2 years ago

Wow, that's an interesting use case. BTW, what I advised to my colleagues from other teams is to fork my team's dotfiles (and of course being responsible of keeping it up-to-date with upstream).

But this seems a reasonable approach to keep everything together, especially if one team is responsible to maintain the dotfiles of multiple other teams.

This does not make a case for my specific needs though, because I believe that dotfiles can safely be generic, considering that each project's specific needs will be stored in their own devcontainers (which by itself also contains a subset of the dotfiles -- the terminal customization, at least).

twpayne commented 2 years ago

Thanks for the kind words :)

Right now, I'm using a script that populates ~/.config/chezmoi/chezmoi.toml with a template variable, in hopes of changing .chezmoiroot according to a user's preferences. I'm curious as to whether this is currently possible.

Currently .chezmoiroot is not treated as template because it is read before everything else, and some template data is not yet available. It might be possible to change that behavior, but...

Instead of populating ~/.config/chezmoi/chezmoi.toml with a script, have you considered using a config file template? Also, you can achieve almost the same effect as .chezmoiroot by setting the sourceDir config file variable. This combination might be sufficient to meet your use case.

felipecrs commented 2 years ago

I've created https://github.com/felipecrs/dotfiles-multiple-teams to illustrate @twpayne's suggestion of using sourceDir.

felipecrs commented 2 years ago
❯ docker run --rm -it --user vscode mcr.microsoft.com/vscode/devcontainers/base:ubuntu-20.04 bash -xc 'curl -fsSL git.io/chezmoi | sh -s -- -b ~/.local/bin && ~/.local/bin/chezmoi init --apply --verbose felipecrs/dotfiles-multiple-teams && cat ~/.example'
+ curl -fsSL git.io/chezmoi
+ sh -s -- -b /home/vscode/.local/bin
info found version 2.9.2 for latest/linux/amd64
info installed /home/vscode/.local/bin/chezmoi
+ /home/vscode/.local/bin/chezmoi init --apply --verbose felipecrs/dotfiles-multiple-teams
Cloning into '/home/vscode/.local/share/chezmoi'...
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 14 (delta 1), reused 11 (delta 1), pack-reused 0
Unpacking objects: 100% (14/14), 2.45 KiB | 835.00 KiB/s, done.
What is your team (default "a")? b
✔ Team set as 'b'
diff --git a/.example b/.example
new file mode 100644
index 0000000000000000000000000000000000000000..63d8dbd40c23542e740659a7168a0ce3138ea748
--- /dev/null
+++ b/.example
@@ -0,0 +1 @@
+b
\ No newline at end of file
+ cat /home/vscode/.example
b
bdavs3 commented 2 years ago

@twpayne Thanks so much for the quick reply! I totally missed the templatized config file approach in the documentation. Even if that doesn't exactly achieve what I'd like it to, it will certainly eliminate the need for my own installation script. I was messing with sourceDir earlier and getting some weird behavior, but it's quite possible I was doing something wrong. I'm more encouraged to try that approach again now.

bdavs3 commented 2 years ago

@felipecrs I really appreciate the example repo you created. I think I'll end up going a similar route. And yes, my main thought is that it would be nice to have everyone's dotfiles in one place. Helps a ton with organization and ease of access.

twpayne commented 2 years ago

Hopefully this is now resolved. Please re-open if needed.

bdavs3 commented 2 years ago

Using the example repository that @felipecrs set up as a guideline, I was able to achieve what I was going for! So I'm gonna go ahead and close the issue for now, but if anyone else comes along and has additional commentary to add, please do.

Edit: looks like @twpayne closed right before me. Perfect timing.