Skip to content

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.

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.

modules/rules.nix
{
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:

modules/hosts.nix
{ 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;
};
}
Terminal window
nix eval .#yen.ok
nix eval --json .#yen.check.violations

Now suppose someone enables debug = true on api in production and gives worker the same IP address:

modules/_bad.nix
{ 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:

nix eval --json .#yen.check.violations
{
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.why and datalog.proof show the exact chain of facts that proved a violation.
  • datalog.whyNot explains why a fact was not derived (for example, web-2 was skipped because drained("web-2") holds).
  • datalog.verifyCertificate re-checks a derivation certificate independently in pure Nix.
datalog.whyNot + verifyCertificate
{
certificateValid = true;
whyNotServesWeb2 = [
{
reason = "no rule has this head";
rule = null;
}
];
}

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:

Terminal window
# 1. Source fleet CI exports the verdict certificate
nix eval --json .#yen.check.lean > verdict.json
# 2. Deploy target or auditor checks it with the standalone Lean 4 binary
nix run github:vic/yen?dir=lean -- verdict.json

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

ContributeCommunitySponsor