2009-07-13 16:18:52 +00: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 13:36:30 +00:00
|
|
|
with import ./options.nix;
|
2009-07-13 16:18:52 +00: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 13:36:30 +00:00
|
|
|
isModule = m:
|
|
|
|
(m ? config && isAttrs m.config && ! isOption m.config)
|
|
|
|
|| (m ? options && isAttrs m.options && ! isOption m.options);
|
|
|
|
|
2009-09-14 13:19:00 +00:00
|
|
|
# Convert module to a set which has imports / options and config
|
|
|
|
# attributes.
|
|
|
|
unifyModuleSyntax = m:
|
2009-07-13 16:18:52 +00: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 13:19:00 +00:00
|
|
|
getConfig = m:
|
|
|
|
removeAttrs (delayProperties m) ["require"];
|
|
|
|
in
|
2009-09-15 13:36:30 +00:00
|
|
|
if isModule m then
|
2009-09-18 15:18:23 +00:00
|
|
|
{ key = "<unknown location>"; } // m
|
2009-09-14 13:19:00 +00:00
|
|
|
else
|
|
|
|
{
|
2009-09-18 15:18:23 +00:00
|
|
|
key = "<unknown location>";
|
2009-09-14 13:19:00 +00:00
|
|
|
imports = getImportedPaths m;
|
|
|
|
config = getConfig m;
|
|
|
|
} // (
|
|
|
|
if getImportedSets m != [] then
|
|
|
|
assert tail (getImportedSets m) == [];
|
|
|
|
{ options = head (getImportedSets m); }
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
2009-10-09 18:11:24 +00:00
|
|
|
|
|
|
|
unifyOptionModule = {key ? "<unknown location>"}: m: (args:
|
|
|
|
let module = lib.applyIfFunction m args; in
|
|
|
|
if lib.isModule module then
|
|
|
|
{ inherit key; } // module
|
|
|
|
else
|
|
|
|
{ inherit key; options = module; }
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2009-09-14 13:19:00 +00:00
|
|
|
moduleClosure = initModules: args:
|
|
|
|
let
|
2009-09-15 00:21:39 +00:00
|
|
|
moduleImport = m:
|
|
|
|
(unifyModuleSyntax (applyIfFunction (importIfPath m) args)) // {
|
2009-09-14 13:19:00 +00:00
|
|
|
# used by generic closure to avoid duplicated imports.
|
2009-09-18 15:18:23 +00:00
|
|
|
key = if isPath m then m else "<unknown location>";
|
2009-09-15 00:21:39 +00:00
|
|
|
};
|
2009-09-14 13:19:00 +00:00
|
|
|
|
|
|
|
getImports = m: attrByPath ["imports"] [] m;
|
2009-09-15 00:21:39 +00:00
|
|
|
|
2009-07-13 16:18:52 +00:00
|
|
|
in
|
2009-09-15 00:21:39 +00:00
|
|
|
(lazyGenericClosure {
|
|
|
|
startSet = map moduleImport (filter isPath initModules);
|
2009-09-14 13:19:00 +00:00
|
|
|
operator = m: map moduleImport (getImports m);
|
2009-09-15 00:21:39 +00:00
|
|
|
}) ++ (map moduleImport (filter (m: ! isPath m) initModules));
|
2009-07-13 16:18:52 +00:00
|
|
|
|
|
|
|
selectDeclsAndDefs = modules:
|
|
|
|
lib.concatMap (m:
|
|
|
|
if m ? config || m ? options then
|
2009-07-14 16:22:42 +00:00
|
|
|
[ (attrByPath ["options"] {} m) ]
|
|
|
|
++ [ (attrByPath ["config"] {} m) ]
|
2009-07-13 16:18:52 +00:00
|
|
|
else
|
|
|
|
[ m ]
|
|
|
|
) modules;
|
|
|
|
|
2009-09-15 13:36:30 +00: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;
|
|
|
|
|
2009-10-09 23:03:24 +00:00
|
|
|
|
2009-09-15 13:36:30 +00:00
|
|
|
delayModule = module:
|
|
|
|
moduleApply { config = delayProperties; } module;
|
|
|
|
|
2009-10-09 23:03:24 +00:00
|
|
|
evalDefinitions = opt: values:
|
2009-10-12 15:16:22 +00:00
|
|
|
if opt ? type && opt.type.delayOnGlobalEval then
|
2009-10-12 13:37:00 +00:00
|
|
|
map (delayPropertiesWithIter opt.type.iter opt.name)
|
|
|
|
(evalLocalProperties values)
|
2009-10-09 23:03:24 +00:00
|
|
|
else
|
|
|
|
evalProperties values;
|
|
|
|
|
|
|
|
|
2009-09-15 13:36:30 +00:00
|
|
|
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:
|
2009-10-12 15:50:59 +00:00
|
|
|
fun (catAttrs name modules)
|
2009-09-15 13:36:30 +00:00
|
|
|
) 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 11:37:07 +00:00
|
|
|
"The error may come from the following files:\n" + (
|
2009-09-15 13:36:30 +00:00
|
|
|
lib.concatStringsSep "\n" (
|
|
|
|
map (m:
|
2009-09-16 11:37:07 +00:00
|
|
|
if m ? key then toString m.key else "<unknown location>"
|
2009-09-15 13:36:30 +00: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 15:10:05 +00:00
|
|
|
let
|
2009-10-09 18:11:24 +00:00
|
|
|
decls = # add location to sub-module options.
|
2009-09-18 15:10:05 +00:00
|
|
|
map (m:
|
2009-10-09 18:11:24 +00:00
|
|
|
mapSubOptions
|
|
|
|
(unifyOptionModule {inherit (m) key;})
|
|
|
|
m.options
|
2009-09-18 15:10:05 +00:00
|
|
|
) (declarationsOf name);
|
|
|
|
in
|
|
|
|
addOptionMakeUp
|
|
|
|
{ name = addName name; recurseInto = recurseForOption; }
|
|
|
|
(mergeOptionDecls decls)
|
2009-09-18 15:10:11 +00:00
|
|
|
// {
|
|
|
|
declarations =
|
|
|
|
map (m: {
|
|
|
|
source = m.key;
|
|
|
|
}) (declarationsOf name);
|
|
|
|
|
|
|
|
definitions =
|
|
|
|
map (m: {
|
|
|
|
source = m.key;
|
2009-09-28 18:22:55 +00:00
|
|
|
value = m.config;
|
2009-09-18 15:10:11 +00:00
|
|
|
}) (definitionsOf name);
|
2009-09-26 23:01:35 +00:00
|
|
|
|
|
|
|
config = builtins.tryEval
|
2009-09-28 18:22:31 +00:00
|
|
|
(builtins.toXML (lib.getAttr name result.config));
|
2009-09-18 15:10:11 +00:00
|
|
|
}
|
2009-09-15 13:36:30 +00: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 18:22:49 +00:00
|
|
|
config = lib.zipWithNames (modulesNames modules) (name: values_:
|
2009-09-15 13:36:30 +00:00
|
|
|
let
|
|
|
|
hasOpt = builtins.hasAttr name result.options;
|
|
|
|
opt = lib.getAttr name result.options;
|
2009-09-28 18:22:49 +00:00
|
|
|
values = values_ ++
|
|
|
|
optionals
|
|
|
|
(hasOpt && isOption opt && opt ? extraConfigs)
|
|
|
|
opt.extraConfigs;
|
2009-09-15 13:36:30 +00:00
|
|
|
|
|
|
|
in if hasOpt && isOption opt then
|
2009-10-09 23:03:24 +00:00
|
|
|
let defs = evalDefinitions opt values; in
|
2009-09-15 13:36:30 +00:00
|
|
|
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 18:22:24 +00:00
|
|
|
moduleClosure initModules (module // args)
|
2009-09-15 13:36:30 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2009-09-14 20:10:41 +00:00
|
|
|
}
|