diff options
author | Frederik Rietdijk <freddyrietdijk@fridh.nl> | 2019-01-15 16:59:03 +0100 |
---|---|---|
committer | Frederik Rietdijk <freddyrietdijk@fridh.nl> | 2019-01-15 16:59:03 +0100 |
commit | 42d276c6b8b698dba0aac0bdb770e7a3e5b8d5b4 (patch) | |
tree | 787093bb48bb6a2b16f0976ac707641a13893b93 /lib | |
parent | 1f0467005dd43c3c0f57c87da1c298aed7ea8b46 (diff) | |
parent | ab8e72b7bb24d73a588d9783240a1d0154a50ff7 (diff) | |
download | nixpkgs-42d276c6b8b698dba0aac0bdb770e7a3e5b8d5b4.tar nixpkgs-42d276c6b8b698dba0aac0bdb770e7a3e5b8d5b4.tar.gz nixpkgs-42d276c6b8b698dba0aac0bdb770e7a3e5b8d5b4.tar.bz2 nixpkgs-42d276c6b8b698dba0aac0bdb770e7a3e5b8d5b4.tar.lz nixpkgs-42d276c6b8b698dba0aac0bdb770e7a3e5b8d5b4.tar.xz nixpkgs-42d276c6b8b698dba0aac0bdb770e7a3e5b8d5b4.tar.zst nixpkgs-42d276c6b8b698dba0aac0bdb770e7a3e5b8d5b4.zip |
Merge staging-next into staging
Diffstat (limited to 'lib')
-rw-r--r-- | lib/default.nix | 2 | ||||
-rw-r--r-- | lib/modules.nix | 34 | ||||
-rwxr-xr-x | lib/tests/modules.sh | 6 | ||||
-rw-r--r-- | lib/tests/modules/alias-with-priority-can-override.nix | 52 | ||||
-rw-r--r-- | lib/tests/modules/alias-with-priority.nix | 52 |
5 files changed, 140 insertions, 6 deletions
diff --git a/lib/default.nix b/lib/default.nix index 40116063bf2..48eb99a7198 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 5fb83a4a538..9f8e196ee0f 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -450,8 +450,7 @@ rec { filterOverrides' = defs: let - defaultPrio = 100; - getPrio = def: if def.value._type or "" == "override" then def.value.priority else defaultPrio; + getPrio = def: if def.value._type or "" == "override" then def.value.priority else defaultPriority; 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; in { @@ -534,6 +533,8 @@ rec { mkBefore = mkOrder 500; 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 # properties from one option to another. This property is useful for @@ -556,8 +557,20 @@ 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 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. */ 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.sh b/lib/tests/modules.sh index b83e1eb7d82..a72777cbf2a 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -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. 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 ====== module tests ====== $pass Pass diff --git a/lib/tests/modules/alias-with-priority-can-override.nix b/lib/tests/modules/alias-with-priority-can-override.nix new file mode 100644 index 00000000000..a6b26895f3a --- /dev/null +++ b/lib/tests/modules/alias-with-priority-can-override.nix @@ -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; + } + ) + ]; +} diff --git a/lib/tests/modules/alias-with-priority.nix b/lib/tests/modules/alias-with-priority.nix new file mode 100644 index 00000000000..923483684cb --- /dev/null +++ b/lib/tests/modules/alias-with-priority.nix @@ -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; + } + ) + ]; +} |