Merge pull request #110707 from Infinisil/functionTo
Bring back `types.functionTo`
This commit is contained in:
commit
d2a41be2f3
@ -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
|
||||
|
25
lib/tests/modules/functionTo/list-order.nix
Normal file
25
lib/tests/modules/functionTo/list-order.nix
Normal file
@ -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 ])
|
||||
];
|
||||
}
|
27
lib/tests/modules/functionTo/merging-attrs.nix
Normal file
27
lib/tests/modules/functionTo/merging-attrs.nix
Normal file
@ -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;
|
||||
})
|
||||
];
|
||||
}
|
24
lib/tests/modules/functionTo/merging-list.nix
Normal file
24
lib/tests/modules/functionTo/merging-list.nix
Normal file
@ -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 ])
|
||||
];
|
||||
}
|
17
lib/tests/modules/functionTo/trivial.nix
Normal file
17
lib/tests/modules/functionTo/trivial.nix
Normal file
@ -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}";
|
||||
}
|
18
lib/tests/modules/functionTo/wrong-type.nix
Normal file
18
lib/tests/modules/functionTo/wrong-type.nix
Normal file
@ -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;
|
||||
}
|
@ -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;
|
||||
|
@ -25,6 +25,7 @@ in {
|
||||
};
|
||||
|
||||
packages = mkOption {
|
||||
type = types.functionTo (types.listOf types.package);
|
||||
default = hp: [];
|
||||
defaultText = "hp: []";
|
||||
example = "hp: with hp; [ text lens ]";
|
||||
|
@ -42,6 +42,7 @@ let
|
||||
};
|
||||
|
||||
extraPackages = mkOption {
|
||||
type = types.functionTo (types.listOf types.package);
|
||||
default = self: [];
|
||||
example = literalExample ''
|
||||
haskellPackages: [
|
||||
|
@ -21,6 +21,7 @@ in
|
||||
};
|
||||
|
||||
extraPackages = mkOption {
|
||||
type = types.functionTo (types.listOf types.package);
|
||||
default = self: [];
|
||||
example = literalExample ''
|
||||
haskellPackages: [
|
||||
|
@ -66,6 +66,7 @@ in
|
||||
};
|
||||
|
||||
plugins = mkOption {
|
||||
type = types.functionTo (types.listOf types.package);
|
||||
default = plugins: [];
|
||||
defaultText = "plugins: []";
|
||||
example = literalExample "plugins: with plugins; [ themeify stlviewer ]";
|
||||
|
@ -77,6 +77,7 @@ in {
|
||||
'';
|
||||
};
|
||||
extraPackages = mkOption {
|
||||
type = types.functionTo (types.listOf types.package);
|
||||
default = ps: [];
|
||||
defaultText = "ps: []";
|
||||
example = literalExample ''
|
||||
|
@ -64,6 +64,7 @@ in
|
||||
};
|
||||
|
||||
extraPackages = mkOption {
|
||||
type = types.functionTo (types.listOf types.package);
|
||||
default = p: [];
|
||||
description = ''
|
||||
Extra Python packages available to supybot plugins. The
|
||||
|
@ -37,6 +37,7 @@ in
|
||||
description = "Enable an uncustomised exwm configuration.";
|
||||
};
|
||||
extraPackages = mkOption {
|
||||
type = types.functionTo (types.listOf types.package);
|
||||
default = self: [];
|
||||
example = literalExample ''
|
||||
epkgs: [
|
||||
|
@ -53,6 +53,7 @@ in {
|
||||
};
|
||||
|
||||
extraPackages = mkOption {
|
||||
type = types.functionTo (types.listOf types.package);
|
||||
default = self: [];
|
||||
defaultText = "self: []";
|
||||
example = literalExample ''
|
||||
|
Loading…
Reference in New Issue
Block a user