summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorTaeer Bar-Yam <taeer@bar-yam.me>2021-11-14 09:57:54 -0500
committerTaeer Bar-Yam <taeer@bar-yam.me>2021-11-15 07:03:41 -0500
commit0bef0c38f74372ca1794e800b8709b90558efba2 (patch)
tree8056bfcf9db3a0d25b68f8615013d65fd47aca3c /lib
parent0e590c91d20efb1be7978347a2d45940a1d2fc2e (diff)
downloadnixpkgs-0bef0c38f74372ca1794e800b8709b90558efba2.tar
nixpkgs-0bef0c38f74372ca1794e800b8709b90558efba2.tar.gz
nixpkgs-0bef0c38f74372ca1794e800b8709b90558efba2.tar.bz2
nixpkgs-0bef0c38f74372ca1794e800b8709b90558efba2.tar.lz
nixpkgs-0bef0c38f74372ca1794e800b8709b90558efba2.tar.xz
nixpkgs-0bef0c38f74372ca1794e800b8709b90558efba2.tar.zst
nixpkgs-0bef0c38f74372ca1794e800b8709b90558efba2.zip
lib.modules: add mkDerivedConfig
mkDerivedConfig : Option a -> (a -> Definition b) -> Definition b

Create config definitions with the same priority as the definition of another option.
This should be used for option definitions where one option sets the value of another as a convenience.
For instance a config file could be set with a `text` or `source` option, where text translates to a `source`
value using `mkDerivedConfig options.text (pkgs.writeText "filename.conf")`.

It takes care of setting the right priority using `mkOverride`.
Diffstat (limited to 'lib')
-rw-r--r--lib/default.nix2
-rw-r--r--lib/modules.nix20
2 files changed, 21 insertions, 1 deletions
diff --git a/lib/default.nix b/lib/default.nix
index 5a85c542117..68d73220fa9 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -119,7 +119,7 @@ let
       mkFixStrictness mkOrder mkBefore mkAfter mkAliasDefinitions
       mkAliasAndWrapDefinitions fixMergeModules mkRemovedOptionModule
       mkRenamedOptionModule mkMergedOptionModule mkChangedOptionModule
-      mkAliasOptionModule doRename;
+      mkAliasOptionModule mkDerivedConfig doRename;
     inherit (self.options) isOption mkEnableOption mkSinkUndeclaredOptions
       mergeDefaultOption mergeOneOption mergeEqualOption getValues
       getFiles optionAttrSetToDocList optionAttrSetToDocList'
diff --git a/lib/modules.nix b/lib/modules.nix
index d9b4000e56b..92ddc8c5bc7 100644
--- a/lib/modules.nix
+++ b/lib/modules.nix
@@ -956,6 +956,26 @@ rec {
     use = id;
   };
 
+  /* mkDerivedConfig : Option a -> (a -> Definition b) -> Definition b
+
+    Create config definitions with the same priority as the definition of another option.
+    This should be used for option definitions where one option sets the value of another as a convenience.
+    For instance a config file could be set with a `text` or `source` option, where text translates to a `source`
+    value using `mkDerivedConfig options.text (pkgs.writeText "filename.conf")`.
+
+    It takes care of setting the right priority using `mkOverride`.
+  */
+  # TODO: make the module system error message include information about `opt` in
+  # error messages about conflicts. E.g. introduce a variation of `mkOverride` which
+  # adds extra location context to the definition object. This will allow context to be added
+  # to all messages that report option locations "this value was derived from <full option name>
+  # which was defined in <locations>". It can provide a trace of options that contributed
+  # to definitions.
+  mkDerivedConfig = opt: f:
+    mkOverride
+      (opt.highestPrio or defaultPriority)
+      (f opt.value);
+
   doRename = { from, to, visible, warn, use, withPriority ? true }:
     { config, options, ... }:
     let