Whole-Fleet Rules & Lean 4 Proofs
While per-host assertions verify each machine individually, yen.invariants.<name> checks rules across your entire fleet together in one pure-Nix Tupu-inspired Datalog solver.
If any rule triggers — such as two hosts sharing an IP address or debug mode enabled in production — yen.mkFlake holds back deployable outputs and points straight at the offending machines.
1. Write two whole-fleet rules
Section titled “1. Write two whole-fleet rules”Each rule names the variables it matches (H, A, B, IP), binds bad to the offending host, and lists the facts (pos) and conditions (when) that represent a mistake.
{ yen, std, types, ...}:{ imports = [ std.modules.all ];
yen.kinds.env = { }; yen.env.prod = { }; yen.env.dev = { };
yen.kinds.host.fields = { env = types.opt yen.kinds.env yen.env.dev; debug = types.opt types.bool false; };
yen.aspects.debug-port = { at = yen.kinds.host.where { debug = true; }; firewall.tcp = [ 9229 ]; };
# Rule 1: Production hosts may never enable debug mode. yen.invariants.no-debug-in-prod = { H }: { bad = H; pos = [ (yen.facts.host.env H yen.env.prod) (yen.facts.host.debug H true) ]; };
# Rule 2: No two hosts may share the same IP address. yen.invariants.unique-ip = { A, B, IP, }: { bad = A; pos = [ (yen.facts.host.addr A IP) (yen.facts.host.addr B IP) ]; when = [ (A.neq B) ]; };}With a clean fleet (modules/hosts.nix), nix eval .#yen.ok returns true and every host builds normally:
{ yen, ... }:{ yen.host.api = { env = yen.env.prod; addr = "198.51.100.10"; };
yen.host.sandbox = { env = yen.env.dev; addr = "198.51.100.20"; debug = true; };}nix eval .#yen.oknix eval --json .#yen.check.violations2. Watch Yen block a broken fleet
Section titled “2. Watch Yen block a broken fleet”Now suppose someone enables debug = true on api in production and gives worker the same IP address:
{ yen, ... }:{ # Mistake: debug enabled on a production host AND duplicate IP address. yen.host.api = { env = yen.env.prod; addr = "198.51.100.10"; debug = true; };
yen.host.worker = { env = yen.env.prod; addr = "198.51.100.10"; };}Yen catches both violations before nixos-rebuild or kubectl ever runs:
{ ok = false; violations = [ { at = "host:api"; invariant = "no-debug-in-prod"; } { at = "host:api"; invariant = "unique-ip"; } { at = "host:worker"; invariant = "unique-ip"; } ];}3. Debugging why a rule did (or did not) fire
Section titled “3. Debugging why a rule did (or did not) fire”When a complex rule surprises you, Yen’s Datalog module can explain both positive and negative derivations:
datalog.whyanddatalog.proofshow the exact chain of facts that proved a violation.datalog.whyNotexplains why a fact was not derived (for example,web-2was skipped becausedrained("web-2")holds).datalog.verifyCertificatere-checks a derivation certificate independently in pure Nix.
{ certificateValid = true; whyNotServesWeb2 = [ { reason = "no rule has this head"; rule = null; } ];}Independent Lean 4 Verification
Section titled “Independent Lean 4 Verification”Sometimes the machine deploying your fleet — or a security reviewer auditing a change — should not have to run Nix on your whole repository or trust Yen’s pure-Nix solver.
Every evaluated fleet exposes a lazy JSON certificate at .#yen.check.lean.
Source CI exports that small JSON file and ships it to the deploy host:
# 1. Source fleet CI exports the verdict certificatenix eval --json .#yen.check.lean > verdict.json
# 2. Deploy target or auditor checks it with the standalone Lean 4 binarynix run github:vic/yen?dir=lean -- verdict.jsonThe lean/ sub-flake is completely opt-in (zero Mathlib, zero sorry).
It independently re-checks every rule and fact from verdict.json and exits non-zero if any violation is true or undefined.
Pass --waves to print parallel deploy waves from the certificate, or --change host:api to see which hosts are affected by a change.
Next, derive multi-hop SSH routes and counts automatically in Derived Topology & Multi-Hop Rules.