diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/asserts.nix | 44 | ||||
-rw-r--r-- | lib/attrsets.nix | 11 | ||||
-rw-r--r-- | lib/customisation.nix | 2 | ||||
-rw-r--r-- | lib/debug.nix | 94 | ||||
-rw-r--r-- | lib/default.nix | 6 | ||||
-rw-r--r-- | lib/generators.nix | 1 | ||||
-rw-r--r-- | lib/licenses.nix | 35 | ||||
-rw-r--r-- | lib/lists.nix | 191 | ||||
-rw-r--r-- | lib/options.nix | 120 | ||||
-rw-r--r-- | lib/sources.nix | 4 | ||||
-rw-r--r-- | lib/strings.nix | 223 | ||||
-rw-r--r-- | lib/systems/default.nix | 20 | ||||
-rw-r--r-- | lib/systems/doubles.nix | 5 | ||||
-rw-r--r-- | lib/systems/examples.nix | 30 | ||||
-rw-r--r-- | lib/systems/inspect.nix | 1 | ||||
-rw-r--r-- | lib/systems/parse.nix | 15 | ||||
-rw-r--r-- | lib/tests/check-eval.nix | 7 | ||||
-rw-r--r-- | lib/tests/misc.nix | 20 | ||||
-rw-r--r-- | lib/tests/systems.nix | 29 | ||||
-rw-r--r-- | lib/trivial.nix | 137 | ||||
-rw-r--r-- | lib/types.nix | 13 |
21 files changed, 788 insertions, 220 deletions
diff --git a/lib/asserts.nix b/lib/asserts.nix new file mode 100644 index 000000000000..8a5f1fb3feb7 --- /dev/null +++ b/lib/asserts.nix @@ -0,0 +1,44 @@ +{ lib }: + +rec { + + /* Print a trace message if pred is false. + Intended to be used to augment asserts with helpful error messages. + + Example: + assertMsg false "nope" + => false + stderr> trace: nope + + assert (assertMsg ("foo" == "bar") "foo is not bar, silly"); "" + stderr> trace: foo is not bar, silly + stderr> assert failed at … + + Type: + assertMsg :: Bool -> String -> Bool + */ + # TODO(Profpatsch): add tests that check stderr + assertMsg = pred: msg: + if pred + then true + else builtins.trace msg false; + + /* Specialized `assertMsg` for checking if val is one of the elements + of a list. Useful for checking enums. + + Example: + let sslLibrary = "libressl" + in assertOneOf "sslLibrary" sslLibrary [ "openssl" "bearssl" ] + => false + stderr> trace: sslLibrary must be one of "openssl", "bearssl", but is: "libressl" + + Type: + assertOneOf :: String -> ComparableVal -> List ComparableVal -> Bool + */ + assertOneOf = name: val: xs: assertMsg + (lib.elem val xs) + "${name} must be one of ${ + lib.generators.toPretty {} xs}, but is: ${ + lib.generators.toPretty {} val}"; + +} diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 1e4142562fa6..2a1b866dbc5e 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -435,12 +435,15 @@ rec { useful for deep-overriding. Example: - x = { a = { b = 4; c = 3; }; } - overrideExisting x { a = { b = 6; d = 2; }; } - => { a = { b = 6; d = 2; }; } + overrideExisting {} { a = 1; } + => {} + overrideExisting { b = 2; } { a = 1; } + => { b = 2; } + overrideExisting { a = 3; b = 2; } { a = 1; } + => { a = 1; b = 2; } */ overrideExisting = old: new: - old // listToAttrs (map (attr: nameValuePair attr (attrByPath [attr] old.${attr} new)) (attrNames old)); + mapAttrs (name: value: new.${name} or value) old; /* Get a package output. If no output is found, fallback to `.out` and then to the default. diff --git a/lib/customisation.nix b/lib/customisation.nix index df9d977e9ec7..68062dd0daf0 100644 --- a/lib/customisation.nix +++ b/lib/customisation.nix @@ -196,7 +196,7 @@ rec { newScope = scope: newScope (self // scope); callPackage = self.newScope {}; overrideScope = g: lib.warn - "`overrideScope` (from `lib.makeScope`) is deprecated. Do `overrideScope' (self: self: { … })` instead of `overrideScope (super: self: { … })`. All other overrides have the parameters in that order, including other definitions of `overrideScope`. This was the only definition violating the pattern." + "`overrideScope` (from `lib.makeScope`) is deprecated. Do `overrideScope' (self: super: { … })` instead of `overrideScope (super: self: { … })`. All other overrides have the parameters in that order, including other definitions of `overrideScope`. This was the only definition violating the pattern." (makeScope newScope (lib.fixedPoints.extends (lib.flip g) f)); overrideScope' = g: makeScope newScope (lib.fixedPoints.extends g f); packages = f; diff --git a/lib/debug.nix b/lib/debug.nix index 383eb32d75d0..2879f72ed2ba 100644 --- a/lib/debug.nix +++ b/lib/debug.nix @@ -23,27 +23,54 @@ rec { # -- TRACING -- - /* Trace msg, but only if pred is true. + /* Conditionally trace the supplied message, based on a predicate. + + Type: traceIf :: bool -> string -> a -> a Example: traceIf true "hello" 3 trace: hello => 3 */ - traceIf = pred: msg: x: if pred then trace msg x else x; + traceIf = + # Predicate to check + pred: + # Message that should be traced + msg: + # Value to return + x: if pred then trace msg x else x; + + /* Trace the supplied value after applying a function to it, and + return the original value. - /* Trace the value and also return it. + Type: traceValFn :: (a -> b) -> a -> a Example: traceValFn (v: "mystring ${v}") "foo" trace: mystring foo => "foo" */ - traceValFn = f: x: trace (f x) x; + traceValFn = + # Function to apply + f: + # Value to trace and return + x: trace (f x) x; + + /* Trace the supplied value and return it. + + Type: traceVal :: a -> a + + Example: + traceVal 42 + # trace: 42 + => 42 + */ traceVal = traceValFn id; /* `builtins.trace`, but the value is `builtins.deepSeq`ed first. + Type: traceSeq :: a -> b -> b + Example: trace { a.b.c = 3; } null trace: { a = <CODE>; } @@ -52,7 +79,11 @@ rec { trace: { a = { b = { c = 3; }; }; } => null */ - traceSeq = x: y: trace (builtins.deepSeq x x) y; + traceSeq = + # The value to trace + x: + # The value to return + y: trace (builtins.deepSeq x x) y; /* Like `traceSeq`, but only evaluate down to depth n. This is very useful because lots of `traceSeq` usages @@ -76,27 +107,49 @@ rec { in trace (generators.toPretty { allowPrettyValues = true; } (modify depth snip x)) y; - /* A combination of `traceVal` and `traceSeq` */ - traceValSeqFn = f: v: traceValFn f (builtins.deepSeq v v); + /* A combination of `traceVal` and `traceSeq` that applies a + provided function to the value to be traced after `deepSeq`ing + it. + */ + traceValSeqFn = + # Function to apply + f: + # Value to trace + v: traceValFn f (builtins.deepSeq v v); + + /* A combination of `traceVal` and `traceSeq`. */ traceValSeq = traceValSeqFn id; + /* A combination of `traceVal` and `traceSeqN` that applies a + provided function to the value to be traced. */ + traceValSeqNFn = + # Function to apply + f: + depth: + # Value to trace + v: traceSeqN depth (f v) v; + /* A combination of `traceVal` and `traceSeqN`. */ - traceValSeqNFn = f: depth: v: traceSeqN depth (f v) v; traceValSeqN = traceValSeqNFn id; # -- TESTING -- - /* Evaluate a set of tests. A test is an attribute set {expr, - expected}, denoting an expression and its expected result. The - result is a list of failed tests, each represented as {name, - expected, actual}, denoting the attribute name of the failing - test and its expected and actual results. Used for regression - testing of the functions in lib; see tests.nix for an example. - Only tests having names starting with "test" are run. - Add attr { tests = ["testName"]; } to run these test only + /* Evaluate a set of tests. A test is an attribute set `{expr, + expected}`, denoting an expression and its expected result. The + result is a list of failed tests, each represented as `{name, + expected, actual}`, denoting the attribute name of the failing + test and its expected and actual results. + + Used for regression testing of the functions in lib; see + tests.nix for an example. Only tests having names starting with + "test" are run. + + Add attr { tests = ["testName"]; } to run these tests only. */ - runTests = tests: lib.concatLists (lib.attrValues (lib.mapAttrs (name: test: + runTests = + # Tests to run + tests: lib.concatLists (lib.attrValues (lib.mapAttrs (name: test: let testsToRun = if tests ? tests then tests.tests else []; in if (substring 0 4 name == "test" || elem name testsToRun) && ((testsToRun == []) || elem name tests.tests) @@ -105,8 +158,11 @@ rec { then [ { inherit name; expected = test.expected; result = test.expr; } ] else [] ) tests)); - # create a test assuming that list elements are true - # usage: { testX = allTrue [ true ]; } + /* Create a test assuming that list elements are `true`. + + Example: + { testX = allTrue [ true ]; } + */ testAllTrue = expr: { inherit expr; expected = map (x: true) expr; }; diff --git a/lib/default.nix b/lib/default.nix index dd6fcec75e21..d7a05fec8338 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -38,10 +38,11 @@ let systems = callLibs ./systems; # misc + asserts = callLibs ./asserts.nix; debug = callLibs ./debug.nix; - generators = callLibs ./generators.nix; misc = callLibs ./deprecated.nix; + # domain-specific fetchers = callLibs ./fetchers.nix; @@ -60,7 +61,6 @@ let boolToString mergeAttrs flip mapNullable inNixShell min max importJSON warn info nixpkgsVersion version mod compare splitByAndCompare functionArgs setFunctionArgs isFunction; - inherit (fixedPoints) fix fix' extends composeExtensions makeExtensible makeExtensibleWithCustomName; inherit (attrsets) attrByPath hasAttrByPath setAttrByPath @@ -117,6 +117,8 @@ let unknownModule mkOption; inherit (types) isType setType defaultTypeMerge defaultFunctor isOptionType mkOptionType; + inherit (asserts) + assertMsg assertOneOf; inherit (debug) addErrorContextToAttrs traceIf traceVal traceValFn traceXMLVal traceXMLValMarked traceSeq traceSeqN traceValSeq traceValSeqFn traceValSeqN traceValSeqNFn traceShowVal diff --git a/lib/generators.nix b/lib/generators.nix index f5faf7007860..863ba847423e 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -143,6 +143,7 @@ rec { }@args: v: with builtins; let isPath = v: typeOf v == "path"; in if isInt v then toString v + else if isFloat v then "~${toString v}" else if isString v then ''"${libStr.escape [''"''] v}"'' else if true == v then "true" else if false == v then "false" diff --git a/lib/licenses.nix b/lib/licenses.nix index c442d74c857c..940baf53b7c3 100644 --- a/lib/licenses.nix +++ b/lib/licenses.nix @@ -309,6 +309,12 @@ lib.mapAttrs (n: v: v // { shortName = n; }) rec { fullName = "GNU General Public License v2.0 only"; }; + gpl2Classpath = { + spdxId = "GPL-2.0-with-classpath-exception"; + fullName = "GNU General Public License v2.0 only (with Classpath exception)"; + url = https://fedoraproject.org/wiki/Licensing/GPL_Classpath_Exception; + }; + gpl2ClasspathPlus = { fullName = "GNU General Public License v2.0 or later (with Classpath exception)"; url = https://fedoraproject.org/wiki/Licensing/GPL_Classpath_Exception; @@ -355,6 +361,11 @@ lib.mapAttrs (n: v: v // { shortName = n; }) rec { fullName = "Independent JPEG Group License"; }; + imagemagick = spdx { + fullName = "ImageMagick License"; + spdxId = "imagemagick"; + }; + inria-compcert = { fullName = "INRIA Non-Commercial License Agreement for the CompCert verified compiler"; url = "http://compcert.inria.fr/doc/LICENSE"; @@ -382,6 +393,14 @@ lib.mapAttrs (n: v: v // { shortName = n; }) rec { fullName = "ISC License"; }; + # Proprietary binaries; free to redistribute without modification. + issl = { + fullName = "Intel Simplified Software License"; + url = https://software.intel.com/en-us/license/intel-simplified-software-license; + free = false; + }; + + lgpl2 = spdx { spdxId = "LGPL-2.0"; fullName = "GNU Library General Public License v2 only"; @@ -495,6 +514,12 @@ lib.mapAttrs (n: v: v // { shortName = n; }) rec { fullName = "Non-Profit Open Software License 3.0"; }; + ocamlpro_nc = { + fullName = "OCamlPro Non Commercial license version 1"; + url = "https://alt-ergo.ocamlpro.com/http/alt-ergo-2.2.0/OCamlPro-Non-Commercial-License.pdf"; + free = false; + }; + ofl = spdx { spdxId = "OFL-1.1"; fullName = "SIL Open Font License 1.1"; @@ -546,6 +571,11 @@ lib.mapAttrs (n: v: v // { shortName = n; }) rec { fullName = "Public Domain"; }; + purdueBsd = { + fullName = " Purdue BSD-Style License"; # also know as lsof license + url = https://enterprise.dejacode.com/licenses/public/purdue-bsd; + }; + qpl = spdx { spdxId = "QPL-1.0"; fullName = "Q Public License 1.0"; @@ -561,6 +591,11 @@ lib.mapAttrs (n: v: v // { shortName = n; }) rec { fullName = "Ruby License"; }; + sendmail = spdx { + spdxId = "Sendmail"; + fullName = "Sendmail License"; + }; + sgi-b-20 = spdx { spdxId = "SGI-B-2.0"; fullName = "SGI Free Software License B v2.0"; diff --git a/lib/lists.nix b/lib/lists.nix index 288882924fff..be541427c247 100644 --- a/lib/lists.nix +++ b/lib/lists.nix @@ -1,4 +1,5 @@ # General list operations. + { lib }: with lib.trivial; let @@ -8,21 +9,23 @@ rec { inherit (builtins) head tail length isList elemAt concatLists filter elem genList; - /* Create a list consisting of a single element. `singleton x' is - sometimes more convenient with respect to indentation than `[x]' + /* Create a list consisting of a single element. `singleton x` is + sometimes more convenient with respect to indentation than `[x]` when x spans multiple lines. + Type: singleton :: a -> [a] + Example: singleton "foo" => [ "foo" ] */ singleton = x: [x]; - /* “right fold” a binary function `op' between successive elements of - `list' with `nul' as the starting value, i.e., - `foldr op nul [x_1 x_2 ... x_n] == op x_1 (op x_2 ... (op x_n nul))'. - Type: - foldr :: (a -> b -> b) -> b -> [a] -> b + /* “right fold” a binary function `op` between successive elements of + `list` with `nul' as the starting value, i.e., + `foldr op nul [x_1 x_2 ... x_n] == op x_1 (op x_2 ... (op x_n nul))`. + + Type: foldr :: (a -> b -> b) -> b -> [a] -> b Example: concat = foldr (a: b: a + b) "z" @@ -42,16 +45,15 @@ rec { else op (elemAt list n) (fold' (n + 1)); in fold' 0; - /* `fold' is an alias of `foldr' for historic reasons */ + /* `fold` is an alias of `foldr` for historic reasons */ # FIXME(Profpatsch): deprecate? fold = foldr; - /* “left fold”, like `foldr', but from the left: + /* “left fold”, like `foldr`, but from the left: `foldl op nul [x_1 x_2 ... x_n] == op (... (op (op nul x_1) x_2) ... x_n)`. - Type: - foldl :: (b -> a -> b) -> b -> [a] -> b + Type: foldl :: (b -> a -> b) -> b -> [a] -> b Example: lconcat = foldl (a: b: a + b) "z" @@ -70,16 +72,20 @@ rec { else op (foldl' (n - 1)) (elemAt list n); in foldl' (length list - 1); - /* Strict version of `foldl'. + /* Strict version of `foldl`. The difference is that evaluation is forced upon access. Usually used with small whole results (in contract with lazily-generated list or large lists where only a part is consumed.) + + Type: foldl' :: (b -> a -> b) -> b -> [a] -> b */ foldl' = builtins.foldl' or foldl; /* Map with index starting from 0 + Type: imap0 :: (int -> a -> b) -> [a] -> [b] + Example: imap0 (i: v: "${v}-${toString i}") ["a" "b"] => [ "a-0" "b-1" ] @@ -88,6 +94,8 @@ rec { /* Map with index starting from 1 + Type: imap1 :: (int -> a -> b) -> [a] -> [b] + Example: imap1 (i: v: "${v}-${toString i}") ["a" "b"] => [ "a-1" "b-2" ] @@ -96,6 +104,8 @@ rec { /* Map and concatenate the result. + Type: concatMap :: (a -> [b]) -> [a] -> [b] + Example: concatMap (x: [x] ++ ["z"]) ["a" "b"] => [ "a" "z" "b" "z" ] @@ -118,15 +128,21 @@ rec { /* Remove elements equal to 'e' from a list. Useful for buildInputs. + Type: remove :: a -> [a] -> [a] + Example: remove 3 [ 1 3 4 3 ] => [ 1 4 ] */ - remove = e: filter (x: x != e); + remove = + # Element to remove from the list + e: filter (x: x != e); /* Find the sole element in the list matching the specified - predicate, returns `default' if no such element exists, or - `multiple' if there are multiple matching elements. + predicate, returns `default` if no such element exists, or + `multiple` if there are multiple matching elements. + + Type: findSingle :: (a -> bool) -> a -> a -> [a] -> a Example: findSingle (x: x == 3) "none" "multiple" [ 1 3 3 ] @@ -136,14 +152,24 @@ rec { findSingle (x: x == 3) "none" "multiple" [ 1 9 ] => "none" */ - findSingle = pred: default: multiple: list: + findSingle = + # Predicate + pred: + # Default value to return if element was not found. + default: + # Default value to return if more than one element was found + multiple: + # Input list + list: let found = filter pred list; len = length found; in if len == 0 then default else if len != 1 then multiple else head found; /* Find the first element in the list matching the specified - predicate or returns `default' if no such element exists. + predicate or return `default` if no such element exists. + + Type: findFirst :: (a -> bool) -> a -> [a] -> a Example: findFirst (x: x > 3) 7 [ 1 6 4 ] @@ -151,12 +177,20 @@ rec { findFirst (x: x > 9) 7 [ 1 6 4 ] => 7 */ - findFirst = pred: default: list: + findFirst = + # Predicate + pred: + # Default value to return + default: + # Input list + list: let found = filter pred list; in if found == [] then default else head found; - /* Return true iff function `pred' returns true for at least element - of `list'. + /* Return true if function `pred` returns true for at least one + element of `list`. + + Type: any :: (a -> bool) -> [a] -> bool Example: any isString [ 1 "a" { } ] @@ -166,8 +200,10 @@ rec { */ any = builtins.any or (pred: foldr (x: y: if pred x then true else y) false); - /* Return true iff function `pred' returns true for all elements of - `list'. + /* Return true if function `pred` returns true for all elements of + `list`. + + Type: all :: (a -> bool) -> [a] -> bool Example: all (x: x < 3) [ 1 2 ] @@ -177,19 +213,25 @@ rec { */ all = builtins.all or (pred: foldr (x: y: if pred x then y else false) true); - /* Count how many times function `pred' returns true for the elements - of `list'. + /* Count how many elements of `list` match the supplied predicate + function. + + Type: count :: (a -> bool) -> [a] -> int Example: count (x: x == 3) [ 3 2 3 4 6 ] => 2 */ - count = pred: foldl' (c: x: if pred x then c + 1 else c) 0; + count = + # Predicate + pred: foldl' (c: x: if pred x then c + 1 else c) 0; /* 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'). + Type: optional :: bool -> a -> [a] + Example: optional true "foo" => [ "foo" ] @@ -200,13 +242,19 @@ rec { /* Return a list or an empty list, depending on a boolean value. + Type: optionals :: bool -> [a] -> [a] + Example: optionals true [ 2 3 ] => [ 2 3 ] optionals false [ 2 3 ] => [ ] */ - optionals = cond: elems: if cond then elems else []; + optionals = + # Condition + cond: + # List to return if condition is true + elems: if cond then elems else []; /* If argument is a list, return it; else, wrap it in a singleton @@ -223,20 +271,28 @@ rec { /* Return a list of integers from `first' up to and including `last'. + Type: range :: int -> int -> [int] + Example: range 2 4 => [ 2 3 4 ] range 3 2 => [ ] */ - range = first: last: + range = + # First integer in the range + first: + # Last integer in the range + last: if first > last then [] else genList (n: first + n) (last - first + 1); - /* Splits the elements of a list in two lists, `right' and - `wrong', depending on the evaluation of a predicate. + /* Splits the elements of a list in two lists, `right` and + `wrong`, depending on the evaluation of a predicate. + + Type: (a -> bool) -> [a] -> { right :: [a], wrong :: [a] } Example: partition (x: x > 2) [ 5 1 2 3 4 ] @@ -252,7 +308,7 @@ rec { /* Splits the elements of a list into many lists, using the return value of a predicate. Predicate should return a string which becomes keys of attrset `groupBy' returns. - `groupBy'' allows to customise the combining function and initial value + `groupBy'` allows to customise the combining function and initial value Example: groupBy (x: boolToString (x > 2)) [ 5 1 2 3 4 ] @@ -268,10 +324,6 @@ rec { xfce = [ { name = "xfce"; script = "xfce4-session &"; } ]; } - - groupBy' allows to customise the combining function and initial value - - Example: groupBy' builtins.add 0 (x: boolToString (x > 2)) [ 5 1 2 3 4 ] => { true = 12; false = 3; } */ @@ -289,17 +341,27 @@ rec { the merging stops at the shortest. How both lists are merged is defined by the first argument. + Type: zipListsWith :: (a -> b -> c) -> [a] -> [b] -> [c] + Example: zipListsWith (a: b: a + b) ["h" "l"] ["e" "o"] => ["he" "lo"] */ - zipListsWith = f: fst: snd: + zipListsWith = + # Function to zip elements of both lists + f: + # First list + fst: + # Second list + snd: genList (n: f (elemAt fst n) (elemAt snd n)) (min (length fst) (length snd)); /* Merges two lists of the same size together. If the sizes aren't the same the merging stops at the shortest. + Type: zipLists :: [a] -> [b] -> [{ fst :: a, snd :: b}] + Example: zipLists [ 1 2 ] [ "a" "b" ] => [ { fst = 1; snd = "a"; } { fst = 2; snd = "b"; } ] @@ -308,6 +370,8 @@ rec { /* Reverse the order of the elements of a list. + Type: reverseList :: [a] -> [a] + Example: reverseList [ "b" "o" "j" ] @@ -321,8 +385,7 @@ rec { `before a b == true` means that `b` depends on `a` (there's an edge from `b` to `a`). - Examples: - + Example: listDfs true hasPrefix [ "/home/user" "other" "/" "/home" ] == { minimal = "/"; # minimal element visited = [ "/home/user" ]; # seen elements (in reverse order) @@ -336,7 +399,6 @@ rec { rest = [ "/home" "other" ]; # everything else */ - listDfs = stopOnCycles: before: list: let dfs' = us: visited: rest: @@ -361,7 +423,7 @@ rec { `before a b == true` means that `b` should be after `a` in the result. - Examples: + Example: toposort hasPrefix [ "/home/user" "other" "/" "/home" ] == { result = [ "/" "/home" "/home/user" "other" ]; } @@ -376,7 +438,6 @@ rec { toposort (a: b: a < b) [ 3 2 1 ] == { result = [ 1 2 3 ]; } */ - toposort = before: list: let dfsthis = listDfs true before list; @@ -467,26 +528,38 @@ rec { /* Return the first (at most) N elements of a list. + Type: take :: int -> [a] -> [a] + Example: take 2 [ "a" "b" "c" "d" ] => [ "a" "b" ] take 2 [ ] => [ ] */ - take = count: sublist 0 count; + take = + # Number of elements to take + count: sublist 0 count; /* Remove the first (at most) N elements of a list. + Type: drop :: int -> [a] -> [a] + Example: drop 2 [ "a" "b" "c" "d" ] => [ "c" "d" ] drop 2 [ ] => [ ] */ - drop = count: list: sublist count (length list) list; + drop = + # Number of elements to drop + count: + # Input list + list: sublist count (length list) list; + + /* Return a list consisting of at most `count` elements of `list`, + starting at index `start`. - /* Return a list consisting of at most ‘count’ elements of ‘list’, - starting at index ‘start’. + Type: sublist :: int -> int -> [a] -> [a] Example: sublist 1 3 [ "a" "b" "c" "d" "e" ] @@ -494,7 +567,13 @@ rec { sublist 1 3 [ ] => [ ] */ - sublist = start: count: list: + sublist = + # Index at which to start the sublist + start: + # Number of elements to take + count: + # Input list + list: let len = length list; in genList (n: elemAt list (n + start)) @@ -504,23 +583,34 @@ rec { /* Return the last element of a list. + This function throws an error if the list is empty. + + Type: last :: [a] -> a + Example: last [ 1 2 3 ] => 3 */ last = list: - assert list != []; elemAt list (length list - 1); + assert lib.assertMsg (list != []) "lists.last: list must not be empty!"; + elemAt list (length list - 1); - /* Return all elements but the last + /* Return all elements but the last. + + This function throws an error if the list is empty. + + Type: init :: [a] -> [a] Example: init [ 1 2 3 ] => [ 1 2 ] */ - init = list: assert list != []; take (length list - 1) list; + init = list: + assert lib.assertMsg (list != []) "lists.init: list must not be empty!"; + take (length list - 1) list; - /* return the image of the cross product of some lists by a function + /* Return the image of the cross product of some lists by a function. Example: crossLists (x:y: "${toString x}${toString y}") [[1 2] [3 4]] @@ -531,8 +621,9 @@ rec { /* Remove duplicate elements from the list. O(n^2) complexity. - Example: + Type: unique :: [a] -> [a] + Example: unique [ 3 2 3 4 ] => [ 3 2 4 ] */ diff --git a/lib/options.nix b/lib/options.nix index 01160b48ec01..791930eafbd0 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -8,33 +8,72 @@ with lib.strings; rec { + /* Returns true when the given argument is an option + + Type: isOption :: a -> bool + + Example: + isOption 1 // => false + isOption (mkOption {}) // => true + */ isOption = lib.isType "option"; + + /* Creates an Option attribute set. mkOption accepts an attribute set with the following keys: + + All keys default to `null` when not given. + + Example: + mkOption { } // => { _type = "option"; } + mkOption { defaultText = "foo"; } // => { _type = "option"; defaultText = "foo"; } + */ mkOption = - { default ? null # Default value used when no definition is given in the configuration. - , defaultText ? null # Textual representation of the default, for in the manual. - , example ? null # Example value used in the manual. - , description ? null # String describing the option. - , relatedPackages ? null # Related packages used in the manual (see `genRelatedPackages` in ../nixos/doc/manual/default.nix). - , type ? null # Option type, providing type-checking and value merging. - , apply ? null # Function that converts the option value to something else. - , internal ? null # Whether the option is for NixOS developers only. - , visible ? null # Whether the option shows up in the manual. - , readOnly ? null # Whether the option can be set only once - , options ? null # Obsolete, used by types.optionSet. + { + # Default value used when no definition is given in the configuration. + default ? null, + # Textual representation of the default, for the manual. + defaultText ? null, + # Example value used in the manual. + example ? null, + # String describing the option. + description ? null, + # Related packages used in the manual (see `genRelatedPackages` in ../nixos/doc/manual/default.nix). + relatedPackages ? null, + # Option type, providing type-checking and value merging. + type ? null, + # Function that converts the option value to something else. + apply ? null, + # Whether the option is for NixOS developers only. + internal ? null, + # Whether the option shows up in the manual. + visible ? null, + # Whether the option can be set only once + readOnly ? null, + # Obsolete, used by types.optionSet. + options ? null } @ attrs: attrs // { _type = "option"; }; - mkEnableOption = name: mkOption { + /* Creates an Option attribute set for a boolean value option i.e an + option to be toggled on or off: + + Example: + mkEnableOption "foo" + => { _type = "option"; default = false; description = "Whether to enable foo."; example = true; type = { ... }; } + */ + mkEnableOption = + # Name for the created option + name: mkOption { default = false; example = true; description = "Whether to enable ${name}."; type = lib.types.bool; }; - # This option accept anything, but it does not produce any result. This - # is useful for sharing a module across different module sets without - # having to implement similar features as long as the value of the options - # are not expected. + /* This option accepts anything, but it does not produce any result. + + This is useful for sharing a module across different module sets + without having to implement similar features as long as the + values of the options are not accessed. */ mkSinkUndeclaredOptions = attrs: mkOption ({ internal = true; visible = false; @@ -74,7 +113,24 @@ rec { else val) (head defs).value defs; + /* Extracts values of all "value" keys of the given list. + + Type: getValues :: [ { value :: a } ] -> [a] + + Example: + getValues [ { value = 1; } { value = 2; } ] // => [ 1 2 ] + getValues [ ] // => [ ] + */ getValues = map (x: x.value); + + /* Extracts values of all "file" keys of the given list + + Type: getFiles :: [ { file :: a } ] -> [a] + + Example: + getFiles [ { file = "file1"; } { file = "file2"; } ] // => [ "file1" "file2" ] + getFiles [ ] // => [ ] + */ getFiles = map (x: x.file); # Generate documentation template from the list of option declaration like @@ -107,10 +163,13 @@ rec { /* This function recursively removes all derivation attributes from - `x' except for the `name' attribute. This is to make the - generation of `options.xml' much more efficient: the XML - representation of derivations is very large (on the order of - megabytes) and is not actually used by the manual generator. */ + `x` except for the `name` attribute. + + This is to make the generation of `options.xml` much more + efficient: the XML representation of derivations is very large + (on the order of megabytes) and is not actually used by the + manual generator. + */ scrubOptionValue = x: if isDerivation x then { type = "derivation"; drvPath = x.name; outPath = x.name; name = x.name; } @@ -119,20 +178,21 @@ rec { else x; - /* For use in the ‘example’ option attribute. It causes the given - text to be included verbatim in documentation. This is necessary - for example values that are not simple values, e.g., - functions. */ + /* For use in the `example` option attribute. It causes the given + text to be included verbatim in documentation. This is necessary + for example values that are not simple values, e.g., functions. + */ literalExample = text: { _type = "literalExample"; inherit text; }; + # Helper functions. - /* Helper functions. */ + /* Convert an option, described as a list of the option parts in to a + safe, human readable version. - # Convert an option, described as a list of the option parts in to a - # safe, human readable version. ie: - # - # (showOption ["foo" "bar" "baz"]) == "foo.bar.baz" - # (showOption ["foo" "bar.baz" "tux"]) == "foo.\"bar.baz\".tux" + Example: + (showOption ["foo" "bar" "baz"]) == "foo.bar.baz" + (showOption ["foo" "bar.baz" "tux"]) == "foo.\"bar.baz\".tux" + */ showOption = parts: let escapeOptionPart = part: let diff --git a/lib/sources.nix b/lib/sources.nix index 704711b20cd9..e64b23414e86 100644 --- a/lib/sources.nix +++ b/lib/sources.nix @@ -26,6 +26,10 @@ rec { (type == "symlink" && lib.hasPrefix "result" baseName) ); + # Filters a source tree removing version control files and directories using cleanSourceWith + # + # Example: + # cleanSource ./. cleanSource = src: cleanSourceWith { filter = cleanSourceFilter; inherit src; }; # Like `builtins.filterSource`, except it will compose with itself, diff --git a/lib/strings.nix b/lib/strings.nix index af932ce6ecff..4d7fa1e774d5 100644 --- a/lib/strings.nix +++ b/lib/strings.nix @@ -12,6 +12,8 @@ rec { /* Concatenate a list of strings. + Type: concatStrings :: [string] -> string + Example: concatStrings ["foo" "bar"] => "foobar" @@ -20,15 +22,19 @@ rec { /* Map a function over a list and concatenate the resulting strings. + Type: concatMapStrings :: (a -> string) -> [a] -> string + Example: concatMapStrings (x: "a" + x) ["foo" "bar"] => "afooabar" */ concatMapStrings = f: list: concatStrings (map f list); - /* Like `concatMapStrings' except that the f functions also gets the + /* Like `concatMapStrings` except that the f functions also gets the position as a parameter. + Type: concatImapStrings :: (int -> a -> string) -> [a] -> string + Example: concatImapStrings (pos: x: "${toString pos}-${x}") ["foo" "bar"] => "1-foo2-bar" @@ -37,17 +43,25 @@ rec { /* Place an element between each element of a list + Type: intersperse :: a -> [a] -> [a] + Example: intersperse "/" ["usr" "local" "bin"] => ["usr" "/" "local" "/" "bin"]. */ - intersperse = separator: list: + intersperse = + # Separator to add between elements + separator: + # Input list + list: if list == [] || length list == 1 then list else tail (lib.concatMap (x: [separator x]) list); /* Concatenate a list of strings with a separator between each element + Type: concatStringsSep :: string -> [string] -> string + Example: concatStringsSep "/" ["usr" "local" "bin"] => "usr/local/bin" @@ -55,43 +69,77 @@ rec { concatStringsSep = builtins.concatStringsSep or (separator: list: concatStrings (intersperse separator list)); - /* First maps over the list and then concatenates it. + /* Maps a function over a list of strings and then concatenates the + result with the specified separator interspersed between + elements. + + Type: concatMapStringsSep :: string -> (string -> string) -> [string] -> string Example: concatMapStringsSep "-" (x: toUpper x) ["foo" "bar" "baz"] => "FOO-BAR-BAZ" */ - concatMapStringsSep = sep: f: list: concatStringsSep sep (map f list); + concatMapStringsSep = + # Separator to add between elements + sep: + # Function to map over the list + f: + # List of input strings + list: concatStringsSep sep (map f list); - /* First imaps over the list and then concatenates it. + /* Same as `concatMapStringsSep`, but the mapping function + additionally receives the position of its argument. - Example: + Type: concatMapStringsSep :: string -> (int -> string -> string) -> [string] -> string + Example: concatImapStringsSep "-" (pos: x: toString (x / pos)) [ 6 6 6 ] => "6-3-2" */ - concatImapStringsSep = sep: f: list: concatStringsSep sep (lib.imap1 f list); + concatImapStringsSep = + # Separator to add between elements + sep: + # Function that receives elements and their positions + f: + # List of input strings + list: concatStringsSep sep (lib.imap1 f list); - /* Construct a Unix-style search path consisting of each `subDir" - directory of the given list of packages. + /* Construct a Unix-style, colon-separated search path consisting of + the given `subDir` appended to each of the given paths. + + Type: makeSearchPath :: string -> [string] -> string Example: makeSearchPath "bin" ["/root" "/usr" "/usr/local"] => "/root/bin:/usr/bin:/usr/local/bin" - makeSearchPath "bin" ["/"] - => "//bin" + makeSearchPath "bin" [""] + => "/bin" */ - makeSearchPath = subDir: packages: - concatStringsSep ":" (map (path: path + "/" + subDir) (builtins.filter (x: x != null) packages)); + makeSearchPath = + # Directory name to append + subDir: + # List of base paths + paths: + concatStringsSep ":" (map (path: path + "/" + subDir) (builtins.filter (x: x != null) paths)); + + /* Construct a Unix-style search path by appending the given + `subDir` to the specified `output` of each of the packages. If no + output by the given name is found, fallback to `.out` and then to + the default. - /* Construct a Unix-style search path, using given package output. - If no output is found, fallback to `.out` and then to the default. + Type: string -> string -> [package] -> string Example: makeSearchPathOutput "dev" "bin" [ pkgs.openssl pkgs.zlib ] => "/nix/store/9rz8gxhzf8sw4kf2j2f1grr49w8zx5vj-openssl-1.0.1r-dev/bin:/nix/store/wwh7mhwh269sfjkm6k5665b5kgp7jrk2-zlib-1.2.8/bin" */ - makeSearchPathOutput = output: subDir: pkgs: makeSearchPath subDir (map (lib.getOutput output) pkgs); + makeSearchPathOutput = + # Package output to use + output: + # Directory name to append + subDir: + # List of packages + pkgs: makeSearchPath subDir (map (lib.getOutput output) pkgs); /* Construct a library search path (such as RPATH) containing the libraries for a set of packages @@ -117,13 +165,12 @@ rec { /* Construct a perl search path (such as $PERL5LIB) - FIXME(zimbatm): this should be moved in perl-specific code - Example: pkgs = import <nixpkgs> { } makePerlPath [ pkgs.perlPackages.libnet ] => "/nix/store/n0m1fk9c960d8wlrs62sncnadygqqc6y-perl-Net-SMTP-1.25/lib/perl5/site_perl" */ + # FIXME(zimbatm): this should be moved in perl-specific code makePerlPath = makeSearchPathOutput "lib" "lib/perl5/site_perl"; /* Construct a perl search path recursively including all dependencies (such as $PERL5LIB) @@ -138,34 +185,51 @@ rec { /* Depending on the boolean `cond', return either the given string or the empty string. Useful to concatenate against a bigger string. + Type: optionalString :: bool -> string -> string + Example: optionalString true "some-string" => "some-string" optionalString false "some-string" => "" */ - optionalString = cond: string: if cond then string else ""; + optionalString = + # Condition + cond: + # String to return if condition is true + string: if cond then string else ""; /* Determine whether a string has given prefix. + Type: hasPrefix :: string -> string -> bool + Example: hasPrefix "foo" "foobar" => true hasPrefix "foo" "barfoo" => false */ - hasPrefix = pref: str: - substring 0 (stringLength pref) str == pref; + hasPrefix = + # Prefix to check for + pref: + # Input string + str: substring 0 (stringLength pref) str == pref; /* Determine whether a string has given suffix. + Type: hasSuffix :: string -> string -> bool + Example: hasSuffix "foo" "foobar" => false hasSuffix "foo" "barfoo" => true */ - hasSuffix = suffix: content: + hasSuffix = + # Suffix to check for + suffix: + # Input string + content: let lenContent = stringLength content; lenSuffix = stringLength suffix; @@ -180,6 +244,8 @@ rec { Also note that Nix treats strings as a list of bytes and thus doesn't handle unicode. + Type: stringtoCharacters :: string -> [string] + Example: stringToCharacters "" => [ ] @@ -194,18 +260,25 @@ rec { /* Manipulate a string character by character and replace them by strings before concatenating the results. + Type: stringAsChars :: (string -> string) -> string -> string + Example: stringAsChars (x: if x == "a" then "i" else x) "nax" => "nix" */ - stringAsChars = f: s: - concatStrings ( + stringAsChars = + # Function to map over each individual character + f: + # Input string + s: concatStrings ( map f (stringToCharacters s) ); - /* Escape occurrence of the elements of ‘list’ in ‘string’ by + /* Escape occurrence of the elements of `list` in `string` by prefixing it with a backslash. + Type: escape :: [string] -> string -> string + Example: escape ["(" ")"] "(foo)" => "\\(foo\\)" @@ -214,6 +287,8 @@ rec { /* Quote string to be used safely within the Bourne shell. + Type: escapeShellArg :: string -> string + Example: escapeShellArg "esc'ape\nme" => "'esc'\\''ape\nme'" @@ -222,6 +297,8 @@ rec { /* Quote all arguments to be safely passed to the Bourne shell. + Type: escapeShellArgs :: [string] -> string + Example: escapeShellArgs ["one" "two three" "four'five"] => "'one' 'two three' 'four'\\''five'" @@ -230,13 +307,15 @@ rec { /* Turn a string into a Nix expression representing that string + Type: string -> string + Example: escapeNixString "hello\${}\n" => "\"hello\\\${}\\n\"" */ escapeNixString = s: escape ["$"] (builtins.toJSON s); - /* Obsolete - use replaceStrings instead. */ + # Obsolete - use replaceStrings instead. replaceChars = builtins.replaceStrings or ( del: new: s: let @@ -256,6 +335,8 @@ rec { /* Converts an ASCII string to lower-case. + Type: toLower :: string -> string + Example: toLower "HOME" => "home" @@ -264,6 +345,8 @@ rec { /* Converts an ASCII string to upper-case. + Type: toUpper :: string -> string + Example: toUpper "home" => "HOME" @@ -273,7 +356,7 @@ rec { /* Appends string context from another string. This is an implementation detail of Nix. - Strings in Nix carry an invisible `context' which is a list of strings + Strings in Nix carry an invisible `context` which is a list of strings representing store paths. If the string is later used in a derivation attribute, the derivation will properly populate the inputDrvs and inputSrcs. @@ -319,8 +402,9 @@ rec { in recurse 0 0; - /* Return the suffix of the second argument if the first argument matches - its prefix. + /* Return a string without the specified prefix, if the prefix matches. + + Type: string -> string -> string Example: removePrefix "foo." "foo.bar.baz" @@ -328,18 +412,23 @@ rec { removePrefix "xxx" "foo.bar.baz" => "foo.bar.baz" */ - removePrefix = pre: s: + removePrefix = + # Prefix to remove if it matches + prefix: + # Input string + str: let - preLen = stringLength pre; - sLen = stringLength s; + preLen = stringLength prefix; + sLen = stringLength str; in - if hasPrefix pre s then - substring preLen (sLen - preLen) s + if hasPrefix prefix str then + substring preLen (sLen - preLen) str else - s; + str; + + /* Return a string without the specified suffix, if the suffix matches. - /* Return the prefix of the second argument if the first argument matches - its suffix. + Type: string -> string -> string Example: removeSuffix "front" "homefront" @@ -347,17 +436,21 @@ rec { removeSuffix "xxx" "homefront" => "homefront" */ - removeSuffix = suf: s: + removeSuffix = + # Suffix to remove if it matches + suffix: + # Input string + str: let - sufLen = stringLength suf; - sLen = stringLength s; + sufLen = stringLength suffix; + sLen = stringLength str; in - if sufLen <= sLen && suf == substring (sLen - sufLen) sufLen s then - substring 0 (sLen - sufLen) s + if sufLen <= sLen && suffix == substring (sLen - sufLen) sufLen str then + substring 0 (sLen - sufLen) str else - s; + str; - /* Return true iff string v1 denotes a version older than v2. + /* Return true if string v1 denotes a version older than v2. Example: versionOlder "1.1" "1.2" @@ -367,7 +460,7 @@ rec { */ versionOlder = v1: v2: builtins.compareVersions v2 v1 == 1; - /* Return true iff string v1 denotes a version equal to or newer than v2. + /* Return true if string v1 denotes a version equal to or newer than v2. Example: versionAtLeast "1.1" "1.0" @@ -410,7 +503,7 @@ rec { components = splitString "/" url; filename = lib.last components; name = builtins.head (splitString sep filename); - in assert name != filename; name; + in assert name != filename; name; /* Create an --{enable,disable}-<feat> string that can be passed to standard GNU Autoconf scripts. @@ -459,6 +552,11 @@ rec { /* Create a fixed width string with additional prefix to match required width. + This function will fail if the input string is longer than the + requested length. + + Type: fixedWidthString :: int -> string -> string + Example: fixedWidthString 5 "0" (toString 15) => "00015" @@ -468,7 +566,10 @@ rec { strw = lib.stringLength str; reqWidth = width - (lib.stringLength filler); in - assert strw <= width; + assert lib.assertMsg (strw <= width) + "fixedWidthString: requested string length (${ + toString width}) must not be shorter than actual length (${ + toString strw})"; if strw == width then str else filler + fixedWidthString reqWidth filler str; /* Format a number adding leading zeroes up to fixed width. @@ -499,12 +600,16 @@ rec { => false */ isStorePath = x: - isCoercibleToString x - && builtins.substring 0 1 (toString x) == "/" - && dirOf (builtins.toPath x) == builtins.storeDir; + if isCoercibleToString x then + let str = toString x; in + builtins.substring 0 1 str == "/" + && dirOf str == builtins.storeDir + else + false; + + /* Parse a string string as an int. - /* Convert string to int - Obviously, it is a bit hacky to use fromJSON that way. + Type: string -> int Example: toInt "1337" @@ -514,17 +619,18 @@ rec { toInt "3.14" => error: floating point JSON numbers are not supported */ + # Obviously, it is a bit hacky to use fromJSON this way. toInt = str: let may_be_int = builtins.fromJSON str; in if builtins.isInt may_be_int then may_be_int else throw "Could not convert ${str} to int."; - /* Read a list of paths from `file', relative to the `rootPath'. Lines - beginning with `#' are treated as comments and ignored. Whitespace - is significant. + /* Read a list of paths from `file`, relative to the `rootPath`. + Lines beginning with `#` are treated as comments and ignored. + Whitespace is significant. - NOTE: this function is not performant and should be avoided + NOTE: This function is not performant and should be avoided. Example: readPathsFromFile /prefix @@ -537,16 +643,17 @@ rec { */ readPathsFromFile = rootPath: file: let - root = toString rootPath; lines = lib.splitString "\n" (builtins.readFile file); removeComments = lib.filter (line: line != "" && !(lib.hasPrefix "#" line)); relativePaths = removeComments lines; - absolutePaths = builtins.map (path: builtins.toPath (root + "/" + path)) relativePaths; + absolutePaths = builtins.map (path: rootPath + "/${path}") relativePaths; in absolutePaths; /* Read the contents of a file removing the trailing \n + Type: fileContents :: path -> string + Example: $ echo "1.0" > ./version diff --git a/lib/systems/default.nix b/lib/systems/default.nix index 5eacc9eb23e1..0b3475fefb9c 100644 --- a/lib/systems/default.nix +++ b/lib/systems/default.nix @@ -32,6 +32,7 @@ rec { else if final.isUClibc then "uclibc" else if final.isAndroid then "bionic" else if final.isLinux /* default */ then "glibc" + else if final.isAvr then "avrlibc" # TODO(@Ericson2314) think more about other operating systems else "native/impure"; extensions = { @@ -46,6 +47,25 @@ rec { # Misc boolean options useAndroidPrebuilt = false; useiOSPrebuilt = false; + + # Output from uname + uname = { + # uname -s + system = { + "linux" = "Linux"; + "windows" = "Windows"; + "darwin" = "Darwin"; + "netbsd" = "NetBSD"; + "freebsd" = "FreeBSD"; + "openbsd" = "OpenBSD"; + }.${final.parsed.kernel.name} or null; + + # uname -p + processor = final.parsed.cpu.name; + + # uname -r + release = null; + }; } // mapAttrs (n: v: v final.parsed) inspect.predicates // args; in assert final.useAndroidPrebuilt -> final.isAndroid; diff --git a/lib/systems/doubles.nix b/lib/systems/doubles.nix index a00165db1716..58677c0bdd90 100644 --- a/lib/systems/doubles.nix +++ b/lib/systems/doubles.nix @@ -15,6 +15,8 @@ let "x86_64-cygwin" "x86_64-darwin" "x86_64-freebsd" "x86_64-linux" "x86_64-netbsd" "x86_64-openbsd" "x86_64-solaris" + + "x86_64-windows" "i686-windows" ]; allParsed = map parse.mkSystemFromString all; @@ -37,12 +39,13 @@ in rec { darwin = filterDoubles predicates.isDarwin; freebsd = filterDoubles predicates.isFreeBSD; # Should be better, but MinGW is unclear. - gnu = filterDoubles (matchAttrs { kernel = parse.kernels.linux; abi = parse.abis.gnu; }); + gnu = filterDoubles (matchAttrs { kernel = parse.kernels.linux; abi = parse.abis.gnu; }) ++ filterDoubles (matchAttrs { kernel = parse.kernels.linux; abi = parse.abis.gnueabi; }) ++ filterDoubles (matchAttrs { kernel = parse.kernels.linux; abi = parse.abis.gnueabihf; }); illumos = filterDoubles predicates.isSunOS; linux = filterDoubles predicates.isLinux; netbsd = filterDoubles predicates.isNetBSD; openbsd = filterDoubles predicates.isOpenBSD; unix = filterDoubles predicates.isUnix; + windows = filterDoubles predicates.isWindows; mesaPlatforms = ["i686-linux" "x86_64-linux" "x86_64-darwin" "armv5tel-linux" "armv6l-linux" "armv7l-linux" "aarch64-linux" "powerpc64le-linux"]; } diff --git a/lib/systems/examples.nix b/lib/systems/examples.nix index c2ee829529dc..a40c38924245 100644 --- a/lib/systems/examples.nix +++ b/lib/systems/examples.nix @@ -28,7 +28,7 @@ rec { }; armv7l-hf-multiplatform = rec { - config = "armv7a-unknown-linux-gnueabihf"; + config = "armv7l-unknown-linux-gnueabihf"; platform = platforms.armv7l-hf-multiplatform; }; @@ -99,6 +99,34 @@ rec { riscv64 = riscv "64"; riscv32 = riscv "32"; + avr = { + config = "avr"; + }; + + arm-embedded = { + config = "arm-none-eabi"; + libc = "newlib"; + }; + + aarch64-embedded = { + config = "aarch64-none-elf"; + libc = "newlib"; + }; + + ppc-embedded = { + config = "powerpc-none-eabi"; + libc = "newlib"; + }; + + i686-embedded = { + config = "i686-elf"; + libc = "newlib"; + }; + + x86_64-embedded = { + config = "x86_64-elf"; + libc = "newlib"; + }; # # Darwin diff --git a/lib/systems/inspect.nix b/lib/systems/inspect.nix index 65f560328af5..2fcf1afe4628 100644 --- a/lib/systems/inspect.nix +++ b/lib/systems/inspect.nix @@ -19,6 +19,7 @@ rec { isRiscV = { cpu = { family = "riscv"; }; }; isSparc = { cpu = { family = "sparc"; }; }; isWasm = { cpu = { family = "wasm"; }; }; + isAvr = { cpu = { family = "avr"; }; }; is32bit = { cpu = { bits = 32; }; }; is64bit = { cpu = { bits = 64; }; }; diff --git a/lib/systems/parse.nix b/lib/systems/parse.nix index bb26c93f3d7a..db97a5c4b33b 100644 --- a/lib/systems/parse.nix +++ b/lib/systems/parse.nix @@ -101,6 +101,8 @@ rec { wasm32 = { bits = 32; significantByte = littleEndian; family = "wasm"; }; wasm64 = { bits = 64; significantByte = littleEndian; family = "wasm"; }; + + avr = { bits = 8; family = "avr"; }; }; ################################################################################ @@ -117,6 +119,7 @@ rec { apple = {}; pc = {}; + none = {}; unknown = {}; }; @@ -200,6 +203,7 @@ rec { cygnus = {}; msvc = {}; eabi = {}; + elf = {}; androideabi = {}; android = { @@ -255,9 +259,16 @@ rec { setType "system" components; mkSkeletonFromList = l: { + "1" = if elemAt l 0 == "avr" + then { cpu = elemAt l 0; kernel = "none"; abi = "unknown"; } + else throw "Target specification with 1 components is ambiguous"; "2" = # We only do 2-part hacks for things Nix already supports if elemAt l 1 == "cygwin" then { cpu = elemAt l 0; kernel = "windows"; abi = "cygnus"; } + else if (elemAt l 1 == "eabi") + then { cpu = elemAt l 0; vendor = "none"; kernel = "none"; abi = elemAt l 1; } + else if (elemAt l 1 == "elf") + then { cpu = elemAt l 0; vendor = "none"; kernel = "none"; abi = elemAt l 1; } else { cpu = elemAt l 0; kernel = elemAt l 1; }; "3" = # Awkwards hacks, beware! if elemAt l 1 == "apple" @@ -268,6 +279,10 @@ rec { then { cpu = elemAt l 0; vendor = elemAt l 1; kernel = "windows"; abi = "gnu"; } else if hasPrefix "netbsd" (elemAt l 2) then { cpu = elemAt l 0; vendor = elemAt l 1; kernel = elemAt l 2; } + else if (elemAt l 2 == "eabi") + then { cpu = elemAt l 0; vendor = elemAt l 1; kernel = "none"; abi = elemAt l 2; } + else if (elemAt l 2 == "elf") + then { cpu = elemAt l 0; vendor = elemAt l 1; kernel = "none"; abi = elemAt l 2; } else throw "Target specification with 3 components is ambiguous"; "4" = { cpu = elemAt l 0; vendor = elemAt l 1; kernel = elemAt l 2; abi = elemAt l 3; }; }.${toString (length l)} diff --git a/lib/tests/check-eval.nix b/lib/tests/check-eval.nix new file mode 100644 index 000000000000..8bd7b605a39b --- /dev/null +++ b/lib/tests/check-eval.nix @@ -0,0 +1,7 @@ +# Throws an error if any of our lib tests fail. + +let tests = [ "misc" "systems" ]; + all = builtins.concatLists (map (f: import (./. + "/${f}.nix")) tests); +in if all == [] + then null + else throw (builtins.toJSON all) diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index cf99aca58c09..1604fbb39cbb 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -112,7 +112,7 @@ runTests { storePathAppendix = isStorePath "${goodPath}/bin/python"; nonAbsolute = isStorePath (concatStrings (tail (stringToCharacters goodPath))); - asPath = isStorePath (builtins.toPath goodPath); + asPath = isStorePath (/. + goodPath); otherPath = isStorePath "/something/else"; otherVals = { attrset = isStorePath {}; @@ -236,6 +236,20 @@ runTests { }; }; + testOverrideExistingEmpty = { + expr = overrideExisting {} { a = 1; }; + expected = {}; + }; + + testOverrideExistingDisjoint = { + expr = overrideExisting { b = 2; } { a = 1; }; + expected = { b = 2; }; + }; + + testOverrideExistingOverride = { + expr = overrideExisting { a = 3; b = 2; } { a = 1; }; + expected = { a = 1; b = 2; }; + }; # GENERATORS # these tests assume attributes are converted to lists @@ -355,9 +369,10 @@ runTests { testToPretty = { expr = mapAttrs (const (generators.toPretty {})) rec { int = 42; + float = 0.1337; bool = true; string = ''fno"rd''; - path = /. + "/foo"; # toPath returns a string + path = /. + "/foo"; null_ = null; function = x: x; functionArgs = { arg ? 4, foo }: arg; @@ -367,6 +382,7 @@ runTests { }; expected = rec { int = "42"; + float = "~0.133700"; bool = "true"; string = ''"fno\"rd"''; path = "/foo"; diff --git a/lib/tests/systems.nix b/lib/tests/systems.nix index 91604280e4e7..5e1293658215 100644 --- a/lib/tests/systems.nix +++ b/lib/tests/systems.nix @@ -12,20 +12,21 @@ let expected = lib.sort lib.lessThan y; }; in with lib.systems.doubles; lib.runTests { - all = assertTrue (mseteq all (linux ++ darwin ++ cygwin ++ freebsd ++ openbsd ++ netbsd ++ illumos)); + testall = mseteq all (linux ++ darwin ++ freebsd ++ openbsd ++ netbsd ++ illumos ++ windows); - arm = assertTrue (mseteq arm [ "armv5tel-linux" "armv6l-linux" "armv7l-linux" ]); - i686 = assertTrue (mseteq i686 [ "i686-linux" "i686-freebsd" "i686-netbsd" "i686-openbsd" "i686-cygwin" ]); - mips = assertTrue (mseteq mips [ "mipsel-linux" ]); - x86_64 = assertTrue (mseteq x86_64 [ "x86_64-linux" "x86_64-darwin" "x86_64-freebsd" "x86_64-openbsd" "x86_64-netbsd" "x86_64-cygwin" "x86_64-solaris" ]); + testarm = mseteq arm [ "armv5tel-linux" "armv6l-linux" "armv7l-linux" ]; + testi686 = mseteq i686 [ "i686-linux" "i686-freebsd" "i686-netbsd" "i686-openbsd" "i686-cygwin" "i686-windows" ]; + testmips = mseteq mips [ "mipsel-linux" ]; + testx86_64 = mseteq x86_64 [ "x86_64-linux" "x86_64-darwin" "x86_64-freebsd" "x86_64-openbsd" "x86_64-netbsd" "x86_64-cygwin" "x86_64-solaris" "x86_64-windows" ]; - cygwin = assertTrue (mseteq cygwin [ "i686-cygwin" "x86_64-cygwin" ]); - darwin = assertTrue (mseteq darwin [ "x86_64-darwin" ]); - freebsd = assertTrue (mseteq freebsd [ "i686-freebsd" "x86_64-freebsd" ]); - gnu = assertTrue (mseteq gnu (linux /* ++ kfreebsd ++ ... */)); - illumos = assertTrue (mseteq illumos [ "x86_64-solaris" ]); - linux = assertTrue (mseteq linux [ "i686-linux" "x86_64-linux" "armv5tel-linux" "armv6l-linux" "armv7l-linux" "aarch64-linux" "mipsel-linux" ]); - netbsd = assertTrue (mseteq netbsd [ "i686-netbsd" "x86_64-netbsd" ]); - openbsd = assertTrue (mseteq openbsd [ "i686-openbsd" "x86_64-openbsd" ]); - unix = assertTrue (mseteq unix (linux ++ darwin ++ freebsd ++ openbsd ++ netbsd ++ illumos)); + testcygwin = mseteq cygwin [ "i686-cygwin" "x86_64-cygwin" ]; + testdarwin = mseteq darwin [ "x86_64-darwin" ]; + testfreebsd = mseteq freebsd [ "i686-freebsd" "x86_64-freebsd" ]; + testgnu = mseteq gnu (linux /* ++ kfreebsd ++ ... */); + testillumos = mseteq illumos [ "x86_64-solaris" ]; + testlinux = mseteq linux [ "i686-linux" "x86_64-linux" "armv5tel-linux" "armv6l-linux" "armv7l-linux" "aarch64-linux" "mipsel-linux" ]; + testnetbsd = mseteq netbsd [ "i686-netbsd" "x86_64-netbsd" ]; + testopenbsd = mseteq openbsd [ "i686-openbsd" "x86_64-openbsd" ]; + testwindows = mseteq windows [ "i686-cygwin" "x86_64-cygwin" "i686-windows" "x86_64-windows" ]; + testunix = mseteq unix (linux ++ darwin ++ freebsd ++ openbsd ++ netbsd ++ illumos ++ cygwin); } diff --git a/lib/trivial.nix b/lib/trivial.nix index b75e81eb7352..e31cf73d27c4 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -9,23 +9,37 @@ rec { Type: id :: a -> a */ - id = x: x; + id = + # The value to return + x: x; /* The constant function - Ignores the second argument. - Or: Construct a function that always returns a static value. + + Ignores the second argument. If called with only one argument, + constructs a function that always returns a static value. Type: const :: a -> b -> a Example: let f = const 5; in f 10 => 5 */ - const = x: y: x; + const = + # Value to return + x: + # Value to ignore + y: x; ## Named versions corresponding to some builtin operators. - /* Concatenate two lists */ + /* Concatenate two lists + + Type: concat :: [a] -> [a] -> [a] + + Example: + concat [ 1 2 ] [ 3 4 ] + => [ 1 2 3 4 ] + */ concat = x: y: x ++ y; /* boolean “or” */ @@ -36,44 +50,57 @@ rec { /* bitwise “and” */ bitAnd = builtins.bitAnd - or import ./zip-int-bits.nix - (a: b: if a==1 && b==1 then 1 else 0); + or (import ./zip-int-bits.nix + (a: b: if a==1 && b==1 then 1 else 0)); /* bitwise “or” */ bitOr = builtins.bitOr - or import ./zip-int-bits.nix - (a: b: if a==1 || b==1 then 1 else 0); + or (import ./zip-int-bits.nix + (a: b: if a==1 || b==1 then 1 else 0)); /* bitwise “xor” */ bitXor = builtins.bitXor - or import ./zip-int-bits.nix - (a: b: if a!=b then 1 else 0); + or (import ./zip-int-bits.nix + (a: b: if a!=b then 1 else 0)); /* bitwise “not” */ bitNot = builtins.sub (-1); /* Convert a boolean to a string. - Note that toString on a bool returns "1" and "". + + This function uses the strings "true" and "false" to represent + boolean values. Calling `toString` on a bool instead returns "1" + and "" (sic!). + + Type: boolToString :: bool -> string */ boolToString = b: if b then "true" else "false"; /* Merge two attribute sets shallowly, right side trumps left + mergeAttrs :: attrs -> attrs -> attrs + Example: mergeAttrs { a = 1; b = 2; } { b = 3; c = 4; } => { a = 1; b = 3; c = 4; } */ - mergeAttrs = x: y: x // y; + mergeAttrs = + # Left attribute set + x: + # Right attribute set (higher precedence for equal keys) + y: x // y; /* Flip the order of the arguments of a binary function. + Type: flip :: (a -> b -> c) -> (b -> a -> c) + Example: flip concat [1] [2] => [ 2 1 ] */ flip = f: a: b: f b a; - /* Apply function if argument is non-null. + /* Apply function if the supplied argument is non-null. Example: mapNullable (x: x+1) null @@ -81,7 +108,11 @@ rec { mapNullable (x: x+1) 22 => 23 */ - mapNullable = f: a: if isNull a then a else f a; + mapNullable = + # Function to call + f: + # Argument to check for null before passing it to `f` + a: if isNull a then a else f a; # Pull in some builtins not included elsewhere. inherit (builtins) @@ -92,29 +123,51 @@ rec { ## nixpks version strings - # The current full nixpkgs version number. + /* Returns the current full nixpkgs version number. */ version = release + versionSuffix; - # The current nixpkgs version number as string. + /* Returns the current nixpkgs release number as string. */ release = lib.strings.fileContents ../.version; - # The current nixpkgs version suffix as string. + /* Returns the current nixpkgs version suffix as string. */ versionSuffix = let suffixFile = ../.version-suffix; in if pathExists suffixFile then lib.strings.fileContents suffixFile else "pre-git"; + /* Attempts to return the the current revision of nixpkgs and + returns the supplied default value otherwise. + + Type: revisionWithDefault :: string -> string + */ + revisionWithDefault = + # Default value to return if revision can not be determined + default: + let + revisionFile = "${toString ./..}/.git-revision"; + gitRepo = "${toString ./..}/.git"; + in if lib.pathIsDirectory gitRepo + then lib.commitIdFromGitRepo gitRepo + else if lib.pathExists revisionFile then lib.fileContents revisionFile + else default; + nixpkgsVersion = builtins.trace "`lib.nixpkgsVersion` is deprecated, use `lib.version` instead!" version; - # Whether we're being called by nix-shell. + /* Determine whether the function is being called from inside a Nix + shell. + + Type: inNixShell :: bool + */ inNixShell = builtins.getEnv "IN_NIX_SHELL" != ""; ## Integer operations - # Return minimum/maximum of two numbers. + /* Return minimum of two numbers. */ min = x: y: if x < y then x else y; + + /* Return maximum of two numbers. */ max = x: y: if x > y then x else y; /* Integer modulus @@ -148,8 +201,9 @@ rec { second subtype, compare elements of a single subtype with `yes` and `no` respectively. - Example: + Type: (a -> bool) -> (a -> a -> int) -> (a -> a -> int) -> (a -> a -> int) + Example: let cmp = splitByAndCompare (hasPrefix "foo") compare compare; in cmp "a" "z" => -1 @@ -160,31 +214,44 @@ rec { # while compare "fooa" "a" => 1 */ - splitByAndCompare = p: yes: no: a: b: + splitByAndCompare = + # Predicate + p: + # Comparison function if predicate holds for both values + yes: + # Comparison function if predicate holds for neither value + no: + # First value to compare + a: + # Second value to compare + b: if p a then if p b then yes a b else -1 else if p b then 1 else no a b; - /* Reads a JSON file. */ + /* Reads a JSON file. + + Type :: path -> any + */ importJSON = path: builtins.fromJSON (builtins.readFile path); - ## Warnings and asserts + ## Warnings - /* See https://github.com/NixOS/nix/issues/749. Eventually we'd like these - to expand to Nix builtins that carry metadata so that Nix can filter out - the INFO messages without parsing the message string. + # See https://github.com/NixOS/nix/issues/749. Eventually we'd like these + # to expand to Nix builtins that carry metadata so that Nix can filter out + # the INFO messages without parsing the message string. + # + # Usage: + # { + # foo = lib.warn "foo is deprecated" oldFoo; + # } + # + # TODO: figure out a clever way to integrate location information from + # something like __unsafeGetAttrPos. - Usage: - { - foo = lib.warn "foo is deprecated" oldFoo; - } - - TODO: figure out a clever way to integrate location information from - something like __unsafeGetAttrPos. - */ warn = msg: builtins.trace "WARNING: ${msg}"; info = msg: builtins.trace "INFO: ${msg}"; diff --git a/lib/types.nix b/lib/types.nix index 4d6ac51c8988..ca6794e274c3 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -119,7 +119,9 @@ rec { let betweenDesc = lowest: highest: "${toString lowest} and ${toString highest} (both inclusive)"; - between = lowest: highest: assert lowest <= highest; + between = lowest: highest: + assert lib.assertMsg (lowest <= highest) + "ints.between: lowest must be smaller than highest"; addCheck int (x: x >= lowest && x <= highest) // { name = "intBetween"; description = "integer between ${betweenDesc lowest highest}"; @@ -192,7 +194,10 @@ rec { # separator between the values). separatedString = sep: mkOptionType rec { name = "separatedString"; - description = "string"; + description = if sep == "" + then "Concatenated string" # for types.string. + else "strings concatenated with ${builtins.toJSON sep}" + ; check = isString; merge = loc: defs: concatStringsSep sep (getValues defs); functor = (defaultFunctor name) // { @@ -439,7 +444,9 @@ rec { # Either value of type `finalType` or `coercedType`, the latter is # converted to `finalType` using `coerceFunc`. coercedTo = coercedType: coerceFunc: finalType: - assert coercedType.getSubModules == null; + assert lib.assertMsg (coercedType.getSubModules == null) + "coercedTo: coercedType must not have submodules (it’s a ${ + coercedType.description})"; mkOptionType rec { name = "coercedTo"; description = "${finalType.description} or ${coercedType.description} convertible to it"; |