- Shell 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| bin | ||
| common | ||
| hosts | ||
| .gitignore | ||
| .sops.yaml | ||
| README.md | ||
home_dir
Dotfiles synced across hosts. Secrets are encrypted at rest with SOPS + age and decrypted only when syncing.
Layout
.sops.yaml # encryption rules (which age / SSH public keys can decrypt)
bin/
sops # sops binary
sync-dotfiles # main sync script
verify-dotfiles # check files on targets against recorded checksums
checksums # build / check / show the checksum manifests
sops-encrypt # encrypts files in place, recording plaintext checksums
sops-decrypt # decrypt a .enc file (stdout, -o FILE, or -i in place)
rekey # re-encrypt files after recipients change in .sops.yaml
bulk_encrypt # encrypt every plaintext file under any secrets/ dir
lib/dotfiles.sh # shared helpers (sourced by the scripts above)
common/ # shared by every host
public/ # plaintext files — copied verbatim to $HOME
public.sha256 # checksums of public/ (plaintext)
secrets/ # sops-encrypted files — decrypted during sync
.dotfiles-checksums.enc # checksums of secrets/ (encrypted; never synced)
hosts/
targets # rsync destinations: DEST [NAME] per line (gitignored)
targets.example # template
<name>/ # optional host-specific overlay for host <name>
public/
public.sha256
secrets/
.dotfiles-checksums.enc
Each public/ and secrets/ dir mirrors the target home directory. A file at common/public/.bashrc lands at ~/.bashrc. A file at common/secrets/.ssh/id_ed25519.enc is decrypted and lands at ~/.ssh/id_ed25519.
Common vs. host-specific files
Every target receives everything in common/. A target whose host name has a hosts/<name>/ directory also receives that directory's public/ and secrets/. The layers are applied in this order, with later layers overwriting earlier ones at the same path:
common/public/common/secrets/hosts/<name>/public/hosts/<name>/secrets/
So to give one host a different .bashrc, keep the shared one in common/public/.bashrc and put the override in hosts/<name>/public/.bashrc. Host-only files (no common counterpart) work the same way.
The host name comes from the optional second column in hosts/targets, otherwise from the hostname in the destination (greg@laptop.local: → hosts/laptop.local/). A host with no overlay directory gets only common/.
Encrypted files always end in .enc. .gitignore refuses anything under common/secrets/ or hosts/*/secrets/ without that suffix, so a stray cp ~/.token common/secrets/.token can't be committed by mistake (use git add -f if you really mean to override).
Prerequisites
sops(bundled atbin/sops)agefor generating keys (pacman -S age/brew install age)rsyncandsshon both source and target hosts- On the source host, a private key that is listed as a recipient in
.sops.yaml: either an age key or an SSH key (ssh-ed25519orssh-rsa). See Decryption keys.
Decryption keys: age or SSH
Secrets are encrypted to a list of recipients in .sops.yaml. Any one matching private key can decrypt them. A recipient can be:
| Recipient type | Public key looks like | Private key sops uses on the source host |
|---|---|---|
| age key | age1... |
$SOPS_AGE_KEY_FILE, or ~/.config/sops/age/keys.txt |
| SSH ed25519 key | ssh-ed25519 AAAA... |
$SOPS_AGE_SSH_PRIVATE_KEY_FILE, or ~/.ssh/id_ed25519 |
| SSH RSA key | ssh-rsa AAAA... |
$SOPS_AGE_SSH_PRIVATE_KEY_FILE, or ~/.ssh/id_rsa |
The bundled sops (3.13) handles SSH recipients natively through age, so no extra tooling is needed. ECDSA and other SSH key types are not supported, and keys held only in ssh-agent or on a hardware token can't be used. sops reads the private key file directly.
You can mix types freely: for example, an age key on your workstation and your existing SSH key on a laptop.
Option A: use an age key
mkdir -p ~/.config/sops/age
age-keygen -o ~/.config/sops/age/keys.txt
chmod 600 ~/.config/sops/age/keys.txt
grep '^# public key:' ~/.config/sops/age/keys.txt # -> age1...
To keep the key somewhere else, export SOPS_AGE_KEY_FILE=/path/to/keys.txt.
Option B: use an existing SSH key
Print the public half. This is the line that goes into .sops.yaml:
cat ~/.ssh/id_ed25519.pub # -> ssh-ed25519 AAAA... comment
sops looks for ~/.ssh/id_ed25519, then ~/.ssh/id_rsa. For a different key:
export SOPS_AGE_SSH_PRIVATE_KEY_FILE=~/.ssh/my_other_key
Passphrase-protected SSH keys: sops asks for the passphrase on the terminal, but it does so once per secret file, because sync-dotfiles runs sops for each file. Without a terminal (cron, CI) decryption fails. To unlock it only once, make a temporary unprotected copy in RAM for the duration of the sync:
umask 077
cp ~/.ssh/id_ed25519 /dev/shm/sync-key
ssh-keygen -p -N '' -f /dev/shm/sync-key # prompts for the passphrase once
SOPS_AGE_SSH_PRIVATE_KEY_FILE=/dev/shm/sync-key bin/sync-dotfiles -nv
shred -u /dev/shm/sync-key
Registering a key (age or SSH) as a recipient
-
Add the public key to the
age:list in.sops.yaml, one entry per line (see the comments in that file). Put the comment part of an SSH public key on its own#line above the entry, or leave it out. -
Re-key the existing secrets so the new key can decrypt them. This step needs a key that is already a recipient:
bin/rekey # every .enc file, including the checksum manifests bin/rekey -i # same, but show sops's key diff and ask per file bin/rekey sv5 # just one layer (common, or a host name)rekeyrunssops updatekeyson each file with the right--input-type, and reports how many files changed and how many were already current. File contents don't change, so the recorded checksums stay valid. -
Commit
.sops.yamland the re-encrypted.encfiles.
To revoke a key, delete its line from .sops.yaml and run bin/rekey -r. The -r also rotates each file's data key (sops rotate). Without it, the removed key could still decrypt future commits if its holder had saved the old data key. This only protects future versions of the files. Anyone holding the old key can still decrypt the old ciphertext in git history, so rotate the underlying secrets too.
If a host's secrets should be decryptable by fewer keys, add a more specific path_regex rule above the catch-all in .sops.yaml with its own recipient list, then run bin/rekey <host> (add -r if keys were removed).
One-time setup on a new source host
- Choose a key, either age or SSH.
- Register it as a recipient. Step 2 must be run from a host that can already decrypt.
- Confirm decryption works:
bin/sync-dotfiles -nvshould list files with no sops errors.
Set up the hosts file:
cp hosts/targets.example hosts/targets
$EDITOR hosts/targets
Adding files
A new plaintext dotfile
Drop it under common/public/ (all hosts) or hosts/<name>/public/ (one host) at the path it should appear in $HOME:
cp ~/.vimrc common/public/.vimrc
mkdir -p hosts/laptop.local/public
cp ~/.gitconfig hosts/laptop.local/public/.gitconfig
A new secret file
Put the plaintext under common/secrets/ or hosts/<name>/secrets/ at the path it should appear in $HOME, then encrypt it:
mkdir -p common/secrets/.ssh
cp ~/.ssh/id_ed25519 common/secrets/.ssh/id_ed25519
bin/sops-encrypt common/secrets/.ssh/id_ed25519
# Result: common/secrets/.ssh/id_ed25519.enc (plaintext is shredded)
To encrypt everything still in plaintext under any secrets/ dir at once, run bin/bulk_encrypt.
sops-encrypt writes <file>.enc and removes the plaintext input. The format is picked from the original extension:
*.yaml/*.yml/*.json/*.env/*.ini→ field-level encryption (keys stay readable, values encrypted — good for review-friendly diffs)- everything else → binary mode (whole file encrypted)
Before encrypting, sops-encrypt takes the SHA-256 of the plaintext. It then decrypts the result to make sure the output matches that checksum, and records the checksum in the layer's encrypted manifest (see Checksums and verification). The plaintext is shredded only after the .enc file and the manifest are both written. If anything fails, the .enc files from that run are removed and the plaintext is left alone, so you can just rerun it.
sops re-serializes structured files (yaml/json/env/ini) on decrypt, so the deployed file can differ byte-for-byte from what you encrypted, for example in indentation. When that happens sops-encrypt refuses, because verification would otherwise always fail. Either reformat the file the way sops emits it, or pass -N to record the checksum of the deployed form.
Editing an existing secret
For structured files (yaml/json/env/ini), sops opens an editor with plaintext and re-encrypts on save. Because the file ends in .enc, pass --input-type so sops knows the format:
bin/sops --input-type yaml --output-type yaml common/secrets/secrets.yaml.enc
bin/checksums update common # editing in place bypasses sops-encrypt
For binary files (SSH keys, .my.cnf, etc.), decrypt → edit → re-encrypt:
bin/sops-decrypt -i common/secrets/.my.cnf.enc # -> common/secrets/.my.cnf (mode 600), .enc removed
$EDITOR common/secrets/.my.cnf
bin/sops-encrypt common/secrets/.my.cnf # re-encrypts and records the new checksum
The plaintext sits under secrets/, so git ignores it and sync-dotfiles refuses to run until it's re-encrypted.
Viewing a secret
bin/sops-decrypt common/secrets/.my.cnf.enc # to stdout
bin/sops-decrypt -o /dev/shm/my.cnf common/secrets/.my.cnf.enc # to a file (mode 600)
sops-decrypt chooses the format from the file name, as sops-encrypt does. If the decrypted content doesn't match the recorded checksum, it prints a warning, meaning the manifest is stale or the file was changed outside sops-encrypt.
Removing a file from sync
Delete it from the relevant public/ or secrets/ dir, then run bin/checksums update so the file is no longer expected on targets. To also remove it from target hosts on the next sync, pass -d (rsync --delete).
Syncing
Always dry-run first:
bin/sync-dotfiles -nv
Then real run:
bin/sync-dotfiles
Flags
| Flag | Effect |
|---|---|
-n |
Dry run (rsync --dry-run) |
-v |
Verbose |
-d |
--delete extraneous files on the target |
-f FILE |
Use an alternate hosts file |
-H NAME |
Use hosts/NAME/ overlay for command-line targets |
-s |
Strict: skip hosts whose tree doesn't match the checksum manifests |
Syncing to a single ad-hoc host
Arguments override hosts/targets:
bin/sync-dotfiles -v greg@laptop.local:
The trailing colon matters — rsync needs it to recognize the path as a remote. The overlay is picked from the hostname (hosts/laptop.local/ here); use -H to choose another, e.g. bin/sync-dotfiles -H laptop greg@10.0.0.5:.
Adding a new target host
Each target only needs ssh access from the source — the source decrypts locally and pushes plaintext over SSH, so targets do not need their own age or SSH decryption key.
- Make sure you can
ssh user@hostwithout a password prompt (key auth). - Add the destination to
hosts/targets, optionally followed by a host name (e.g.greg@10.0.0.5: laptop). - Optionally create
hosts/<name>/public/and/orhosts/<name>/secrets/for host-specific files. - Run
bin/sync-dotfiles -nvto dry-run, then drop-nonce it looks right. - Run
bin/verify-dotfiles user@host:to confirm the host has the expected files.
Checksums and verification
Every layer keeps SHA-256 checksums of its plaintext files, meaning the bytes that land on the target, never the ciphertext. The format is the same as sha256sum, with paths relative to $HOME:
| File | Covers | Stored as |
|---|---|---|
<layer>/public.sha256 |
<layer>/public/ |
plaintext (the files are public anyway) |
<layer>/secrets/.dotfiles-checksums.enc |
<layer>/secrets/ |
sops-encrypted |
The secrets manifest is encrypted because a bare SHA-256 of a short secret, such as a password or PIN, can be brute-forced offline. Reading it needs the same key as the secrets themselves. The sync skips it, so it is never deployed.
Keeping manifests current
-
Secrets added with
bin/sops-encrypt/bin/bulk_encryptare recorded automatically, from the plaintext, before encryption. -
Anything else needs a rebuild: public file changes, secrets edited in place with
sops, deleted files, or existing.encfiles from before checksums were added. Run:bin/checksums update # all layers bin/checksums update common # or just one layer (common, or a host name)This hashes public files directly and decrypts secrets into a shredded tmp dir to hash them.
-
bin/checksums checkreports any drift between the repo and the manifests, and exits 1 if there is any. -
bin/sync-dotfileschecks every host's tree against the manifests before pushing. It warns on drift, or with-sit refuses to push that host.
Commit manifest changes along with the files they describe, so git log shows when an expected checksum changed.
Verifying targets
bin/verify-dotfiles # every target in hosts/targets
bin/verify-dotfiles -v greg@laptop.local: # one host, list matching files too
bin/verify-dotfiles -o BatchMode=yes # extra ssh options (repeatable)
For each target, verify-dotfiles merges the manifests the same way the sync layers files (common, then hosts/<name>/). It then runs a small POSIX sh script over SSH that hashes only the expected paths with sha256sum (or shasum -a 256 on macOS) and returns the hashes. The comparison happens locally, so no expected checksums or file contents are sent to the target. Each problem is reported as:
| Status | Meaning |
|---|---|
MISMATCH |
file present, content differs |
MISSING |
file not present on the target |
UNREADABLE |
file present but the SSH user can't read it |
The exit status is 0 only if every target matches, so it can be used from cron or CI. Hosts are chosen the same way as in sync-dotfiles (-f FILE, -H NAME, positional DEST).
Limits: only files the repo manages are checked, so extra files on the target are ignored. Permissions and ownership aren't compared. A compromised target can report whatever hashes it likes. Verification catches drift and accidents, not a hostile host.
bin/checksums show <host> prints the merged manifest a host is expected to match. It includes secret checksums, so don't paste it anywhere public.
How secrets are handled during sync
sync-dotfilescreates a staging directory under$TMPDIR(mode0700).- A common tree is built: files in
common/public/are copied in verbatim, then files incommon/secrets/are decrypted at the same relative path with the.encsuffix stripped andchmod 600. - For each distinct host name, the common tree is copied to a per-host tree and
hosts/<name>/public/andhosts/<name>/secrets/are layered on top the same way..ssh/becomes0700. The sync aborts if any file in asecrets/dir lacks.enc. - Each host's tree is hashed and compared to the checksum manifests (warning, or skip with
-s). rsync -a -e sshpushes each host's tree to its target's$HOME.- On exit (including errors / Ctrl-C), a trap
shreds the staged files and removes the directory.
Plaintext only ever exists on disk on the source host (briefly, in a tmp dir) and on the target hosts (persistently, where it belongs). Set TMPDIR=/dev/shm if you want the staging dir in RAM.
Threat model notes
- An attacker with read access to this repo (e.g. on GitHub) sees only ciphertext.
- An attacker with any recipient's private key can decrypt everything that key is a recipient for. Treat
~/.config/sops/age/keys.txtas a root-equivalent secret, and every SSH key listed in.sops.yamlthe same way. - Using an SSH key as a recipient ties secret access to that key. If the same key is also synced by this repo (for example,
common/secrets/.ssh/id_ed25519.enc), every target host that receives it can decrypt the repo too. That may be what you want. If not, use a dedicated age key or a key that isn't synced. - Use a passphrase on SSH keys where practical, and keep any unlocked copy in RAM (
/dev/shm) only for the length of a sync. - Target hosts hold plaintext secrets on disk. Use full-disk encryption on every target.