diff options
author | Yury G. Kudryashov <urkud.urkud@gmail.com> | 2008-01-15 00:55:21 +0000 |
---|---|---|
committer | Yury G. Kudryashov <urkud.urkud@gmail.com> | 2008-01-15 00:55:21 +0000 |
commit | a7703662a4ee043c1ba684d30ede016bb5dc2e41 (patch) | |
tree | 959345e65cd959b7a4cac52c4e5def9f4608494b /pkgs/lib | |
parent | e17a21b8202dffa6fd5a34792a3f9c73b1b80c7a (diff) | |
download | nixlib-a7703662a4ee043c1ba684d30ede016bb5dc2e41.tar nixlib-a7703662a4ee043c1ba684d30ede016bb5dc2e41.tar.gz nixlib-a7703662a4ee043c1ba684d30ede016bb5dc2e41.tar.bz2 nixlib-a7703662a4ee043c1ba684d30ede016bb5dc2e41.tar.lz nixlib-a7703662a4ee043c1ba684d30ede016bb5dc2e41.tar.xz nixlib-a7703662a4ee043c1ba684d30ede016bb5dc2e41.tar.zst nixlib-a7703662a4ee043c1ba684d30ede016bb5dc2e41.zip |
Merged with trunk
svn path=/nixpkgs/branches/stdenv-updates/; revision=10145
Diffstat (limited to 'pkgs/lib')
-rw-r--r-- | pkgs/lib/default-unstable.nix | 63 | ||||
-rw-r--r-- | pkgs/lib/default.nix | 61 |
2 files changed, 109 insertions, 15 deletions
diff --git a/pkgs/lib/default-unstable.nix b/pkgs/lib/default-unstable.nix index 33fb68f58578..5653508dca9a 100644 --- a/pkgs/lib/default-unstable.nix +++ b/pkgs/lib/default-unstable.nix @@ -37,6 +37,7 @@ rec { else [(head list) separator] ++ (intersperse separator (tail list)); + toList = x : if (__isList x) then x else [x]; concatStringsSep = separator: list: concatStrings (intersperse separator list); @@ -55,7 +56,7 @@ rec { # "y"] applied to some set e returns e.x.y, if it exists. The # default value is returned otherwise. # comment: I'd rename this to getAttrRec or something like that .. (has the same name as builtin.getAttr) - Marc Weber - getAttr = attrPath: default: e: + getAttr = attrPath : default : e : let { attr = head attrPath; body = @@ -64,6 +65,20 @@ rec { then getAttr (tail attrPath) default (builtins.getAttr attr e) else default; }; + #getAttr = attrPath: default: e: getAttrMap id; + + # the same as getAttr but if the element exists map the value using function f + # corresponds to the maybe function of haskell + getAttrMap = f : attrPath : default : e : + let { + attr = head attrPath; + body = + if attrPath == [] then e + else if builtins ? hasAttr && builtins.hasAttr attr e + then f (getAttr (tail attrPath) default (builtins.getAttr attr e)) + else default; + }; + # Filter a list using a predicate; that is, return a list containing @@ -116,7 +131,9 @@ rec { # 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 []; + optional = cond: elem: if (cond) then [elem] else []; + + whenFlip = x : cond : if (cond) then x else ""; # Return a list of integers from `first' up to and including `last'. @@ -187,11 +204,12 @@ rec { mapRecordFlatten = f : r : map (attr: f attr (builtins.getAttr attr r) ) (attrNames r); # to be used with listToAttrs (_a_ttribute _v_alue) + # TODO should be renamed to nv because niksnut has renamed the attribute attr to name av = name : value : { inherit name value; }; # attribute set containing one attribute - avs = attr : value : listToAttrs [ (av attr value) ]; + avs = name : value : listToAttrs [ (av name value) ]; # adds / replaces an attribute of an attribute set - setAttr = set : attr : v : set // (avs attr v); + setAttr = set : name : v : set // (avs name v); id = x : x; # true if all/ at least one element(s) satisfy f @@ -203,12 +221,37 @@ rec { mergeAttrs = fold ( x : y : x // y) {}; + # Using f = a : b = b the result is similar to // + # merge attributes with custom function handling the case that the attribute + # exists in both sets + mergeAttrsWithFunc = f : set1 : set2 : + fold (n: set : if (__hasAttr n set) + then setAttr set n (f (__getAttr n set) (__getAttr n set2)) + else set ) + set1 (__attrNames set2); + + # merging two attribute set concatenating the values of same attribute names + # eg { a = 7; } { a = [ 2 3 ]; } becomes { a = [ 7 2 3 ]; } + mergeAttrsConcatenateValues = mergeAttrsWithFunc ( a : b : (toList a) ++ (toList b) ); + # returns atribute values as a list flattenAttrs = set : map ( attr : builtins.getAttr attr set) (attrNames set); mapIf = cond : f : fold ( x : l : if (cond x) then [(f x)] ++ l else l) []; # Marc 2nd proposal: (not everything has been tested in detail yet..) - + + # usage / example + # flagConfig = { + # } // (enableDisableFeature "flagName" "configure_feature" extraAttrs;) + # + # is equal to + # flagConfig = { + # flagName = { cfgOption = "--enable-configure_feature"; } // extraAttrs; + # no_flagName = { cfgOption = "--disable-configure_feature"; }; + enableDisableFeature = flagName : configure_feature : extraAttrs : + listToAttrs [ ( av flagName ({ cfgOption = "--enable-${configure_feature}"; } // extraAttrs ) ) + ( av "no_${flagName}" ({ cfgOption = "--disable-${configure_feature}"; } ) )]; + # calls chooseOptionsByFlags2 with some preprocessing # chooseOptionsByFlags2 returns an attribute set meant to be used to create new derivaitons. # see mkDerivationByConfiguration in all-packages.nix and the examples given below. @@ -266,6 +309,9 @@ rec { collectAttrs = attr : catAttrs attr flatOptions; optsConcatStrs = delimiter : attrs : concatStrings ( intersperse delimiter (flatten ( collectAttrs attrs ) ) ); + + ifStringGetArg = x : if (__isAttrs x) then x # ( TODO implement __isString ?) + else avs x (__getAttr x args); in assert ( all id ( mapRecordFlatten ( attr : r : if ( all id ( flatten (getAttr ["assertion"] [] r ) ) ) then true else throw "assertion failed flag ${attr}" ) @@ -279,13 +325,15 @@ rec { buildInputs = map ( attr: if (! hasAttr attr args) then throw "argument ${attr} is missing!" else (builtins.getAttr attr args) ) (flatten (catAttrs "buildInputs" flatOptions)); + propagatedBuildInputs = map ( attr: if (! hasAttr attr args) then throw "argument ${attr} is missing!" else (builtins.getAttr attr args) ) + (flatten (catAttrs "propagatedBuildInputs" flatOptions)); configureFlags = optsConcatStrs " " "cfgOption"; #flags = listToAttrs (map ( flag: av flag (hasAttr flag options) ) (attrNames flagConfig) ); flags_prefixed = listToAttrs (map ( flag: av ("flag_set_"+flag) (hasAttr flag options) ) (attrNames flagConfig) ); - pass = mergeAttrs (flatten (collectAttrs "pass") ); + pass = mergeAttrs ( map ifStringGetArg ( flatten (collectAttrs "pass") ) ); } # now add additional phase actions (see examples) // listToAttrs ( map ( x : av x (optsConcatStrs "\n" x) ) collectExtraPhaseActions ) ); } @@ -319,7 +367,8 @@ rec { # features: # * configure options are passed automatically # * buildInputs are collected (they are special, see the setup script) - # * they can be passed by additional name as well using pass = + # * they can be passed by additional name as well using pass = { inherit (args) python } + # ( or short (value not attrs) : pass = "python" ) # * an attribute named the same way as the flag is added indicating # true/ false (flag has been set/ not set) # * extra phase dependend commands can be added diff --git a/pkgs/lib/default.nix b/pkgs/lib/default.nix index 3ad861ef19d1..108d62b3d8a3 100644 --- a/pkgs/lib/default.nix +++ b/pkgs/lib/default.nix @@ -10,6 +10,12 @@ in rec { listOfListsToAttrs = ll : builtins.listToAttrs (map (l : { name = (head l); value = (head (tail l)); }) ll); + + # Identity function. + id = x: x; + + + # !!! need documentation... innerSumArgs = f : x : y : (if y == null then (f x) else (innerSumArgs f (x // y))); sumArgs = f : innerSumArgs f {}; @@ -20,6 +26,7 @@ rec { f (tail (tail l)); pairMap = innerPairMap []; + # "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 @@ -66,14 +73,12 @@ rec { # "y"] applied to some set e returns e.x.y, if it exists. The # default value is returned otherwise. getAttr = attrPath: default: e: - let { - attr = head attrPath; - body = - if attrPath == [] then e - else if builtins ? hasAttr && builtins.hasAttr attr e - then getAttr (tail attrPath) default (builtins.getAttr attr e) - else default; - }; + 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; # Filter a list using a predicate; that is, return a list containing @@ -96,15 +101,33 @@ rec { else head found; + # 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; + + # 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); + # Workaround, but works in stable Nix now. eqStrings = a: b: (a+(substring 0 0 b)) == ((substring 0 0 a)+b); + # Determine whether a filename ends in the given suffix. hasSuffix = ext: fileName: let lenFileName = stringLength fileName; @@ -126,6 +149,15 @@ rec { in src: builtins.filterSource filter src; + # 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; + + # 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'). @@ -191,6 +223,19 @@ rec { in uniqList {outputList=newOutputList; inputList = (tail inputList);}; + 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; + }; + + condConcat = name: list: checker: if list == [] then name else |