# sops-nix Atomic secret provisioning for NixOS based on [sops](https://github.com/mozilla/sops). ## How it works Sops-nix decrypts secrets [sops files](https://github.com/mozilla/sops#2usage) on the target machine to files specified in the NixOS configuration at activation time. It also adjusts file permissions/owner/group. It uses either host ssh keys or GPG keys for decryption. In future we will also support cloud key management APIs such as AWS KMS, GCP KMS, Azure Key Vault. ## Features - Compatible with all NixOS deployment frameworks: [NixOps](https://github.com/NixOS/nixops), nixos-rebuild, [krops](https://github.com/krebs/krops/), [morph](https://github.com/DBCDK/morph) - Version-control friendly: Since all files are encrypted they can directly committed to version control. The format is readable in diffs and there are also ways of showing [git diffs in cleartext](https://github.com/mozilla/sops#showing-diffs-in-cleartext-in-git) - CI friendly: Since nixops files can be added to the nix store as well without leaking secrets, machine definition can be build as a whole. - Atomic upgrades: New secrets are written to a new directory which replaces the old directory in an atomic step. - Rollback support: If sops files are added to Nix store, old secrets can be rolled back. This is optional. - Fast: Unlike solutions implemented by NixOps, krops and morph there is no extra step required to upload secrets - Different storage formats: Secrets can be stored in Yaml, JSON or binary. ## Usage example ### 1. Install nix-sops TODO ### 2. Generate a GPG key for yourself First generate yourself [a GPG key](https://docs.github.com/en/github/authenticating-to-github/generating-a-new-gpg-key) or use nix-sops conversion tool to convert an existing ssh key (we only support RSA keys right now): ``` $ nix-shell -p ssh-to-pgp $ ssh-to-pgp -privkey $HOME/.ssh/id_rsa | gpg --import --quiet 2504791468b153b8a3963cc97ba53d1919c5dfd4 ``` If you get: ``` ssh-to-pgp: failed to parse private ssh key: ssh: this private key is passphrase protected ``` then your ssh key is encrypted with your password and you need to create a encrypted copy temporarily: ``` $ cp $HOME/.ssh/id_rsa /tmp/id_rsa $ ssh-keygen -p -N "" -f /tmp/id_rsa $ ssh-to-pgp -privkey /tmp/id_rsa | gpg --import --quiet ``` The hex string printed here is your GPG fingerprint that can be exported to `SOPS_PGP_FP`. ``` export SOPS_PGP_FP=2504791468b153b8a3963cc97ba53d1919c5dfd4 ``` If you have generated a gnupg key directly you can get your fingerprint like this: ``` gpg --list-secret-keys --fingerprint /tmp/tmp.JA07D1aVRD/pubring.kbx ------------------------------- sec rsa2048 1970-01-01 [SCE] 9F89 C5F6 9A10 281A 8350 14B0 9C3D C61F 7520 87EF uid [ unknown] root ``` The fingerprint here is `9F89 C5F6 9A10 281A 8350 14B0 9C3D C61F 7520 87EF`, you need to remove the space in-between manually. ### 3. Get a GPG key for your machine The easiest way to add new hosts is using ssh host keys (requires openssh to be enabled). Since sops does not natively supports ssh keys yet, nix-sops supports a conversion tool to store them as gpg keys. ``` $ nix-shell -p ssh-to-gpg # One can use ssh-keyscan over the network $ ssh-keyscan -t rsa server01 | ssh-to-pgp -pubkey - > server01.asc # server01:22 SSH-2.0-OpenSSH_8.2 0fd60c8c3b664aceb1796ce02b318df330331003 # via ssh command: $ ssh server01 "cat /etc/ssh/ssh_host_rsa_key.pub" | ssh-to-gpg -pubkey - > hosts/server01.asc 0fd60c8c3b664aceb1796ce02b318df330331003 # Or just read them locally (or in a ssh session) $ ssh-to-pgp -pubkey /etc/ssh/ssh_host_rsa_key.pub > server01.asc 0fd60c8c3b664aceb1796ce02b318df330331003 ``` Also the hex string here is the fingerprint of your server's gpg key that can be exported append to `SOPS_PGP_FP`: ``` export SOPS_PGP_FP=${SOPS_PGP_FP}:2504791468b153b8a3963cc97ba53d1919c5dfd4 ``` If you prefer having a separate gnupg key, sops-nix also comes with a helper tool: ``` $ nix-shell -p sops-init-gpg-key $ sops-init-gpg-key --hostname server01 --gpghome /tmp/newkey You can use the following command to save it to a file: cat > server01.asc < {}; mkShell { # imports all files ending in .asc/.gpg and sets $SOPS_PGP_FP. sopsGPGKeyDirs = [ "./keys/hosts" "./keys/users" ]; # Also single files can be imported. #sopsGPGKeys = [ # "./keys/users/mic92.asc" # "./keys/hosts/server01.asc" #]; nativeBuildInputs = [ (pkgs.callPackage {}).sops-shell-hook sops ## you may also need gnupg # gnupg ]; } ``` After that you can create a new file with sops ``` sops secrets.yaml ```