How I Stopped Losing My Configs Every Time I Switch Linux Distros

Recently I wanted to change my Linux distro from Pop!_OS to Linux Mint for I don't know how many times; I keep going back to Mint. But this time I didn't want to lose all my config files.
I'm sure there are so many ways of doing this, but this is how I did it.

Simple Steps

  • Install Chezmoi on your current machine.
  • Create a repository on GitHub where your dotfiles live. I named mine dotfiles and made it private.
  • Open your terminal on your machine and run chezmoi init to create a local repo at ~/.local/share/chezmoi where all your dotfiles live.
  • Run chezmoi add [FILE_NAME]] to add your dotfiles one by one. For instance chezmoi add ~/.bashrc to add .bashrc
  • After adding all your files, run chezmoi cd to get to the directory where the dotfiles live.
  • In this repo, git add, commit and connect it to your remote repo to push it to GitHub.
    • git add .
    • git commit -m "message"
    • git remote add origin git@github.com:USERNAME/dotfiles.git
  • On your new machine later on, use chezmoi init --apply git@github.com:USERNAME/dotfiles.git to pull your dotfiles from your GitHub.

NOTE: You might have to first configure your GitHub on this device first to use it.

  • Generate a new SSH key on the new machine first, add it to GitHub as a deploy/personal key, use that to clone, or
  • Use HTTPS with a personal access token for the initial clone instead of SSH, or
  • Manually copy the SSH key over via USB/other trusted channel before running chezmoi init --apply, or
  • Install gh, run gh auth login interactively on the new machine (it handles auth without needing a pre-existing key), then gh repo clone

Encrypt sensitive files.

  • I used age to encrypt my files. You can find the installation instructions for your device here.
  • After installation, first generate a keypair
    • mkdir -p ~/.config/age to creates a directory where the age encryption lives
    • age-keygen -o ~/.config/age/key.txt to generate a public and private key
  • The public key is safe to share, but the private one should stay on the machine and NEVER in the repo. Copy it manually to another drive or a cloud you trust.
  • Tell Chezmoi to use age
    • Edit or create ~/.config/chezmoi/chezmoi.toml
    • Add this to your file.
      encryption = "age"
      [age]
      identity = "~/.config/age/key.txt"
      recipient = "YOUR-PUBLIC-KEY"
    • identity is the private key, the directory, and the file we created earlier.
    • recipient is the public key; copy and paste it.
  • Then add secrets as encrypted like this chezmoi add --encrypt ~/.ssh/id_ed25519
  • Before running chezmoi init --apply in a new machine, the private key needs to be present.
    • mkdir -p ~/.config/age and copy key.txt manually.
    • Then chezmoi init --apply git@github.com:USERNAME/dotfiles.git

A few files you need to encrypt

  • ~/.git-credentials
  • ~/.ssh/id_ed25519
  • ~/.ssh/config (If it contains sensitive host-names/IPS, use your judgment)
  • ~/.env
  • ~/.netrc
  • ~/.config/gh/hosts.yml GitHub CLI

A few more commands that might be useful.

  • You can use .chezmoiignore to exclude certain things per-machine
  • chezmoi diff can show you a diff between your actual home directory files and what chezmoi's source state says they should be.

If you know a better way of doing this, let me know here