at 17.09-beta 2.5 kB view raw
1rec { 2 3 /* The identity function 4 For when you need a function that does nothing. 5 6 Type: id :: a -> a 7 */ 8 id = x: x; 9 10 /* The constant function 11 Ignores the second argument. 12 Or: Construct a function that always returns a static value. 13 14 Type: const :: a -> b -> a 15 Example: 16 let f = const 5; in f 10 17 => 5 18 */ 19 const = x: y: x; 20 21 22 ## Named versions corresponding to some builtin operators. 23 24 /* Concat two strings */ 25 concat = x: y: x ++ y; 26 27 /* boolean or */ 28 or = x: y: x || y; 29 30 /* boolean and */ 31 and = x: y: x && y; 32 33 /* Convert a boolean to a string. 34 Note that toString on a bool returns "1" and "". 35 */ 36 boolToString = b: if b then "true" else "false"; 37 38 /* Merge two attribute sets shallowly, right side trumps left 39 40 Example: 41 mergeAttrs { a = 1; b = 2; } { b = 3; c = 4; } 42 => { a = 1; b = 3; c = 4; } 43 */ 44 mergeAttrs = x: y: x // y; 45 46 # Flip the order of the arguments of a binary function. 47 flip = f: a: b: f b a; 48 49 # Apply function if argument is non-null 50 mapNullable = f: a: if isNull a then a else f a; 51 52 # Pull in some builtins not included elsewhere. 53 inherit (builtins) 54 pathExists readFile isBool isFunction 55 isInt add sub lessThan 56 seq deepSeq genericClosure; 57 58 inherit (import ./strings.nix) fileContents; 59 60 # Return the Nixpkgs version number. 61 nixpkgsVersion = 62 let suffixFile = ../.version-suffix; in 63 fileContents ../.version 64 + (if pathExists suffixFile then fileContents suffixFile else "pre-git"); 65 66 # Whether we're being called by nix-shell. 67 inNixShell = builtins.getEnv "IN_NIX_SHELL" != ""; 68 69 # Return minimum/maximum of two numbers. 70 min = x: y: if x < y then x else y; 71 max = x: y: if x > y then x else y; 72 73 /* Integer modulus 74 75 Example: 76 mod 11 10 77 => 1 78 mod 1 10 79 => 1 80 */ 81 mod = base: int: base - (int * (builtins.div base int)); 82 83 /* Reads a JSON file. */ 84 importJSON = path: 85 builtins.fromJSON (builtins.readFile path); 86 87 /* See https://github.com/NixOS/nix/issues/749. Eventually we'd like these 88 to expand to Nix builtins that carry metadata so that Nix can filter out 89 the INFO messages without parsing the message string. 90 91 Usage: 92 { 93 foo = lib.warn "foo is deprecated" oldFoo; 94 } 95 96 TODO: figure out a clever way to integrate location information from 97 something like __unsafeGetAttrPos. 98 */ 99 warn = msg: builtins.trace "WARNING: ${msg}"; 100 info = msg: builtins.trace "INFO: ${msg}"; 101}