diff options
author | John Ericson <John.Ericson@Obsidian.Systems> | 2018-09-17 22:48:25 -0400 |
---|---|---|
committer | John Ericson <John.Ericson@Obsidian.Systems> | 2018-09-17 22:48:25 -0400 |
commit | 226d574870943ac52fd225ed13b902db28776816 (patch) | |
tree | ab105d14aff48d6fa0adc07b491103e8967111b2 /lib | |
parent | 67694371869eaa43933f47f5cbcb87024945a72d (diff) | |
parent | f1737fa64663b5ab203c0974675298288eda84a3 (diff) | |
download | nixlib-226d574870943ac52fd225ed13b902db28776816.tar nixlib-226d574870943ac52fd225ed13b902db28776816.tar.gz nixlib-226d574870943ac52fd225ed13b902db28776816.tar.bz2 nixlib-226d574870943ac52fd225ed13b902db28776816.tar.lz nixlib-226d574870943ac52fd225ed13b902db28776816.tar.xz nixlib-226d574870943ac52fd225ed13b902db28776816.tar.zst nixlib-226d574870943ac52fd225ed13b902db28776816.zip |
Merge remote-tracking branch 'upstream/master' into darwin-android-ndk-for-master
Diffstat (limited to 'lib')
-rw-r--r-- | lib/asserts.nix | 44 | ||||
-rw-r--r-- | lib/default.nix | 6 | ||||
-rw-r--r-- | lib/licenses.nix | 10 | ||||
-rw-r--r-- | lib/lists.nix | 7 | ||||
-rw-r--r-- | lib/strings.nix | 12 | ||||
-rw-r--r-- | lib/tests/check-eval.nix | 7 | ||||
-rw-r--r-- | lib/tests/misc.nix | 4 | ||||
-rw-r--r-- | lib/trivial.nix | 14 | ||||
-rw-r--r-- | lib/types.nix | 8 |
9 files changed, 92 insertions, 20 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/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/licenses.nix b/lib/licenses.nix index c442d74c857c..c4db280645a4 100644 --- a/lib/licenses.nix +++ b/lib/licenses.nix @@ -355,6 +355,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"; @@ -546,6 +551,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"; diff --git a/lib/lists.nix b/lib/lists.nix index 288882924fff..9ecd8f220038 100644 --- a/lib/lists.nix +++ b/lib/lists.nix @@ -509,7 +509,8 @@ rec { => 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 @@ -517,7 +518,9 @@ rec { 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 diff --git a/lib/strings.nix b/lib/strings.nix index af932ce6ecff..0c4095bb55cd 100644 --- a/lib/strings.nix +++ b/lib/strings.nix @@ -410,7 +410,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. @@ -468,7 +468,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. @@ -501,7 +504,7 @@ rec { isStorePath = x: isCoercibleToString x && builtins.substring 0 1 (toString x) == "/" - && dirOf (builtins.toPath x) == builtins.storeDir; + && dirOf x == builtins.storeDir; /* Convert string to int Obviously, it is a bit hacky to use fromJSON that way. @@ -537,11 +540,10 @@ 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; 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..d3bd7746d89c 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 {}; @@ -357,7 +357,7 @@ runTests { int = 42; bool = true; string = ''fno"rd''; - path = /. + "/foo"; # toPath returns a string + path = /. + "/foo"; null_ = null; function = x: x; functionArgs = { arg ? 4, foo }: arg; diff --git a/lib/trivial.nix b/lib/trivial.nix index b75e81eb7352..b1eea0bf1247 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -36,18 +36,18 @@ 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); @@ -171,7 +171,7 @@ rec { 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 diff --git a/lib/types.nix b/lib/types.nix index 4d6ac51c8988..4e44e7521c4b 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}"; @@ -439,7 +441,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"; |