Skip to content

Safe AI & CI Edits (agent/)

Letting an AI coding agent or CI bot edit your NixOS fleet is risky if a small web tweak accidentally opens a database port or weakens one of your fleet invariants.

The zero-dependency agent/ sub-flake (github:vic/yen?dir=agent) dry-runs any proposed change against your base fleet in pure Nix and checks whether it stays inside an explicit permission boundary.

1. Define a permission grant with real refs

Section titled “1. Define a permission grant with real refs”

Notice webGrant uses checked references (yen.host.web and the machine firewall sink). By default allowLaws = false, so an agent can never silently weaken or delete a yen.invariants rule either.

modules/base.nix
{
yen,
std,
agent,
...
}:
let
b = builtins;
in
{
imports = [ std.modules.all ];
yen.host.web.addr = "198.51.100.10";
yen.host.db.addr = "198.51.100.20";
yen.aspects.web-https = {
at = yen.host.web;
firewall.tcp = [ 443 ];
};
yen.aspects.db-pg = {
at = yen.host.db;
firewall.tcp = [ 5432 ];
};
# Expose a ref-typed grant allowing edits ONLY on `web`'s `firewall` sink (zero law edits).
yen.aspects.policy = {
at = yen.root;
out.webGrant = agent.grants.grant {
scopes = [ yen.host.web ];
sinks = [ yen.kinds.machine.sinks.firewall ];
};
};
yen.kinds.host.sinks.firewall.into.out =
{ host, firewall, ... }:
{
ports.${host.name} = b.concatMap (r: r.tcp or [ ]) firewall;
};
}

2. Stage an allowed edit vs a forbidden edit

Section titled “2. Stage an allowed edit vs a forbidden edit”

Adding port 80 on web stays inside webGrant and passes:

changes/ok.nix
{ yen, ... }:
{
# Allowed: touches only `web`'s `firewall` sink.
yen.aspects.web-http = {
at = yen.host.web;
firewall.tcp = [ 80 ];
};
}

Opening port 9000 on db touches a host outside webGrant and is blocked with grant-denied:

changes/denied.nix
{ yen, ... }:
{
# Refused: tries to open a port on `db`, which is outside `webGrant`.
yen.aspects.open-db = {
at = yen.host.db;
firewall.tcp = [ 9000 ];
};
}

Stage any proposed change in CI or from an MCP server with agent.stage.stage:

verdict = agent.stage.stage {
base = [ ./modules ];
change = [ ./changes/proposed.nix ];
grant = webGrant;
};
agent.stage.stage verdicts
{
deniedChange = {
ok = false;
reasons = [ "grant-denied" ];
};
okChange = {
ok = true;
reasons = [ ];
webPorts = [ 80 443 ];
};
patchProgramWebPorts = [ 8443 443 ];
}
Module What it protects
agent.stage.stage Dry-runs change on top of base, returning { ok; reasons; diff; newViolations; lean; } with zero disk writes
agent.grants.grant / narrow Restricts edits to specific hosts/sinks; narrow g1 g2 only shrinks permissions for sub-agents
agent.floor.check Trust floor: blocks any change that touches a host with trusted = true or lowers trust
agent.program Builds validated patch modules (add, drop, setField, use) directly in Nix without writing temp files
agent.without Counterfactual check: ‘what breaks if we remove this host or aspect?’

Next, see how Yen reports structured errors and compares side-by-side with NixOS modules in Errors as Data & Yen vs NixOS Modules.

ContributeCommunitySponsor