mirror of
https://github.com/NixOS/nix.git
synced 2025-11-20 01:09:37 +01:00
Add poor man's module system
This commit is contained in:
parent
38b339d447
commit
13735a63b4
3 changed files with 52 additions and 1 deletions
|
|
@ -1,7 +1,8 @@
|
|||
corepkgs_FILES = \
|
||||
unpack-channel.nix \
|
||||
derivation.nix \
|
||||
fetchurl.nix
|
||||
fetchurl.nix \
|
||||
module.nix
|
||||
|
||||
$(foreach file,config.nix $(corepkgs_FILES),$(eval $(call install-data-in,$(d)/$(file),$(datadir)/nix/corepkgs)))
|
||||
|
||||
|
|
|
|||
46
corepkgs/module.nix
Normal file
46
corepkgs/module.nix
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
let
|
||||
|
||||
showPos = pos:
|
||||
if pos == null
|
||||
then "<unknown location>"
|
||||
else "${pos.file}:${toString pos.line}:${toString pos.column}";
|
||||
|
||||
getAnyPos = attrs:
|
||||
builtins.foldl' (prev: name: if prev == null then builtins.unsafeGetAttrPos name attrs else prev) null (builtins.attrNames attrs);
|
||||
|
||||
in
|
||||
|
||||
{ description ? null, extends ? [], options ? {}, config ? ({ config }: {}) } @ inArgs:
|
||||
|
||||
let thisModule = rec {
|
||||
type = "module";
|
||||
|
||||
_module = {
|
||||
inherit description extends options config;
|
||||
};
|
||||
|
||||
_allModules = [thisModule] ++ builtins.concatLists (map (mod: assert mod.type or "<untyped>" == "module"; mod._allModules) extends);
|
||||
|
||||
_allOptions = builtins.foldl' (xs: mod: xs // mod._module.options) {} _allModules;
|
||||
|
||||
_allConfigs = map (mod: mod._module.config { config = final; }) _allModules;
|
||||
|
||||
_allDefinitions = builtins.mapAttrs (name: value: map (x: x) (builtins.catAttrs name _allConfigs)) _allOptions;
|
||||
|
||||
final = builtins.mapAttrs
|
||||
(name: defs:
|
||||
if defs == []
|
||||
then
|
||||
_allOptions.${name}.default
|
||||
or (throw "Option '${name}' is not defined by module at ${showPos (getAnyPos inArgs)} and has no default value.")
|
||||
else
|
||||
# FIXME: support merge functions.
|
||||
if builtins.isList (builtins.head defs)
|
||||
then builtins.concatLists defs
|
||||
else
|
||||
if builtins.isAttrs (builtins.head defs)
|
||||
then builtins.foldl' (xs: ys: xs // ys) {} defs
|
||||
else builtins.head defs)
|
||||
_allDefinitions;
|
||||
|
||||
}; in thisModule
|
||||
|
|
@ -2416,6 +2416,10 @@ void EvalState::createBaseEnv()
|
|||
evalFile(path, v);
|
||||
addConstant("derivation", v);
|
||||
|
||||
path = canonPath(settings.nixDataDir + "/nix/corepkgs/module.nix", true);
|
||||
evalFile(path, v);
|
||||
addConstant("module", v);
|
||||
|
||||
/* Add a value containing the current Nix expression search path. */
|
||||
mkList(v, searchPath.size());
|
||||
int n = 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue