A template for keeping age-encrypted secrets in their own repository. You list public keys and files in Nix, edit the files with ragenix, and point your system configuration at the repository. It is built with purr.
Use the GitHub template and keep the repository private:
gh repo create my-secrets --template nixcafe-develop/nix-secrets --private --cloneOr copy the files with Nix and initialize git before running nix develop or nix run .#update-secrets:
nix flake init -t "github:nixcafe/develop-templates#secrets"
git init && git add -ATo use a shorter registry name:
nix registry add beans "github:nixcafe/develop-templates"
nix flake init -t beans#secrets
git init && git add -AIf you use cattery-modules, the beans registry entry already exists.
Then enter the development shell with nix develop, or direnv allow if you use direnv.
The shell provides ragenix, age-plugin-yubikey, vim, neovim and nano, and installs the git hooks. If VS Code is on your PATH and you are not connected over SSH, it sets EDITOR to code --wait.
Replace the placeholder keys in develop/lib/keys/default.nix and develop/lib/settings/default.nix with your own SSH or age public keys before running ragenix or nix run .#update-secrets.
| Attribute | Content |
|---|---|
recoveryKeys |
Public keys added to every global secret. Keep the private keys offline. |
users.<user>.publicKeys |
Public keys of a user. |
hosts.<host>.hostNames |
Names written to known_hosts. |
hosts.<host>.publicKeys |
Public keys of a host, usually /etc/ssh/ssh_host_ed25519_key.pub. |
hosts.<host>.endpoint |
hostName, port, user and optionally identityFile for the SSH config. |
machines.<machine>.host |
The entry in hosts that this machine uses. |
machines.<machine>.users |
The users on this machine. |
machines.<machine>.sharedSecretRecipient |
Optional boolean (default true). Set to false to exclude the machine from shared/global secrets while keeping it available for SSH configs. |
Do not use the same public key twice. ragenix rejects a file with duplicate recipients.
keys.computed.<machine> holds hostKeys, userKeys, keys, endpoint and sharedSecretRecipient for each machine. keys.computed.allKeys is the recovery keys plus the keys of every machine with sharedSecretRecipient = true.
List every encrypted file under secrets in develop/lib/settings/default.nix:
secrets = {
hosts.hostname = {
global.wireguard = [ "example.conf.age" ];
users.alice.wireguard = [ "example.conf.age" ];
};
shared = {
global.wireguard = [ "example.conf.age" ];
users.alice.ssh = [ "known_hosts.age" ];
};
};The position in this set decides the path and who can decrypt the file:
| Path | Recipients |
|---|---|
hosts/<machine>/global/<program>/<file> |
Host keys of the machine, keys of its users, recovery keys |
hosts/<machine>/users/<user>/<program>/<file> |
Keys of that user |
shared/global/<program>/<file> |
Keys of all machines with sharedSecretRecipient = true and their users, recovery keys |
shared/users/<user>/<program>/<file> |
Keys of that user |
Recovery keys cannot decrypt user files.
<machine> must be a name from machines and <user> a name from users. The root secrets.nix reads this list, so you never edit it by hand.
The template ships plain text placeholder .age files rather than encrypted blobs. Once you swap in your own keys, delete the placeholders before creating real encrypted files with ragenix, and run nix run .#update-secrets to replace the generated files with encrypted output:
find hosts shared -name '*.age' -deleteCreate or edit a file from the repository root. Make the directory first if it is new.
mkdir -p shared/global/wireguard
ragenix -e shared/global/wireguard/example.conf.agePass -i <private key> if your key is not in the default SSH location. After you change keys, encrypt everything again with identities that can open every file:
ragenix -i ~/.ssh/id_ed25519 --rekeyFor a YubiKey, put its age1yubikey1... recipient in publicKeys and pass the identity file printed by age-plugin-yubikey --identity to -i.
Two secrets are built from the keys instead of typed by hand:
| Package | Output file |
|---|---|
known-hosts |
shared/users/alice/ssh/known_hosts.age |
match-blocks |
shared/users/alice/ssh/block_config.age |
Their input is set under ssh in lib/settings:
| Attribute | Content |
|---|---|
fromMachines |
Machines to include. |
format |
"compact" puts all names of a host on one known_hosts line. "expanded" writes one line per name. |
knownHostsExtra |
Extra known_hosts entries with hostNames and publicKeys. |
matchBlocksExtra |
Extra SSH config blocks with an endpoint. |
After replacing the placeholder keys with real public keys, inspect a result before installing it:
nix build .#known-hosts && cat result
nix build '.#known-hosts^encrypted'Replace the generated placeholder files with age-encrypted output, then commit them:
nix run .#update-secretsRun it inside the git repository. Target directories are created automatically.
The plaintext of a generated secret is stored in the Nix store, which every local user can read. Only generate content that can be exposed there. Generated files are encrypted during the build without age plugins, so their recipients cannot be YubiKeys.
-
Create
develop/packages/motd/default.nix:{ lib, pkgs, ... }: lib.builders.mkGeneratedAgeEncrypted { inherit pkgs; name = "motd"; package = "motd"; text = "welcome\n"; }
-
Add
motd.agetosecrets.shared.users.alice.motdinlib/settings. -
Add the mapping to
generatedAgeFiles:{ package = "motd"; output = "encrypted"; user = "alice"; program = "motd"; file = "motd.age"; }
-
Run
git add developandnix run .#update-secrets.
buildMachines takes nix.buildMachines entries. Give each one a publicKey with the full host key line, or null. lib.builders.mkBuildMachines pkgs turns it into the base64 publicHostKey that Nix expects.
hosts is derived from the machine endpoints in the shape of networking.hosts. The keys are the endpoint.hostName values, so use it for that option only when they are IP addresses.
Both nix-config and colmena-config have a my-secrets input. Point it at your repository in their flake.nix:
my-secrets = {
url = "git+ssh://git@github.com/<you>/my-secrets.git";
flake = false;
};Turn secrets on in their lib/secrets/default.nix:
{ inputs, ... }:
{
enable = true;
secretsPath = "${inputs.my-secrets}";
}Then request files in a module. The attribute name is the path below global/ or users/<user>/ without .age:
{
# hosts/<current host>/global/wireguard/example.conf.age
cattery.secrets.hosts.global.files."wireguard/example.conf" = { };
# shared/global/wireguard/example.conf.age
cattery.secrets.shared.global.files."wireguard/example.conf" = { };
}User files go through users.<user>, for example cattery.secrets.shared.users.alice.files."ssh/known_hosts". Declare them in Home Manager modules, where the user key is available for decryption. The decrypted location is the path attribute of each entry.
The system directory name in your configuration must match the machine name here, because it selects the folder under hosts/.
After you push changes to the secrets repository, run nix flake update my-secrets in the configuration repository.
Remove flake = false to use the library output as well. secretsPath keeps working.
{ inputs, pkgs, ... }:
{
users.users.alice.openssh.authorizedKeys.keys = inputs.my-secrets.lib.keys.users.alice.publicKeys;
nix.buildMachines = inputs.my-secrets.lib.builders.mkBuildMachines pkgs;
}| Output | Content |
|---|---|
lib.keys |
Public keys, hosts, machines and computed |
lib.settings |
secrets, ssh, generatedAgeFiles, buildMachines, hosts |
lib.secrets |
Functions that map declared files to paths and recipients |
lib.builders |
mkAgeEncrypted, mkGeneratedAgeEncrypted, mkBuildMachines, generatedAgeFiles |
packages |
known-hosts, match-blocks |
apps |
update-secrets |
checks |
git-hooks |
devShells |
default |
formatter |
nixfmt-tree |
flake.nix inputs, purr call, and formatter
secrets.nix rules file read by ragenix
statix.toml statix linter configuration
develop/
lib/ keys, settings, secrets, builders
packages/ known-hosts, match-blocks
apps/ update-secrets
checks/ git hooks
shells/ development shell
hosts/ secrets per machine
shared/ secrets shared by machines
nix fmt
nix flake checkThe git hooks run nixfmt, deadnix and statix on each commit. Nix only sees files that git tracks, so git add new files before you build.