2006-09-25 10:07:59 +00:00
|
|
|
# Utility functions.
|
|
|
|
|
2006-11-27 16:58:08 +00:00
|
|
|
let
|
|
|
|
|
2007-01-29 14:53:23 +00:00
|
|
|
inherit (builtins)
|
|
|
|
head tail isList stringLength substring lessThan sub;
|
2006-11-27 16:58:08 +00:00
|
|
|
|
|
|
|
in
|
|
|
|
|
2006-09-25 10:07:59 +00:00
|
|
|
rec {
|
|
|
|
|
2007-10-27 17:54:20 +00:00
|
|
|
innerSumArgs = f : x : y : (if y == null then (f x)
|
|
|
|
else (innerSumArgs f (x // y)));
|
|
|
|
sumArgs = f : innerSumArgs f {};
|
|
|
|
|
2007-11-05 08:32:20 +00:00
|
|
|
innerPairMap = acc: f: l:
|
|
|
|
if l == [] then acc else
|
|
|
|
innerPairMap (acc ++ [(f (head l)(head (tail l)))])
|
|
|
|
f (tail (tail l));
|
|
|
|
pairMap = innerPairMap [];
|
|
|
|
|
2006-09-25 10:07:59 +00:00
|
|
|
# "Fold" a binary function `op' between successive elements of
|
|
|
|
# `list' with `nul' as the starting value, i.e., `fold op nul [x_1
|
|
|
|
# x_2 ... x_n] == op x_1 (op x_2 ... (op x_n nul))'. (This is
|
|
|
|
# Haskell's foldr).
|
|
|
|
fold = op: nul: list:
|
|
|
|
if list == []
|
|
|
|
then nul
|
2006-11-27 16:58:08 +00:00
|
|
|
else op (head list) (fold op nul (tail list));
|
2006-09-25 10:07:59 +00:00
|
|
|
|
|
|
|
|
2007-03-06 00:01:27 +00:00
|
|
|
# Concatenate a list of lists.
|
|
|
|
concatLists =
|
|
|
|
fold (x: y: x ++ y) [];
|
|
|
|
|
|
|
|
|
2006-09-25 10:07:59 +00:00
|
|
|
# Concatenate a list of strings.
|
|
|
|
concatStrings =
|
|
|
|
fold (x: y: x + y) "";
|
|
|
|
|
|
|
|
|
2006-10-12 10:53:16 +00:00
|
|
|
# Place an element between each element of a list, e.g.,
|
|
|
|
# `intersperse "," ["a" "b" "c"]' returns ["a" "," "b" "," "c"].
|
|
|
|
intersperse = separator: list:
|
2006-11-27 16:58:08 +00:00
|
|
|
if list == [] || tail list == []
|
2006-10-12 10:53:16 +00:00
|
|
|
then list
|
2006-11-27 16:58:08 +00:00
|
|
|
else [(head list) separator]
|
|
|
|
++ (intersperse separator (tail list));
|
2006-10-12 10:53:16 +00:00
|
|
|
|
|
|
|
|
2007-05-27 14:34:01 +00:00
|
|
|
concatStringsSep = separator: list:
|
|
|
|
concatStrings (intersperse separator list);
|
|
|
|
|
|
|
|
|
2006-09-25 10:07:59 +00:00
|
|
|
# Flatten the argument into a single list; that is, nested lists are
|
|
|
|
# spliced into the top-level lists. E.g., `flatten [1 [2 [3] 4] 5]
|
|
|
|
# == [1 2 3 4 5]' and `flatten 1 == [1]'.
|
|
|
|
flatten = x:
|
2006-11-27 16:58:08 +00:00
|
|
|
if isList x
|
2006-09-25 10:07:59 +00:00
|
|
|
then fold (x: y: (flatten x) ++ y) [] x
|
|
|
|
else [x];
|
|
|
|
|
|
|
|
|
|
|
|
# Return an attribute from nested attribute sets. For instance ["x"
|
|
|
|
# "y"] applied to some set e returns e.x.y, if it exists. The
|
|
|
|
# default value is returned otherwise.
|
|
|
|
getAttr = attrPath: default: e:
|
|
|
|
let {
|
2006-11-27 16:58:08 +00:00
|
|
|
attr = head attrPath;
|
2006-09-25 10:07:59 +00:00
|
|
|
body =
|
|
|
|
if attrPath == [] then e
|
|
|
|
else if builtins ? hasAttr && builtins.hasAttr attr e
|
2006-11-27 16:58:08 +00:00
|
|
|
then getAttr (tail attrPath) default (builtins.getAttr attr e)
|
2006-09-25 10:07:59 +00:00
|
|
|
else default;
|
|
|
|
};
|
2006-11-27 16:58:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Filter a list using a predicate; that is, return a list containing
|
|
|
|
# every element from `list' for which `pred' returns true.
|
|
|
|
filter = pred: list:
|
|
|
|
fold (x: y: if pred x then [x] ++ y else y) [] list;
|
|
|
|
|
|
|
|
|
2007-01-08 22:49:26 +00:00
|
|
|
# Return true if `list' has an element `x':
|
|
|
|
elem = x: list: fold (a: bs: x == a || bs) false list;
|
|
|
|
|
|
|
|
|
2006-12-21 00:09:40 +00:00
|
|
|
# Find the sole element in the list matching the specified
|
2007-10-03 13:26:24 +00:00
|
|
|
# predicate, returns `default' if no such element exists, or
|
|
|
|
# `multiple' if there are multiple matching elements.
|
|
|
|
findSingle = pred: default: multiple: list:
|
2006-12-21 00:09:40 +00:00
|
|
|
let found = filter pred list;
|
|
|
|
in if found == [] then default
|
2007-10-03 13:26:24 +00:00
|
|
|
else if tail found != [] then multiple
|
2006-12-21 00:09:40 +00:00
|
|
|
else head found;
|
|
|
|
|
|
|
|
|
2006-11-27 16:58:08 +00:00
|
|
|
# Return true if each element of a list is equal, false otherwise.
|
|
|
|
eqLists = xs: ys:
|
|
|
|
if xs == [] && ys == [] then true
|
|
|
|
else if xs == [] || ys == [] then false
|
|
|
|
else head xs == head ys && eqLists (tail xs) (tail ys);
|
|
|
|
|
2007-10-29 10:52:04 +00:00
|
|
|
# Workaround, but works in stable Nix now.
|
|
|
|
eqStrings = a: b: (a+(substring 0 0 b)) == ((substring 0 0 a)+b);
|
2007-01-29 14:53:23 +00:00
|
|
|
|
|
|
|
# Determine whether a filename ends in the given suffix.
|
|
|
|
hasSuffix = ext: fileName:
|
|
|
|
let lenFileName = stringLength fileName;
|
|
|
|
lenExt = stringLength ext;
|
|
|
|
in !(lessThan lenFileName lenExt) &&
|
|
|
|
substring (sub lenFileName lenExt) lenFileName fileName == ext;
|
|
|
|
|
2007-10-29 10:52:04 +00:00
|
|
|
hasSuffixHack = a: b: hasSuffix (a+(substring 0 0 b)) ((substring 0 0 a)+b);
|
2007-01-29 14:53:23 +00:00
|
|
|
|
|
|
|
# Bring in a path as a source, filtering out all Subversion and CVS
|
|
|
|
# directories, as well as backup files (*~).
|
2007-01-15 09:20:18 +00:00
|
|
|
cleanSource =
|
2007-01-29 14:53:23 +00:00
|
|
|
let filter = name: type: let baseName = baseNameOf (toString name); in ! (
|
|
|
|
# Filter out Subversion and CVS directories.
|
|
|
|
(type == "directory" && (name == ".svn" || name == "CVS")) ||
|
|
|
|
# Filter out backup files.
|
|
|
|
(hasSuffix "~" name)
|
|
|
|
);
|
2007-01-15 09:20:18 +00:00
|
|
|
in src: builtins.filterSource filter src;
|
|
|
|
|
2007-05-20 20:24:43 +00:00
|
|
|
|
|
|
|
# Return a singleton list or an empty list, depending on a boolean
|
|
|
|
# value. Useful when building lists with optional elements
|
|
|
|
# (e.g. `++ optional (system == "i686-linux") flashplayer').
|
|
|
|
optional = cond: elem: if cond then [elem] else [];
|
|
|
|
|
2007-06-09 19:45:55 +00:00
|
|
|
|
|
|
|
# Return a list of integers from `first' up to and including `last'.
|
|
|
|
range = first: last:
|
|
|
|
if builtins.lessThan last first
|
|
|
|
then []
|
|
|
|
else [first] ++ range (builtins.add first 1) last;
|
|
|
|
|
2007-09-11 20:05:54 +00:00
|
|
|
|
2007-08-11 10:34:07 +00:00
|
|
|
# Return true only if there is an attribute and it is true.
|
|
|
|
checkFlag = attrSet: name:
|
|
|
|
if (name == "true") then true else
|
2007-08-11 11:14:36 +00:00
|
|
|
if (name == "false") then false else
|
2007-08-15 20:54:11 +00:00
|
|
|
if (isInList (getAttr ["flags"] [] attrSet) name) then true else
|
2007-08-11 10:34:07 +00:00
|
|
|
getAttr [name] false attrSet ;
|
|
|
|
|
2007-09-11 20:05:54 +00:00
|
|
|
|
2007-08-11 10:34:07 +00:00
|
|
|
logicalOR = x: y: x || y;
|
|
|
|
logicalAND = x: y: x && y;
|
|
|
|
|
2007-09-11 20:05:54 +00:00
|
|
|
|
2007-08-11 10:34:07 +00:00
|
|
|
# Input : attrSet, [ [name default] ... ], name
|
|
|
|
# Output : its value or default.
|
|
|
|
getValue = attrSet: argList: name:
|
2007-08-15 20:54:11 +00:00
|
|
|
( getAttr [name] (if checkFlag attrSet name then true else
|
|
|
|
if argList == [] then null else
|
2007-08-11 10:34:07 +00:00
|
|
|
let x = builtins.head argList; in
|
|
|
|
if (head x) == name then
|
|
|
|
(head (tail x))
|
|
|
|
else (getValue attrSet
|
|
|
|
(tail argList) name)) attrSet );
|
|
|
|
|
2007-09-11 20:05:54 +00:00
|
|
|
|
2007-08-11 10:34:07 +00:00
|
|
|
# Input : attrSet, [[name default] ...], [ [flagname reqs..] ... ]
|
|
|
|
# Output : are reqs satisfied? It's asserted.
|
|
|
|
checkReqs = attrSet : argList : condList :
|
|
|
|
(
|
|
|
|
fold logicalAND true
|
|
|
|
(map (x: let name = (head x) ; in
|
|
|
|
|
|
|
|
((checkFlag attrSet name) ->
|
|
|
|
(fold logicalAND true
|
|
|
|
(map (y: let val=(getValue attrSet argList y); in
|
|
|
|
(val!=null) && (val!=false))
|
|
|
|
(tail x))))) condList)) ;
|
|
|
|
|
|
|
|
|
|
|
|
isInList = list: x:
|
|
|
|
if (list == []) then false else
|
|
|
|
if (x == (head list)) then true else
|
|
|
|
isInList (tail list) x;
|
2007-09-11 20:05:54 +00:00
|
|
|
|
|
|
|
|
2007-08-11 11:14:36 +00:00
|
|
|
uniqList = {inputList, outputList ? []}:
|
|
|
|
if (inputList == []) then outputList else
|
|
|
|
let x=head inputList;
|
|
|
|
newOutputList = outputList ++
|
|
|
|
(if (isInList outputList x) then [] else [x]);
|
|
|
|
in uniqList {outputList=newOutputList;
|
|
|
|
inputList = (tail inputList);};
|
|
|
|
|
2007-09-11 20:05:54 +00:00
|
|
|
|
2007-08-15 21:17:11 +00:00
|
|
|
condConcat = name: list: checker:
|
|
|
|
if list == [] then name else
|
|
|
|
if checker (head list) then
|
|
|
|
condConcat
|
|
|
|
(name + (head (tail list)))
|
|
|
|
(tail (tail list))
|
|
|
|
checker
|
|
|
|
else condConcat
|
|
|
|
name (tail (tail list)) checker;
|
|
|
|
|
2007-11-09 18:05:32 +00:00
|
|
|
|
|
|
|
/* Options. */
|
|
|
|
|
|
|
|
mkOption = attrs: attrs // {_type = "option";};
|
|
|
|
|
|
|
|
typeOf = x: if x ? _type then x._type else "";
|
|
|
|
|
2007-11-09 18:07:14 +00:00
|
|
|
addDefaultOptionValues = defs: opts: opts //
|
2007-11-09 18:05:32 +00:00
|
|
|
builtins.listToAttrs (map (defName:
|
|
|
|
{ name = defName;
|
|
|
|
value =
|
|
|
|
let
|
|
|
|
defValue = builtins.getAttr defName defs;
|
|
|
|
optValue = builtins.getAttr defName opts;
|
|
|
|
in
|
|
|
|
if typeOf defValue == "option"
|
|
|
|
then
|
|
|
|
# `defValue' is an option.
|
|
|
|
if builtins.hasAttr defName opts
|
|
|
|
then builtins.getAttr defName opts
|
|
|
|
else defValue.default
|
|
|
|
else
|
|
|
|
# `defValue' is an attribute set containing options.
|
|
|
|
# So recurse.
|
|
|
|
if builtins.hasAttr defName opts && builtins.isAttrs optValue
|
2007-11-09 18:07:14 +00:00
|
|
|
then addDefaultOptionValues defValue optValue
|
|
|
|
else addDefaultOptionValues defValue {};
|
2007-11-09 18:05:32 +00:00
|
|
|
}
|
|
|
|
) (builtins.attrNames defs));
|
2007-11-11 09:27:52 +00:00
|
|
|
|
|
|
|
optionAttrSetToDocList = (l: attrs:
|
|
|
|
(if (getAttr ["_type"] "" attrs) == "option" then
|
|
|
|
[({
|
|
|
|
inherit (attrs) description;
|
|
|
|
}
|
|
|
|
//(if attrs ? example then {inherit(attrs) example;} else {} )
|
|
|
|
//(if attrs ? default then {inherit(attrs) default;} else {} )
|
|
|
|
//{name = l;}
|
|
|
|
)]
|
|
|
|
else (concatLists (map (s: (optionAttrSetToDocList
|
|
|
|
(l + (if l=="" then "" else ".") + s) (builtins.getAttr s attrs)))
|
|
|
|
(builtins.attrNames attrs)))));
|
2007-11-09 18:05:32 +00:00
|
|
|
|
2007-11-12 16:42:13 +00:00
|
|
|
innerModifySumArgs = f: x: a: b: if b == null then (f a b) // x else
|
|
|
|
innerModifySumArgs f x (a // b);
|
|
|
|
modifySumArgs = f: x: innerModifySumArgs f x {};
|
|
|
|
|
2007-11-22 20:26:00 +00:00
|
|
|
debugVal = if builtins ? trace then x: (builtins.trace x x) else x: x;
|
|
|
|
debugXMLVal = if builtins ? trace then x: (builtins.trace (builtins.toXML x) x) else x: x;
|
|
|
|
|
2007-12-01 16:20:23 +00:00
|
|
|
innerClosePropagation = ready: list: if list == [] then ready else
|
|
|
|
if (head list) ? propagatedBuildInputs then
|
|
|
|
innerClosePropagation (ready ++ [(head list)])
|
|
|
|
((head list).propagatedBuildInputs ++ (tail list)) else
|
|
|
|
innerClosePropagation (ready ++ [(head list)]) (tail list);
|
|
|
|
|
|
|
|
closePropagation = list: (uniqList {inputList = (innerClosePropagation [] list);});
|
|
|
|
|
2006-11-27 16:58:08 +00:00
|
|
|
}
|