···
+
## Simple (higher order) functions
For when you need a function that does “nothing”.
···
## Named versions corresponding to some builtin operators.
+
/* Concatenate two lists */
···
mergeAttrs = x: y: x // y;
+
/* Flip the order of the arguments of a binary function.
+
/* Apply function if argument is non-null.
+
mapNullable (x: x+1) null
+
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;
+
## nixpks version strings
+
# The current full nixpkgs version number.
version = release + versionSuffix;
+
# The current nixpkgs version number as string.
+
release = lib.strings.fileContents ../.version;
+
# The current nixpkgs version suffix as string.
+
let suffixFile = ../.version-suffix;
+
in if pathExists suffixFile
+
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" != "";
# 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);
+
## 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}";
+
## Function annotations
+
/* Add metadata about expected function arguments to a function.
+
The metadata should match the format given by
+
builtins.functionArgs, i.e. a set from expected argument to a bool
+
representing whether that argument has a default or not.
+
setFunctionArgs : (a → b) → Map String Bool → (a → b)
+
This function is necessary because you can't dynamically create a
+
function of the { a, b ? foo, ... }: format, but some facilities
+
like callPackage expect to be able to query expected arguments.
setFunctionArgs = f: args:
{ # TODO: Should we add call-time "type" checking like built in?
+
/* Extract the expected function arguments from a function.
+
This works both with nix-native { a, b ? foo, ... }: style
+
functions and functions with args set with 'setFunctionArgs'. It
+
has the same return type and semantics as builtins.functionArgs.
+
setFunctionArgs : (a → b) → Map String Bool.
functionArgs = f: f.__functionArgs or (builtins.functionArgs f);
+
/* Check whether something is a function or something
+
annotated with function args.
isFunction = f: builtins.isFunction f ||
(f ? __functor && isFunction (f.__functor f));