Skip to content

Commit

Permalink
lib: Add beforeApply option attribute
Browse files Browse the repository at this point in the history
See #299736 for the reasons.
  • Loading branch information
dasJ committed Apr 9, 2024
1 parent 4f1d724 commit d8616a1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,9 @@ let

in warnDeprecation opt //
{ value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
# The merged value before applying the options `apply` function to it.
# In general though, `apply` should not be used, it's an anti-pattern.
beforeApply = res.mergedValue;
inherit (res.defsFinal') highestPrio;
definitions = map (def: def.value) res.defsFinal;
files = map (def: def.file) res.defsFinal;
Expand Down
2 changes: 2 additions & 0 deletions lib/tests/modules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ checkConfigOutput '^"one two"$' config.result ./shorthand-meta.nix

checkConfigOutput '^true$' config.result ./test-mergeAttrDefinitionsWithPrio.nix

checkConfigOutput '^true$' config.okChecks ./before-apply.nix

# Check that a module argument is passed, also when a default is available
# (but not needed)
#
Expand Down
24 changes: 24 additions & 0 deletions lib/tests/modules/before-apply.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{ options, lib, ... }:

# Tests wheter a apply function properly generates the `beforeApply` option attribute

{
options = {
optionWithoutApply = lib.mkOption {
default = false;
type = lib.types.bool;
};

optionWithApply = lib.mkOption {
default = false;
type = lib.types.bool;
apply = x: !x;
};

okChecks = lib.mkOption {};
};
config.okChecks = builtins.addErrorContext "while evaluating the assertions" (
assert options.optionWithoutApply.beforeApply == options.optionWithoutApply.value;
assert options.optionWithApply.beforeApply == !options.optionWithApply.value;
true);
}

0 comments on commit d8616a1

Please sign in to comment.