<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Chezmoi on Clément Joly – Open-Source, Rust &amp; SQLite</title><link>https://cj.rs/tags/chezmoi/</link><description>Recent content in Chezmoi on Clément Joly – Open-Source, Rust &amp; SQLite</description><image><title>Clément Joly – Open-Source, Rust &amp; SQLite</title><url>https://cj.rs/images/open-graph-pages.jpg</url><link>https://cj.rs/images/open-graph-pages.jpg</link></image><generator>Hugo</generator><language>en</language><copyright>Clément Joly</copyright><lastBuildDate>Thu, 28 May 2026 23:48:29 +0000</lastBuildDate><atom:link href="https://cj.rs/tags/chezmoi/index.xml" rel="self" type="application/rss+xml"/><item><title>Configuration Management</title><link>https://cj.rs/blog/my-setup/chezmoi/</link><pubDate>Fri, 03 Sep 2021 19:59:01 +0100</pubDate><guid>https://cj.rs/blog/my-setup/chezmoi/</guid><description>I use Chezmoi to manage my configuration</description><content:encoded><![CDATA[



  
  
  
  

  <div class="alert alert-tldr">
    <p class="alert-heading">
      ⚡
      
        TL;DR
      
    </p>
    <p>Chezmoi follows a declarative approach to configuration management. Even if you already use a configuration-management system, you should <a href="https://www.chezmoi.io/docs/comparison/">check out how it compares</a>!</p>
  </div>



<h2 id="a-custom-configuration-makes-you-feel-at-home">A Custom Configuration Makes You Feel at Home</h2>
<p>Your git aliases, shell setup, keyboard shortcuts and even your favorite fonts are tweaked for your own needs and tastes. It’s part of what makes your computer, <em>your</em> computer. All this configuration evolves over years and is slowly refined over time. It may actually be your longuest-living project.</p>
<p>Do you want to start from scratch when you switch computer? Of course not! And you wouldn’t want to break it with adventurous changes either! Versioning it in <a href="https://drewdevault.com/2019/12/30/dotfiles.html">git</a> backs up the history and eases the management of this important work. Configuration updates are also more convenient to share between machines this way.</p>
<h2 id="enter-chezmoi">Enter Chezmoi</h2>
<p>In the git-based configuration management described above, how to account for the small variations (like between your <a href="https://utf9k.net/blog/conditional-gitconfig/">work environment and personal computer</a>)? Git supports <a href="https://git.kernel.org/pub/scm/git/git.git/tree/Documentation/RelNotes/2.13.0.txt#n127">conditional inclusion of configuration files</a>, but how about the other tools? What if you want to encrypt your secrets in your config files, to publish your configuration on GitHub?</p>
<p><a href="https://www.chezmoi.io">Chezmoi</a> follows a <em>declarative approach</em>: it has a separate <em>source repository</em><sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup> to describe a target state, that then gets applied to your home directory. This is possible through templates, that are activated selectively for some files. The rest of this post shows how powerful templating can be.</p>
<h2 id="account-for-variations-between-machines">Account for Variations Between Machines</h2>
<p>Let’s start with a simple example. In the git configuration, you put your email, that’s then included in all your commits. Thus, you often want to have a different email in <code>.gitconfig</code> for your work and personal profiles. To tackle this issue, I put the following in my <code>.gitconfig</code> chezmoi template:</p>
<pre tabindex="0"><code>[user]
    email = {{ .email }}
</code></pre><p>and it gets rendered to this:</p>
<pre tabindex="0"><code>[user]
    email = me@my.home
</code></pre><p>on my personal computer and this:</p>
<pre tabindex="0"><code>[user]
    email = me@my.work
