···
41
+
## Simple (higher order) functions
For when you need a function that does “nothing”.
···
## Named versions corresponding to some builtin operators.
62
-
/* Concat two strings */
64
+
/* Concatenate two lists */
···
mergeAttrs = x: y: x // y;
96
-
# Flip the order of the arguments of a binary function.
98
+
/* Flip the order of the arguments of a binary function.
101
+
flip concat [1] [2]
99
-
# Apply function if argument is non-null
106
+
/* Apply function if argument is non-null.
109
+
mapNullable (x: x+1) null
111
+
mapNullable (x: x+1) 22
mapNullable = f: a: if isNull a then a else f a;
# Pull in some builtins not included elsewhere.
···
isInt isFloat add sub lessThan
seq deepSeq genericClosure;
108
-
inherit (lib.strings) fileContents;
110
-
release = fileContents ../.version;
111
-
versionSuffix = let suffixFile = ../.version-suffix; in
112
-
if pathExists suffixFile then fileContents suffixFile else "pre-git";
123
+
## nixpks version strings
114
-
# Return the Nixpkgs version number.
125
+
# The current full nixpkgs version number.
version = release + versionSuffix;
128
+
# The current nixpkgs version number as string.
129
+
release = lib.strings.fileContents ../.version;
131
+
# The current nixpkgs version suffix as string.
133
+
let suffixFile = ../.version-suffix;
134
+
in if pathExists suffixFile
135
+
then lib.strings.fileContents suffixFile
nixpkgsVersion = builtins.trace "`lib.nixpkgsVersion` is deprecated, use `lib.version` instead!" version;
# Whether we're being called by nix-shell.
inNixShell = builtins.getEnv "IN_NIX_SHELL" != "";
144
+
## Integer operations
# Return minimum/maximum of two numbers.
min = x: y: if x < y then x else y;
···
mod = base: int: base - (int * (builtins.div base int));
···
splitByAndCompare = p: yes: no: a: b:
then if p b then yes a b else -1
else if p b then 1 else no a b;
builtins.fromJSON (builtins.readFile path);
204
+
## Warnings and asserts
/* 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
···
warn = msg: builtins.trace "WARNING: ${msg}";
info = msg: builtins.trace "INFO: ${msg}";
191
-
# | Add metadata about expected function arguments to a function.
192
-
# The metadata should match the format given by
193
-
# builtins.functionArgs, i.e. a set from expected argument to a bool
194
-
# representing whether that argument has a default or not.
195
-
# setFunctionArgs : (a → b) → Map String Bool → (a → b)
197
-
# This function is necessary because you can't dynamically create a
198
-
# function of the { a, b ? foo, ... }: format, but some facilities
199
-
# like callPackage expect to be able to query expected arguments.
222
+
## Function annotations
224
+
/* Add metadata about expected function arguments to a function.
225
+
The metadata should match the format given by
226
+
builtins.functionArgs, i.e. a set from expected argument to a bool
227
+
representing whether that argument has a default or not.
228
+
setFunctionArgs : (a → b) → Map String Bool → (a → b)
230
+
This function is necessary because you can't dynamically create a
231
+
function of the { a, b ? foo, ... }: format, but some facilities
232
+
like callPackage expect to be able to query expected arguments.
setFunctionArgs = f: args:
{ # TODO: Should we add call-time "type" checking like built in?
206
-
# | Extract the expected function arguments from a function.
207
-
# This works both with nix-native { a, b ? foo, ... }: style
208
-
# functions and functions with args set with 'setFunctionArgs'. It
209
-
# has the same return type and semantics as builtins.functionArgs.
210
-
# setFunctionArgs : (a → b) → Map String Bool.
240
+
/* Extract the expected function arguments from a function.
241
+
This works both with nix-native { a, b ? foo, ... }: style
242
+
functions and functions with args set with 'setFunctionArgs'. It
243
+
has the same return type and semantics as builtins.functionArgs.
244
+
setFunctionArgs : (a → b) → Map String Bool.
functionArgs = f: f.__functionArgs or (builtins.functionArgs f);
248
+
/* Check whether something is a function or something
249
+
annotated with function args.
isFunction = f: builtins.isFunction f ||
(f ? __functor && isFunction (f.__functor f));