2009-07-13 18:18:52 +02:00
|
|
|
# NixOS module handling.
|
|
|
|
|
|
|
|
let lib = import ./default.nix; in
|
|
|
|
|
|
|
|
with { inherit (builtins) head tail; };
|
|
|
|
with import ./trivial.nix;
|
|
|
|
with import ./lists.nix;
|
|
|
|
with import ./misc.nix;
|
|
|
|
with import ./attrsets.nix;
|
2009-09-15 15:36:30 +02:00
|
|
|
with import ./options.nix;
|
2009-07-13 18:18:52 +02:00
|
|
|
with import ./properties.nix;
|
|
|
|
|
|
|
|
rec {
|
|
|
|
|
|
|
|
# Unfortunately this can also be a string.
|
|
|
|
isPath = x: !(
|
|
|
|
builtins.isFunction x
|
|
|
|
|| builtins.isAttrs x
|
|
|
|
|| builtins.isInt x
|
|
|
|
|| builtins.isBool x
|
|
|
|
|| builtins.isList x
|
|
|
|
);
|
|
|
|
|
|
|
|
importIfPath = path:
|
|
|
|
if isPath path then
|
|
|
|
import path
|
|
|
|
else
|
|
|
|
path;
|
|
|
|
|
|
|
|
applyIfFunction = f: arg:
|
|
|
|
if builtins.isFunction f then
|
|
|
|
f arg
|
|
|
|
else
|
|
|
|
f;
|
|
|
|
|
2009-09-15 15:36:30 +02:00
|
|
|
isModule = m:
|
|
|
|
(m ? config && isAttrs m.config && ! isOption m.config)
|
|
|
|
|| (m ? options && isAttrs m.options && ! isOption m.options);
|
|
|
|
|
2009-09-14 15:19:00 +02:00
|
|
|
# Convert module to a set which has imports / options and config
|
|
|
|
# attributes.
|
|
|
|
unifyModuleSyntax = m:
|
2009-07-13 18:18:52 +02:00
|
|
|
let
|
|
|
|
getImports = m:
|
|
|
|
if m ? config || m ? options then
|
|
|
|
attrByPath ["imports"] [] m
|
|
|
|
else
|
|
|
|
toList (rmProperties (attrByPath ["require"] [] (delayProperties m)));
|
|
|
|
|
|
|
|
getImportedPaths = m: filter isPath (getImports m);
|
|
|
|
getImportedSets = m: filter (x: !isPath x) (getImports m);
|
|
|
|
|
2009-09-14 15:19:00 +02:00
|
|
|
getConfig = m:
|
|
|
|
removeAttrs (delayProperties m) ["require"];
|
|
|
|
in
|
2009-09-15 15:36:30 +02:00
|
|
|
if isModule m then
|
2009-09-18 17:18:23 +02:00
|
|
|
{ key = "<unknown location>"; } // m
|
2009-09-14 15:19:00 +02:00
|
|
|
else
|
|
|
|
{
|
2009-09-18 17:18:23 +02:00
|
|
|
key = "<unknown location>";
|
2009-09-14 15:19:00 +02:00
|
|
|
imports = getImportedPaths m;
|
|
|
|
config = getConfig m;
|
|
|
|
} // (
|
|
|
|
if getImportedSets m != [] then
|
|
|
|
assert tail (getImportedSets m) == [];
|
|
|
|
{ options = head (getImportedSets m); }
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
|
|
|
moduleClosure = initModules: args:
|
|
|
|
let
|
2009-09-15 02:21:39 +02:00
|
|
|
moduleImport = m:
|
|
|
|
(unifyModuleSyntax (applyIfFunction (importIfPath m) args)) // {
|
2009-09-14 15:19:00 +02:00
|
|
|
# used by generic closure to avoid duplicated imports.
|
2009-09-18 17:18:23 +02:00
|
|
|
key = if isPath m then m else "<unknown location>";
|
2009-09-15 02:21:39 +02:00
|
|
|
};
|
2009-09-14 15:19:00 +02:00
|
|
|
|
|
|
|
getImports = m: attrByPath ["imports"] [] m;
|
2009-09-15 02:21:39 +02:00
|
|
|
|
2009-07-13 18:18:52 +02:00
|
|
|
in
|
2009-09-15 02:21:39 +02:00
|
|
|
(lazyGenericClosure {
|
|
|
|
startSet = map moduleImport (filter isPath initModules);
|
2009-09-14 15:19:00 +02:00
|
|
|
operator = m: map moduleImport (getImports m);
|
2009-09-15 02:21:39 +02:00
|
|
|
}) ++ (map moduleImport (filter (m: ! isPath m) initModules));
|
2009-07-13 18:18:52 +02:00
|
|
|
|
|
|
|
selectDeclsAndDefs = modules:
|
|
|
|
lib.concatMap (m:
|
|
|
|
if m ? config || m ? options then
|
2009-07-14 18:22:42 +02:00
|
|
|
[ (attrByPath ["options"] {} m) ]
|
|
|
|
++ [ (attrByPath ["config"] {} m) ]
|
2009-07-13 18:18:52 +02:00
|
|
|
else
|
|
|
|
[ m ]
|
|
|
|
) modules;
|
|
|
|
|
2009-09-15 15:36:30 +02:00
|
|
|
|
|
|
|
moduleApply = funs: module:
|
|
|
|
lib.mapAttrs (name: value:
|
|
|
|
if builtins.hasAttr name funs then
|
|
|
|
let fun = lib.getAttr name funs; in
|
|
|
|
fun value
|
|
|
|
else
|
|
|
|
value
|
|
|
|
) module;
|
|
|
|
|
|
|
|
delayModule = module:
|
|
|
|
moduleApply { config = delayProperties; } module;
|
|
|
|
|
|
|
|
selectModule = name: m:
|
|
|
|
{ inherit (m) key;
|
|
|
|
} // (
|
|
|
|
if m ? options && builtins.hasAttr name m.options then
|
|
|
|
{ options = lib.getAttr name m.options; }
|
|
|
|
else {}
|
|
|
|
) // (
|
|
|
|
if m ? config && builtins.hasAttr name m.config then
|
|
|
|
{ config = lib.getAttr name m.config; }
|
|
|
|
else {}
|
|
|
|
);
|
|
|
|
|
|
|
|
filterModules = name: modules:
|
|
|
|
filter (m: m ? config || m ? options) (
|
|
|
|
map (selectModule name) modules
|
|
|
|
);
|
|
|
|
|
|
|
|
modulesNames = modules:
|
|
|
|
lib.concatMap (m: []
|
|
|
|
++ optionals (m ? options) (lib.attrNames m.options)
|
|
|
|
++ optionals (m ? config) (lib.attrNames m.config)
|
|
|
|
) modules;
|
|
|
|
|
|
|
|
moduleZip = funs: modules:
|
|
|
|
lib.mapAttrs (name: fun:
|
|
|
|
fun (
|
|
|
|
map (lib.getAttr name) (
|
|
|
|
filter (builtins.hasAttr name) modules
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) funs;
|
|
|
|
|
|
|
|
moduleMerge = path: modules:
|
|
|
|
let modules_ = modules; in
|
|
|
|
let
|
|
|
|
addName = name:
|
|
|
|
if path == "" then name else path + "." + name;
|
|
|
|
|
|
|
|
modules = map delayModule modules_;
|
|
|
|
|
|
|
|
modulesOf = name: filterModules name modules;
|
|
|
|
declarationsOf = name: filter (m: m ? options) (modulesOf name);
|
|
|
|
definitionsOf = name: filter (m: m ? config ) (modulesOf name);
|
|
|
|
|
|
|
|
recurseInto = name: modules:
|
|
|
|
moduleMerge (addName name) (modulesOf name);
|
|
|
|
|
|
|
|
recurseForOption = name: modules:
|
|
|
|
moduleMerge name (
|
|
|
|
map unifyModuleSyntax modules
|
|
|
|
);
|
|
|
|
|
|
|
|
errorSource = modules:
|
2009-09-16 13:37:07 +02:00
|
|
|
"The error may come from the following files:\n" + (
|
2009-09-15 15:36:30 +02:00
|
|
|
lib.concatStringsSep "\n" (
|
|
|
|
map (m:
|
2009-09-16 13:37:07 +02:00
|
|
|
if m ? key then toString m.key else "<unknown location>"
|
2009-09-15 15:36:30 +02:00
|
|
|
) modules
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
eol = "\n";
|
|
|
|
|
|
|
|
errDefinedWithoutDeclaration = name:
|
|
|
|
let
|
|
|
|
badModules =
|
|
|
|
filter (m: ! isAttrs m.config)
|
|
|
|
(definitionsOf name);
|
|
|
|
in
|
|
|
|
"${eol
|
|
|
|
}Option '${addName name}' defined without option declaration.${eol
|
|
|
|
}${errorSource badModules}${eol
|
|
|
|
}";
|
|
|
|
|
|
|
|
endRecursion = { options = {}; config = {}; };
|
|
|
|
|
|
|
|
in if modules == [] then endRecursion else
|
|
|
|
|
|
|
|
lib.fix (result:
|
|
|
|
moduleZip {
|
|
|
|
options = lib.zip (name: values:
|
|
|
|
if any isOption values then
|
2009-09-18 17:10:05 +02:00
|
|
|
let
|
|
|
|
# locations to sub-options declarations
|
|
|
|
decls =
|
|
|
|
map (m:
|
|
|
|
mapSubOptions (subModule:
|
|
|
|
let module = lib.applyIfFunction subModule {}; in
|
|
|
|
if lib.isModule module then
|
|
|
|
{ inherit (m) key; } // subModule
|
|
|
|
else
|
|
|
|
args: {
|
|
|
|
inherit (m) key;
|
|
|
|
options = lib.applyIfFunction subModule args;
|
|
|
|
}
|
|
|
|
) m.options
|
|
|
|
) (declarationsOf name);
|
|
|
|
in
|
|
|
|
addOptionMakeUp
|
|
|
|
{ name = addName name; recurseInto = recurseForOption; }
|
|
|
|
(mergeOptionDecls decls)
|
2009-09-18 17:10:11 +02:00
|
|
|
// {
|
|
|
|
declarations =
|
|
|
|
map (m: {
|
|
|
|
source = m.key;
|
|
|
|
}) (declarationsOf name);
|
|
|
|
|
|
|
|
definitions =
|
|
|
|
map (m: {
|
|
|
|
source = m.key;
|
2009-09-28 20:22:55 +02:00
|
|
|
value = m.config;
|
2009-09-18 17:10:11 +02:00
|
|
|
}) (definitionsOf name);
|
2009-09-27 01:01:35 +02:00
|
|
|
|
|
|
|
config = builtins.tryEval
|
2009-09-28 20:22:31 +02:00
|
|
|
(builtins.toXML (lib.getAttr name result.config));
|
2009-09-18 17:10:11 +02:00
|
|
|
}
|
2009-09-15 15:36:30 +02:00
|
|
|
else if all isAttrs values then
|
|
|
|
(recurseInto name modules).options
|
|
|
|
else
|
|
|
|
throw "${eol
|
|
|
|
}Unexpected type where option declarations are expected.${eol
|
|
|
|
}${errorSource (declarationsOf name)}${eol
|
|
|
|
}"
|
|
|
|
);
|
|
|
|
|
2009-09-28 20:22:49 +02:00
|
|
|
config = lib.zipWithNames (modulesNames modules) (name: values_:
|
2009-09-15 15:36:30 +02:00
|
|
|
let
|
|
|
|
hasOpt = builtins.hasAttr name result.options;
|
|
|
|
opt = lib.getAttr name result.options;
|
2009-09-28 20:22:49 +02:00
|
|
|
values = values_ ++
|
|
|
|
optionals
|
|
|
|
(hasOpt && isOption opt && opt ? extraConfigs)
|
|
|
|
opt.extraConfigs;
|
2009-09-15 15:36:30 +02:00
|
|
|
|
|
|
|
in if hasOpt && isOption opt then
|
|
|
|
let defs = evalProperties values; in
|
|
|
|
lib.addErrorContext "${eol
|
|
|
|
}while evaluating the option '${addName name}'.${eol
|
|
|
|
}${errorSource (modulesOf name)}${eol
|
|
|
|
}" (
|
|
|
|
opt.apply (
|
|
|
|
if defs == [] then
|
|
|
|
if opt ? default then opt.default
|
|
|
|
else throw "Not defined."
|
|
|
|
else opt.merge defs
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
else if hasOpt && lib.attrNames opt == [] then
|
|
|
|
throw (errDefinedWithoutDeclaration name)
|
|
|
|
|
|
|
|
else if any (v: isOption (rmProperties v)) values then
|
|
|
|
let
|
|
|
|
badModules =
|
|
|
|
filter (m: isOption m.config)
|
|
|
|
(definitionsOf name);
|
|
|
|
in
|
|
|
|
throw "${eol
|
|
|
|
}Option ${addName name} is defined in the configuration section.${eol
|
|
|
|
}${errorSource badModules}${eol
|
|
|
|
}"
|
|
|
|
|
|
|
|
else if all isAttrs values then
|
|
|
|
(recurseInto name modules).config
|
|
|
|
else
|
|
|
|
throw (errDefinedWithoutDeclaration name)
|
|
|
|
);
|
|
|
|
|
|
|
|
} modules
|
|
|
|
);
|
|
|
|
|
|
|
|
fixMergeModules = initModules: {...}@args:
|
|
|
|
lib.fix (result:
|
|
|
|
# This trick avoid an infinite loop because names of attribute are
|
|
|
|
# know and it is not require to evaluate the result of moduleMerge to
|
|
|
|
# know which attribute are present as argument.
|
|
|
|
let module = { inherit (result) options config; }; in
|
|
|
|
|
|
|
|
moduleMerge "" (
|
2009-09-28 20:22:24 +02:00
|
|
|
moduleClosure initModules (module // args)
|
2009-09-15 15:36:30 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2009-09-14 22:10:41 +02:00
|
|
|
}
|