diff options
author | Nicolas B. Pierron <nicolas.b.pierron@gmail.com> | 2015-03-12 23:01:47 +0100 |
---|---|---|
committer | Nicolas B. Pierron <nicolas.b.pierron@gmail.com> | 2015-03-12 23:42:58 +0100 |
commit | ed91474e9b94f06a44b7aa5bda9911292f709f54 (patch) | |
tree | ddbb4b53861c4743fde7201db6b502faaee742e4 /lib/modules.nix | |
parent | dd4f5f6b789c7fb2cac45cb8024e87bdb13c9740 (diff) | |
download | nixpkgs-ed91474e9b94f06a44b7aa5bda9911292f709f54.tar nixpkgs-ed91474e9b94f06a44b7aa5bda9911292f709f54.tar.gz nixpkgs-ed91474e9b94f06a44b7aa5bda9911292f709f54.tar.bz2 nixpkgs-ed91474e9b94f06a44b7aa5bda9911292f709f54.tar.lz nixpkgs-ed91474e9b94f06a44b7aa5bda9911292f709f54.tar.xz nixpkgs-ed91474e9b94f06a44b7aa5bda9911292f709f54.tar.zst nixpkgs-ed91474e9b94f06a44b7aa5bda9911292f709f54.zip |
Share common code for merging option definitions.
This move idioms which were used in `evalOptionValue` and in the `merge` functions of `listOf` and `attrsOf` types, such that we can use a names such as `isDefined` and `optionalValue` instead or repeating identical comparisons of `defsFinal == []`.
Diffstat (limited to 'lib/modules.nix')
-rw-r--r-- | lib/modules.nix | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/lib/modules.nix b/lib/modules.nix index ad573da990f..a3c3fe7fc1a 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -250,14 +250,14 @@ rec { # Add in the default value for this option, if any. defs' = (optional (opt ? default) { file = head opt.declarations; value = mkOptionDefault opt.default; }) ++ defs; + # Handle properties, check types, and merge everything together - inherit (mergeDefinitions loc opt.type defs') defsFinal mergedValue; + inherit (mergeDefinitions loc opt.type defs') isDefined defsFinal mergedValue; files = map (def: def.file) defsFinal; merged = - if defsFinal == [] then - throw "The option `${showOption loc}' is used but not defined." - else - mergedValue; + if isDefined then mergedValue + else throw "The option `${showOption loc}' is used but not defined."; + # Finally, apply the ‘apply’ function to the merged # value. This allows options to yield a value computed # from the definitions. @@ -265,8 +265,7 @@ rec { in opt // { value = addErrorContext "while evaluating the option `${showOption loc}':" value; definitions = map (def: def.value) defsFinal; - isDefined = defsFinal != []; - inherit files; + inherit isDefined files; }; # Merge definitions of a value of a given type @@ -295,6 +294,11 @@ rec { if type.check def.value then res else throw "The option value `${showOption loc}' in `${def.file}' is not a ${type.name}.") (type.merge loc defsFinal) defsFinal; + + isDefined = defsFinal != []; + optionalValue = + if isDefined then { value = mergedValue; } + else {}; }; /* Given a config set, expand mkMerge properties, and push down the |