nixos,lib: move environment generation related copy-paste to lib

This commit is contained in:
Jan Malakhovski 2015-08-17 17:55:35 +00:00
parent c8ea6c07c6
commit 33ae3f2fe4
3 changed files with 20 additions and 29 deletions

View file

@ -6,7 +6,7 @@ with import ./attrsets.nix;
with import ./options.nix;
with import ./trivial.nix;
with import ./strings.nix;
with {inherit (import ./modules.nix) mergeDefinitions; };
with {inherit (import ./modules.nix) mergeDefinitions filterOverrides; };
rec {
@ -166,6 +166,23 @@ rec {
substSubModules = m: loaOf (elemType.substSubModules m);
};
# List or element of ...
loeOf = elemType: mkOptionType {
name = "element or list of ${elemType.name}s";
check = x: isList x || elemType.check x;
merge = loc: defs:
let
defs' = filterOverrides defs;
res = (head defs').value;
in
if isList res then concatLists (getValues defs')
else if lessThan 1 (length defs') then
throw "The option `${showOption loc}' is defined multiple times, in ${showFiles (getFiles defs)}."
else if !isString res then
throw "The option `${showOption loc}' does not have a string value, in ${showFiles (getFiles defs)}."
else res;
};
uniq = elemType: mkOptionType {
inherit (elemType) name check;
merge = mergeOneOption;

View file

@ -41,20 +41,7 @@ in
strings. The latter is concatenated, interspersed with colon
characters.
'';
type = types.attrsOf (mkOptionType {
name = "a string or a list of strings";
merge = loc: defs:
let
defs' = filterOverrides defs;
res = (head defs').value;
in
if isList res then concatLists (getValues defs')
else if lessThan 1 (length defs') then
throw "The option `${showOption loc}' is defined multiple times, in ${showFiles (getFiles defs)}."
else if !isString res then
throw "The option `${showOption loc}' does not have a string value, in ${showFiles (getFiles defs)}."
else res;
});
type = types.attrsOf (types.loeOf types.str);
apply = mapAttrs (n: v: if isList v then concatStringsSep ":" v else v);
};

View file

@ -23,20 +23,7 @@ in
strings. The latter is concatenated, interspersed with colon
characters.
'';
type = types.attrsOf (mkOptionType {
name = "a string or a list of strings";
merge = loc: defs:
let
defs' = filterOverrides defs;
res = (head defs').value;
in
if isList res then concatLists (getValues defs')
else if lessThan 1 (length defs') then
throw "The option `${showOption loc}' is defined multiple times, in ${showFiles (getFiles defs)}."
else if !isString res then
throw "The option `${showOption loc}' does not have a string value, in ${showFiles (getFiles defs)}."
else res;
});
type = types.attrsOf (types.loeOf types.str);
apply = mapAttrs (n: v: if isList v then concatStringsSep ":" v else v);
};