Installing and Using chezmoi in a Dev Container
What Is chezmoi and Why Is It Useful?
When you work on different computers, cloud servers, or Dev Containers, you often have to set up the same settings every time like your shell configuration, Git settings, or editor preferences. These settings are usually stored in files that start with a dot, such as .bashrc or .gitconfig. These are called dotfiles.
chezmoi is a tool that helps you manage and sync all these dotfiles easily. It lets you keep your personal setup in one place and quickly apply it to any system you use. Instead of manually copying files each time, you can just use chezmoi to install and update them automatically.
You can also connect chezmoi with GitHub, so your dotfiles are version-controlled and backed up. This means if you start using a new machine, you can clone your repository and run one command to set up everything exactly how you like.
In simple words, chezmoi helps you:
- Keep your environment consistent across all your systems
- Save time when setting up new machines or containers
- Store and back up your configuration files with GitHub
- Review and apply changes safely before updating
Using chezmoi is a smart way to make your development setup more organized, portable, and professional.
Installing and Using chezmoi in a Dev Container
This week, I was exploring how to use chezmoi inside a Dev Container to manage and sync my configuration files (dotfiles).
Here is a simple step-by-step guide I created based on what I learned.
Step 1: Install chezmoi
Run this command in your terminal to install chezmoi:
sh -c "$(curl -fsLS get.chezmoi.io)" -- -b $HOME/.local/bin
This installs chezmoi in the folder ~/.local/bin.
Step 2: Initialize chezmoi
After installation, start chezmoi with:
chezmoi init
This prepares your system to manage dotfiles.
Step 3: Manage Your First File
To start managing a file, for example .bashrc, run:
chezmoi add ~/.bashrc
To edit the source version of the file (chezmoi keeps a copy in a source folder):
chezmoi edit ~/.bashrc
Step 4: Preview and Apply Changes
Before applying any updates, you can preview what will change:
chezmoi diff
If everything looks correct, apply the changes:
chezmoi -v apply
Step 5: Save and Push Dotfiles to GitHub
After managing your files with chezmoi, it is a good idea to keep them backed up and version-controlled.
You can do this by using Git and GitHub.
First, go to chezmoi’s source directory. This is where chezmoi stores all your managed files.
chezmoi cd
Inside this directory, you can create a Git repository to track your configuration files:
git init
git add .
git commit -m "Initial commit"
Next, go to GitHub and create a new repository — for example, dotfiles-demo.
After creating it, connect it with your local repository and push the files:
git remote add origin git@github.com:mohammadrezachegini/dotfiles-demo.git
git branch -M main
git push -u origin main
Now, your dotfiles are saved online on GitHub.
This means you can pull them from any machine and restore your setup at any time.
You also get version control, so you can see what you changed and when.
If you already have an existing repository and only want to update or sync it, you can use:
git fetch origin
git reset --hard origin/master
git pull --rebase origin master
git rebase --continue
git add .
git commit -m "Initial chezmoi commit"
git push
This will make sure your local files match your remote GitHub version.
Step 6: Automate chezmoi Setup in Dev Container
When you work inside a Dev Container, you often rebuild it or move between different environments.
Instead of installing chezmoi every time, you can automate the setup using a simple script.
This script checks whether chezmoi is already installed, and if not, installs it and applies your configuration automatically.
Create a file named setup.sh inside your project:
#!/bin/bash
set -euo pipefail
if ! command -v chezmoi >/dev/null; then
sh -c "$(curl -fsLS get.chezmoi.io)" -- init --apply git@github.com:mischavandenburg/dotfiles-demo.git
fi
exit 0
Let’s break down what this script does:
set -euo pipefailmakes the script stop if any command fails.command -v chezmoichecks if chezmoi is already installed.- If it is not found, the script runs the curl command to install chezmoi and immediately initialize it with your GitHub dotfiles repository.
exit 0simply means the script finished successfully.
You can add this script to your Dev Container setup (for example, in a Dockerfile or postCreateCommand) so that every new container automatically installs your environment with your preferred settings.
This saves time and makes sure all your Dev Containers look and behave the same.
Step 7: Check Installed Editors
If chezmoi cannot find your preferred text editor (like nvim), check available editors with:
command -v nvim
echo $?
command -v vi
echo $?
If the exit code is 0, the editor exists.
If it is 1, it is not installed.
Example output:
/usr/bin/vi
This means vi is available.
Summary
- Install chezmoi with a single curl command
- Initialize and manage files easily
- Push and sync your dotfiles with GitHub
- Automate setup in your Dev Container
- Make sure your editor is available