Secrets
cattery.secrets is a layer over agenix. You keep
age-encrypted files in a directory with a fixed layout, request them by name, and cattery
turns each request into an age.secrets entry. Service modules that need config files
from secrets request them for you and link the decrypted files into /etc.
The same options exist on NixOS and nix-darwin (modules/shared/secrets) and in Home
Manager (modules/home/secrets). agenix's NixOS, nix-darwin, and Home Manager modules
are part of the cattery default bundles.
How a secret flows
- The secrets repository holds one directory per scope, described below.
- A module or your host config requests a file under
cattery.secrets. - cattery adds an
age.secretsentry with the encrypted file and its owner and mode. - agenix decrypts it at activation time. The
pathof the request points at the result. - Service modules optionally link the decrypted file into
/etc.
Enable secrets
{ inputs, ... }:
{
cattery.secrets = {
enable = true;
# Directory with the encrypted files. Defaults to "<cattery.user.home>/agenix".
secretsPath = "${inputs.my-secrets}";
};
}| Option | Default | Description |
|---|---|---|
enable | false | Turns on agenix secrets and installs the agenix command |
secretsPath | "${cattery.user.home}/agenix" | Directory with the .age files |
secretsDir | null | Sets age.secretsDir. null keeps the agenix default |
secretsMountPoint | null | Sets age.secretsMountPoint. null keeps the agenix default |
files | read-only | Every requested secret, keyed by its age.secrets name |
agenix decrypts with the host's SSH host keys on NixOS and nix-darwin, and with the
user's SSH keys in Home Manager. With impermanence, cattery points
age.identityPaths at the copies on the persistent volume.
Scopes and layout
Encrypted files are sorted by who may read them and which host they belong to:
<secretsPath>/
├── hosts/<host>/global/<program>/<file>.age # hosts-global
├── hosts/<host>/users/<user>/<program>/<file>.age # hosts-user
├── shared/global/<program>/<file>.age # shared-global
└── shared/users/<user>/<program>/<file>.age # shared-user| Scope | Directory | Requested under |
|---|---|---|
hosts-global | hosts/<host>/global | cattery.secrets.hosts.global.files |
hosts-user | hosts/<host>/users/<user> | cattery.secrets.hosts.users.<user>.files |
shared-global | shared/global | cattery.secrets.shared.global.files |
shared-user | shared/users/<user> | cattery.secrets.shared.users.<user>.files |
<host> is purr.meta.host, the directory name of the host in a purr flake. Outside purr
pass it in specialArgs.purr.meta.host. Without it, cattery uses localhost.
Request a file
The attribute name is the path below the scope directory, without .age:
{ config, ... }:
{
# hosts/<host>/global/wireguard/wg0.conf.age
cattery.secrets.hosts.global.files."wireguard/wg0.conf" = { };
# shared/users/alice/ssh/known_hosts.age, owned by alice
cattery.secrets.shared.users.alice.files."ssh/known_hosts" = {
mode = "0444";
};
services.foo.configFile =
config.cattery.secrets.hosts.global.files."wireguard/wg0.conf".path;
}Each request accepts these options:
| Option | Default | Description |
|---|---|---|
mode | "0400" | File mode of the decrypted file |
owner | the user, or root for global | Owner of the decrypted file. System only |
group | the owner's group | Group of the decrypted file. System only |
symlink | true | Whether agenix symlinks the decrypted file into place |
file | derived | The encrypted file under secretsPath |
secretName | derived | The age.secrets key, for example hosts/<host>/global/wireguard/wg0.conf |
path | read-only | Where the decrypted file ends up |
In Home Manager, request user files under users.<user>, where the user's key can
decrypt them. Home Manager requests have no owner or group.
Secrets of service modules
Services and programs that read config files from secrets have a secrets option next to
their other options, for example cattery.services.nginx.secrets. It is enabled when both
the service and cattery.secrets are enabled:
| Option | Description |
|---|---|
enable | Whether to request the files. Defaults to the service's enable and cattery.secrets.enable |
scope | hosts-global, hosts-user, shared-global, or shared-user |
configNames | Extra file names to request from the app directory |
files.<name> | One entry per file, with fileName, source, mode, and the read-only target. System modules add owner and group |
owner, group, mode | Defaults for every file of the app. System only for owner and group |
etc.enable | Whether to link the files into /etc. Defaults to true |
etc.dirPath | Directory below /etc, for example nginx/conf.d |
etc.useSymlink | true links each file to the decrypted path. false copies it with the per-file mode, owner (as user), and group |
Copies matter for programs that refuse symlinks or check file ownership. With
etc.useSymlink = false, each /etc entry is written with the same owner and mode as the
decrypted file.
Example: WireGuard
cattery.services.wg-quick requests one hosts-global file per interface from the
wireguard directory and links it into /etc/wireguard:
{
cattery.secrets.enable = true;
# Decrypts hosts/<host>/global/wireguard/wg0.conf.age
# and links it to /etc/wireguard/wg0.conf.
cattery.services.wg-quick.configNames = [ "wg0" ];
}Other modules follow the same pattern: nginx links into /etc/nginx/conf.d, sing-box
reads /etc/sing-box/config.json, cattery.nix.secrets adds an !include of an
encrypted nix.conf fragment, and cli-apps.ssh in Home Manager reads ssh files from
the shared-user scope.
Write your own secret-backed module
lib.cattery.secrets has the option builders the cattery modules use. Inside a module
it is lib.${namespace}.secrets. Put the secrets option in its own secrets/default.nix
next to the module:
# modules/nixos/services/myapp/secrets/default.nix
{ config, lib, namespace, purr ? { }, ... }:
let
inherit (lib.${namespace}.secrets) mkAppSecretsOption;
inherit (config.${namespace}.secrets) files;
cfgParent = config.${namespace}.services.myapp;
cfg = cfgParent.secrets;
in
{
options.${namespace}.services.myapp.secrets = mkAppSecretsOption {
enable = cfgParent.enable && config.${namespace}.secrets.enable;
appName = "myapp";
dirPath = "myapp";
fixedConfig = [
{
name = "token";
fileName = "token.env";
}
];
scope = "hosts-global";
currentInfo = {
host = purr.meta.host or "localhost";
user = config.${namespace}.user.name;
};
buildTargetPath = name: files.${name}.path;
owner = "myapp";
};
config = lib.mkIf cfg.enable {
${namespace}.secrets = cfg.secretMappingFiles;
environment.etc = lib.mkIf cfg.etc.enable cfg.etc.files;
};
}| Builder | For | Default scope | Owner options and /etc links |
|---|---|---|---|
mkAppSecretsOption | NixOS and nix-darwin services | hosts-global | Yes, owner defaults to root |
mkHomeAppSecretsOption | Home Manager programs | hosts-user | No |
Both take appName, dirPath (defaults to appName), configNames, fixedConfig (a
list of { name, fileName ? name, description ? ... }), scope, currentInfo with
host and user, buildTargetPath, and mode (defaults to "0400"). The returned
option has a read-only secretMappingFiles, which is assigned to cattery.secrets and
becomes the age.secrets entries.
lib.cattery.secrets also exports scopeList, getSecretPrefix, and the lower-level
builders mkMappingOption, mkHomeMappingOption, secretFileOptions,
secretSetOptions, and toAgeSecrets.
A secrets repository
The secrets template creates a
repository with exactly this layout. You list recipients and files in Nix, edit files
with ragenix, and generate known_hosts and SSH config files with
nix run .#update-secrets:
gh repo create my-secrets --template nixcafe-develop/nix-secrets --private --cloneThe nix-config and colmena-config templates already have a non-flake my-secrets
input. Point it at your repository and turn secrets on in lib/secrets/default.nix:
# flake.nix
my-secrets = {
url = "git+ssh://[email protected]/alice/my-secrets.git";
flake = false;
};# lib/secrets/default.nix
{ inputs, ... }:
{
enable = true;
secretsPath = "${inputs.my-secrets}";
}The host directory names in your configuration must match the machine names in the
secrets repository, because they select the hosts/<host> directory. After you push new
secrets, run nix flake update my-secrets in the configuration repository.