twpayne / chezmoi

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

Testing .chezmoiignore for KDE/GTK #1893

Closed peterlobster closed 2 years ago

peterlobster commented 2 years ago

So I may have a bit of a unique, (or maybe not so unique) use case.

First, to give some background, I've been a long time chezmoi user on macOS, but now I'm more on Linux. So I decided to try to setup chezmoi on Linux system.

Anywho, I tend to prefer Gentoo as my distro of choice; and in the spirit of Gentoo, I switch off every so often between systemd and OpenRC, as with KDE and GNOME. However depending on my setup, my .dotfiles need to change.

Currently, as a basic test. I've checked in a Konsole .colorscheme file, located at ~/.local/share/konsole/my.colorscheme. Konsole is a KDE app, so we'd only want these files created if we're using KDE. So what I've done is I've created a .chezmoidata.yaml and .chezmoiignore file in my source directory.

In that .chezmoidata.yaml...

system:
    de: "KDE Plasma"
    init: "systemd"

...and in that .chezmoiignore.

...
{{- if ne .system.de "KDE Plasma" }}
dot_local/share/konsole/**
dot_local/share/konsole/
{{- end }}

My question is, am I doing this right for my use case or is there something I'm missing?

If I'm doing it right, I'm think I'm having a problem testing it, or I must not be doing something right.

When I use $ chezmoi execute-template -p .system.de="GNOME" < .local/share/chezmoi/.chezmoiignore I get the same output as without the ...-p .system.de="GNOME". If I try to use --init, then I get...

chezmoi: template: /home/peter/.local/share/chezmoi/.chezmoiignore:3:17: executing "/home/peter/.local/share/chezmoi/.chezmoiignore" at <.system.de>: map has no entry for key "system"

Any help on clarifying this would be much appreciated. 🙏

twpayne commented 2 years ago

I think that what's happening here is that the -p option to chezmoi execute-template does not behave in the way you think it does.

Looking at the example

$ chezmoi execute-template -p .system.de="GNOME" < .local/share/chezmoi/.chezmoiignore

it looks like you're using -p to set the value of the .system.de template variable.

What -p actually does is set the value returned by the promptString template function. As you're not using promptString (and promptString is only available when generating the config file), -p does nothing in this case, which is why you're always getting the same output.

I note also your .chezmoiignore:

...
{{- if ne .system.de "KDE Plasma" }}
dot_local/share/konsole/**
dot_local/share/konsole/
{{- end }}

.chezmoiignore needs filenames in the target state (i.e. your home directory) not your source state (the local git repo). So, your .chezmoiignore should instead be:

...
{{- if ne .system.de "KDE Plasma" }}
.local/share/konsole/**
.local/share/konsole/
{{- end }}

Also, for info, blank lines in .chezmoiignore are ignored, so you can be less strict about whitespace, i.e. you can use {{ instead of {{- in your .chezmoiignore file.

Fundamentally, the variables that you define (.system.de and .system.init) should be in your chezmoi config file. Your chezmoi config file should be created from a config file template. It may be helpful to look at my chezmoi config file template.

Hope this helps. Please continue to ask questions.

twpayne commented 2 years ago

Hope this answered your question. Please re-open if needed.