Merge pull request #53397 from cdepillabout/aliasoptionmodule-set-priority
lib/modules: Add function to create option alias that respects priority
This commit is contained in:
commit
a3beabf327
|
@ -109,7 +109,7 @@ let
|
||||||
mkFixStrictness mkOrder mkBefore mkAfter mkAliasDefinitions
|
mkFixStrictness mkOrder mkBefore mkAfter mkAliasDefinitions
|
||||||
mkAliasAndWrapDefinitions fixMergeModules mkRemovedOptionModule
|
mkAliasAndWrapDefinitions fixMergeModules mkRemovedOptionModule
|
||||||
mkRenamedOptionModule mkMergedOptionModule mkChangedOptionModule
|
mkRenamedOptionModule mkMergedOptionModule mkChangedOptionModule
|
||||||
mkAliasOptionModule doRename filterModules;
|
mkAliasOptionModule mkAliasOptionModuleWithPriority doRename filterModules;
|
||||||
inherit (options) isOption mkEnableOption mkSinkUndeclaredOptions
|
inherit (options) isOption mkEnableOption mkSinkUndeclaredOptions
|
||||||
mergeDefaultOption mergeOneOption mergeEqualOption getValues
|
mergeDefaultOption mergeOneOption mergeEqualOption getValues
|
||||||
getFiles optionAttrSetToDocList optionAttrSetToDocList'
|
getFiles optionAttrSetToDocList optionAttrSetToDocList'
|
||||||
|
|
|
@ -450,8 +450,7 @@ rec {
|
||||||
|
|
||||||
filterOverrides' = defs:
|
filterOverrides' = defs:
|
||||||
let
|
let
|
||||||
defaultPrio = 100;
|
getPrio = def: if def.value._type or "" == "override" then def.value.priority else defaultPriority;
|
||||||
getPrio = def: if def.value._type or "" == "override" then def.value.priority else defaultPrio;
|
|
||||||
highestPrio = foldl' (prio: def: min (getPrio def) prio) 9999 defs;
|
highestPrio = foldl' (prio: def: min (getPrio def) prio) 9999 defs;
|
||||||
strip = def: if def.value._type or "" == "override" then def // { value = def.value.content; } else def;
|
strip = def: if def.value._type or "" == "override" then def // { value = def.value.content; } else def;
|
||||||
in {
|
in {
|
||||||
|
@ -534,6 +533,8 @@ rec {
|
||||||
mkBefore = mkOrder 500;
|
mkBefore = mkOrder 500;
|
||||||
mkAfter = mkOrder 1500;
|
mkAfter = mkOrder 1500;
|
||||||
|
|
||||||
|
# The default priority for things that don't have a priority specified.
|
||||||
|
defaultPriority = 100;
|
||||||
|
|
||||||
# Convenient property used to transfer all definitions and their
|
# Convenient property used to transfer all definitions and their
|
||||||
# properties from one option to another. This property is useful for
|
# properties from one option to another. This property is useful for
|
||||||
|
@ -556,8 +557,20 @@ rec {
|
||||||
#
|
#
|
||||||
mkAliasDefinitions = mkAliasAndWrapDefinitions id;
|
mkAliasDefinitions = mkAliasAndWrapDefinitions id;
|
||||||
mkAliasAndWrapDefinitions = wrap: option:
|
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 defaultPriority.
|
||||||
|
mkAliasAndWrapDefsWithPriority = wrap: option:
|
||||||
|
let
|
||||||
|
prio = option.highestPrio or defaultPriority;
|
||||||
|
defsWithPrio = map (mkOverride prio) option.definitions;
|
||||||
|
in mkAliasIfDef option (wrap (mkMerge defsWithPrio));
|
||||||
|
|
||||||
|
mkAliasIfDef = option:
|
||||||
|
mkIf (isOption option && option.isDefined);
|
||||||
|
|
||||||
/* Compatibility. */
|
/* Compatibility. */
|
||||||
fixMergeModules = modules: args: evalModules { inherit modules args; check = false; };
|
fixMergeModules = modules: args: evalModules { inherit modules args; check = false; };
|
||||||
|
@ -690,7 +703,16 @@ rec {
|
||||||
use = id;
|
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, ... }:
|
{ config, options, ... }:
|
||||||
let
|
let
|
||||||
fromOpt = getAttrFromPath from options;
|
fromOpt = getAttrFromPath from options;
|
||||||
|
@ -708,7 +730,9 @@ rec {
|
||||||
warnings = optional (warn && fromOpt.isDefined)
|
warnings = optional (warn && fromOpt.isDefined)
|
||||||
"The option `${showOption from}' defined in ${showFiles fromOpt.files} has been renamed to `${showOption to}'.";
|
"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)
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -149,6 +149,12 @@ checkConfigOutput "1 2 3 4 5 6 7 8 9 10" config.result ./loaOf-with-long-list.ni
|
||||||
# Check loaOf with many merges of lists.
|
# Check loaOf with many merges of lists.
|
||||||
checkConfigOutput "1 2 3 4 5 6 7 8 9 10" config.result ./loaOf-with-many-list-merges.nix
|
checkConfigOutput "1 2 3 4 5 6 7 8 9 10" config.result ./loaOf-with-many-list-merges.nix
|
||||||
|
|
||||||
|
# Check mkAliasOptionModuleWithPriority.
|
||||||
|
checkConfigOutput "true" config.enable ./alias-with-priority.nix
|
||||||
|
checkConfigOutput "true" config.enableAlias ./alias-with-priority.nix
|
||||||
|
checkConfigOutput "false" config.enable ./alias-with-priority-can-override.nix
|
||||||
|
checkConfigOutput "false" config.enableAlias ./alias-with-priority-can-override.nix
|
||||||
|
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
====== module tests ======
|
====== module tests ======
|
||||||
$pass Pass
|
$pass Pass
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
# This is a test to show that mkAliasOptionModule sets the priority correctly
|
||||||
|
# for aliased options.
|
||||||
|
|
||||||
|
{ config, lib, ... }:
|
||||||
|
|
||||||
|
with lib;
|
||||||
|
|
||||||
|
{
|
||||||
|
options = {
|
||||||
|
# A simple boolean option that can be enabled or disabled.
|
||||||
|
enable = lib.mkOption {
|
||||||
|
type = types.nullOr types.bool;
|
||||||
|
default = null;
|
||||||
|
example = true;
|
||||||
|
description = ''
|
||||||
|
Some descriptive text
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
# mkAliasOptionModule sets warnings, so this has to be defined.
|
||||||
|
warnings = mkOption {
|
||||||
|
internal = true;
|
||||||
|
default = [];
|
||||||
|
type = types.listOf types.str;
|
||||||
|
example = [ "The `foo' service is deprecated and will go away soon!" ];
|
||||||
|
description = ''
|
||||||
|
This option allows modules to show warnings to users during
|
||||||
|
the evaluation of the system configuration.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
imports = [
|
||||||
|
# Create an alias for the "enable" option.
|
||||||
|
(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.
|
||||||
|
( { config, lib, ... }:
|
||||||
|
{
|
||||||
|
enableAlias = lib.mkForce false;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Enable the normal (non-aliased) option.
|
||||||
|
( { config, lib, ... }:
|
||||||
|
{
|
||||||
|
enable = true;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
];
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
# This is a test to show that mkAliasOptionModule sets the priority correctly
|
||||||
|
# for aliased options.
|
||||||
|
|
||||||
|
{ config, lib, ... }:
|
||||||
|
|
||||||
|
with lib;
|
||||||
|
|
||||||
|
{
|
||||||
|
options = {
|
||||||
|
# A simple boolean option that can be enabled or disabled.
|
||||||
|
enable = lib.mkOption {
|
||||||
|
type = types.nullOr types.bool;
|
||||||
|
default = null;
|
||||||
|
example = true;
|
||||||
|
description = ''
|
||||||
|
Some descriptive text
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
# mkAliasOptionModule sets warnings, so this has to be defined.
|
||||||
|
warnings = mkOption {
|
||||||
|
internal = true;
|
||||||
|
default = [];
|
||||||
|
type = types.listOf types.str;
|
||||||
|
example = [ "The `foo' service is deprecated and will go away soon!" ];
|
||||||
|
description = ''
|
||||||
|
This option allows modules to show warnings to users during
|
||||||
|
the evaluation of the system configuration.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
imports = [
|
||||||
|
# Create an alias for the "enable" option.
|
||||||
|
(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.
|
||||||
|
( { config, lib, ... }:
|
||||||
|
{
|
||||||
|
enableAlias = lib.mkDefault false;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Enable the normal (non-aliased) option.
|
||||||
|
( { config, lib, ... }:
|
||||||
|
{
|
||||||
|
enable = true;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
];
|
||||||
|
}
|
Loading…
Reference in New Issue