I had a problem. Not a big deal, but slightly annoying. Each time I've changed or formatted one of my laptops (which happened a couple of times recently) I had to re-create my dotfiles from scratch. I would not call it a big deal since I don't have large configs, but it still took some time to set everything up.
Zsh, plugins, gitconfig and neovim. For neovim I already have a separate repo to store my config but it was still one more thing to remember.
I've decided to do a research (again) and this time I've managed to come up with a solution. There are multiple ways to approach this problem. Some of them require additional applications, and also you could use bare git repository.
I've went through a couple of solutions and found out that chezmoi works best for me.
Managing dotfiles with chezmoi
My needs are not big. I just want to commit selected dotfiles, and then be able to replace and edit them. And I want to do it across multiple machines. What chezmoi gives me is simplicity.
All you need to do is to install chezmoi, and then follow the quick start guide, like so:
chezmoi init
chezmoi add ~/.zshrc
chezmoi cd
git commit -m 'init'
git remote add # set remote to your repo
git branch -M main
git push
and that's it. Now you have your `.zshrc`
backed up in your repo. In order to have this file on another computer, all you need to execute is
chezmoi init https://github.com/$GITHUB_USERNAME/dotfiles.git
chezmoi diff # to see what changes will be applied
chezmoi apply
and that's it. You have successfully synced `.zshrc`
file between two computers. And now, you can type
chezmoi edit ~/.zshrc
chezmoi diff #to check what's going to change
chezmoi apply
chezmoi cd
git commit -m 'changes'
git push
and in case you'd edit the file directly (so you forgot to edit it via chezmoi), there is a re-add
parameter, so you just type
chezmoi re-add
chezmoi cd
git commit -m 'changes'
git push
and all good!
Another good thing about chezmoi is that it works out of the box with the whole directories - I wanted to sync my `~/.oh-my-zsh`
and all it required was
chezmoi add ~/.oh-my-zsh
In case you'd want to use your own editor (like nvim for example) in `chezmoi edit`
, all you need to do is to set env variable:
export EDITOR="nvim"
Managing per machine config
I'm using the same .gitconfig on my personal and on my work laptop. Obviously I want to have a different email on both. In order to do this, I need to convert `~/.gitconfig` file in chezmoi from file to template.
chezmoi add --template ~/.gitconfig
and change entry in `.gitconfig` to take email from local variables:
[user]
email = {{ .email | quote }}
Now chezmoi will tell me if the email is missing, on `chezmoi diff`
or `chezmoi apply`
command:
chezmoi: .gitconfig: template: dot_gitconfig.tmpl:7:12: executing "dot_gitconfig.tmpl" at <.email>: map has no entry for key "email"
All I need to do is to go to `~/.config/chezmoi/chezmoi.toml`
and add email entry, like so:
[data]
email = "work.email@email.com"
and ofc do exactly same thing on personal computer with personal email. There are ways of further automating things, but I'm not there yet - you can create variables and whole configs per machine name or os type, and even download packages via package manager.
Chezmoi has everything or even slightly more than I was expecting from it, while keeping it stupid simple. I cannot recommend it enough.
You can find my chezmoi repo here