Skip to content
LogoLogo

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

secrets repositoryscope directorycattery.secrets.*.filesage.secretsdecrypted file/etc entryservice or program
  1. The secrets repository holds one directory per scope, described below.
  2. A module or your host config requests a file under cattery.secrets.
  3. cattery adds an age.secrets entry with the encrypted file and its owner and mode.
  4. agenix decrypts it at activation time. The path of the request points at the result.
  5. 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}";
  };
}
OptionDefaultDescription
enablefalseTurns on agenix secrets and installs the agenix command
secretsPath"${cattery.user.home}/agenix"Directory with the .age files
secretsDirnullSets age.secretsDir. null keeps the agenix default
secretsMountPointnullSets age.secretsMountPoint. null keeps the agenix default
filesread-onlyEvery 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
ScopeDirectoryRequested under
hosts-globalhosts/<host>/globalcattery.secrets.hosts.global.files
hosts-userhosts/<host>/users/<user>cattery.secrets.hosts.users.<user>.files
shared-globalshared/globalcattery.secrets.shared.global.files
shared-usershared/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:

OptionDefaultDescription
mode"0400"File mode of the decrypted file
ownerthe user, or root for globalOwner of the decrypted file. System only
groupthe owner's groupGroup of the decrypted file. System only
symlinktrueWhether agenix symlinks the decrypted file into place
filederivedThe encrypted file under secretsPath
secretNamederivedThe age.secrets key, for example hosts/<host>/global/wireguard/wg0.conf
pathread-onlyWhere 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:

OptionDescription
enableWhether to request the files. Defaults to the service's enable and cattery.secrets.enable
scopehosts-global, hosts-user, shared-global, or shared-user
configNamesExtra 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, modeDefaults for every file of the app. System only for owner and group
etc.enableWhether to link the files into /etc. Defaults to true
etc.dirPathDirectory below /etc, for example nginx/conf.d
etc.useSymlinktrue 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;
  };
}
BuilderForDefault scopeOwner options and /etc links
mkAppSecretsOptionNixOS and nix-darwin serviceshosts-globalYes, owner defaults to root
mkHomeAppSecretsOptionHome Manager programshosts-userNo

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 --clone

The 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.