summary refs log tree commit diff
path: root/lib/modules.nix
diff options
context:
space:
mode:
authorShea Levy <shea@shealevy.com>2014-05-05 15:18:53 -0400
committerNicolas B. Pierron <nicolas.b.pierron@gmail.com>2015-03-12 23:42:57 +0100
commit1d62ad474694c0717017c2c8aa79909a890407b5 (patch)
treef541c744e6e3747155bb2b5bd25491d634cc6abd /lib/modules.nix
parent4f5c6330c9f3df2533daf33ecaf0c52420979674 (diff)
downloadnixpkgs-1d62ad474694c0717017c2c8aa79909a890407b5.tar
nixpkgs-1d62ad474694c0717017c2c8aa79909a890407b5.tar.gz
nixpkgs-1d62ad474694c0717017c2c8aa79909a890407b5.tar.bz2
nixpkgs-1d62ad474694c0717017c2c8aa79909a890407b5.tar.lz
nixpkgs-1d62ad474694c0717017c2c8aa79909a890407b5.tar.xz
nixpkgs-1d62ad474694c0717017c2c8aa79909a890407b5.tar.zst
nixpkgs-1d62ad474694c0717017c2c8aa79909a890407b5.zip
modules.nix: Generate the extra argument set from the configuration
This allows for module arguments to be handled modularly, in particular
allowing the nixpkgs module to handle the nixpkgs import internally.
This creates the __internal option namespace, which should only be added
to by the module system itself.
Diffstat (limited to 'lib/modules.nix')
-rw-r--r--lib/modules.nix33
1 files changed, 30 insertions, 3 deletions
diff --git a/lib/modules.nix b/lib/modules.nix
index 8bf8016b431..b514544c1e0 100644
--- a/lib/modules.nix
+++ b/lib/modules.nix
@@ -12,8 +12,26 @@ rec {
      and ‘config’: the nested set of all option values. */
   evalModules = { modules, prefix ? [], args ? {}, check ? true }:
     let
-      args' = args // { lib = import ./.; } // result;
-      closed = closeModules modules args';
+      internalModule = {
+        _file = ./modules.nix;
+
+        key = ./modules.nix;
+
+        options = {
+          __internal.args = mkOption {
+            description = "Arguments passed to each module.";
+
+            type = types.attrsOf types.unspecified;
+
+            internal = true;
+          };
+        };
+
+        config = {
+          __internal.args = args;
+        };
+      };
+      closed = closeModules (modules ++ [ internalModule ]) { inherit config options; lib = import ./.; };
       # Note: the list of modules is reversed to maintain backward
       # compatibility with the old module system.  Not sure if this is
       # the most sensible policy.
@@ -74,7 +92,16 @@ rec {
         config = removeAttrs m ["key" "_file" "require" "imports"];
       };
 
-  applyIfFunction = f: arg: if isFunction f then f arg else f;
+  applyIfFunction = f: arg@{ config, options, lib }: if isFunction f then
+    let
+      requiredArgs = builtins.attrNames (builtins.functionArgs f);
+      extraArgs = builtins.listToAttrs (map (name: {
+        inherit name;
+        value = config.__internal.args.${name};
+      }) requiredArgs);
+    in f (extraArgs // arg)
+  else
+    f;
 
   /* Merge a list of modules.  This will recurse over the option
      declarations in all modules, combining them into a single set.