0
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-07-13 21:50:33 +03:00

lib.modules: Add mergeAttrDefinitionsWithPrio

This will let us make assertions involving _module.args.pkgs, which
is not an option but a value attribute, and therefore doesn't have
its own highestPrio to inspect. The new function gives us that info.
This commit is contained in:
Robert Hensing 2023-06-16 21:43:12 +02:00
parent a742767baf
commit 36ea2bbfe8
3 changed files with 65 additions and 0 deletions

View file

@ -61,6 +61,8 @@ checkConfigError() {
# Shorthand meta attribute does not duplicate the config
checkConfigOutput '^"one two"$' config.result ./shorthand-meta.nix
checkConfigOutput '^true$' config.result ./test-mergeAttrDefinitionsWithPrio.nix
# Check boolean option.
checkConfigOutput '^false$' config.enable ./declare-enable.nix
checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*: true' config.enable ./define-enable.nix

View file

@ -0,0 +1,21 @@
{ lib, options, ... }:
let
defs = lib.modules.mergeAttrDefinitionsWithPrio options._module.args;
assertLazy = pos: throw "${pos.file}:${toString pos.line}:${toString pos.column}: The test must not evaluate this the assertLazy thunk, but it did. Unexpected strictness leads to unexpected errors and performance problems.";
in
{
options.result = lib.mkOption { };
config._module.args = {
default = lib.mkDefault (assertLazy __curPos);
regular = null;
force = lib.mkForce (assertLazy __curPos);
unused = assertLazy __curPos;
};
config.result =
assert defs.default.highestPrio == (lib.mkDefault (assertLazy __curPos)).priority;
assert defs.regular.highestPrio == lib.modules.defaultOverridePriority;
assert defs.force.highestPrio == (lib.mkForce (assertLazy __curPos)).priority;
true;
}