@@ -38,6 +38,8 @@ let
3838in
3939rec {
4040
41+ ## Simple (higher order) functions
42+
4143 /* The identity function
4244 For when you need a function that does “nothing”.
4345
@@ -59,7 +61,7 @@ rec {
5961
6062 ## Named versions corresponding to some builtin operators.
6163
62− /* Concat two strings */
64+ /* Concatenate two lists */
6365 concat = x: y: x ++ y;
6466
6567 /* boolean “or” */
@@ -93,10 +95,22 @@ rec {
9395 */
9496 mergeAttrs = x: y: x // y;
9597
96− # Flip the order of the arguments of a binary function.
98+ /* Flip the order of the arguments of a binary function.
99+
100+ Example:
101+ flip concat [1] [2]
102+ => [ 2 1 ]
103+ */
97104 flip = f: a: b: f b a;
98105
99− # Apply function if argument is non-null
106+ /* Apply function if argument is non-null.
107+
108+ Example:
109+ mapNullable (x: x+1) null
110+ => null
111+ mapNullable (x: x+1) 22
112+ => 23
113+ */
100114 mapNullable = f: a: if isNull a then a else f a;
101115
102116 # Pull in some builtins not included elsewhere.
@@ -105,20 +119,30 @@ rec {
105119 isInt isFloat add sub lessThan
106120 seq deepSeq genericClosure;
107121
108− inherit (lib.strings) fileContents;
109122
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
113124
114− # Return the Nixpkgs version number.
125+ # The current full nixpkgs version number.
115126 version = release + versionSuffix;
116127
128+ # The current nixpkgs version number as string.
129+ release = lib.strings.fileContents ../.version;
130+
131+ # The current nixpkgs version suffix as string.
132+ versionSuffix =
133+ let suffixFile = ../.version-suffix;
134+ in if pathExists suffixFile
135+ then lib.strings.fileContents suffixFile
136+ else "pre-git";
137+
117138 nixpkgsVersion = builtins.trace "`lib.nixpkgsVersion` is deprecated, use `lib.version` instead!" version;
118139
119140 # Whether we're being called by nix-shell.
120141 inNixShell = builtins.getEnv "IN_NIX_SHELL" != "";
121142
143+
144+ ## Integer operations
145+
122146 # Return minimum/maximum of two numbers.
123147 min = x: y: if x < y then x else y;
124148 max = x: y: if x > y then x else y;
@@ -133,6 +157,9 @@ rec {
133157 */
134158 mod = base: int: base - (int * (builtins.div base int));
135159
160+
161+ ## Comparisons
162+
136163 /* C-style comparisons
137164
138165 a < b, compare a b => -1
@@ -162,17 +189,20 @@ rec {
162189 cmp "fooa" "a" => -1
163190 # while
164191 compare "fooa" "a" => 1
165−
166192 */
167193 splitByAndCompare = p: yes: no: a: b:
168194 if p a
169195 then if p b then yes a b else -1
170196 else if p b then 1 else no a b;
171197
198+
172199 /* Reads a JSON file. */
173200 importJSON = path:
174201 builtins.fromJSON (builtins.readFile path);
175202
203+
204+ ## Warnings and asserts
205+
176206 /* See https://github.com/NixOS/nix/issues/749. Eventually we'd like these
177207 to expand to Nix builtins that carry metadata so that Nix can filter out
178208 the INFO messages without parsing the message string.
@@ -188,28 +218,36 @@ rec {
188218 warn = msg: builtins.trace "WARNING: ${msg}";
189219 info = msg: builtins.trace "INFO: ${msg}";
190220
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)
196− #
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.
221+
222+ ## Function annotations
223+
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)
229+
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.
233+ */
200234 setFunctionArgs = f: args:
201235 { # TODO: Should we add call-time "type" checking like built in?
202236 __functor = self: f;
203237 __functionArgs = args;
204238 };
205239
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.
245+ */
211246 functionArgs = f: f.__functionArgs or (builtins.functionArgs f);
212247
248+ /* Check whether something is a function or something
249+ annotated with function args.
250+ */
213251 isFunction = f: builtins.isFunction f ||
214252 (f ? __functor && isFunction (f.__functor f));
215253}