summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorSilvan Mosberger <contact@infinisil.com>2020-08-10 17:25:46 +0200
committerSilvan Mosberger <contact@infinisil.com>2020-08-14 22:49:04 +0200
commit42cf8130d76ac811f8d1c934f9b44976050cc90f (patch)
treea3a9f312fbf0768f59946585320382cdff831e71 /lib
parente0ded8f4ba1529fdde28ff027d370c883864fcc8 (diff)
downloadnixpkgs-42cf8130d76ac811f8d1c934f9b44976050cc90f.tar
nixpkgs-42cf8130d76ac811f8d1c934f9b44976050cc90f.tar.gz
nixpkgs-42cf8130d76ac811f8d1c934f9b44976050cc90f.tar.bz2
nixpkgs-42cf8130d76ac811f8d1c934f9b44976050cc90f.tar.lz
nixpkgs-42cf8130d76ac811f8d1c934f9b44976050cc90f.tar.xz
nixpkgs-42cf8130d76ac811f8d1c934f9b44976050cc90f.tar.zst
nixpkgs-42cf8130d76ac811f8d1c934f9b44976050cc90f.zip
lib/modules: Add syntactic sugar for config._module.freeformType
This introduces `freeformType` as a top-level module attribute, allowing
definitions like

  {
    freeformType = ...;
    options = ...;
    config = ...;
  }
Diffstat (limited to 'lib')
-rw-r--r--lib/modules.nix16
-rw-r--r--lib/tests/modules/freeform-attrsOf.nix2
-rw-r--r--lib/tests/modules/freeform-lazyAttrsOf.nix2
3 files changed, 12 insertions, 8 deletions
diff --git a/lib/modules.nix b/lib/modules.nix
index 1d2c4a1acbb..55a53b3909a 100644
--- a/lib/modules.nix
+++ b/lib/modules.nix
@@ -200,12 +200,16 @@ rec {
   /* Massage a module into canonical form, that is, a set consisting
      of ‘options’, ‘config’ and ‘imports’ attributes. */
   unifyModuleSyntax = file: key: m:
-    let addMeta = config: if m ? meta
-      then mkMerge [ config { meta = m.meta; } ]
-      else config;
+    let
+      addMeta = config: if m ? meta
+        then mkMerge [ config { meta = m.meta; } ]
+        else config;
+      addFreeformType = config: if m ? freeformType
+        then mkMerge [ config { _module.freeformType = m.freeformType; } ]
+        else config;
     in
     if m ? config || m ? options then
-      let badAttrs = removeAttrs m ["_file" "key" "disabledModules" "imports" "options" "config" "meta"]; in
+      let badAttrs = removeAttrs m ["_file" "key" "disabledModules" "imports" "options" "config" "meta" "freeformType"]; in
       if badAttrs != {} then
         throw "Module `${key}' has an unsupported attribute `${head (attrNames badAttrs)}'. This is caused by introducing a top-level `config' or `options' attribute. Add configuration attributes immediately on the top level instead, or move all of them (namely: ${toString (attrNames badAttrs)}) into the explicit `config' attribute."
       else
@@ -214,7 +218,7 @@ rec {
           disabledModules = m.disabledModules or [];
           imports = m.imports or [];
           options = m.options or {};
-          config = addMeta (m.config or {});
+          config = addFreeformType (addMeta (m.config or {}));
         }
     else
       { _file = m._file or file;
@@ -222,7 +226,7 @@ rec {
         disabledModules = m.disabledModules or [];
         imports = m.require or [] ++ m.imports or [];
         options = {};
-        config = addMeta (removeAttrs m ["_file" "key" "disabledModules" "require" "imports"]);
+        config = addFreeformType (addMeta (removeAttrs m ["_file" "key" "disabledModules" "require" "imports" "freeformType"]));
       };
 
   applyIfFunction = key: f: args@{ config, options, lib, ... }: if isFunction f then
diff --git a/lib/tests/modules/freeform-attrsOf.nix b/lib/tests/modules/freeform-attrsOf.nix
index 5dbf4a9d307..8cc577f38a6 100644
--- a/lib/tests/modules/freeform-attrsOf.nix
+++ b/lib/tests/modules/freeform-attrsOf.nix
@@ -1,3 +1,3 @@
 { lib, ... }: {
-  config._module.freeformType = with lib.types; attrsOf (either str (attrsOf str));
+  freeformType = with lib.types; attrsOf (either str (attrsOf str));
 }
diff --git a/lib/tests/modules/freeform-lazyAttrsOf.nix b/lib/tests/modules/freeform-lazyAttrsOf.nix
index 524efa6bd93..36d6c0b13fc 100644
--- a/lib/tests/modules/freeform-lazyAttrsOf.nix
+++ b/lib/tests/modules/freeform-lazyAttrsOf.nix
@@ -1,3 +1,3 @@
 { lib, ... }: {
-  config._module.freeformType = with lib.types; lazyAttrsOf (either str (lazyAttrsOf str));
+  freeformType = with lib.types; lazyAttrsOf (either str (lazyAttrsOf str));
 }