agenix or sops-nix are often the recommended way for managing secrets in your configuration. However I feel there aren’t many explanations of the actual tools at use.
Instead most blogs go something like this:
- Generate a key with
ssh-to-age - Create a
.sops.yamland fill it with this setup - Use
sops <filename>.yamlto edit secrets - Add the
sops-nixflake to your configuration and import the module - Define some
sops.<x>configuration variables and you’re good to go :)
This will probably work fine for most people until they move machines or have to use multiple devices to work on the configuration. At that point they’ll probably want to understand the whole chain and this is where this series comes in. My goal is to introduce all of the building blocks of the standard NixOS secrets management starting with age!
Age
Taken straight from their GitHub page:
age is a simple, modern and secure file encryption tool, format, and Go library.
The age tool is at it’s core a simple CLI for encrypting and decrypting files. Let’s try that!
First I’m going to need a key pair (more on that later).
age-keygen > key.ageThis file contains the following:
┬───┬──────────────────────────────────────────────────────────────────────────────┬
│ │ File: key.age │
┼───┼──────────────────────────────────────────────────────────────────────────────┼
│ 1 │ # created: 2026-11-06T19:53:48+01:00 │
│ 2 │ # public key: age1x89ygg0jyhkfye48uw204el4fe2mfpej4x2ntgdl0stw3wglvvfq5cg0pq │
│ 3 │ AGE-SECRET-KEY-1F786VF28F6KPY7MGVMZP2XHYM468EA7XZ2S8ZFPZZAMZP3XQQ5DSPAQJUC │
┴───┴──────────────────────────────────────────────────────────────────────────────┴You can see that it features a public key and a secret key (usually referred to as a private key). The public key can be known by anybody however the private key should never be shared. age public keys start with age1 or age1pq1 and the private keys start with AGE-SECRET-KEY-1 or AGE-SECRET-PQ-KEY-1 (the pq in the key names indicates the keys are used with post-quantum cryptography).
Encrypting a file
Now if your friend wants to send you an encrypted file he can use your public key to encrypt a file of his choosing. Let’s say he wants to send you a super secret message 1337.
echo "1337" > message.txtNow he will use your public key to encrypt the file. He doesn’t and shouldn’t know your private key at all.
age --encrypt\
--recipient="age1x89ygg0jyhkfye48uw204el4fe2mfpej4x2ntgdl0stw3wglvvfq5cg0pq"\
--output=message.txt.age\
message.txtHe will get a file like this:
┬───┬────────────────────────────────────────────────────────┬
│ │ File: message.txt.age │
┼───┼────────────────────────────────────────────────────────┼
│ 1 │ age-encryption.org/v1 │
│ 2 │ -> X25519 KHXsVrjM4vbRoiV/6cHXhJYMuQLOKvbyMuxRwE+vcVk │
│ 3 │ 3BGoaPQ36GzlCdmIxx06YsHnaXslBYuRK7Bx76qNBCs │
│ 4 │ --- sdPssmd0Jz51U3dMUc89muomcC+5SgpTeB6cq31xdVA │
│ 5 │ Sv���)��^L_y���u�a<$+�^SM�p^T?����� │
┴───┴────────────────────────────────────────────────────────┴The age project also provides a nice age-inspect tool to get some information about the encrypted data.
Now let’s say our friend sends us this encrypted file. We can now decrypt it with a simmilar command our friend used to encrypt it except now we have to specify our private key (stored in key.age) that corresponds to the public key we’ve given our friend. We’re going to decrypt it into a new file called decrypted.txt.
age --decrypt\
--identity=key.age\
--output=decrypted.txt\
message.txt.ageAnd looking at the file we’ve recieved 1337!
Identities and recipients
Now if our friend wants to send a secret message only to us we can simply use our private key to decrypt it. However what if he wants to send a secret message to our group chat?
Well we could share a private key among our group but now we have to distribute the private key somehow and what if one friend leaves our group? We would have to change redistribute a new private key.
age solves this by allowing to use multiple public keys while encrypting the file. Each public key is called a recipient and we can provide a list of recipients by giving age a file with each recipient (the public key) on a separate line.
Let’s say our friend wants to send us and 2 other friends a message. He will create a file with all of our public keys:
┬───┬─────────────────────────────────────────────────────────────────┬
│ │ File: recipients.txt │
┼───┼─────────────────────────────────────────────────────────────────┼
│ 1 │ age1x89ygg0jyhkfye48uw204el4fe2mfpej4x2ntgdl0stw3wglvvfq5cg0pq │
│ 2 │ age1hz3pxuauma7ytr2k46zaakyrhe7jwlrkvx6llkme3yp9z0qvzp0sf7uwrg │
│ 3 │ age1cenmnlp8grded2jd5gm6k48p85rygm0ry4dea9ytz20ek7fdc58s35g244 │
┴───┴─────────────────────────────────────────────────────────────────┴Then he encrypts the secret message with the --recipients-file flag:
age --encrypt\
--recipients-file=recipients.txt\
--output=message.txt.age\
message.txtNow any one of us can decrypt the file with our own private key! This private key is also called an identity and one person can have multiple identites (or in this case private keys). We can even provide age with a concatenated list of our identities (private keys) and it will use the first identity that can decrypt the file.
Keys
At the start I generated a key with age-keygen. It should be noted that age keys don’t have any passwords as opposed to something like SSH keys. However SSH keys are supported even though the manual suggests to use them only when a native age key is not available. The recipients (public keys) should be provided in the same format the authorized_keys file uses.
Wrapping up…
And that’s pretty much it! There are certainly parts of age I didn’t cover such as post-quantum encryption support or plugins for supporting different encryption workflows such as using hardware keys like YubiKey. Go to their GitHub page or RTFM (man age, man age-keygen, age-inspect)!
Next up is integrating age into NixOS via the agenix module! We’ll look into where and how NixOS stores secrets and what agenix does in your configuration. In subsequent posts we’ll then explore sops and finally try out sops-nix!