Derived Topology & Multi-Hop Rules
yen.relations lets you express multi-hop graph queries — like SSH bastion reachability or team access rules — declaratively in pure Nix using Yen’s Tupu-inspired Datalog solver.
Keeping relations in their own semantic file (modules/access.nix or modules/routing.nix) separates topology logic cleanly from NixOS options, terminates deterministically even on cyclic graphs, and plugs straight into at = yen.relations.<name>.
Multi-hop SSH bastion reachability
Section titled “Multi-hop SSH bastion reachability”Here laptop jumps through bastion to reach vault, while isolated has no route at all:
{ yen, std, types, ...}:{ imports = [ std.modules.all ];
yen.kinds.host.fields.jumpVia = types.maybe yen.kinds.host;
yen.host.vault.addr = "198.51.100.30";
yen.host.bastion = { addr = "198.51.100.20"; jumpVia = yen.host.vault; };
yen.host.laptop = { addr = "198.51.100.10"; jumpVia = yen.host.bastion; };
yen.host.isolated.addr = "198.51.100.40";}Two short rules derive reaches (transitive closure over jumpVia) and canReachVault.
Both bastion (one hop) and laptop (two hops) receive the Vault SSH configuration automatically, while isolated gets nothing.
{ yen, at, ...}:{ # Derive multi-hop reachability in pure Nix — terminates cleanly even with cycles. yen.relations.reaches = [ ( { X, Y }: { head = yen.relations.reaches X Y; pos = [ (yen.facts.host.jumpVia X Y) ]; } ) ( { X, Y, Z, }: { head = yen.relations.reaches X Z; pos = [ (yen.relations.reaches X Y) (yen.facts.host.jumpVia Y Z) ]; } ) ];
# Any host with a multi-hop route to `vault` gets a Vault SSH matchBlock. yen.relations.canReachVault = { S }: { head = yen.relations.canReachVault S; pos = [ (yen.relations.reaches S yen.host.vault) ]; };
yen.aspects.vault-ssh = { at = yen.relations.canReachVault; nixos.programs.ssh.extraConfig = "Host vault\n ForwardAgent no"; };
# Inline rule: every host with a direct `jumpVia` gets ProxyJump wired to its hop's IP. yen.aspects.proxy-jump = { at = at.rule ( { S, Y }: { pos = [ (yen.facts.host.jumpVia S Y) ]; } ); nixos = { host, ... }: { environment.variables.SSH_JUMP_HOST = host.jumpVia.addr; }; };}Inspect the generated NixOS config:
{ bastion = { environment = { variables = { SSH_JUMP_HOST = "198.51.100.30"; }; }; programs = { ssh = { extraConfig = "Host vault\n ForwardAgent no"; }; }; }; laptop = { environment = { variables = { SSH_JUMP_HOST = "198.51.100.20"; }; }; programs = { ssh = { extraConfig = "Host vault\n ForwardAgent no"; }; }; };}Counting and summing across your fleet (datalog.count, sum, min, max)
Section titled “Counting and summing across your fleet (datalog.count, sum, min, max)”Need to verify minimum cluster sizes or total RAM across a group?
yen.lib.datalog ships built-in aggregates (count, sum, min, max) that run inside the same pure-Nix solver:
aggs = [ (datalog.count { out = { var = "N"; }; pos = [ [ "in_cluster" { var = "H"; } { var = "C"; } ] ]; })];[ [ "cluster_size" "eu" 2 ] [ "cluster_size" "us" 1 ]]Next, answer “what breaks if I touch this host?” and plan parallel rollout waves in Blast Radius, Explain & Rollout Waves.