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.
wip/yesman
Shea Levy 10 years ago committed by Nicolas B. Pierron
parent 4f5c6330c9
commit 1d62ad4746
  1. 33
      lib/modules.nix
  2. 7
      nixos/modules/security/pam.nix
  3. 4
      nixos/modules/services/misc/nixos-manual.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.

@ -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;

@ -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 {

Loading…
Cancel
Save