diff options
author | (cdep)illabout <cdep.illabout@gmail.com> | 2019-01-03 23:15:01 +0900 |
---|---|---|
committer | (cdep)illabout <cdep.illabout@gmail.com> | 2019-01-04 18:35:10 +0900 |
commit | b81b3ad1b052aeb2edcf493167df7348b7bfb293 (patch) | |
tree | cbbd8cfdc23e362ab18763caea1c44808e2dd6ee /lib/modules.nix | |
parent | da00ec4b45f5cfc5b47825818b57db1d1950757d (diff) |
lib/modules: Add a function to create an option alias that respects the priority
This commit adds a function `mkAliasOptionModuleWithPriority`. This
function will make an alias to an existing option and copy over the
priority.
This functionality is needed for PRs like #53041. In that case
`nixos-generate-config` added an option to `hardware-configuration.nix`
with `mkDefault`. That option was then changed and an alias created for
the old name.
The end user should be able to set the non-alias option in their
`configuration.nix` and have everything work correctly. Without this
function, the priority for the option won't be copied over correctly
and the end-user will get a message saying they have the same option
set to two different values.
Diffstat (limited to 'lib/modules.nix')
-rw-r--r-- | lib/modules.nix | 30 |
1 files changed, 27 insertions, 3 deletions
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) ]; }; |