How to properly manage dotfiles?

Sat, 04 Jan 2025

When I migrated to Linux from Windows, I discovered a new world in the area of software development, which greatly improved my capabilities as a developer. One of the most interesting things in the Linux world (and UNIX in general) is the concept of dotfiles. The possibility of configuring a certain software from a local configuration file makes it easier to manage these settings between different environments, e.g., your personal computer and your work computer. The possibility of separating environments and, above all, versioning them allows for greater dynamism in configuring your environment, especially for those who adhere to r/unixporn.

Dotfiles & Environments

I started developing softwares when I was still in high school, so the only environment I had was my personal computer. When you have only one environment, it is easy to manage your dotfiles. You edit your configuration files directly in the home (usually ~/.config) directory, and that's it, you solved your problem. Every time you need to change something, you open your terminal and edit the file, and so on. But when I started working on my first job, I had to use a different computer, and I had to configure it all over again, not just that, but I had to sightly change some configurations to adapt to the new environment, like my ~/.gitconfig file, for example, which had my personal email configured, and I had to change it to my work email.

The first and most obvious solution to this problem is to create a repository on GitHub and push your dotfiles there. This way, you can clone your repository on any computer and have your configurations ready to use. But this solution is not perfect, remember that I said that I had to change my email in the ~/.gitconfig file? If I push this file to GitHub, I will have to change it every time I clone my repository on a new computer. This is not a big deal, but it is inconvenient. The real problem is when you have more files like this, and you have to change them every time you clone your repository. If you change something in your configuration files, you will have to push the changes to your GitHub repository, and then pull them on the other computer, change the files that need some adaptation, and so on. This is not a good solution and can be very annoying. After some time, I bought a MacBook, but hey, I can't use the same configuration files on my MacBook that I use on my Linux machine; Visual Studio Code has a different configuration directory on macOS, and I have to change it in my dotfiles repository every time I clone it on my MacBook. This is getting out of hand. This is when I discovered chezmoi.

When you use some tool to manage your dotfiles, you can have a single repository with all your configurations, and you can manage them in a more organized way. You can specify which files should be copied to which environment, you can use templates to avoid repeating the same configuration over and over again, you can use encryption to store sensitive information, and so on. Obviously, there are many tools to manage your dotfiles, chezmoi is just one of them. I chose chezmoi because it was the one that best suited my needs, but you can use any other tool that you like.

Secrets Management

One of the most interesting features of chezmoi is the possibility of managing secrets. One of the biggest problems with managing dotfiles is managing sensitive information, like your private SSH key or GPG key, for example. You can't just push these files to GitHub. One solution is to encrypt these files and push them to GitHub, but then you will have to decrypt them every time you clone your repository on a new computer. The solution that I use is to store these files in a password manager, specifically Bitwarden, and use chezmoi to fetch these files from my password manager, since chezmoi has a built-in integration with Bitwarden. This way, I can have my secrets stored in a secure place and have them available on any computer that I clone my repository. For example, I use WakaTime to track my coding time, and I have to store my API key in a file in my configuration directory. I don't want to push this file to GitHub, so I store it in Bitwarden and fetch it with chezmoi. This way, I can have my API key available on any computer that I clone my repository, and I don't have to worry about pushing it to GitHub:

[settings]
api_key = {{ (bitwarden "item" "wakatime_key").notes }}

Scripting

When you have more complex configurations, you may need to run some scripts to configure your environment. For example, I use Zsh as my shell, and I have to install some plugins and themes to make it look the way I want. To automate this process, I use chezmoi to run some scripts when I clone my repository on a new computer. This way, I can have my environment configured the way I want without having to do it manually. You can even specify when these scripts should be run, if the script should be run only once or every time that is changed. This way, you can have a more dynamic configuration and adapt it to your needs.

Conclusion

Managing dotfiles can be a daunting task, especially when you have to manage them on different environments. But with the right tools, you can have a more organized and dynamic configuration that adapts to your needs. I use chezmoi to manage my dotfiles, but you can use any other tool that you like. The important thing is to have a tool that helps you manage your configurations in a more organized way. If you are interested in chezmoi, you can check the official documentation.

  • development
  • chezmoi