My Tooling - Part 2 - Setting up dotfiles manager

Table of Contents
[Subtitle] For a software developer, just like chefs or artists, their setup/tools are an extension of their personal expression.
Its not just business, but very much personal !!!
Context #
Every developer is an artist, whose creations depend a lot on their tools and setup. It enables them to transform their ideas and manifest not just as functional, but a things of beauty.
Here, I will be sharing the my dev-setup tech tooling, that I love and keep tinkering with - I guess it will exist as a continuous work-in-progress.
Part 1 : My Tooling - Part 1 - Securing a new dev box
Part 2 : My Tooling - Part 2 - Setting up my dotfiles manager
Part 3 : My Tooling - Part 3 - Installing my apps and packages
Setting up my dotfiles manager #
(As of now, I will be focusing on MacOS Device, but will update as I venture into using Linux/Windows as my dev machines)
To automate managing my dotfiles, I depend on Chezmoi.
But, first, let’s a take step back and state the obvious, “What are dotfiles?” and “Why should anyone care?”
What are dotfiles? #
Dotfiles are configuration files for various programs that help manage their functionality. They are named “dotfiles” because each file and directory starts with a dot (.). On Unix-based systems, dotfiles are hidden by the Operating System by default.
These files control the settings and preferences for applications and your system environment. They can include scripts, aliases, functions, and more that you can use to customize your shell and applications on macOS, Linux, and Windows.
Dotfiles are very important as they backup, restore, and sync the preferences and settings for your toolbox.
Why should a developer care about dotfiles? #
Becasue
Customization and Productivity: Developers spend a significant amount of time tweaking and optimizing settings that best suit their workflow and preferences. Dotfiles provide a way to customize your system to your liking and setup scripts and whatever to making their machine more organized and primed to help make them more productive.
Consistency: Dotfiles provide a consistent environment across not only reboots, but also different machines, in case one frequently switches between different systems.
Backup and Restore: Dotfiles can be used to backup your configuration settings. If your system crashes or if you switch to a new machine, you can easily restore your settings.
Shareability: Dotfiles are easily shareable and reusable by other people.
In summary, the time invested in creating and maintaining your dotfiles will reward you many times over during your development work.
Chezmoi #
Pronounced as “shay-mwa” (french word)
The WHAT? #
Chezmoi offers a robust, secure, and efficient way to manage dotfiles (configuration files for tools and applications) across multiple machines and operating systems, providing both consistency and customization where needed.
It uses a version-controlled source directory to keep track of changes and apply them to any number of systems.
Chezmoi is particularly adept at handling situations where configuration needs to differ slightly between machines, thanks to its support for templating and scripting within dotfiles.
For more details, see What is Chezmoi?
The WHY? #
Cross-Platform Consistency: Chezmoi works on macOS, Linux, and Windows, making it an ideal tool for users who work across different operating systems but want to maintain a consistent working environment.
Secure and Private: It has built-in support for securely managing secrets (like API keys or passwords) within your dotfiles, ensuring sensitive information is safely handled.
Version Control Integration: Chezmoi is built to work with version control systems like Git, making it easy to track changes to your dotfiles, share them across machines, and even collaborate with others.
Customization and Flexibility: Through templating, Chezmoi allows you to have base configurations that can be customized per machine or environment without duplicating effort. This is particularly useful for settings that need to be slightly different on a work computer versus a personal one.
Simplicity and Automation: Chezmoi simplifies the process of setting up a new machine to your preferences. With a single command, you can have all your familiar tools and configurations ready to go, automating what would otherwise be a manual and error-prone process.
Idempotency: Chezmoi’s operations are idempotent, meaning you can reapply your configurations multiple times without worrying about creating duplicate entries or unintended side effects. This makes it safe and predictable to use.
For more info, see
Use Chezmoi commands as much as possible, and resist the temptation to using git or other seemingly equivalent commands ... as they are not the same.
Chezmoi commands does extra book keeping and that is needed to keep the configuration system healthy and working as expected.
The HOW? #
Here is a common usage workflow of Chezmoi
Installation
First, you need to install Chezmoi. This can be done via various package managers or by downloading a binary directly. For example, on macOS with Homebrew:
brew install chezmoiFirst time initialization
Initialize Chezmoi for the first time. This will create a Chezmoi source directory where your dotfiles and scripts will be stored.
chezmoi initAdding Dotfiles
Add your existing dotfiles to the Chezmoi
sourcedirectory (~/.local/share/chezmoi). Chezmoi will track these files and manage their deployment to your home directory.chezmoi add ~/.bashrc chezmoi add ~/.vimrcDealing with symlinks
If an existing dotfile in home folder is a symlink, if youchezmoi addsuch a symlink, chezmoi will add the symlink, not the file. To assist with migrating from symlink-based systems, use the--followoption to chezmoi add - see Migrate from a dotfile manager that uses symlinksAdding files from sub-folders of home diectory
To add files that are in subfolders of your home folder, you can use the--exactflag withchezmoi addto indicate that the file’s full path should be preserved. To this, Chezmoi creates a path within the source directory that mirrors the file’s intended path with each folder getting a prefix ofexact_.For example
$ chezmoi add --exact ~/subfolder1/subfolder2/configIt results in
~/.local/share/chezmoi/exact_subfolder1/exact_subfolder2/config(note:~/.local/share/chezmoiis Chezmoisourcedirectory)NOTE: One can also use Chezmoi to manage files outside of user folder (say
/etc/ssl/openssl.cnf). But it is an involved process.Managing File Permissions
Chezmoi can also manage file permissions. When you add a file with Chezmoi, it records the file’s permissions in the source state file (chezmoistate.boltdbby default). When Chezmoi applies the file, it sets the permissions to match the recorded state.You can manually specify permissions when adding a file using the
--chmodflag:chezmoi add --chmod=644 /path/to/your/fileOr, for files already managed by Chezmoi, you can modify permissions directly in the source directory’s filename. For example, to set a script to be executable:
chmod +x ~/.local/share/chezmoi/path/to/your/script chezmoi add ~/.local/share/chezmoi/path/to/your/scriptHow/Where does Chezmoi store File Attributes
Chezmoi stores file attributes, including permissions, in the source state file (chezmoistate.boltdb). This database includes metadata about each managed file, such as its permissions and whether it’s a template or an encrypted file. For version 1.x of Chezmoi, this information was stored in the filename itself (e.g.,executable_,private_prefixes), but Chezmoi version 2.x uses the state file for a cleaner and more flexible approach.Initial Commit
Once all the files
git remote add origin git@github.com:your-awesome-username/dotfiles-branchname.git git branch -M mainEditing Dotfiles
Edit your dotfiles directly through Chezmoi. This ensures changes are made in the
sourcedirectory (~/.local/share/chezmoi) and can be applied or pushed to your repository.chezmoi edit ~/.bashrcThe editor used will be based on
Chezmoi being configured to use a specific editor by setting the
edit.commandvariable in the chezmoi configuration file (~/.config/chezmoi/chezmoi.toml).
For example, to useVSCode, you can add the following lines to the chezmoi configuration file:[edit] command = "code" args = ["--wait"]If above doesn’t apply, Chezmoi opens the default editor set by the user (based on
$VISUALor$EDITORenv variables).If neither of the above apply, it falls back to a default editor depending on your operating system (
vion UNIX-like operating systems,notepad.exeon Windows)
Applying Changes
After making changes using previous step, apply the changes to your home directory. This makes the current state of the Chezmoi source directory (
~/.local/share/chezmoi) reflected in your actual dotfiles (home folder)chezmoi applyCommitting Changes
After making and verifying the changes,
committhem to your dotfiles repository. This involves using Git commands within the Chezmoi source directory.chezmoi cd git add . git commit -S -m "Update dotfiles" git pushNon-first initializations (on a new machine where we want to use existing Chezmoi config)
If you already have a dotfiles repository on github, Chezmoi can clone it for you and initialize with it:
chezmoi init <your-git-repo-url>Besides official
<your-git-repo-url>, Chezmoi also takes a few simpler patterns (like<github-username>/<github-repository-name>) and tries to guess the github repo link from it, for example. For more details of all patterns allowed, see Chezmoi docs for init command.Updating (pulling) changes to configuration files (Dotfiles etc.) from remote repo
To pull and apply changes from your dotfiles repository to another machine, use:
chezmoi updateJust to have a better understanding,
chezmoi updatecommand triggersgit pull --autostash --rebase --recurse-submodulesbesides other book keeping activities.Managing Secrets
For managing secrets (like API keys or passwords), Chezmoi integrates with many of the commonly used password managers. See https://www.chezmoi.io/user-guide/password-managers/
Script Execution
Chezmoi allows you to run scripts automatically during the apply process. Scripts prefixed with
run_once_are executed only once, while those withrun_can be executed every time.chezmoi add ~/.local/share/chezmoi/run_once_setup-script.shReapplying or Forcing Scripts
If you need to reapply all changes or force the re-execution of
run_once_scripts:chezmoi apply --forceThis workflow covers the basics of getting started with Chezmoi, managing your dotfiles, and keeping them synchronized across multiple machines. Chezmoi’s documentation provides more detailed information on advanced features and configurations to tailor its behavior to your needs.
Resources
- Dotfiles
- Introduction to Dotfiles - DEV Community
- Dotfiles — What is a Dotfile, Why do we need Dotfile and How do we create it in Mac
- Introduction to Dotfiles
- The Ultimate Guide to Mastering Dotfiles - daytona.io
- Chezmoi
- How To Manage Dotfiles With Chezmoi
──────────── 〔✿〕────────────