Skip to content

Idiomatic Yen Style

A few small habits make a huge difference once a fleet grows past five hosts:

  1. Refs over strings: declare empty enum kinds (yen.kinds.env = {}; yen.env.prod = {};) instead of string tags, and compare references with yen.lib.same.
  2. 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.
  3. Lift tree.select queries to the top let: defining allHosts = { 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.
  4. One let per file, zero with or rec: qualify b = builtins; at the top and avoid with pkgs; so every identifier is obvious to read and refactor.
modules/idiomatic.nix
{
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)
);
};
};
}
nix eval .#nixos + .#peers
{
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.

ContributeCommunitySponsor