summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorRobert Hensing <roberth@users.noreply.github.com>2021-01-27 10:50:13 +0100
committerGitHub <noreply@github.com>2021-01-27 10:50:13 +0100
commitd2a41be2f3847120a06e5229054592983329d62f (patch)
tree65e9d65e701cfd6b02ef289251b6e80b1b304ccf /lib
parentad97ca4e510a433e869a4e8d8a50744ed1e1b002 (diff)
parentc2f3556dc75ee09890c2cfc867cdb10df23accf6 (diff)
Merge pull request #110707 from Infinisil/functionTo
Bring back `types.functionTo`
Diffstat (limited to 'lib')
-rwxr-xr-xlib/tests/modules.sh7
-rw-r--r--lib/tests/modules/functionTo/list-order.nix25
-rw-r--r--lib/tests/modules/functionTo/merging-attrs.nix27
-rw-r--r--lib/tests/modules/functionTo/merging-list.nix24
-rw-r--r--lib/tests/modules/functionTo/trivial.nix17
-rw-r--r--lib/tests/modules/functionTo/wrong-type.nix18
-rw-r--r--lib/types.nix10
7 files changed, 128 insertions, 0 deletions
diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh
index 309c5311361c..f843d303e440 100755
--- a/lib/tests/modules.sh
+++ b/lib/tests/modules.sh
@@ -262,6 +262,13 @@ checkConfigOutput true config.value.mkbefore ./types-anything/mk-mods.nix
checkConfigOutput 1 config.value.nested.foo ./types-anything/mk-mods.nix
checkConfigOutput baz config.value.nested.bar.baz ./types-anything/mk-mods.nix
+## types.functionTo
+checkConfigOutput "input is input" config.result ./functionTo/trivial.nix
+checkConfigOutput "a b" config.result ./functionTo/merging-list.nix
+checkConfigError 'A definition for option .fun.\[function body\]. is not of type .string.. Definition values:\n- In .*wrong-type.nix' config.result ./functionTo/wrong-type.nix
+checkConfigOutput "b a" config.result ./functionTo/list-order.nix
+checkConfigOutput "a c" config.result ./functionTo/merging-attrs.nix
+
cat <<EOF
====== module tests ======
$pass Pass
diff --git a/lib/tests/modules/functionTo/list-order.nix b/lib/tests/modules/functionTo/list-order.nix
new file mode 100644
index 000000000000..77a1a43a84f0
--- /dev/null
+++ b/lib/tests/modules/functionTo/list-order.nix
@@ -0,0 +1,25 @@
+
+{ lib, config, ... }:
+let
+ inherit (lib) types;
+in {
+ options = {
+ fun = lib.mkOption {
+ type = types.functionTo (types.listOf types.str);
+ };
+
+ result = lib.mkOption {
+ type = types.str;
+ default = toString (config.fun {
+ a = "a";
+ b = "b";
+ c = "c";
+ });
+ };
+ };
+
+ config.fun = lib.mkMerge [
+ (input: lib.mkAfter [ input.a ])
+ (input: [ input.b ])
+ ];
+}
diff --git a/lib/tests/modules/functionTo/merging-attrs.nix b/lib/tests/modules/functionTo/merging-attrs.nix
new file mode 100644
index 000000000000..97c015f928ab
--- /dev/null
+++ b/lib/tests/modules/functionTo/merging-attrs.nix
@@ -0,0 +1,27 @@
+{ lib, config, ... }:
+let
+ inherit (lib) types;
+in {
+ options = {
+ fun = lib.mkOption {
+ type = types.functionTo (types.attrsOf types.str);
+ };
+
+ result = lib.mkOption {
+ type = types.str;
+ default = toString (lib.attrValues (config.fun {
+ a = "a";
+ b = "b";
+ c = "c";
+ }));
+ };
+ };
+
+ config.fun = lib.mkMerge [
+ (input: { inherit (input) a; })
+ (input: { inherit (input) b; })
+ (input: {
+ b = lib.mkForce input.c;
+ })
+ ];
+}
diff --git a/lib/tests/modules/functionTo/merging-list.nix b/lib/tests/modules/functionTo/merging-list.nix
new file mode 100644
index 000000000000..15fcd2bdcc42
--- /dev/null
+++ b/lib/tests/modules/functionTo/merging-list.nix
@@ -0,0 +1,24 @@
+{ lib, config, ... }:
+let
+ inherit (lib) types;
+in {
+ options = {
+ fun = lib.mkOption {
+ type = types.functionTo (types.listOf types.str);
+ };
+
+ result = lib.mkOption {
+ type = types.str;
+ default = toString (config.fun {
+ a = "a";
+ b = "b";
+ c = "c";
+ });
+ };
+ };
+
+ config.fun = lib.mkMerge [
+ (input: [ input.a ])
+ (input: [ input.b ])
+ ];
+}
diff --git a/lib/tests/modules/functionTo/trivial.nix b/lib/tests/modules/functionTo/trivial.nix
new file mode 100644
index 000000000000..0962a0cf893d
--- /dev/null
+++ b/lib/tests/modules/functionTo/trivial.nix
@@ -0,0 +1,17 @@
+{ lib, config, ... }:
+let
+ inherit (lib) types;
+in {
+ options = {
+ fun = lib.mkOption {
+ type = types.functionTo types.str;
+ };
+
+ result = lib.mkOption {
+ type = types.str;
+ default = config.fun "input";
+ };
+ };
+
+ config.fun = input: "input is ${input}";
+}
diff --git a/lib/tests/modules/functionTo/wrong-type.nix b/lib/tests/modules/functionTo/wrong-type.nix
new file mode 100644
index 000000000000..fd65b75088da
--- /dev/null
+++ b/lib/tests/modules/functionTo/wrong-type.nix
@@ -0,0 +1,18 @@
+
+{ lib, config, ... }:
+let
+ inherit (lib) types;
+in {
+ options = {
+ fun = lib.mkOption {
+ type = types.functionTo types.str;
+ };
+
+ result = lib.mkOption {
+ type = types.str;
+ default = config.fun 0;
+ };
+ };
+
+ config.fun = input: input + 1;
+}
diff --git a/lib/types.nix b/lib/types.nix
index ee891f8231b6..77245158d9f8 100644
--- a/lib/types.nix
+++ b/lib/types.nix
@@ -453,6 +453,16 @@ rec {
functor = (defaultFunctor name) // { wrapped = elemType; };
};
+ functionTo = elemType: mkOptionType {
+ name = "function that evaluates to a(n) ${elemType.name}";
+ check = isFunction;
+ merge = loc: defs:
+ fnArgs: (mergeDefinitions (loc ++ [ "[function body]" ]) elemType (map (fn: { inherit (fn) file; value = fn.value fnArgs; }) defs)).mergedValue;
+ getSubOptions = elemType.getSubOptions;
+ getSubModules = elemType.getSubModules;
+ substSubModules = m: functionTo (elemType.substSubModules m);
+ };
+
# A submodule (like typed attribute set). See NixOS manual.
submodule = modules: submoduleWith {
shorthandOnlyDefinesConfig = true;