@@ -1,4 +1,5 @@
11with import ./lists.nix;
2+with import ./strings.nix;
23with import ./trivial.nix;
34with import ./attrsets.nix;
45with import ./options.nix;
@@ -545,6 +546,58 @@ rec {
545546 use = builtins.trace "Obsolete option `${showOption from}' is used. It was renamed to `${showOption to}'.";
546547 };
547548
549+ /* Return a module that causes a warning to be shown if any of the "from"
550+ option is defined; the defined values can be used in the "mergeFn" to set
551+ the "to" value.
552+ This function can be used to merge multiple options into one that has a
553+ different type.
554+
555+ "mergeFn" takes the module "config" as a parameter and must return a value
556+ of "to" option type.
557+
558+ mkMergedOptionModule
559+ [ [ "a" "b" "c" ]
560+ [ "d" "e" "f" ] ]
561+ [ "x" "y" "z" ]
562+ (config:
563+ let value = p: getAttrFromPath p config;
564+ in
565+ if (value [ "a" "b" "c" ]) == true then "foo"
566+ else if (value [ "d" "e" "f" ]) == true then "bar"
567+ else "baz")
568+
569+ - options.a.b.c is a removed boolean option
570+ - options.d.e.f is a removed boolean option
571+ - options.x.y.z is a new str option that combines a.b.c and d.e.f
572+ functionality
573+
574+ This show a warning if any a.b.c or d.e.f is set, and set the value of
575+ x.y.z to the result of the merge function
576+ */
577+ mkMergedOptionModule = from: to: mergeFn:
578+ { config, options, ... }:
579+ {
580+ options = foldl recursiveUpdate {} (map (path: setAttrByPath path (mkOption {
581+ visible = false;
582+ # To use the value in mergeFn without triggering errors
583+ default = "_mkMergedOptionModule";
584+ })) from);
585+
586+ config = {
587+ warnings = filter (x: x != "") (map (f:
588+ let val = getAttrFromPath f config;
589+ opt = getAttrFromPath f options;
590+ in
591+ optionalString
592+ (val != "_mkMergedOptionModule")
593+ "The option `${showOption f}' defined in ${showFiles opt.files} has been changed to `${showOption to}' that has a different type. Please read `${showOption to}' documentation and update your configuration accordingly."
594+ ) from);
595+ } // setAttrByPath to (mkMerge
596+ (optional
597+ (any (f: (getAttrFromPath f config) != "_mkMergedOptionModule") from)
598+ (mergeFn config)));
599+ };
600+
548601 /* Like ‘mkRenamedOptionModule’, but doesn't show a warning. */
549602 mkAliasOptionModule = from: to: doRename {
550603 inherit from to;