From 0e590c91d20efb1be7978347a2d45940a1d2fc2e Mon Sep 17 00:00:00 2001 From: Taeer Bar-Yam Date: Wed, 10 Nov 2021 22:01:31 -0500 Subject: [PATCH 1/2] etc module: make `.text` and `.source` the same priority Before this change, one could set environment.etc.*.text and .source. .source would always take precedence, regardless of the priorities set. This change means that if, for instance, .text is set with mkForce but .source is set normally, the .text content will be the one to take effect. If they are set with the same priority they will conflict. --- nixos/modules/system/etc/etc.nix | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/nixos/modules/system/etc/etc.nix b/nixos/modules/system/etc/etc.nix index 8f14f04a1f64..225a5683e79e 100644 --- a/nixos/modules/system/etc/etc.nix +++ b/nixos/modules/system/etc/etc.nix @@ -85,7 +85,7 @@ in ''; type = with types; attrsOf (submodule ( - { name, config, ... }: + { name, config, options, ... }: { options = { enable = mkOption { @@ -172,7 +172,9 @@ in target = mkDefault name; source = mkIf (config.text != null) ( let name' = "etc-" + baseNameOf name; - in mkDefault (pkgs.writeText name' config.text)); + in mkOverride + (options.text.highestPrio or lib.modules.defaultPriority) + (pkgs.writeText name' config.text)); }; })); From 0bef0c38f74372ca1794e800b8709b90558efba2 Mon Sep 17 00:00:00 2001 From: Taeer Bar-Yam Date: Sun, 14 Nov 2021 09:57:54 -0500 Subject: [PATCH 2/2] lib.modules: add mkDerivedConfig mkDerivedConfig : Option a -> (a -> Definition b) -> Definition b Create config definitions with the same priority as the definition of another option. This should be used for option definitions where one option sets the value of another as a convenience. For instance a config file could be set with a `text` or `source` option, where text translates to a `source` value using `mkDerivedConfig options.text (pkgs.writeText "filename.conf")`. It takes care of setting the right priority using `mkOverride`. --- lib/default.nix | 2 +- lib/modules.nix | 20 ++++++++++++++++++++ nixos/modules/system/etc/etc.nix | 5 ++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/default.nix b/lib/default.nix index 5a85c5421172..68d73220fa9a 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -119,7 +119,7 @@ let mkFixStrictness mkOrder mkBefore mkAfter mkAliasDefinitions mkAliasAndWrapDefinitions fixMergeModules mkRemovedOptionModule mkRenamedOptionModule mkMergedOptionModule mkChangedOptionModule - mkAliasOptionModule doRename; + mkAliasOptionModule mkDerivedConfig doRename; inherit (self.options) isOption mkEnableOption mkSinkUndeclaredOptions mergeDefaultOption mergeOneOption mergeEqualOption getValues getFiles optionAttrSetToDocList optionAttrSetToDocList' diff --git a/lib/modules.nix b/lib/modules.nix index d9b4000e56bd..92ddc8c5bc70 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -956,6 +956,26 @@ rec { use = id; }; + /* mkDerivedConfig : Option a -> (a -> Definition b) -> Definition b + + Create config definitions with the same priority as the definition of another option. + This should be used for option definitions where one option sets the value of another as a convenience. + For instance a config file could be set with a `text` or `source` option, where text translates to a `source` + value using `mkDerivedConfig options.text (pkgs.writeText "filename.conf")`. + + It takes care of setting the right priority using `mkOverride`. + */ + # TODO: make the module system error message include information about `opt` in + # error messages about conflicts. E.g. introduce a variation of `mkOverride` which + # adds extra location context to the definition object. This will allow context to be added + # to all messages that report option locations "this value was derived from + # which was defined in ". It can provide a trace of options that contributed + # to definitions. + mkDerivedConfig = opt: f: + mkOverride + (opt.highestPrio or defaultPriority) + (f opt.value); + doRename = { from, to, visible, warn, use, withPriority ? true }: { config, options, ... }: let diff --git a/nixos/modules/system/etc/etc.nix b/nixos/modules/system/etc/etc.nix index 225a5683e79e..6cc8c341e6df 100644 --- a/nixos/modules/system/etc/etc.nix +++ b/nixos/modules/system/etc/etc.nix @@ -172,9 +172,8 @@ in target = mkDefault name; source = mkIf (config.text != null) ( let name' = "etc-" + baseNameOf name; - in mkOverride - (options.text.highestPrio or lib.modules.defaultPriority) - (pkgs.writeText name' config.text)); + in mkDerivedConfig options.text (pkgs.writeText name') + ); }; }));