Secrets Management
Platforms: NixOS and nix-darwin, through nixosModules.secrets and darwinModules.secrets. See Darwin.
Secrets are managed with sops-nix using age encryption. The identity used for editing can be backed by a YubiKey.
Architecture
This repository holds no secrets. othrys.system.secrets provides the sops-nix
plumbing (age decryption, YubiKey identities), and the encrypted material lives
in the consuming flake, typically as a private secrets input that its hosts
declare and its CI stubs out.
- Encrypted with age, backed by a YubiKey identity
- Decrypted at activation time by sops-nix, never stored in the Nix store
- Modules take runtime file paths, so what provides them is the consumer’s choice
Importing
The plumbing is the separate export nixosModules.secrets, which
nixosModules.default does not import. It and its darwin form are the only
othrys modules that write into the sops option namespace, so a host that wants it imports the export
together with the sops-nix module, and a host that installs its secrets another
way imports neither:
# flake inputs
sops-nix.follows = "othrys/sops-nix";
# host modules
inputs.othrys.nixosModules.secrets
inputs.sops-nix.nixosModules.sops
The export without the sops-nix module fails at evaluation, since a write into an undeclared option namespace fails whatever guards it. A host with neither still uses every other module, since each option that takes a secret takes a path and reads the file at run time.
A nix-darwin host imports darwinModules.secrets with the
darwinModules.sops of the sops-nix flake, or takes both from
darwinModules.full. The options are the same on both platforms.
Options
options.othrys.system.secrets = {
enable = lib.mkEnableOption "sops-nix secrets infrastructure";
# Expose secrets repo paths for host configs to reference
secretFiles = lib.mkOption {
# Deliberately lib.types.path. These are ENCRYPTED sops files from the
# secrets input, read at evaluation, so the store is where they belong.
type = lib.types.attrsOf lib.types.path;
default = secretsInput;
defaultText = lib.literalExpression "inputs.secrets.secretFiles, or {} when no secrets input is declared";
readOnly = true;
description = ''
Encrypted sops files, read from an optional `secrets` flake input in
the consuming flake. othrys declares no such input itself.
The contract is that when a consumer declares an input named `secrets`,
it exposes `secretFiles` as an attrset of names to encrypted files, for
example `{ common = ./common.yaml; host-atlas = ./atlas.yaml; }`.
Declaring no `secrets` input is fine and leaves this empty. Declaring
one that does not match the shape is an evaluation error rather than a
silently empty attrset.
'';
};
ageIdentityStubs = lib.mkOption {
type = lib.types.nullOr lib.types.lines;
default = null;
example = ''
#recipient: age1yubikey1qw3na0f9example...
AGE-PLUGIN-YUBIKEY-1EXAMPLE...
'';
description = ''
Age identity lines for manual sops editing, written to
`~/.config/sops/age/keys.txt`.
The content becomes a world-readable Nix store path, so this option is
safe only for identities that hold no key material on their own. An
age-plugin-yubikey stub qualifies, since the private key never leaves
the token and the stub is only a pointer to it. A raw
`AGE-SECRET-KEY-1...` does not, and an assertion rejects one. Use
ageIdentityFile for anything secret.
'';
};
ageIdentityFile = lib.mkOption {
type = lib.types.nullOr othrysTypes.secretPath;
default = null;
example = "/run/secrets/sops/age-identity";
description = ''
Runtime path to an age identity file, pointed at by SOPS_AGE_KEY_FILE
for manual sops editing. The required form for an identity that
contains key material, since the file is read from the secrets provider
at use time and nothing is copied into the store.
'';
};
};
The manual-editing identity
Editing an encrypted file by hand needs an age identity that sops can find, and there are two options for supplying one. They differ in where the identity ends up, which is the whole of the choice.
ageIdentityStubs takes the identity inline and writes it to
~/.config/sops/age/keys.txt. Home Manager renders that content into the Nix
store, where it is world-readable and stays readable for the life of every
generation referencing it. That is acceptable for an age-plugin-yubikey stub,
which names a token rather than carrying a key, since the private key never
leaves the hardware and the stub is useless without it. An assertion rejects
content containing AGE-SECRET-KEY, because a raw age identity pasted here
would publish the sops master key to every user on the host.
ageIdentityFile takes a runtime path instead and points SOPS_AGE_KEY_FILE
straight at it. No file is generated and nothing is copied, so this is the form
to use whenever the identity is itself secret.
Host Configuration
Secrets are declared in host configs (not in modules) using othrys.system.secrets for infrastructure and sops.secrets.* for individual secret declarations. See Host Configuration for a real example.
No othrys module declares a sops secret. modules/system/secrets.nixos.nix and
modules/system/secrets.darwin.nix write the decryption settings under
sops.age, and they are the only files in the module tree that write into
the sops namespace.
When a secret is installed
sops-nix installs secrets in two rounds. Secrets declared with
neededForUsers = true land under /run/secrets-for-users/ before the
users activation step, and every other secret lands under /run/secrets/
after it. The user password is read in that step, so
othrys.system.users.passwordFile has to name a secret from the first round.
A path under /run/secrets/ does not exist yet when the account is created,
which leaves it with no usable password, and the module warns on that prefix.
sops.secrets."users/alice/password".neededForUsers = true;
othrys.system.users.passwordFile = config.sops.secrets."users/alice/password".path;
A secret defaults to root and mode 0400. A service that reads its secret
as its own user needs owner set on the secret, which inadyn, headscale
and grafana all do. Each of those options says so in its description.
The decryption key
sops-nix decrypts with the host’s ed25519 ssh key, read through
sops.age.sshKeyPaths, and falls back to an age key it generates at
sops.age.keyFile. On an impermanence host both paths sit under the persist
root, where othrys.system.persistence keeps the ssh host keys, so the module
asserts that persistence.enable and services.openssh.enable are on
whenever impermanence is. Without them the key is regenerated on every boot
and nothing decrypts. The three sops.age values are set with mkDefault,
so a host that keeps its key elsewhere overrides them directly.
On nix-darwin the adapter sets sops.age.sshKeyPaths and
sops.gnupg.sshKeyPaths to empty lists, since a Mac may run no sshd and
have no host key. It turns sops.age.generateKey on, as the NixOS adapter
does, so sops-nix creates an age key at sops.age.keyFile on the first
activation. The host names that path, since sops-nix gives it no default
on darwin, and a host with no age key file, ssh key or GnuPG home is
refused.
Secret Files
| File | Purpose |
|---|---|
common.yaml | Secrets shared by every host (user passwords, service credentials) |
<hostname>.yaml | Host-specific secrets (e.g. the boot recovery password) |
YubiKey Setup
The othrys.services.security.yubikey module provides age-plugin-yubikey for manual sops editing. The module sets no touch policy. The policy belongs to the identity on the key and is chosen when that identity is generated, and the onboarding script generates it with a touch required for every decryption.
To provision a new YubiKey with an age identity for sops-nix (along with PGP keys and U2F credentials):
# New key generation
just yubikey-onboard
# Rotate keys using existing master key backup
just yubikey-onboard -- --from-backup /mnt/usb/yubikey-backup
The script generates the ageIdentityStubs value and age recipient needed for the host configs and .sops.yaml. When provisioning multiple YubiKeys for redundancy, add all age recipients to .sops.yaml. See the YubiKey Onboarding guide for the full workflow.
Key Rotation
A YubiKey backs four independent credential surfaces. Rotation means updating each one it touches. Redundancy is the backup strategy: enroll several YubiKeys so losing one is a revocation, not a lockout. The boot passphrase (sops security/boot/password) is the break-glass slot.
| Surface | Where it’s declared | Rotated with |
|---|---|---|
| age / sops identity | othrys.system.secrets.ageIdentityStubs (host) + recipients in .sops.yaml (secrets repo) | sops updatekeys |
| LUKS root unlock | othrys.system.disko.luks.fido2.enable (FIDO2 keyslot on the device) | just luks-enroll / systemd-cryptenroll --wipe-slot |
| SSH (GPG auth) | othrys.services.security.yubikey.sshKeygrips (host) | edit list + rebuild |
| U2F login/sudo | othrys.services.security.yubikey.u2fMappings.<user> (host) | edit list + rebuild |
The secret files and
.sops.yamllive in the privatesecretsflake input, so rotation spans two repos: this config (host options) and the secrets repo (.sops.yaml+sops updatekeys).
Add a YubiKey (grow redundancy)
- Provision the key:
just yubikey-onboard(orjust yubikey-onboard -- --from-backup <path>to clone an existing identity from its master-key backup). Note the printed age recipient, keygrip, and U2F line. - age: add the new recipient to
.sops.yamlin the secrets repo, then re-encrypt every secret so the new key can read it:sops updatekeys secrets/*.yaml - LUKS: with the root unlocked, add a FIDO2 keyslot for the new key (repeat per host):
just luks-enroll # touch the key when it blinks, break-glass passphrase authorizes the new slot sudo systemd-cryptenroll /dev/disk/by-id/<root> # verify the token count - SSH / U2F: append the new keygrip to
sshKeygripsand the new credential tou2fMappings.<user>in the host config. just switch <host>to apply, then confirm login/sudo/SSH work with the new key before relying on it.
Revoke a YubiKey (lost or compromised)
Do this from a machine that still has a working key. Order matters. Re-encrypting before removing the compromised recipient’s access is impossible, so revoke everywhere:
- age: remove the lost key’s recipient from
.sops.yaml, thensops updatekeysall secret files. The old key can no longer decrypt them. - LUKS: wipe its keyslot on every host (list first to find the slot):
backing=$(sudo cryptsetup status cryptroot | awk '/device:/ {print $2}') sudo systemd-cryptenroll "$backing" # identify the FIDO2 slot sudo systemd-cryptenroll --wipe-slot=<n> "$backing" # or --wipe-slot=fido2 to drop all FIDO2 slots and re-enroll survivors - Config: delete the key’s
sshKeygripsentry, itsu2fMappings.<user>credential, and itsageIdentityStubsblock if it was the manual-edit identity. Ensure at least one surviving key remains in each list (theu2fMappingsassertion blocks a login lockout). - Rotate exposed secrets: anything the lost key could decrypt is potentially compromised. Regenerate user/boot passwords and any service credentials, re-encrypt, and
just switchevery host. - Confirm the revoked key is rejected (SSH, sudo, and, after a reboot, LUKS unlock).