DEV Community

Cover image for Managing Configuration Files with Chezmoi
johanputra
johanputra

Posted on • Edited on

Managing Configuration Files with Chezmoi

Managing Configuration Files with Chezmoi

Chezmoi is a powerful tool for managing dotfiles and personal configuration files in a consistent, reproducible, and version-controlled manner.

Official documentation:
https://www.chezmoi.io/

Installing Chezmoi

You can install Chezmoi by specifying a custom installation directory. The following command installs Chezmoi into $HOME/.local/bin:

sh -c "$(curl -fsLS get.chezmoi.io)" -- -b $HOME/.local/bin
Enter fullscreen mode Exit fullscreen mode

Ensure that $HOME/.local/bin is included in your PATH.

Verifying the Installation

To confirm that Chezmoi has been installed successfully, check the version and binary location:

chezmoi --version
Enter fullscreen mode Exit fullscreen mode

Example output:

chezmoi version v2.40.0, commit 6a8ca1634654734bb33a036ffb9c21e6b9f4d28d, built at 2023-09-19T09:56:08Z
Enter fullscreen mode Exit fullscreen mode

Verify the binary path:

which chezmoi
Enter fullscreen mode Exit fullscreen mode

Expected output:

/home/xyz/.local/bin/chezmoi
Enter fullscreen mode Exit fullscreen mode

Adding Files to Chezmoi

To start managing configuration files, add them to Chezmoi. For example, to manage .zshrc and .vimrc:

chezmoi add ~/.zshrc ~/.vimrc
Enter fullscreen mode Exit fullscreen mode

Chezmoi will copy these files into its source directory, which by default is:

/home/xyz/.local/share/chezmoi
Enter fullscreen mode Exit fullscreen mode

This directory represents the source of truth for your dotfiles.

Pushing Chezmoi Configuration to GitHub

Navigate to the Chezmoi source directory and initialize or configure your Git repository:

cd ~/.local/share/chezmoi
git remote set-url origin git@github.com:user/dotfiles.git
git add .
git commit -m "Initial commit"
git push origin main
Enter fullscreen mode Exit fullscreen mode

At this point, your dotfiles are safely version-controlled in GitHub.

Updating Managed Files

When you modify a managed file (for example, .zshrc) directly in your home directory, you need to reflect those changes back into Chezmoi’s source directory:

chezmoi add ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

This updates the corresponding file under ~/.local/share/chezmoi, after which you can commit and push the changes to Git.

Note:
chezmoi update is used to pull and apply changes from the remote repository to your local machine, not to capture local file changes into the repository.

Applying Configuration on a New Machine

On a new system, you can apply all managed configurations with:

chezmoi init git@github.com:user/dotfiles.git
chezmoi apply
Enter fullscreen mode Exit fullscreen mode

This will recreate your dotfiles exactly as defined in the repository.

Conclusion

This article only covers the basic usage of Chezmoi, such as installation, adding files, and Git integration. Chezmoi also supports advanced features including:

  • Templates
  • Conditional configuration
  • Secrets management
  • Encrypted files
  • Cross-platform setups

Further exploration is recommended as your configuration management needs grow. Feel free to leave comments or suggestions, and we can continue learning and improving this setup together.

Top comments (0)