</code></pre><p>on my work computer. It serves a purpose similar to <a href="https://utf9k.net/blog/conditional-gitconfig/">conditionally setting your gitconfig</a>, but it is applicable to any program.</p>
<h2 id="even-more-variations-with-templates">Even More Variations with Templates</h2>
<p>Now, let’s cover a more powerful use of templates.</p>
<p>You can have multiple files generated from one using <a href="https://www.chezmoi.io/docs/templating/#using-chezmoitemplates">“global” templates</a>. For instance, if you use <a href="https://i3wm.org/">i3</a> and <a href="https://swaywm.org/">sway</a> as your windows manager, you likely have slightly different configurations, given that sway’s configuration is mostly compatible with that of i3. To keep everything in one file, while taking advantages of sway-specific features, you can (in your chezmoi repository):</p>
<ol>
<li>Put your configuration in <code>.chezmoitemplates/sway_i3</code>. You can keep any sway specific section in a templated <code>if</code> statement, like so:
<pre tabindex="0"><code>{{- if eq .f &#34;sway&#34; }}
# Sway specific stuff here, like output configuration
{{- end }}
</code></pre></li>
<li>In <code>dot_config/i3/config.tmpl</code> (and <code>dot_config/sway/config.tmpl</code>), use something like:
<pre tabindex="0"><code>{{/* Environment */}}
{{- $env := . -}}

{{/* Flavor */}}
{{- $f := &#34;i3&#34; -}} # &lt;--- Change this to &#34;sway&#34; in dot_config/sway/config.tmpl

{{- template &#34;sway_i3&#34; dict &#34;f&#34; $f &#34;env&#34; $env -}}
</code></pre>As you may have noticed, chezmoi renames some files in the source state, to encode parameters and for readability. So <code>dot_config/sway/config.tmpl</code> becomes <code>.config/sway/config</code> (<code>.tmpl</code> means the file is executed as a template).    <br>
Also, <code>$env</code> is used to keep the general template context, for instance to check the OS version in our template config file.</li>
</ol>
<h2 id="chezmoiignore">.chezmoiignore</h2>
<p>Finally, chezmoi supports an ignore file named <code>.chezmoiignore</code>. The concept is similar to the well known <code>.gitignore</code>: patterns listed in this file are ignored by chezmoi. In other words, some files are stored in your chezmoi repository, but do not reflect in your home. This can be used if you put a <code>README.md</code> in your configuration repository, as you don’t want this file in your home directory. Just write:</p>
<pre tabindex="0"><code>README.md
</code></pre><p>in <code>.chezmoiignore</code> and the <code>README.md</code> won’t be copied in your home.</p>
<p>You can do more advanced things with <code>.chezmoiignore</code> as it is a template by default. From <a href="https://www.chezmoi.io/docs/reference/#chezmoiignore">the documentation</a>:</p>
<pre tabindex="0"><code>{{- if ne .email &#34;firstname.lastname@company.com&#34; }}
# Ignore .company-directory unless configured with a company email
.company-directory # note that the pattern is not dot_company-directory
{{- end }}
</code></pre><h2 id="conclusion">Conclusion</h2>
<p>Chezmoi has lot more <a href="https://www.chezmoi.io/docs/reference/">features</a>, like <a href="https://www.chezmoi.io/docs/how-to/#encrypt-whole-files-with-gpg">encryption</a> or <a href="https://www.chezmoi.io/docs/how-to/#use-chezmoi-with-github-codespaces-visual-studio-codespaces-or-visual-studio-code-remote---containers">installation in GitHub Codespaces</a>. It is quite <a href="https://www.chezmoi.io">well documented</a>, in particular with a <a href="https://www.chezmoi.io/docs/quick-start/">quick start guide</a>.</p>
<p>I’ve switched to it almost two years ago, I don’t regret it at all!</p>
<h2 id="bonus-vim-snippet">Bonus: Vim Snippet</h2>
<p>To update the target state automatically when editing the source file in the chezmoi repo, I use this vim snippet (with the <a href="https://fishshell.com/">fish shell</a>):</p>
<pre tabindex="0"><code>autocmd BufWritePost ~/.local/share/chezmoi/* ! chezmoi apply --source-path %; or for f in (rg -l &#39;template &#34;%:t&#34;&#39;); chezmoi apply --source-path $f; end
</code></pre><div class="footnotes" role="doc-endnotes">
<hr>
<ol>
<li id="fn:1">
<p>You should store this source repository in a git repository to easily revert changes and share configuration update between various machines.&#160;<a href="#fnref:1" class="footnote-backref" role="doc-backlink">&#x21a9;&#xfe0e;</a></p>
</li>
</ol>
</div>
]]></content:encoded></item></channel></rss>