Skip to content

Zero-Drift Secrets & Shared pkgs

Two practical optimizations pay off as soon as your fleet grows past a couple of NixOS machines:

  1. Typed secrets: instead of string secret names scattered across sops.secrets or age.secrets, declare each secret as typed data (yen.secret.<name>) and enforce an invariant that no secret is orphaned.
  2. Shared pkgs: evaluating nixpkgs separately for five identical x86_64-linux servers multiplies evaluation RAM and time. Elect one leader per architecture automatically and pass its pkgs to followers (nixpkgs.pkgs = nixosConfigurations.${leader}.pkgs).
modules/secrets-and-pkgs.nix
{
yen,
std,
types,
...
}:
let
b = builtins;
allHosts = {
instances = yen.kinds.host;
};
in
{
imports = [ std.modules.all ];
# 1. Declare secrets as typed fleet data (never raw plaintext).
yen.kinds.secret.fields = {
owner = types.maybe yen.kinds.host;
sopsFile = types.str;
};
yen.kinds.host.fields.needSecrets = types.opt (types.listOf yen.kinds.secret) [ ];
yen.secret.db-pass = {
owner = yen.host.app1;
sopsFile = "secrets/prod.yaml";
};
# Two identical x86_64-linux hosts opt into `sharedPkgs = true`.
yen.host.app1 = {
system = yen.system.x86_64-linux;
sharedPkgs = true;
needSecrets = [ yen.secret.db-pass ];
};
yen.host.app2 = {
system = yen.system.x86_64-linux;
sharedPkgs = true;
};
# Emit real `sops.secrets` entries from typed secret refs.
yen.aspects.wire-sops = {
at = yen.kinds.host;
nixos =
{ host, ... }:
{
sops.secrets = b.listToAttrs (
map (s: {
inherit (s) name;
value.sopsFile = s.sopsFile;
}) host.needSecrets
);
};
};
# Whole-fleet rule: every declared secret must have an owner host.
yen.relations.secretHasOwner =
{ S, H }:
{
head = yen.relations.secretHasOwner S;
pos = [ (yen.facts.secret.owner S H) ];
};
yen.invariants.secret-has-owner =
{ S }:
{
bad = S;
pos = [ (yen.facts.kind yen.kinds.secret S) ];
neg = [ (yen.relations.secretHasOwner S) ];
};
# 2. Elect one `pkgs` leader per architecture (`app1`) so followers (`app2`) reuse its `pkgs`.
yen.aspects.shared-pkgs-plan = {
at = yen.kinds.host.where { sharedPkgs = true; };
cycle =
{ host, tree, ... }:
(
peers:
(leader: {
tree = [ allHosts ];
out.pkgsPlan.${host.name} =
if yen.lib.same host leader then
{ role = "leader"; }
else
{
role = "follower";
inheritFrom = leader.name;
};
})
(b.head peers)
)
(b.filter (h: h.sharedPkgs && yen.lib.same h.system host.system) (tree.select allHosts));
};
}

Inspect the generated sops.secrets wiring and the automatic pkgs leader/follower plan:

nix eval .#nixos.app1.sops + .#pkgsPlan
{
app1Sops = {
secrets = {
db-pass = {
sopsFile = "secrets/prod.yaml";
};
};
};
pkgsPlan = {
app1 = {
role = "leader";
};
app2 = {
inheritFrom = "app1";
role = "follower";
};
};
}

If someone leaves an unowned secret behind (yen.secret.forgotten-key.sopsFile = "secrets/old.yaml";), secret-has-owner catches it immediately:

nix eval --json .#yen.check.violations (unowned secret)
[
{
at = "inst:secret:forgotten-key";
invariant = "secret-has-owner";
}
]

Next, adopt Yen alongside your existing NixOS flake without a rewrite in Adopting Yen Incrementally.

ContributeCommunitySponsor