lol

doc: migrate lib.trivial to use doc-comments (#297270)

* doc: migrate lib.trivial to use doc-comments

* Apply suggestions from code review

---------

Co-authored-by: Daniel Sidhion <DanielSidhion@users.noreply.github.com>

authored by

Johannes Kirschbauer
Daniel Sidhion
and committed by
GitHub
956fff08 a737a781

+726 -213
+726 -213
lib/trivial.nix
··· 16 16 17 17 ## Simple (higher order) functions 18 18 19 - /* The identity function 20 - For when you need a function that does “nothing”. 19 + /** 20 + The identity function 21 + For when you need a function that does “nothing”. 22 + 23 + 24 + # Inputs 25 + 26 + `x` 27 + 28 + : The value to return 29 + 30 + # Type 21 31 22 - Type: id :: a -> a 32 + ``` 33 + id :: a -> a 34 + ``` 23 35 */ 24 - id = 25 - # The value to return 26 - x: x; 36 + id = x: x; 37 + 38 + /** 39 + The constant function 40 + 41 + Ignores the second argument. If called with only one argument, 42 + constructs a function that always returns a static value. 43 + 44 + 45 + # Inputs 46 + 47 + `x` 48 + 49 + : Value to return 50 + 51 + `y` 52 + 53 + : Value to ignore 54 + 55 + # Type 56 + 57 + ``` 58 + const :: a -> b -> a 59 + ``` 27 60 28 - /* The constant function 61 + # Examples 62 + :::{.example} 63 + ## `lib.trivial.const` usage example 29 64 30 - Ignores the second argument. If called with only one argument, 31 - constructs a function that always returns a static value. 65 + ```nix 66 + let f = const 5; in f 10 67 + => 5 68 + ``` 32 69 33 - Type: const :: a -> b -> a 34 - Example: 35 - let f = const 5; in f 10 36 - => 5 70 + ::: 37 71 */ 38 72 const = 39 - # Value to return 40 73 x: 41 - # Value to ignore 42 74 y: x; 43 75 44 - /* Pipes a value through a list of functions, left to right. 76 + /** 77 + Pipes a value through a list of functions, left to right. 78 + 79 + # Inputs 80 + 81 + `value` 82 + 83 + : Value to start piping. 84 + 85 + `fns` 86 + 87 + : List of functions to apply sequentially. 88 + 89 + # Type 90 + 91 + ``` 92 + pipe :: a -> [<functions>] -> <return type of last function> 93 + ``` 94 + 95 + # Examples 96 + :::{.example} 97 + ## `lib.trivial.pipe` usage example 98 + 99 + ```nix 100 + pipe 2 [ 101 + (x: x + 2) # 2 + 2 = 4 102 + (x: x * 2) # 4 * 2 = 8 103 + ] 104 + => 8 45 105 46 - Type: pipe :: a -> [<functions>] -> <return type of last function> 47 - Example: 48 - pipe 2 [ 49 - (x: x + 2) # 2 + 2 = 4 50 - (x: x * 2) # 4 * 2 = 8 51 - ] 52 - => 8 106 + # ideal to do text transformations 107 + pipe [ "a/b" "a/c" ] [ 53 108 54 - # ideal to do text transformations 55 - pipe [ "a/b" "a/c" ] [ 109 + # create the cp command 110 + (map (file: ''cp "${src}/${file}" $out\n'')) 56 111 57 - # create the cp command 58 - (map (file: ''cp "${src}/${file}" $out\n'')) 112 + # concatenate all commands into one string 113 + lib.concatStrings 59 114 60 - # concatenate all commands into one string 61 - lib.concatStrings 115 + # make that string into a nix derivation 116 + (pkgs.runCommand "copy-to-out" {}) 62 117 63 - # make that string into a nix derivation 64 - (pkgs.runCommand "copy-to-out" {}) 118 + ] 119 + => <drv which copies all files to $out> 65 120 66 - ] 67 - => <drv which copies all files to $out> 121 + The output type of each function has to be the input type 122 + of the next function, and the last function returns the 123 + final value. 124 + ``` 68 125 69 - The output type of each function has to be the input type 70 - of the next function, and the last function returns the 71 - final value. 126 + ::: 72 127 */ 73 128 pipe = builtins.foldl' (x: f: f x); 74 129 ··· 79 134 80 135 ## Named versions corresponding to some builtin operators. 81 136 82 - /* Concatenate two lists 137 + /** 138 + Concatenate two lists 139 + 140 + 141 + # Inputs 142 + 143 + `x` 144 + 145 + : 1\. Function argument 146 + 147 + `y` 148 + 149 + : 2\. Function argument 150 + 151 + # Type 152 + 153 + ``` 154 + concat :: [a] -> [a] -> [a] 155 + ``` 156 + 157 + # Examples 158 + :::{.example} 159 + ## `lib.trivial.concat` usage example 83 160 84 - Type: concat :: [a] -> [a] -> [a] 161 + ```nix 162 + concat [ 1 2 ] [ 3 4 ] 163 + => [ 1 2 3 4 ] 164 + ``` 85 165 86 - Example: 87 - concat [ 1 2 ] [ 3 4 ] 88 - => [ 1 2 3 4 ] 166 + ::: 89 167 */ 90 168 concat = x: y: x ++ y; 91 169 92 - /* boolean “or” */ 170 + /** 171 + boolean “or” 172 + 173 + 174 + # Inputs 175 + 176 + `x` 177 + 178 + : 1\. Function argument 179 + 180 + `y` 181 + 182 + : 2\. Function argument 183 + */ 93 184 or = x: y: x || y; 94 185 95 - /* boolean “and” */ 186 + /** 187 + boolean “and” 188 + 189 + 190 + # Inputs 191 + 192 + `x` 193 + 194 + : 1\. Function argument 195 + 196 + `y` 197 + 198 + : 2\. Function argument 199 + */ 96 200 and = x: y: x && y; 97 201 98 - /* bitwise “not” */ 202 + /** 203 + bitwise “not” 204 + */ 99 205 bitNot = builtins.sub (-1); 100 206 101 - /* Convert a boolean to a string. 207 + /** 208 + Convert a boolean to a string. 102 209 103 - This function uses the strings "true" and "false" to represent 104 - boolean values. Calling `toString` on a bool instead returns "1" 105 - and "" (sic!). 210 + This function uses the strings "true" and "false" to represent 211 + boolean values. Calling `toString` on a bool instead returns "1" 212 + and "" (sic!). 213 + 106 214 107 - Type: boolToString :: bool -> string 215 + # Inputs 216 + 217 + `b` 218 + 219 + : 1\. Function argument 220 + 221 + # Type 222 + 223 + ``` 224 + boolToString :: bool -> string 225 + ``` 108 226 */ 109 227 boolToString = b: if b then "true" else "false"; 110 228 111 - /* Merge two attribute sets shallowly, right side trumps left 229 + /** 230 + Merge two attribute sets shallowly, right side trumps left 231 + 232 + mergeAttrs :: attrs -> attrs -> attrs 233 + 112 234 113 - mergeAttrs :: attrs -> attrs -> attrs 235 + # Inputs 114 236 115 - Example: 116 - mergeAttrs { a = 1; b = 2; } { b = 3; c = 4; } 117 - => { a = 1; b = 3; c = 4; } 237 + `x` 238 + 239 + : Left attribute set 240 + 241 + `y` 242 + 243 + : Right attribute set (higher precedence for equal keys) 244 + 245 + 246 + # Examples 247 + :::{.example} 248 + ## `lib.trivial.mergeAttrs` usage example 249 + 250 + ```nix 251 + mergeAttrs { a = 1; b = 2; } { b = 3; c = 4; } 252 + => { a = 1; b = 3; c = 4; } 253 + ``` 254 + 255 + ::: 118 256 */ 119 257 mergeAttrs = 120 - # Left attribute set 121 258 x: 122 - # Right attribute set (higher precedence for equal keys) 123 259 y: x // y; 124 260 125 - /* Flip the order of the arguments of a binary function. 261 + /** 262 + Flip the order of the arguments of a binary function. 263 + 264 + 265 + # Inputs 266 + 267 + `f` 268 + 269 + : 1\. Function argument 270 + 271 + `a` 272 + 273 + : 2\. Function argument 274 + 275 + `b` 276 + 277 + : 3\. Function argument 278 + 279 + # Type 280 + 281 + ``` 282 + flip :: (a -> b -> c) -> (b -> a -> c) 283 + ``` 284 + 285 + # Examples 286 + :::{.example} 287 + ## `lib.trivial.flip` usage example 126 288 127 - Type: flip :: (a -> b -> c) -> (b -> a -> c) 289 + ```nix 290 + flip concat [1] [2] 291 + => [ 2 1 ] 292 + ``` 128 293 129 - Example: 130 - flip concat [1] [2] 131 - => [ 2 1 ] 294 + ::: 132 295 */ 133 296 flip = f: a: b: f b a; 134 297 135 - /* Apply function if the supplied argument is non-null. 298 + /** 299 + Apply function if the supplied argument is non-null. 136 300 137 - Example: 138 - mapNullable (x: x+1) null 139 - => null 140 - mapNullable (x: x+1) 22 141 - => 23 301 + 302 + # Inputs 303 + 304 + `f` 305 + 306 + : Function to call 307 + 308 + `a` 309 + 310 + : Argument to check for null before passing it to `f` 311 + 312 + 313 + # Examples 314 + :::{.example} 315 + ## `lib.trivial.mapNullable` usage example 316 + 317 + ```nix 318 + mapNullable (x: x+1) null 319 + => null 320 + mapNullable (x: x+1) 22 321 + => 23 322 + ``` 323 + 324 + ::: 142 325 */ 143 326 mapNullable = 144 - # Function to call 145 327 f: 146 - # Argument to check for null before passing it to `f` 147 328 a: if a == null then a else f a; 148 329 149 330 # Pull in some builtins not included elsewhere. ··· 155 336 156 337 ## nixpkgs version strings 157 338 158 - /* Returns the current full nixpkgs version number. */ 339 + /** 340 + Returns the current full nixpkgs version number. 341 + */ 159 342 version = release + versionSuffix; 160 343 161 - /* Returns the current nixpkgs release number as string. */ 344 + /** 345 + Returns the current nixpkgs release number as string. 346 + */ 162 347 release = lib.strings.fileContents ./.version; 163 348 164 - /* The latest release that is supported, at the time of release branch-off, 165 - if applicable. 349 + /** 350 + The latest release that is supported, at the time of release branch-off, 351 + if applicable. 166 352 167 - Ideally, out-of-tree modules should be able to evaluate cleanly with all 168 - supported Nixpkgs versions (master, release and old release until EOL). 169 - So if possible, deprecation warnings should take effect only when all 170 - out-of-tree expressions/libs/modules can upgrade to the new way without 171 - losing support for supported Nixpkgs versions. 353 + Ideally, out-of-tree modules should be able to evaluate cleanly with all 354 + supported Nixpkgs versions (master, release and old release until EOL). 355 + So if possible, deprecation warnings should take effect only when all 356 + out-of-tree expressions/libs/modules can upgrade to the new way without 357 + losing support for supported Nixpkgs versions. 172 358 173 - This release number allows deprecation warnings to be implemented such that 174 - they take effect as soon as the oldest release reaches end of life. */ 359 + This release number allows deprecation warnings to be implemented such that 360 + they take effect as soon as the oldest release reaches end of life. 361 + */ 175 362 oldestSupportedRelease = 176 363 # Update on master only. Do not backport. 177 364 2311; 178 365 179 - /* Whether a feature is supported in all supported releases (at the time of 180 - release branch-off, if applicable). See `oldestSupportedRelease`. */ 366 + /** 367 + Whether a feature is supported in all supported releases (at the time of 368 + release branch-off, if applicable). See `oldestSupportedRelease`. 369 + 370 + 371 + # Inputs 372 + 373 + `release` 374 + 375 + : Release number of feature introduction as an integer, e.g. 2111 for 21.11. 376 + Set it to the upcoming release, matching the nixpkgs/.version file. 377 + */ 181 378 isInOldestRelease = 182 - /* Release number of feature introduction as an integer, e.g. 2111 for 21.11. 183 - Set it to the upcoming release, matching the nixpkgs/.version file. 184 - */ 185 379 release: 186 380 release <= lib.trivial.oldestSupportedRelease; 187 381 188 - /* Returns the current nixpkgs release code name. 382 + /** 383 + Returns the current nixpkgs release code name. 189 384 190 - On each release the first letter is bumped and a new animal is chosen 191 - starting with that new letter. 385 + On each release the first letter is bumped and a new animal is chosen 386 + starting with that new letter. 192 387 */ 193 388 codeName = "Uakari"; 194 389 195 - /* Returns the current nixpkgs version suffix as string. */ 390 + /** 391 + Returns the current nixpkgs version suffix as string. 392 + */ 196 393 versionSuffix = 197 394 let suffixFile = ../.version-suffix; 198 395 in if pathExists suffixFile 199 396 then lib.strings.fileContents suffixFile 200 397 else "pre-git"; 201 398 202 - /* Attempts to return the the current revision of nixpkgs and 203 - returns the supplied default value otherwise. 399 + /** 400 + Attempts to return the the current revision of nixpkgs and 401 + returns the supplied default value otherwise. 402 + 403 + 404 + # Inputs 405 + 406 + `default` 407 + 408 + : Default value to return if revision can not be determined 204 409 205 - Type: revisionWithDefault :: string -> string 410 + # Type 411 + 412 + ``` 413 + revisionWithDefault :: string -> string 414 + ``` 206 415 */ 207 416 revisionWithDefault = 208 - # Default value to return if revision can not be determined 209 417 default: 210 418 let 211 419 revisionFile = "${toString ./..}/.git-revision"; ··· 217 425 218 426 nixpkgsVersion = warn "lib.nixpkgsVersion is a deprecated alias of lib.version." version; 219 427 220 - /* Determine whether the function is being called from inside a Nix 221 - shell. 428 + /** 429 + Determine whether the function is being called from inside a Nix 430 + shell. 431 + 432 + # Type 222 433 223 - Type: inNixShell :: bool 434 + ``` 435 + inNixShell :: bool 436 + ``` 224 437 */ 225 438 inNixShell = builtins.getEnv "IN_NIX_SHELL" != ""; 226 439 227 - /* Determine whether the function is being called from inside pure-eval mode 228 - by seeing whether `builtins` contains `currentSystem`. If not, we must be in 229 - pure-eval mode. 440 + /** 441 + Determine whether the function is being called from inside pure-eval mode 442 + by seeing whether `builtins` contains `currentSystem`. If not, we must be in 443 + pure-eval mode. 444 + 445 + # Type 230 446 231 - Type: inPureEvalMode :: bool 447 + ``` 448 + inPureEvalMode :: bool 449 + ``` 232 450 */ 233 451 inPureEvalMode = ! builtins ? currentSystem; 234 452 235 453 ## Integer operations 236 454 237 - /* Return minimum of two numbers. */ 455 + /** 456 + Return minimum of two numbers. 457 + 458 + 459 + # Inputs 460 + 461 + `x` 462 + 463 + : 1\. Function argument 464 + 465 + `y` 466 + 467 + : 2\. Function argument 468 + */ 238 469 min = x: y: if x < y then x else y; 239 470 240 - /* Return maximum of two numbers. */ 471 + /** 472 + Return maximum of two numbers. 473 + 474 + 475 + # Inputs 476 + 477 + `x` 478 + 479 + : 1\. Function argument 480 + 481 + `y` 482 + 483 + : 2\. Function argument 484 + */ 241 485 max = x: y: if x > y then x else y; 242 486 243 - /* Integer modulus 487 + /** 488 + Integer modulus 489 + 490 + 491 + # Inputs 492 + 493 + `base` 494 + 495 + : 1\. Function argument 496 + 497 + `int` 498 + 499 + : 2\. Function argument 500 + 501 + 502 + # Examples 503 + :::{.example} 504 + ## `lib.trivial.mod` usage example 505 + 506 + ```nix 507 + mod 11 10 508 + => 1 509 + mod 1 10 510 + => 1 511 + ``` 244 512 245 - Example: 246 - mod 11 10 247 - => 1 248 - mod 1 10 249 - => 1 513 + ::: 250 514 */ 251 515 mod = base: int: base - (int * (builtins.div base int)); 252 516 253 517 254 518 ## Comparisons 255 519 256 - /* C-style comparisons 520 + /** 521 + C-style comparisons 257 522 258 - a < b, compare a b => -1 259 - a == b, compare a b => 0 260 - a > b, compare a b => 1 523 + a < b, compare a b => -1 524 + a == b, compare a b => 0 525 + a > b, compare a b => 1 526 + 527 + 528 + # Inputs 529 + 530 + `a` 531 + 532 + : 1\. Function argument 533 + 534 + `b` 535 + 536 + : 2\. Function argument 261 537 */ 262 538 compare = a: b: 263 539 if a < b ··· 266 542 then 1 267 543 else 0; 268 544 269 - /* Split type into two subtypes by predicate `p`, take all elements 270 - of the first subtype to be less than all the elements of the 271 - second subtype, compare elements of a single subtype with `yes` 272 - and `no` respectively. 545 + /** 546 + Split type into two subtypes by predicate `p`, take all elements 547 + of the first subtype to be less than all the elements of the 548 + second subtype, compare elements of a single subtype with `yes` 549 + and `no` respectively. 550 + 551 + 552 + # Inputs 553 + 554 + `p` 555 + 556 + : Predicate 273 557 274 - Type: (a -> bool) -> (a -> a -> int) -> (a -> a -> int) -> (a -> a -> int) 558 + `yes` 275 559 276 - Example: 277 - let cmp = splitByAndCompare (hasPrefix "foo") compare compare; in 560 + : Comparison function if predicate holds for both values 278 561 279 - cmp "a" "z" => -1 280 - cmp "fooa" "fooz" => -1 562 + `no` 281 563 282 - cmp "f" "a" => 1 283 - cmp "fooa" "a" => -1 284 - # while 285 - compare "fooa" "a" => 1 564 + : Comparison function if predicate holds for neither value 565 + 566 + `a` 567 + 568 + : First value to compare 569 + 570 + `b` 571 + 572 + : Second value to compare 573 + 574 + # Type 575 + 576 + ``` 577 + (a -> bool) -> (a -> a -> int) -> (a -> a -> int) -> (a -> a -> int) 578 + ``` 579 + 580 + # Examples 581 + :::{.example} 582 + ## `lib.trivial.splitByAndCompare` usage example 583 + 584 + ```nix 585 + let cmp = splitByAndCompare (hasPrefix "foo") compare compare; in 586 + 587 + cmp "a" "z" => -1 588 + cmp "fooa" "fooz" => -1 589 + 590 + cmp "f" "a" => 1 591 + cmp "fooa" "a" => -1 592 + # while 593 + compare "fooa" "a" => 1 594 + ``` 595 + 596 + ::: 286 597 */ 287 598 splitByAndCompare = 288 - # Predicate 289 - p: 290 - # Comparison function if predicate holds for both values 291 - yes: 292 - # Comparison function if predicate holds for neither value 293 - no: 294 - # First value to compare 295 - a: 296 - # Second value to compare 297 - b: 599 + p: yes: no: a: b: 298 600 if p a 299 601 then if p b then yes a b else -1 300 602 else if p b then 1 else no a b; 301 603 302 604 303 - /* Reads a JSON file. 605 + /** 606 + Reads a JSON file. 607 + 608 + 609 + # Inputs 610 + 611 + `path` 304 612 305 - Type: importJSON :: path -> any 613 + : 1\. Function argument 614 + 615 + # Type 616 + 617 + ``` 618 + importJSON :: path -> any 619 + ``` 306 620 */ 307 621 importJSON = path: 308 622 builtins.fromJSON (builtins.readFile path); 309 623 310 - /* Reads a TOML file. 624 + /** 625 + Reads a TOML file. 626 + 627 + 628 + # Inputs 629 + 630 + `path` 631 + 632 + : 1\. Function argument 633 + 634 + # Type 311 635 312 - Type: importTOML :: path -> any 636 + ``` 637 + importTOML :: path -> any 638 + ``` 313 639 */ 314 640 importTOML = path: 315 641 builtins.fromTOML (builtins.readFile path); ··· 329 655 # TODO: figure out a clever way to integrate location information from 330 656 # something like __unsafeGetAttrPos. 331 657 332 - /* 658 + /** 333 659 Print a warning before returning the second argument. This function behaves 334 660 like `builtins.trace`, but requires a string message and formats it as a 335 661 warning, including the `warning: ` prefix. ··· 337 663 To get a call stack trace and abort evaluation, set the environment variable 338 664 `NIX_ABORT_ON_WARN=true` and set the Nix options `--option pure-eval false --show-trace` 339 665 340 - Type: string -> a -> a 666 + # Inputs 667 + 668 + `msg` 669 + 670 + : Warning message to print. 671 + 672 + `val` 673 + 674 + : Value to return as-is. 675 + 676 + # Type 677 + 678 + ``` 679 + string -> a -> a 680 + ``` 341 681 */ 342 682 warn = 343 683 if lib.elem (builtins.getEnv "NIX_ABORT_ON_WARN") ["1" "true" "yes"] 344 684 then msg: builtins.trace "warning: ${msg}" (abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors.") 345 685 else msg: builtins.trace "warning: ${msg}"; 346 686 347 - /* 687 + /** 348 688 Like warn, but only warn when the first argument is `true`. 349 689 350 - Type: bool -> string -> a -> a 690 + 691 + # Inputs 692 + 693 + `cond` 694 + 695 + : 1\. Function argument 696 + 697 + `msg` 698 + 699 + : 2\. Function argument 700 + 701 + `val` 702 + 703 + : Value to return as-is. 704 + 705 + # Type 706 + 707 + ``` 708 + bool -> string -> a -> a 709 + ``` 351 710 */ 352 711 warnIf = cond: msg: if cond then warn msg else x: x; 353 712 354 - /* 713 + /** 355 714 Like warnIf, but negated (warn if the first argument is `false`). 356 715 357 - Type: bool -> string -> a -> a 716 + 717 + # Inputs 718 + 719 + `cond` 720 + 721 + : 1\. Function argument 722 + 723 + `msg` 724 + 725 + : 2\. Function argument 726 + 727 + `val` 728 + 729 + : Value to return as-is. 730 + 731 + # Type 732 + 733 + ``` 734 + bool -> string -> a -> a 735 + ``` 358 736 */ 359 737 warnIfNot = cond: msg: if cond then x: x else warn msg; 360 738 361 - /* 739 + /** 362 740 Like the `assert b; e` expression, but with a custom error message and 363 741 without the semicolon. 364 742 ··· 369 747 Calls can be juxtaposed using function application, as `(r: r) a = a`, so 370 748 `(r: r) (r: r) a = a`, and so forth. 371 749 372 - Type: bool -> string -> a -> a 373 750 374 - Example: 751 + # Inputs 375 752 376 - throwIfNot (lib.isList overlays) "The overlays argument to nixpkgs must be a list." 377 - lib.foldr (x: throwIfNot (lib.isFunction x) "All overlays passed to nixpkgs must be functions.") (r: r) overlays 378 - pkgs 753 + `cond` 379 754 755 + : 1\. Function argument 756 + 757 + `msg` 758 + 759 + : 2\. Function argument 760 + 761 + # Type 762 + 763 + ``` 764 + bool -> string -> a -> a 765 + ``` 766 + 767 + # Examples 768 + :::{.example} 769 + ## `lib.trivial.throwIfNot` usage example 770 + 771 + ```nix 772 + throwIfNot (lib.isList overlays) "The overlays argument to nixpkgs must be a list." 773 + lib.foldr (x: throwIfNot (lib.isFunction x) "All overlays passed to nixpkgs must be functions.") (r: r) overlays 774 + pkgs 775 + ``` 776 + 777 + ::: 380 778 */ 381 779 throwIfNot = cond: msg: if cond then x: x else throw msg; 382 780 383 - /* 781 + /** 384 782 Like throwIfNot, but negated (throw if the first argument is `true`). 385 783 386 - Type: bool -> string -> a -> a 784 + 785 + # Inputs 786 + 787 + `cond` 788 + 789 + : 1\. Function argument 790 + 791 + `msg` 792 + 793 + : 2\. Function argument 794 + 795 + # Type 796 + 797 + ``` 798 + bool -> string -> a -> a 799 + ``` 387 800 */ 388 801 throwIf = cond: msg: if cond then throw msg else x: x; 389 802 390 - /* Check if the elements in a list are valid values from a enum, returning the identity function, or throwing an error message otherwise. 803 + /** 804 + Check if the elements in a list are valid values from a enum, returning the identity function, or throwing an error message otherwise. 805 + 806 + 807 + # Inputs 808 + 809 + `msg` 810 + 811 + : 1\. Function argument 812 + 813 + `valid` 814 + 815 + : 2\. Function argument 816 + 817 + `given` 818 + 819 + : 3\. Function argument 820 + 821 + # Type 822 + 823 + ``` 824 + String -> List ComparableVal -> List ComparableVal -> a -> a 825 + ``` 826 + 827 + # Examples 828 + :::{.example} 829 + ## `lib.trivial.checkListOfEnum` usage example 391 830 392 - Example: 393 - let colorVariants = ["bright" "dark" "black"] 394 - in checkListOfEnum "color variants" [ "standard" "light" "dark" ] colorVariants; 395 - => 396 - error: color variants: bright, black unexpected; valid ones: standard, light, dark 831 + ```nix 832 + let colorVariants = ["bright" "dark" "black"] 833 + in checkListOfEnum "color variants" [ "standard" "light" "dark" ] colorVariants; 834 + => 835 + error: color variants: bright, black unexpected; valid ones: standard, light, dark 836 + ``` 397 837 398 - Type: String -> List ComparableVal -> List ComparableVal -> a -> a 838 + ::: 399 839 */ 400 840 checkListOfEnum = msg: valid: given: 401 841 let ··· 410 850 411 851 ## Function annotations 412 852 413 - /* Add metadata about expected function arguments to a function. 414 - The metadata should match the format given by 415 - builtins.functionArgs, i.e. a set from expected argument to a bool 416 - representing whether that argument has a default or not. 417 - setFunctionArgs : (a → b) → Map String Bool → (a → b) 853 + /** 854 + Add metadata about expected function arguments to a function. 855 + The metadata should match the format given by 856 + builtins.functionArgs, i.e. a set from expected argument to a bool 857 + representing whether that argument has a default or not. 858 + setFunctionArgs : (a → b) → Map String Bool → (a → b) 859 + 860 + This function is necessary because you can't dynamically create a 861 + function of the { a, b ? foo, ... }: format, but some facilities 862 + like callPackage expect to be able to query expected arguments. 863 + 864 + 865 + # Inputs 866 + 867 + `f` 868 + 869 + : 1\. Function argument 870 + 871 + `args` 418 872 419 - This function is necessary because you can't dynamically create a 420 - function of the { a, b ? foo, ... }: format, but some facilities 421 - like callPackage expect to be able to query expected arguments. 873 + : 2\. Function argument 422 874 */ 423 875 setFunctionArgs = f: args: 424 876 { # TODO: Should we add call-time "type" checking like built in? ··· 426 878 __functionArgs = args; 427 879 }; 428 880 429 - /* Extract the expected function arguments from a function. 430 - This works both with nix-native { a, b ? foo, ... }: style 431 - functions and functions with args set with 'setFunctionArgs'. It 432 - has the same return type and semantics as builtins.functionArgs. 433 - setFunctionArgs : (a → b) → Map String Bool. 881 + /** 882 + Extract the expected function arguments from a function. 883 + This works both with nix-native { a, b ? foo, ... }: style 884 + functions and functions with args set with 'setFunctionArgs'. It 885 + has the same return type and semantics as builtins.functionArgs. 886 + setFunctionArgs : (a → b) → Map String Bool. 887 + 888 + 889 + # Inputs 890 + 891 + `f` 892 + 893 + : 1\. Function argument 434 894 */ 435 895 functionArgs = f: 436 896 if f ? __functor 437 897 then f.__functionArgs or (functionArgs (f.__functor f)) 438 898 else builtins.functionArgs f; 439 899 440 - /* Check whether something is a function or something 441 - annotated with function args. 900 + /** 901 + Check whether something is a function or something 902 + annotated with function args. 903 + 904 + 905 + # Inputs 906 + 907 + `f` 908 + 909 + : 1\. Function argument 442 910 */ 443 911 isFunction = f: builtins.isFunction f || 444 912 (f ? __functor && isFunction (f.__functor f)); 445 913 446 - /* 914 + /** 447 915 `mirrorFunctionArgs f g` creates a new function `g'` with the same behavior as `g` (`g' x == g x`) 448 916 but its function arguments mirroring `f` (`lib.functionArgs g' == lib.functionArgs f`). 449 917 450 - Type: 451 - mirrorFunctionArgs :: (a -> b) -> (a -> c) -> (a -> c) 452 918 453 - Example: 454 - addab = {a, b}: a + b 455 - addab { a = 2; b = 4; } 456 - => 6 457 - lib.functionArgs addab 458 - => { a = false; b = false; } 459 - addab1 = attrs: addab attrs + 1 460 - addab1 { a = 2; b = 4; } 461 - => 7 462 - lib.functionArgs addab1 463 - => { } 464 - addab1' = lib.mirrorFunctionArgs addab addab1 465 - addab1' { a = 2; b = 4; } 466 - => 7 467 - lib.functionArgs addab1' 468 - => { a = false; b = false; } 919 + # Inputs 920 + 921 + `f` 922 + 923 + : Function to provide the argument metadata 924 + 925 + `g` 926 + 927 + : Function to set the argument metadata to 928 + 929 + # Type 930 + 931 + ``` 932 + mirrorFunctionArgs :: (a -> b) -> (a -> c) -> (a -> c) 933 + ``` 934 + 935 + # Examples 936 + :::{.example} 937 + ## `lib.trivial.mirrorFunctionArgs` usage example 938 + 939 + ```nix 940 + addab = {a, b}: a + b 941 + addab { a = 2; b = 4; } 942 + => 6 943 + lib.functionArgs addab 944 + => { a = false; b = false; } 945 + addab1 = attrs: addab attrs + 1 946 + addab1 { a = 2; b = 4; } 947 + => 7 948 + lib.functionArgs addab1 949 + => { } 950 + addab1' = lib.mirrorFunctionArgs addab addab1 951 + addab1' { a = 2; b = 4; } 952 + => 7 953 + lib.functionArgs addab1' 954 + => { a = false; b = false; } 955 + ``` 956 + 957 + ::: 469 958 */ 470 959 mirrorFunctionArgs = 471 - # Function to provide the argument metadata 472 960 f: 473 961 let 474 962 fArgs = functionArgs f; 475 963 in 476 - # Function to set the argument metadata to 477 964 g: 478 965 setFunctionArgs g fArgs; 479 966 480 - /* 967 + /** 481 968 Turns any non-callable values into constant functions. 482 969 Returns callable values as is. 483 970 484 - Example: 971 + 972 + # Inputs 973 + 974 + `v` 975 + 976 + : Any value 977 + 978 + 979 + # Examples 980 + :::{.example} 981 + ## `lib.trivial.toFunction` usage example 982 + 983 + ```nix 984 + nix-repl> lib.toFunction 1 2 985 + 1 485 986 486 - nix-repl> lib.toFunction 1 2 487 - 1 987 + nix-repl> lib.toFunction (x: x + 1) 2 988 + 3 989 + ``` 488 990 489 - nix-repl> lib.toFunction (x: x + 1) 2 490 - 3 991 + ::: 491 992 */ 492 993 toFunction = 493 - # Any value 494 994 v: 495 995 if isFunction v 496 996 then v 497 997 else k: v; 498 998 499 - /* Convert the given positive integer to a string of its hexadecimal 500 - representation. For example: 999 + /** 1000 + Convert the given positive integer to a string of its hexadecimal 1001 + representation. For example: 501 1002 502 - toHexString 0 => "0" 1003 + toHexString 0 => "0" 503 1004 504 - toHexString 16 => "10" 1005 + toHexString 16 => "10" 505 1006 506 - toHexString 250 => "FA" 1007 + toHexString 250 => "FA" 507 1008 */ 508 1009 toHexString = let 509 1010 hexDigits = { ··· 520 1021 else hexDigits.${toString d}; 521 1022 in i: lib.concatMapStrings toHexDigit (toBaseDigits 16 i); 522 1023 523 - /* `toBaseDigits base i` converts the positive integer i to a list of its 524 - digits in the given base. For example: 1024 + /** 1025 + `toBaseDigits base i` converts the positive integer i to a list of its 1026 + digits in the given base. For example: 1027 + 1028 + toBaseDigits 10 123 => [ 1 2 3 ] 1029 + 1030 + toBaseDigits 2 6 => [ 1 1 0 ] 1031 + 1032 + toBaseDigits 16 250 => [ 15 10 ] 525 1033 526 - toBaseDigits 10 123 => [ 1 2 3 ] 527 1034 528 - toBaseDigits 2 6 => [ 1 1 0 ] 1035 + # Inputs 529 1036 530 - toBaseDigits 16 250 => [ 15 10 ] 1037 + `base` 1038 + 1039 + : 1\. Function argument 1040 + 1041 + `i` 1042 + 1043 + : 2\. Function argument 531 1044 */ 532 1045 toBaseDigits = base: i: 533 1046 let