summary refs log tree commit diff
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2022-05-25 17:45:28 +0200
committerRobert Hensing <robert@roberthensing.nl>2022-06-14 23:01:27 +0200
commit19a069ab8b17834da249b5ecb507c2eabe76a3e3 (patch)
treee5984b60e5cc0b51be41663f6bc1f50e2db09fcd
parent781c2e0789f7f6b75454a8e986c675ad36e6ee36 (diff)
downloadnixpkgs-19a069ab8b17834da249b5ecb507c2eabe76a3e3.tar
nixpkgs-19a069ab8b17834da249b5ecb507c2eabe76a3e3.tar.gz
nixpkgs-19a069ab8b17834da249b5ecb507c2eabe76a3e3.tar.bz2
nixpkgs-19a069ab8b17834da249b5ecb507c2eabe76a3e3.tar.lz
nixpkgs-19a069ab8b17834da249b5ecb507c2eabe76a3e3.tar.xz
nixpkgs-19a069ab8b17834da249b5ecb507c2eabe76a3e3.tar.zst
nixpkgs-19a069ab8b17834da249b5ecb507c2eabe76a3e3.zip
lib.types: Add deferredModuleWith
-rw-r--r--lib/modules.nix4
-rw-r--r--lib/types.nix24
2 files changed, 25 insertions, 3 deletions
diff --git a/lib/modules.nix b/lib/modules.nix
index d8ae497fb2d..1e8ba3471dd 100644
--- a/lib/modules.nix
+++ b/lib/modules.nix
@@ -467,7 +467,9 @@ rec {
         disabledModules = m.disabledModules or [];
         imports = m.require or [] ++ m.imports or [];
         options = {};
-        config = addFreeformType (addMeta (removeAttrs m ["_file" "key" "disabledModules" "require" "imports" "freeformType"]));
+        config =
+          lib.throwIfNot (isAttrs m) "module ${file} (${key}) does not look like a module."
+          addFreeformType (addMeta (removeAttrs m ["_file" "key" "disabledModules" "require" "imports" "freeformType"]));
       };
 
   applyModuleArgsIfFunction = key: f: args@{ config, options, lib, ... }: if isFunction f then
diff --git a/lib/types.nix b/lib/types.nix
index f5d13ea10d2..68dfa5843de 100644
--- a/lib/types.nix
+++ b/lib/types.nix
@@ -540,11 +540,31 @@ rec {
     };
 
     # A module to be imported in some other part of the configuration.
-    deferredModule = mkOptionType {
+    deferredModule = deferredModuleWith { };
+
+    # A module to be imported in some other part of the configuration.
+    # `staticModules`' options will be added to the documentation, unlike
+    # options declared via `config`.
+    deferredModuleWith = attrs@{ staticModules ? [] }: mkOptionType {
       name = "deferredModule";
       description = "module";
       check = x: isAttrs x || isFunction x || path.check x;
-      merge = loc: defs: map (def: lib.setDefaultModuleLocation "${def.file}, via option ${showOption loc}" def.value) defs;
+      merge = loc: defs: staticModules ++ map (def: lib.setDefaultModuleLocation "${def.file}, via option ${showOption loc}" def.value) defs;
+      inherit (submoduleWith { modules = staticModules; })
+        getSubOptions
+        getSubModules;
+      substSubModules = m: deferredModuleWith (attrs // {
+        staticModules = m;
+      });
+      functor = defaultFunctor "deferredModuleWith" // {
+        type = types.deferredModuleWith;
+        payload = {
+          inherit staticModules;
+        };
+        binOp = lhs: rhs: {
+          staticModules = lhs.staticModules ++ rhs.staticModules;
+        };
+      };
     };
 
     # The type of a type!