2006-09-25 12:07:59 +02:00
|
|
|
# Utility functions.
|
|
|
|
|
2006-11-27 17:58:08 +01:00
|
|
|
let
|
|
|
|
|
2007-01-29 15:53:23 +01:00
|
|
|
inherit (builtins)
|
|
|
|
head tail isList stringLength substring lessThan sub;
|
2006-11-27 17:58:08 +01:00
|
|
|
|
|
|
|
in
|
|
|
|
|
2006-09-25 12:07:59 +02:00
|
|
|
rec {
|
2007-11-17 15:05:55 +01:00
|
|
|
listOfListsToAttrs = ll : builtins.listToAttrs (map (l : { name = (head l); value = (head (tail l)); }) ll);
|
2006-09-25 12:07:59 +02:00
|
|
|
|
2008-01-15 01:55:21 +01:00
|
|
|
|
|
|
|
# Identity function.
|
|
|
|
id = x: x;
|
|
|
|
|
|
|
|
|
|
|
|
# !!! need documentation...
|
2007-10-27 19:54:20 +02:00
|
|
|
innerSumArgs = f : x : y : (if y == null then (f x)
|
|
|
|
else (innerSumArgs f (x // y)));
|
|
|
|
sumArgs = f : innerSumArgs f {};
|
|
|
|
|
2007-11-05 09:32:20 +01:00
|
|
|
innerPairMap = acc: f: l:
|
|
|
|
if l == [] then acc else
|
|
|
|
innerPairMap (acc ++ [(f (head l)(head (tail l)))])
|
|
|
|
f (tail (tail l));
|
|
|
|
pairMap = innerPairMap [];
|
|
|
|
|
2008-01-15 01:55:21 +01:00
|
|
|
|
2006-09-25 12:07:59 +02: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 17:58:08 +01:00
|
|
|
else op (head list) (fold op nul (tail list));
|
2006-09-25 12:07:59 +02:00
|
|
|
|
|
|
|
|
2007-03-06 01:01:27 +01:00
|
|
|
# Concatenate a list of lists.
|
|
|
|
concatLists =
|
|
|
|
fold (x: y: x ++ y) [];
|
|
|
|
|
|
|
|
|
2006-09-25 12:07:59 +02:00
|
|
|
# Concatenate a list of strings.
|
|
|
|
concatStrings =
|
|
|
|
fold (x: y: x + y) "";
|
|
|
|
|
|
|
|
|
2006-10-12 12:53:16 +02: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 17:58:08 +01:00
|
|
|
if list == [] || tail list == []
|
2006-10-12 12:53:16 +02:00
|
|
|
then list
|
2006-11-27 17:58:08 +01:00
|
|
|
else [(head list) separator]
|
|
|
|
++ (intersperse separator (tail list));
|
2006-10-12 12:53:16 +02:00
|
|
|
|
|
|
|
|
2007-05-27 16:34:01 +02:00
|
|
|
concatStringsSep = separator: list:
|
|
|
|
concatStrings (intersperse separator list);
|
|
|
|
|
|
|
|
|
2006-09-25 12:07:59 +02: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 17:58:08 +01:00
|
|
|
if isList x
|
2006-09-25 12:07:59 +02: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:
|
2008-01-15 01:55:21 +01:00
|
|
|
let attr = head attrPath;
|
|
|
|
in
|
|
|
|
if attrPath == [] then e
|
|
|
|
else if builtins ? hasAttr && builtins.hasAttr attr e
|
|
|
|
then getAttr (tail attrPath) default (builtins.getAttr attr e)
|
|
|
|
else default;
|
2006-11-27 17:58:08 +01: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 23:49:26 +01:00
|
|
|
# Return true if `list' has an element `x':
|
|
|
|
elem = x: list: fold (a: bs: x == a || bs) false list;
|
|
|
|
|
|
|
|
|
2006-12-21 01:09:40 +01:00
|
|
|
# Find the sole element in the list matching the specified
|
2007-10-03 15:26:24 +02: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 01:09:40 +01:00
|
|
|
let found = filter pred list;
|
|
|
|
in if found == [] then default
|
2007-10-03 15:26:24 +02:00
|
|
|
else if tail found != [] then multiple
|
2006-12-21 01:09:40 +01:00
|
|
|
else head found;
|
|
|
|
|
|
|
|
|
2008-01-15 01:55:21 +01:00
|
|
|
# Return true iff function `pred' returns true for at least element
|
|
|
|
# of `list'.
|
|
|
|
any = pred: list:
|
|
|
|
if list == [] then false
|
|
|
|
else if pred (head list) then true
|
|
|
|
else any pred (tail list);
|
|
|
|
|
|
|
|
|
|
|
|
# Return true iff function `pred' returns true for all elements of
|
|
|
|
# `list'.
|
|
|
|
all = pred: list:
|
|
|
|
if list == [] then true
|
|
|
|
else if pred (head list) then all pred (tail list)
|
|
|
|
else false;
|
|
|
|
|
|
|
|
|
2006-11-27 17:58:08 +01: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);
|
|
|
|
|
2008-01-15 01:55:21 +01:00
|
|
|
|
2007-10-29 11:52:04 +01:00
|
|
|
# Workaround, but works in stable Nix now.
|
|
|
|
eqStrings = a: b: (a+(substring 0 0 b)) == ((substring 0 0 a)+b);
|
2007-01-29 15:53:23 +01:00
|
|
|
|
2008-01-15 01:55:21 +01:00
|
|
|
|
2007-01-29 15:53:23 +01: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 11:52:04 +01:00
|
|
|
hasSuffixHack = a: b: hasSuffix (a+(substring 0 0 b)) ((substring 0 0 a)+b);
|
2007-01-29 15:53:23 +01:00
|
|
|
|
|
|
|
# Bring in a path as a source, filtering out all Subversion and CVS
|
|
|
|
# directories, as well as backup files (*~).
|
2007-01-15 10:20:18 +01:00
|
|
|
cleanSource =
|
2007-01-29 15:53:23 +01: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 10:20:18 +01:00
|
|
|
in src: builtins.filterSource filter src;
|
|
|
|
|
2007-05-20 22:24:43 +02:00
|
|
|
|
2008-01-15 01:55:21 +01:00
|
|
|
# Get all files ending with the specified suffices from the given
|
|
|
|
# directory. E.g. `sourceFilesBySuffices ./dir [".xml" ".c"]'.
|
|
|
|
sourceFilesBySuffices = path: exts:
|
|
|
|
let filter = name: type:
|
|
|
|
let base = baseNameOf (toString name);
|
|
|
|
in type != "directory" && any (ext: hasSuffix ext base) exts;
|
|
|
|
in builtins.filterSource filter path;
|
|
|
|
|
|
|
|
|
2007-05-20 22:24:43 +02: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 21:45:55 +02: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 22:05:54 +02:00
|
|
|
|
2007-08-11 12:34:07 +02: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 13:14:36 +02:00
|
|
|
if (name == "false") then false else
|
2007-08-15 22:54:11 +02:00
|
|
|
if (isInList (getAttr ["flags"] [] attrSet) name) then true else
|
2007-08-11 12:34:07 +02:00
|
|
|
getAttr [name] false attrSet ;
|
|
|
|
|
2007-09-11 22:05:54 +02:00
|
|
|
|
2007-08-11 12:34:07 +02:00
|
|
|
logicalOR = x: y: x || y;
|
|
|
|
logicalAND = x: y: x && y;
|
|
|
|
|
2007-09-11 22:05:54 +02:00
|
|
|
|
2007-08-11 12:34:07 +02:00
|
|
|
# Input : attrSet, [ [name default] ... ], name
|
|
|
|
# Output : its value or default.
|
|
|
|
getValue = attrSet: argList: name:
|
2007-08-15 22:54:11 +02:00
|
|
|
( getAttr [name] (if checkFlag attrSet name then true else
|
|
|
|
if argList == [] then null else
|
2007-08-11 12:34:07 +02: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 22:05:54 +02:00
|
|
|
|
2007-08-11 12:34:07 +02: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 22:05:54 +02:00
|
|
|
|
|
|
|
|
2007-08-11 13:14:36 +02: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);};
|
|
|
|
|
2008-01-15 01:55:21 +01:00
|
|
|
uniqListExt = {inputList, outputList ? [],
|
|
|
|
getter ? (x : x), compare ? (x: y: x==y)}:
|
|
|
|
if (inputList == []) then outputList else
|
|
|
|
let x=head inputList;
|
|
|
|
isX = y: (compare (getter y) (getter x));
|
|
|
|
newOutputList = outputList ++
|
|
|
|
(if any isX outputList then [] else [x]);
|
|
|
|
in uniqListExt {outputList=newOutputList;
|
|
|
|
inputList = (tail inputList);
|
|
|
|
inherit getter compare;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-09-11 22:05:54 +02:00
|
|
|
|
2007-08-15 23:17:11 +02: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 19:05:32 +01:00
|
|
|
|
|
|
|
/* Options. */
|
|
|
|
|
|
|
|
mkOption = attrs: attrs // {_type = "option";};
|
|
|
|
|
|
|
|
typeOf = x: if x ? _type then x._type else "";
|
|
|
|
|
2007-11-09 19:07:14 +01:00
|
|
|
addDefaultOptionValues = defs: opts: opts //
|
2007-11-09 19:05:32 +01: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 19:07:14 +01:00
|
|
|
then addDefaultOptionValues defValue optValue
|
|
|
|
else addDefaultOptionValues defValue {};
|
2007-11-09 19:05:32 +01:00
|
|
|
}
|
|
|
|
) (builtins.attrNames defs));
|
2007-11-11 10:27:52 +01: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 19:05:32 +01:00
|
|
|
|
2007-11-16 22:05:15 +01: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-12-08 02:07:13 +01: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-08 02:10:32 +01: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 17:58:08 +01:00
|
|
|
}
|