Mike Kasberg

Husband. Father. Software engineer. Ubuntu Linux user.

Image for My Dotfiles Story: A Journey to Chezmoi

My Dotfiles Story: A Journey to Chezmoi

12 May 2021

When you’re a developer (or perhaps even a normal power-user), you seem to acquire a bunch of custom config, scripts, and tools over time. Often, these are little snippets. A one-liner command alias that you paste into ~/.bashrc. A custom format for your PS1 prompt. A default setting for Vim.

Then, at some point, you get a new computer, and you realize you want all the config that was on your old computer. For me, this was how it started. In fact, I started trying to solve this problem before I knew what Git was and before I knew what dotfiles were. Back then, I kept a txt file with notes about what Windows settings I had changed to makes things work the way I liked, so I could change the same settings if I had to set up a new computer for myself!

But you start to realize that maybe you don’t want to simply re-do your config every time you get a new computer. And although with Linux dotfiles you can simply copy the files over with a USB stick, you don’t want to have to think about what to copy every time this happens. Also, you start to realize that maybe you want to back some of this config up so you don’t lose it if your computer crashes!

My first solution to these problems was simple enough – I was a heavy user of Dropbox at the time, and I created a folder in my Dropbox called Config. I stored a few files (like .bashrc and .gitconfig) in there and tried to remember to copy my changes to Dropbox if I made any edits.

And that worked pretty well for a while, but eventually became a little tedious to manage. You update the file on one computer, but forget to copy it down on another one and the files get out of sync. Also, over time, you begin to realize that you might want slightly different config on different computers. Maybe there’s a bash alias you only want on your work computer, or maybe there’s a few lines that apply to your macOS machine but not to your Linux computer. Around this time in my own dotfiles journey, I was into bash scripting. So, I wrote a tool in bash to diff the dotfiles in my Dropbox with the dotfiles installed to my computer. I could then use normal diff tools (including the GUI Meld) to merge the lines I cared about, and leave certain lines out if I wanted to! And the script would report which files were up to date and which files had differences.

To give you a rough idea what I’m talking about, I’d run the script like this:

$ pwd
~/Dropbox/Config
$ ./diff-dotfiles.sh -l
The following files have differences:
  .bashrc
  .gitconfig

$ ./diff-dotfiles.sh .bashrc 
--- /home/mkasberg/.bashrc	2020-08-19 20:49:42.509801503 -0600
+++ .bashrc	2019-05-27 21:30:08.264957695 -0600
@@ -16,8 +16,8 @@
 # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
-HISTSIZE=20000
-HISTFILESIZE=20000
+HISTSIZE=5000
+HISTFILESIZE=10000

This bash script/Dropbox approach actually worked pretty well for a couple years, and I’m really pleased with how powerful the script was for something that was less than 100 lines long! The fact that I was able to get by with something like this for more than a year is a testament to the power of the command line tools available on any Linux computer. But it also speaks to the fact that “managing your dotfiles” is fundamentally a very simple problem where simple solutions can work well. If you want to check out, use, or build upon my script, you’re welcome to – here’s the Gist!

But, alas, I’m not using my diff-dotfiles.sh script anymore. As I collected more and more bits of config, managing differences with diff became tedious, and I searched for something that was a little more powerful and purpose-built than my bash script. And that’s how I found Chezmoi. Chezmoi is like what you might get if you re-wrote my bash script in Go, came up with better solutions than diff for managing config on multiple machines, added in secrets management and other useful dotfile tools, and tweaked and perfected it over years. Although it’s slightly intimidating to move from a “copy your dotfiles from Dropbox” approach to a custom-built tool like Chezmoi, the Chezmoi docs are great and you start to see the benefits as soon as you start using it!

When I began using Chezmoi, I also migrated my dotfiles off Dropbox and onto Git. Which was a great decision because Git provides version control and commit history, so I can remember why I made a minor change years later. And now that I’m using Git, I’ve also published my dotfiles on GitHub. Storing my dotfiles on GitHub makes it easy to sync between different computers, and also makes it easy to share with others who might be interested in them. (It’s really cool to just drop a link like this when I want to share a quick tip with someone!)

As a brief aside, here are some of my favorite tricks from my own dotfiles:

  • My Bash PS1 and Zsh Theme, which add an extra newline before the prompt, use custom colors, display my current git branch, and put the $ on a new line.
  • My .vimrc, which includes some light customization for things like color schemes, formatting, and special display rules for tab characters.
  • A script that will install all my most frequently used non-default apps on Ubuntu.
  • Template support to account for differences between machines.

I’ve been using Chezmoi for more than a year now, across at least 3 computers simultaneously, and I really love it. Most of all, I love how fast I can configure a new machine when I use it. In just a couple minutes of work, I can kick off a process on a brand-new computer that will set up my dotfiles and install all my usual software so it feels like a computer I’ve been using for years. I also appreciate features like secrets management, which allow me to share my dotfiles while keeping my secrets safe. Overall, I love the way Chezmoi fits so perfectly into the niche of managing dotfiles.

If you want to try Chezmoi yourself, you can! It’s a free, open source tool and it’s easy to get started. The best way to start is to find a file you’ve already made a small customization to, like .bashrc or .vimrc, and use that as a starting point for your own dotfiles repository. Don’t worry about starting small and simple, you’ll acquire a lot of config changes over the years! Chezmoi provides a good quick start guide to get you started. To give you the gist of it here, you run chezmoi init to create a bare dotfiles repo and then chezmoi add ~/.bashrc (or any other file you want) to add your first dotfile. After that, it’s chezmoi cd, git add ., git commit, and you’re done!

So, what does this all mean for you? I’m not necessarily suggesting that you should dive head-first into Chezmoi overnight! (Though of course I recommend it if you’re interested.) If you’ve never given your dotfiles any thought, I hope this introduces you to the world of easy customizations you can do to make your computer work better for you. If you already have a few custom lines in your dotfiles, I hope this inspires you to add some more customization, back them up somewhere, and share them. And if you’ve been looking for a tool to manager your own dotfiles, I hope this inspires you to check out Chezmoi, but ultimately I hope you find the tool that works best for you!

Did you find this inspirational? Want to talk more about dotfiles? Let me know @mike_kasberg.

About the Author

Mike Kasberg

👋 Hi, I'm Mike! I'm a husband, I'm a father, and I'm a senior software engineer at Strava. I use Ubuntu Linux daily at work and at home. And I enjoy writing about Linux, open source, programming, 3D printing, tech, and other random topics. I'd love to have you follow me on X or LinkedIn to show your support and see when I write new content!

Share!

If you enjoyed this blog post, I'd love it if you could share it with your network!

I run this blog in my spare time. There's no need to pay to access any of the content on this site, but if you find my content useful and would like to show your support, buying me a coffee is a small gesture to let me know what you like and encourage me to write more great content!

You can also support me by visiting LinuxLaptopPrices.com, a website I run as a side project.

Related Posts