From 1d62ad474694c0717017c2c8aa79909a890407b5 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Mon, 5 May 2014 15:18:53 -0400 Subject: [PATCH] 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. --- lib/modules.nix | 33 ++++++++++++++++++-- nixos/modules/security/pam.nix | 7 +++-- nixos/modules/services/misc/nixos-manual.nix | 4 +-- 3 files changed, 36 insertions(+), 8 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. diff --git a/nixos/modules/security/pam.nix b/nixos/modules/security/pam.nix index e81278a95d5..631e8317cb4 100644 --- a/nixos/modules/security/pam.nix +++ b/nixos/modules/security/pam.nix @@ -6,8 +6,9 @@ with lib; let + parentConfig = config; - pamOpts = args: { + pamOpts = { config, name, ... }: let cfg = config; in let config = parentConfig; in { options = { @@ -180,8 +181,8 @@ let }; - config = let cfg = args.config; in { - name = mkDefault args.name; + config = { + name = mkDefault name; setLoginUid = mkDefault cfg.startSession; limits = mkDefault config.security.pam.loginLimits; diff --git a/nixos/modules/services/misc/nixos-manual.nix b/nixos/modules/services/misc/nixos-manual.nix index c0d7885280a..72923f2b56a 100644 --- a/nixos/modules/services/misc/nixos-manual.nix +++ b/nixos/modules/services/misc/nixos-manual.nix @@ -3,7 +3,7 @@ # of the virtual consoles. The latter is useful for the installation # CD. -{ config, lib, pkgs, baseModules, ... } @ extraArgs: +{ config, lib, pkgs, baseModules, ... }: with lib; @@ -18,7 +18,7 @@ let eval = evalModules { modules = [ versionModule ] ++ baseModules; - args = (removeAttrs extraArgs ["config" "options"]) // { modules = [ ]; }; + args = (config.__internal.args) // { modules = [ ]; }; }; manual = import ../../../doc/manual {