Using `den.flakeModules.strict` fails when using nixos class
I found the Strict Mode in the docs, and wanted to use it to force correctness in my config. However, when I added it I get this error:
error:
… while evaluating the attribute 'root.result'
at «flakes-internal»/call-flake.nix:94:7:
93| {
94| result =
| ^
95| if node.flake or true then
… in the left operand of the update (//) operator
at «flakes-internal»/call-flake.nix:75:9:
74| result =
75| outputs
| ^
76| # We add the sourceInfo attribute for its metadata, as they are
… while evaluating the option `flake':
… while evaluating definitions from `/nix/store/jb9w9vg5r706c890hkcn5f2v0vfz0wf7-source/modules/outputs.nix':
… while evaluating the option `den.classes':
… while evaluating definitions from `/nix/store/jb9w9vg5r706c890hkcn5f2v0vfz0wf7-source/modules/aspect-schema.nix':
(stack trace truncated; use '--show-trace' to show the full, detailed trace)
error: STRICT MODE
Attempted to set the option "nixos" in "den.aspects.jetpack" but no explicit definition exists. If this wasn't a mistake, disable STRICT mode or configure an option. e.g.
den.schema.aspects.options.nixos = lib.mkOption { ... };
See https://documentation.example
I'm not sure if I'm missing something obvious here, like maybe I have to enable nixos evaluation somewhere. But it certainly seems like nixos should pass the strict check. Any help would be appreciated!
If this is a bug, and I could be pointed in the right direction to look for how to fix it, I would be happy to help contribute. Thanks!
@rhedgeco I'm about to replace the current system with https://github.com/sini/gen-schema + https://github.com/sini/gen-aspects/ which properly provides schema-first support. A full pipeline rewrite based on https://github.com/sini/gen is currently live at https://github.com/sini/den-hoag and I should be able to ship it within the next week. Once that lands, any feedback you could give would be invaluable. :)
Also any general feedback on gen as architected is also welcome.
Amazing! Thank you for your effort @sini I will certainly take a look. Looking forward to that landing
Confirmed as a bug, with a reproduction and a root cause. Summary first, then the detail and a workaround — and @rhedgeco, there's a concrete map at the end if you still want to take it.
You weren't missing anything. nixos is a registered class, so it genuinely is "an explicit definition". Strict mode rejects it anyway, which makes strict mode unusable with any aspect that carries class content — i.e. every real aspect.
Reproduction
{
imports = [ inputs.den.flakeModules.strict ];
den.hosts.x86_64-linux.igloo.users.tux = { };
den.aspects.igloo.nixos.networking.hostName = "igloo";
}
Throws Attempted to set the option "nixos" in "den.aspects.igloo". The identical fixture without flakeModules.strict builds fine, so it's strict mode specifically and not class registration.
Cause
Strict mode works by replacing the aspect submodule's _module.freeformType with a type that throws (nix/lib/strict.nix). That's correct for entity kinds — a host's keys really are its options.
It's wrong for aspects, because class keys reach the pipeline through that freeform type. aspectKeyType is what wraps class content for dispatch, so swapping the freeform type out rejects every registered class on the way in, indistinguishably from an unknown key.
The message's suggested remedy is also misleading: den.schema.aspect.options.nixos = lib.mkOption { ... } is not how a class is registered (den.classes is), so following it won't help.
Workaround
Apply strict mode to the entity kinds and omit the aspect kind:
{
den.schema.host = den.lib.strict;
den.schema.user = den.lib.strict;
den.schema.home = den.lib.strict;
# den.schema.aspect deliberately omitted
}
instead of imports = [ inputs.den.flakeModules.strict ]. Verified: hosts still reject undeclared keys, and aspects with class content build. You lose strict checking on aspect keys only.
Fix direction
The check belongs in aspectKeyType.merge (nix/lib/aspects/types.nix), which already branches on lib.last loc for structural keys — one more branch, no new plumbing. Under strict, a key is accounted for if it is structural, in den.classes, or in den.quirks; anything else is content nothing will route, which is what strict mode exists to reject.
Reading those registries at merge time is what keeps it cycle-free — measured, and it was the main risk. They're derived from aspect values, so consulting them while declaring options would close a loop; that's why den.classes lives outside den.schema to begin with (see the note in nix/lib/aspects/fx/key-classification.nix). Merge time is the same point the classifier already reads them.
One open problem, and it's why this isn't merged yet. The strict flag needs somewhere to live, and den.schema.aspect won't work: that value is imported as a module into every aspect (types.nix, imports = [ (den.schema.aspect or { }) ]), so setting strict = true there defines a freeform aspect key literally named strict — which the new predicate then rejects. Three candidates: a top-level option, a den-internal option outside den.schema, or making den.lib.strict kind-aware so it sets a flag for aspects while keeping the freeform swap for entities. That's a public-surface choice rather than a code problem.
Note the two spellings behave differently and should converge as part of any fix: den.flakeModules.strict would pick up the new check, while the lower-level den.schema.aspect = den.lib.strict keeps the freeform swap and so keeps rejecting classes.
Longer term this dissolves rather than gets patched — with a schema-declared key surface, class keys are accounted for by construction and there's no flag or predicate at all, which is the direction @sini described above.