From b454af298d0511f43564c5ba2c2b9576bffc025a Mon Sep 17 00:00:00 2001 From: Bas van Dijk Date: Tue, 7 Aug 2018 18:34:38 +0200 Subject: [PATCH 1/5] Revert "Remove types.functionTo." This reverts commit 4ff1ab5a56f1280d2de319ad4eb4b2796e07ed35. We need this to type options like: services.xserver.windowManager.xmonad.extraPackages that specify functions that take an attribute set containing packages / plugins and return a list containing a selection of the values in this set. The reason we need a dedicated type for this is to have the correct merge behaviour. Without the functionTo type merging multiple function option definitions results in an evaluation error. The functionTo type merges definitions by returning a new function that applies the functions of all the definitions to the given input and merges the result. (cherry picked from commit 7ed41ff5e7e633dd64866398ee497ac02736a3a5) --- lib/types.nix | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/types.nix b/lib/types.nix index ee891f8231b6..0810a43ed0eb 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: elemType.merge loc (map (fn: { inherit (fn) file; value = fn.value fnArgs; }) defs); + 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; From 43243539b33d8fb0b2d14a3f9b79d6b641b74e2c Mon Sep 17 00:00:00 2001 From: Bas van Dijk Date: Tue, 7 Aug 2018 18:44:58 +0200 Subject: [PATCH 2/5] lib/tests/modules: add a test for the functionTo type (cherry picked from commit 478af112e83df806bd8a51174834d2a130fbdeb9) --- lib/tests/modules.sh | 3 +++ lib/tests/modules/functionTo.nix | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 lib/tests/modules/functionTo.nix diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index 309c5311361c..2d3a696e980a 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -262,6 +262,9 @@ 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 +# Check the merge behaviour of the functionTo type. +checkConfigOutput "a b" config.result ./functionTo.nix + cat < Date: Tue, 7 Aug 2018 18:53:56 +0200 Subject: [PATCH 3/5] nixos: use functionTo to prevent evaluation errors while merging Without this patch merging options like services.xserver.windowManager.xmonad.extraPackages results in the evaluation error: error: value is a list while a set was expected, at nixpkgs/lib/options.nix:77:23 With this patch we get the desired merging behaviour that just concatenates the resulting package lists. (cherry picked from commit 6e99f9fdecb1f28308c8e0aed0fc851737354864) Co-Authored-By: Silvan Mosberger --- nixos/modules/services/development/hoogle.nix | 1 + nixos/modules/services/misc/gitit.nix | 1 + nixos/modules/services/misc/ihaskell.nix | 1 + nixos/modules/services/misc/octoprint.nix | 1 + nixos/modules/services/monitoring/netdata.nix | 1 + nixos/modules/services/networking/supybot.nix | 1 + nixos/modules/services/x11/window-managers/exwm.nix | 1 + nixos/modules/services/x11/window-managers/xmonad.nix | 1 + 8 files changed, 8 insertions(+) diff --git a/nixos/modules/services/development/hoogle.nix b/nixos/modules/services/development/hoogle.nix index a661e3acae3e..6d6c88b9b2aa 100644 --- a/nixos/modules/services/development/hoogle.nix +++ b/nixos/modules/services/development/hoogle.nix @@ -25,6 +25,7 @@ in { }; packages = mkOption { + type = types.functionTo (types.listOf types.package); default = hp: []; defaultText = "hp: []"; example = "hp: with hp; [ text lens ]"; diff --git a/nixos/modules/services/misc/gitit.nix b/nixos/modules/services/misc/gitit.nix index 1ec030549f98..f09565283f3c 100644 --- a/nixos/modules/services/misc/gitit.nix +++ b/nixos/modules/services/misc/gitit.nix @@ -42,6 +42,7 @@ let }; extraPackages = mkOption { + type = types.functionTo (types.listOf types.package); default = self: []; example = literalExample '' haskellPackages: [ diff --git a/nixos/modules/services/misc/ihaskell.nix b/nixos/modules/services/misc/ihaskell.nix index 684a242d7385..c7332b87803a 100644 --- a/nixos/modules/services/misc/ihaskell.nix +++ b/nixos/modules/services/misc/ihaskell.nix @@ -21,6 +21,7 @@ in }; extraPackages = mkOption { + type = types.functionTo (types.listOf types.package); default = self: []; example = literalExample '' haskellPackages: [ diff --git a/nixos/modules/services/misc/octoprint.nix b/nixos/modules/services/misc/octoprint.nix index a69e65073050..5a64946f9f63 100644 --- a/nixos/modules/services/misc/octoprint.nix +++ b/nixos/modules/services/misc/octoprint.nix @@ -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 ]"; diff --git a/nixos/modules/services/monitoring/netdata.nix b/nixos/modules/services/monitoring/netdata.nix index db51fdbd2c61..d5b679097b30 100644 --- a/nixos/modules/services/monitoring/netdata.nix +++ b/nixos/modules/services/monitoring/netdata.nix @@ -77,6 +77,7 @@ in { ''; }; extraPackages = mkOption { + type = types.functionTo (types.listOf types.package); default = ps: []; defaultText = "ps: []"; example = literalExample '' diff --git a/nixos/modules/services/networking/supybot.nix b/nixos/modules/services/networking/supybot.nix index 864c3319c547..332c3ced06f0 100644 --- a/nixos/modules/services/networking/supybot.nix +++ b/nixos/modules/services/networking/supybot.nix @@ -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 diff --git a/nixos/modules/services/x11/window-managers/exwm.nix b/nixos/modules/services/x11/window-managers/exwm.nix index 3e97d28d83b5..0743f35c1d31 100644 --- a/nixos/modules/services/x11/window-managers/exwm.nix +++ b/nixos/modules/services/x11/window-managers/exwm.nix @@ -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: [ diff --git a/nixos/modules/services/x11/window-managers/xmonad.nix b/nixos/modules/services/x11/window-managers/xmonad.nix index 2bb4827be9d5..fe8ed3812511 100644 --- a/nixos/modules/services/x11/window-managers/xmonad.nix +++ b/nixos/modules/services/x11/window-managers/xmonad.nix @@ -53,6 +53,7 @@ in { }; extraPackages = mkOption { + type = types.functionTo (types.listOf types.package); default = self: []; defaultText = "self: []"; example = literalExample '' From e9101d4a3bc90dfaaa076d8ec00e5ed68e518f60 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Tue, 26 Jan 2021 02:02:12 +0100 Subject: [PATCH 4/5] lib/types: Improved functionTo merging Now type checks the resulting function values and allows mkMerge and co. Also indicates that the type check is done in the function body Co-Authored-By: Robert Hensing --- lib/types.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/types.nix b/lib/types.nix index 0810a43ed0eb..77245158d9f8 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -457,7 +457,7 @@ rec { name = "function that evaluates to a(n) ${elemType.name}"; check = isFunction; merge = loc: defs: - fnArgs: elemType.merge loc (map (fn: { inherit (fn) file; value = fn.value fnArgs; }) 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); From c2f3556dc75ee09890c2cfc867cdb10df23accf6 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Tue, 26 Jan 2021 22:30:07 +0100 Subject: [PATCH 5/5] lib/tests: More functionTo tests --- lib/tests/modules.sh | 8 +++-- lib/tests/modules/functionTo.nix | 29 ------------------- lib/tests/modules/functionTo/list-order.nix | 25 ++++++++++++++++ .../modules/functionTo/merging-attrs.nix | 27 +++++++++++++++++ lib/tests/modules/functionTo/merging-list.nix | 24 +++++++++++++++ lib/tests/modules/functionTo/trivial.nix | 17 +++++++++++ lib/tests/modules/functionTo/wrong-type.nix | 18 ++++++++++++ 7 files changed, 117 insertions(+), 31 deletions(-) delete mode 100644 lib/tests/modules/functionTo.nix create mode 100644 lib/tests/modules/functionTo/list-order.nix create mode 100644 lib/tests/modules/functionTo/merging-attrs.nix create mode 100644 lib/tests/modules/functionTo/merging-list.nix create mode 100644 lib/tests/modules/functionTo/trivial.nix create mode 100644 lib/tests/modules/functionTo/wrong-type.nix diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index 2d3a696e980a..f843d303e440 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -262,8 +262,12 @@ 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 -# Check the merge behaviour of the functionTo type. -checkConfigOutput "a b" config.result ./functionTo.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 <