Validation, Defaults & Redacted Secrets
Yen validates every host in your fleet in one pass, collects all type errors and spelling suggestions together, and hashes types.secret values before exporting inventory JSON.
{ yen, std, types, ...}:{ imports = [ std.modules.all ];
yen.kinds.host.fields = { httpsPort = types.opt types.port 443; fqdn = types.derive types.str (h: "${h.name}.example.com"); extraDomains = types.opt (types.listOf types.str) [ ]; cloudflareToken = types.secret types.str; };
yen.host.edge = { addr = "198.51.100.10"; extraDomains = [ "www.example.com" ]; cloudflareToken = "cf-live-api-token"; };
yen.aspects.caddy-proxy = { at = yen.kinds.host; nixos = { host, ... }: { services.caddy.enable = true; services.caddy.virtualHosts.${host.fqdn}.serverAliases = host.extraDomains; networking.firewall.allowedTCPPorts = [ host.httpsPort ]; }; };}Here httpsPort defaults to 443 and checks that any override stays in 1..65535.
fqdn derives "edge.example.com" automatically from host.name.
cloudflareToken is wrapped in types.secret, so nix eval --json .#yen.inventory replaces it with its SHA-256 digest.
{ nixos = { networking = { firewall = { allowedTCPPorts = [ 443 ]; }; }; services = { caddy = { enable = true; virtualHosts = { "edge.example.com" = { serverAliases = [ "www.example.com" ]; }; }; }; }; }; redactedToken = "sha256:5f5016f6fef750b0ef3db9a66df97c74ed935e7229c4393cf72d1c348643be9c";}Catching multiple mistakes in one pass
Section titled “Catching multiple mistakes in one pass”Suppose a second file adds broken with an out-of-range port and a typo (adrr instead of addr):
{ yen.host.bad = { addr = "192.0.2.99"; httpsPort = 99999; cloudflareToken = "tok"; adrr = "typo"; };}Instead of aborting on the first problem, nix eval --json .#yen.errors returns both errors together along with the spelling suggestion addr:
[ { code = "unknown-field"; error = "unknown field `adrr` on kind `host`"; near = [ "addr" "drop" "order" ]; } { code = "type-mismatch"; error = "expected an integer TCP/UDP port (1..65535)"; near = [ ]; } { code = "unknown-field"; error = "unknown field `adrr` on kind `host`"; near = [ "addr" "drop" "order" ]; } { code = "type-mismatch"; error = "expected an integer TCP/UDP port (1..65535)"; near = [ ]; }]Built-in types at a glance
Section titled “Built-in types at a glance”| Type | Purpose |
|---|---|
types.str, int, number, bool, port, path, attrs, any |
Primitives (port enforces 1..65535) |
types.opt T default / types.maybe T |
Optional field with a fallback value, or nullable defaulting to null |
types.derive T (inst: ...) |
Default computed from other fields on the same instance |
types.listOf T / types.attrsOf T |
Typed lists and attribute sets |
types.record { field = T; } |
Structured typed records with field validation, defaults, and secret redaction (composable inside listOf / attrsOf) |
types.secret T |
Marks a field sensitive so exported inventory replaces it with sha256:... |
types.is pred "label" |
Custom predicate check with a clear error description |
types.rawFunction |
Passes a function through untouched (for Nixpkgs overlays or custom callbacks) |
yen.kinds.<k> / types.selector |
Checked references to another declared instance or an at selector |
Next, attach users and disks cleanly without copying host lists in Organizing Hosts, Users & Sub-Resources.