This chapter focuses on practical consumption patterns: service args, app config, activation scripts, and module wiring.
Declare secret, then consume .path.
sops.secrets.api_key = {};
programs.someapp.apiKeyFile = config.sops.secrets.api_key.path;Do not use builtins.readFile on secret plaintext during evaluation.
Many modules expect ...File options:
services.example.tokenFile = config.sops.secrets.token.path;This is ideal when supported.
Sometimes app config supports cmd: indirection.
Example from this repo (modules/editor.nix):
api_key = "cmd:cat ${config.sops.secrets.anthropic_api_key.path}"This keeps the secret off static config literals and uses runtime file reads.
In activation scripts, reference the secret path at runtime.
Example pattern (generic — no activation-script consumer in this repo currently):
KEY_PATH="${config.sops.secrets."my-binary-key".path}"
if [ ! -r "$KEY_PATH" ]; then
echo "secret missing" >&2
exit 1
fiThis is useful for signing, materializing policy files, or generating artifacts that must not be done at evaluation time.
sops.secrets.private_key = {
mode = "0400";
owner = "appuser";
group = "appgroup";
};Use this when service user is not root/default.
System module and Home Manager module run in different activation contexts. Keep consumer and declaration in matching context unless you intentionally bridge them.
- Declaration:
sops.secrets.anthropic_api_key = {}; - Consumption: command reads from
config.sops.secrets.anthropic_api_key.path
Before running nh ... switch, verify:
- secret is declared where used,
sopsFileresolves correctly (if overridden),- permission fields make sense for target runtime user,
- consumer references
.path, not plaintext values.