Idiomatic Yen Style
A few small habits make a huge difference once a fleet grows past five hosts:
- Refs over strings: declare empty enum kinds (
yen.kinds.env = {}; yen.env.prod = {};) instead of string tags, and compare references withyen.lib.same. - Split reusable features from personal hosts: put generic aspects, kits, and shared sinks in a reusable folder (or flake), and keep personal hostnames, users, and overrides in
./modules. - Lift
tree.selectqueries to the toplet: definingallHosts = { instances = yen.kinds.host; };once at the top lets Yen reuse the same query result across every host instead of re-running it per machine. - One
letper file, zerowithorrec: qualifyb = builtins;at the top and avoidwith pkgs;so every identifier is obvious to read and refactor.
{ yen, std, at, types, ...}:let b = builtins;
# Lift `tree.select` queries to top-level `let` so Yen memoizes them cleanly across hosts. allHosts = { instances = yen.kinds.host; };in{ imports = [ std.modules.all ];
# 1. Checked ref enum instead of string tags. yen.kinds.env = { }; yen.env = { prod = { }; dev = { }; };
yen.kinds.host.fields.env = types.opt yen.kinds.env yen.env.dev;
yen.host.web = { env = yen.env.prod; addr = "198.51.100.10"; }; yen.host.db = { env = yen.env.prod; addr = "198.51.100.20"; };
# 2. Expressive `at` selector using checked refs. yen.aspects.prod-ssh = { at = at.all [ yen.kinds.host (yen.kinds.host.where { env = yen.env.prod; }) ]; nixos.services.openssh.enable = true; };
# 3. Compare refs with `yen.lib.same`, never raw string equality. yen.aspects.peers = { at = yen.kinds.host; cycle = { host, tree, ... }: { tree = [ allHosts ]; out.peers.${host.name} = map (h: h.name) ( b.filter (h: !(yen.lib.same h host)) (tree.select allHosts) ); }; };}{ nixos = { db = { services = { openssh = { enable = true; }; }; }; web = { services = { openssh = { enable = true; }; }; }; }; peers = { db = [ "web" ]; web = [ "db" ]; };}For contributor rules and test commands for Yen itself, see Contributing.