
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
dotfilesand made it private. - Open your terminal on your machine and run
chezmoi initto create a local repo at~/.local/share/chezmoiwhere all your dotfiles live. - Run
chezmoi add [FILE_NAME]]to add your dotfiles one by one. For instancechezmoi add ~/.bashrcto add.bashrc - After adding all your files, run
chezmoi cdto get to the directory where the dotfiles live. - In this repo,
git add,commitand 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.gitto 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, rungh auth logininteractively on the new machine (it handles auth without needing a pre-existing key), thengh repo clone
Encrypt sensitive files.
- I used
ageto encrypt my files. You can find the installation instructions for your device here. - After installation, first generate a keypair
mkdir -p ~/.config/ageto creates a directory where theageencryption livesage-keygen -o ~/.config/age/key.txtto 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
Chezmoito useage- 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.
- Edit or create
- Then add secrets as encrypted like this
chezmoi add --encrypt ~/.ssh/id_ed25519 - Before running
chezmoi init --applyin a new machine, the private key needs to be present.mkdir -p ~/.config/ageand copykey.txtmanually.- 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
.chezmoiignoreto exclude certain things per-machine chezmoi diffcan 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