Closed max13fr closed 6 months ago
Thank you for the very complete problem report and your kind words!
The underlying problem here is that you're managing one of chezmoi's config files (~/.local/share/chezmoi/.chezmoidata.yaml
) with chezmoi itself. This leads to the problem that chezmoi reads is config before making changes and so the changes to the config are only visible after chezmoi has run.
The fix is to use chezmoi's config file template mechanism.
Specifically, I'd recommend adding your bash aliases to the data section in ~/.local/share/chezmoi/.chezmoi.yaml.tmpl
:
data:
bash_aliases:
- name: backend
server: myhost
path: /srv/backend
- name: intranet
server: myhost
path: /srv/www/intranet
- name: extranet
server: myhost
path: /srv/www/extranet
This will make the .bash_aliases
template variable available in all your templates. You can remove the dot_chezmoidata.yaml
file.
Thanks for your reply.
The issue when using ~/.local/share/chezmoi/.chezmoi.yaml.tmpl
is that it's work only when calling first chezmoi init
but not with later refresh with chezmoi update
.
But you give me another idea : as git init
is cloning all files in ~/.local/share/chezmoi/
why not just create .chezmoidata.yaml
at the root of my repository (instead of dot_local/share/chezmoi/dot_chezmoidata.yaml
).
Here everything working fine, ~/.local/share/chezmoi/chezmoidata.yaml
already exists when chezmoi init is deploy files (and so variables are correctly loaded).
Thanks for you help and your awesome work !
This might be worth adding as a post to Show & Tell.
What exactly are you trying to do?
I would like to deploy automatically the dot_chezmoidata.yaml file and use the variables inside in other templates files.
dot_local/share/chezmoi/dot_chezmoidata.yaml :
dot_bash_aliases.tmpl :
When I run
chezmoi init
, I got the following error :The template file dot_bash_aliases.tmpl doesn't find the variable .bash_aliases.
What have you tried so far?
As dot_bash_aliases.tmpl is deployed before dot_local (chezmoi deploys files sorted by name), I tried to add a before script to force deploy of dot_chezmoidata.yaml before deploying other files (and so dot_bash_aliases.tmpl).
run_before_10_copy_chezmoidata.sh :
It fixes the issue of the deployement order but
chezmoi init
doesn't load the variables from .chezmoidata.yaml (I assume thatchezmoi
already tried to load .chezmoidata before running that script and didn't find any file, so the variable is not available) :Now, if I run
chezmoi update
after thechezmoi init
, it's now working as .chezmoidata.yaml already existing before running that command :And now, .bash_aliases is available & correct.
Where else have you checked for solutions?
Output of
chezmoi doctor
Additional context
Is there any solution to predeploy some chezmoi config files in a cleaner way, or perhaps a specific flag on
chezmoi init
to force to reload the configs files before deploying templates files ?Thanks in advance, Best regards, Max
PS : a great thanks for your tools, it's really good & useful !