diff --git a/lib/default.nix b/lib/default.nix index 916f6e05190b..17a74cd21355 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -109,7 +109,7 @@ let mkFixStrictness mkOrder mkBefore mkAfter mkAliasDefinitions mkAliasAndWrapDefinitions fixMergeModules mkRemovedOptionModule mkRenamedOptionModule mkMergedOptionModule mkChangedOptionModule - mkAliasOptionModule doRename filterModules; + mkAliasOptionModule mkAliasOptionModuleWithPriority doRename filterModules; inherit (options) isOption mkEnableOption mkSinkUndeclaredOptions mergeDefaultOption mergeOneOption mergeEqualOption getValues getFiles optionAttrSetToDocList optionAttrSetToDocList' diff --git a/lib/modules.nix b/lib/modules.nix index 5fb83a4a538c..4e259cce2ba4 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -556,8 +556,21 @@ rec { # mkAliasDefinitions = mkAliasAndWrapDefinitions id; mkAliasAndWrapDefinitions = wrap: option: - mkIf (isOption option && option.isDefined) (wrap (mkMerge option.definitions)); + mkAliasIfDef option (wrap (mkMerge option.definitions)); + # Similar to mkAliasAndWrapDefinitions but copies over the priority from the + # option as well. + # + # If a priority is not set, it assumes a priority of 100. + mkAliasAndWrapDefsWithPriority = wrap: option: + let + defaultPrio = 100; + prio = option.highestPrio or defaultPrio; + defsWithPrio = map (mkOverride prio) option.definitions; + in mkAliasIfDef option (wrap (mkMerge defsWithPrio)); + + mkAliasIfDef = option: + mkIf (isOption option && option.isDefined); /* Compatibility. */ fixMergeModules = modules: args: evalModules { inherit modules args; check = false; }; @@ -690,7 +703,16 @@ rec { use = id; }; - doRename = { from, to, visible, warn, use }: + /* Like ‘mkAliasOptionModule’, but copy over the priority of the option as well. */ + mkAliasOptionModuleWithPriority = from: to: doRename { + inherit from to; + visible = true; + warn = false; + use = id; + withPriority = true; + }; + + doRename = { from, to, visible, warn, use, withPriority ? false }: { config, options, ... }: let fromOpt = getAttrFromPath from options; @@ -708,7 +730,9 @@ rec { warnings = optional (warn && fromOpt.isDefined) "The option `${showOption from}' defined in ${showFiles fromOpt.files} has been renamed to `${showOption to}'."; } - (mkAliasAndWrapDefinitions (setAttrByPath to) fromOpt) + (if withPriority + then mkAliasAndWrapDefsWithPriority (setAttrByPath to) fromOpt + else mkAliasAndWrapDefinitions (setAttrByPath to) fromOpt) ]; }; diff --git a/lib/tests/modules/alias-with-priority.nix b/lib/tests/modules/alias-with-priority.nix index ba25b527aa2d..923483684cb1 100644 --- a/lib/tests/modules/alias-with-priority.nix +++ b/lib/tests/modules/alias-with-priority.nix @@ -32,7 +32,7 @@ with lib; imports = [ # Create an alias for the "enable" option. - (mkAliasOptionModule [ "enableAlias" ] [ "enable" ]) + (mkAliasOptionModuleWithPriority [ "enableAlias" ] [ "enable" ]) # Disable the aliased option, but with a default (low) priority so it # should be able to be overridden by the next import.