samodra

3 min read

What I changed when I cleaned up my dotfiles

I started with a small goal: tidy my dotfiles so I could put them on GitHub. Once I looked at what was in there, I found the setup was messier than I remembered.

My old backup script copied folders and files as they were, and Chezmoi replaced it. The change came down to four problems and what I did about each one.

1. Secrets could end up in the repo history

Before. If a password, token or API key sits in .zshrc, .bashrc or any other shell config file, a raw rsync or cp backup can carry that value straight into the repo. Once it is committed, it is in the history:

git show HEAD
+ export API_KEY="sk-..."
+ export GITHUB_TOKEN="ghp_..."

After. Environment variables are separate from shell configuration. Sensitive values live in files like .zshrc.local or .env. Git ignores both, and I manage them locally on each device:

~/.zshrc.local   # ignored
~/.env           # ignored

My rule now is that .zshrc holds shell configuration and nothing else. .zshrc.local holds settings that belong to one machine, .env holds the variables individual scripts use, and secret files stay local and are listed in .gitignore.

2. Everything piled up in .zshrc

Before. Aliases, PATH entries and secret values all lived in one file, with a question mark about which OS each line was for:

# ~/.zshrc
export PATH="/opt/homebrew/bin:$PATH"
export PATH="$HOME/.local/bin:$PATH"
export DB_PASSWORD="supersecret"
# macOS? Linux?

After. One source file, with the platform branches written out. The global configuration stays in one place, and the macOS and Linux parts are explicit. Chezmoi templates make that possible:

# dot_zshrc.tmpl
export EDITOR=nvim

{{ if eq .chezmoi.os "darwin" }}
  export PATH="/opt/homebrew/bin:$PATH"
{{ else if eq .chezmoi.os "linux" }}
  export PATH="$HOME/.local/bin:$PATH"
{{ end }}

dot_zshrc.tmpl is still easy to read, and I no longer have to guess which line applies to which machine.

3. Sync could overwrite whole files

Before. Running rsync or cp from a different machine can overwrite an existing file, the whole file at once. The --delete flag makes it worse, and the same command run in the opposite direction does the same damage the other way:

rsync -av --delete ~/dotfiles/ user@linux:~/
# or worse, the other direction...

After. The configuration state is applied, not mirrored. Chezmoi makes the repository the managed source of configuration, and each machine applies it according to its own environment. On a new device that is one command:

chezmoi init --apply <github-username>

4. Custom scripts were mixed together

Before. My scripts had inconsistent names, and scripts for different platforms sat side by side. It was hard to tell which one was safe to run on a given machine:

scripts/
├── backup.sh
├── mac-setup.sh
├── setup_linux.sh
├── install
└── update_dotfiles_v2.sh

After. Custom scripts are grouped by platform, and general scripts stay in the root scripts folder:

scripts/
├── linux/
│   ├── setup.sh
│   └── install-deps.sh
├── macos/
│   ├── setup.sh
│   └── install-deps.sh
└── common/
    └── utils.sh

The same idea covers the rest of the OS-specific configuration, including the Linux desktop settings. It is kept apart from what both systems share.

What Git manages

The sentence I keep coming back to is this: what Git manages is the configuration, not the entire state of my device. My old backup treated the two as the same thing, which is how secrets ended up next to shell settings. Now the repo holds what I want to reproduce on any machine, and everything specific to one machine stays on that machine.