234 Commits

Author SHA1 Message Date
Yueh-Shun Li
653dd12f1c lib.functionArgs: fix correctness for nested functors
Restore the correct behaviour prior to commit bb71feaec1
("lib.functionArgs: save a function call on every lookup")
while preserving its performance gain with best efforts.
2026-10-16 07:38:34 +08:00
Yueh-Shun Li
85effadebf lib.isFunction: fix correctness when taking nested functors
Restore the correct behaviour prior to commit 77334757d7
("lib.isFunction: save an attribute lookup")
while preserving its performance gain.
2026-10-16 07:38:33 +08:00
Ethan Carter Edwards
2c2686cbbe lib.meta.getDarwinApp: init, add tests
This is useful for nix-darwin system.defaults.dock.persistent-apps

Signed-off-by: Ethan Carter Edwards <ethan@ethancedwards.com>
2026-09-20 17:17:38 -04:00
Ethan Carter Edwards
7246360b7d lib.meta.getDarwinApp': init, add tests
This is useful for nix-darwin system.defaults.dock.persistent-apps

Signed-off-by: Ethan Carter Edwards <ethan@ethancedwards.com>
2026-09-20 17:16:41 -04:00
Samuel Ainsworth
17be3e0800 lib/fileset/internal.nix: introduce lib.lists.commonPrefixLength (#507639) 2026-09-17 21:52:01 +00:00
Ryan Hendrickson
418b436480 lib.escapeShellArg: handle paths (#556219) 2026-09-15 17:03:22 +00:00
Ryan Hendrickson
c5ba1a0b3a lib.escapeShellArg: handle paths
Path values should be copied into the store if they're going to be used
in a shell script. In other words, `"my-command ${arg}"` and
`"my-command ${lib.escapeShellArg arg}"` should always do the same thing
if `arg` is a path; it should not be the case that the first is safe and
the second results in an out-of-store reference.
2026-08-24 20:10:20 -04:00
Shahar "Dawn" Or
703a4f5e00 lib.isFunction: failing test for non-function __functor
This test errors trying to call the non-function `__functor`.
2026-08-14 23:54:03 +07:00
Shahar "Dawn" Or
77f6b3a664 lib.isFunction: add tests 2026-08-14 23:42:47 +07:00
Emily
af8b1be8bb lib/tests/misc: use x86_64-freebsd instead of x86_64-darwin 2026-06-26 07:58:16 +01:00
Eman Resu
f3e6fa555f lib.attrsets.concatMapAttrs: add test for handling duplicates 2026-06-21 18:04:34 -04:00
Robert Hensing
e29bf2412b lib.modules.mapDefinitionValue: init 2026-05-24 15:32:04 +02:00
Michael Daniels
18e82c4591 lib: remove unused let bindings and args@{} uses 2026-04-30 18:43:56 -04:00
Johannes Kirschbauer
5c9f6077a7 lib/concatLines: add test for empty list 2026-04-28 10:08:32 +02:00
jopejoe1
dbdb37908f lib/tests: add tests for lib.licenses 2026-04-19 12:22:11 +02:00
Samuel Ainsworth
45f2fdec4a lib/fileset/internal.nix: nixfmt 2026-04-07 11:50:34 -04:00
Samuel Ainsworth
9a70ebfb14 lib/fileset/internal.nix: introduce lib.lists.commonPrefixLength
Extracts the existing length-computation logic from `commonPrefix` into
its own function so callers that only need the length can avoid the
intermediate list allocation. `commonPrefix` is reimplemented in terms
of the new function. Updates the two TODOs in `lib/fileset/internal.nix`
that were waiting for this.
2026-04-07 11:42:49 -04:00
Johannes Kirschbauer
075bb50b1a lib.modules: default to emptyValue (#500104) 2026-03-25 11:00:39 +00:00
cinereal
4c7577dd94 lib.options: reflect emptyValue-based option defaults in documentation
Signed-off-by: cinereal <cinereal@riseup.net>
2026-03-24 14:02:55 +01:00
Philip Taron
4085f24e25 lib.makeScope: add tests for callPackage overriding
Test that:
- makeScope provides a default callPackage
- the scope function can override callPackage
- the override persists through overrideScope
2026-03-19 14:11:11 -07:00
Dyego Aurélio
28096cc5e3 treewide: apply nixfmt 1.2.0 2026-01-22 18:37:56 -03:00
Glen Huang
0445cc8aab lib.lists.replaceElemAt: init
It's very easy to resort to lib.take + lib.drop to accomplish this, but it's not obvious that it actually performs quite badly.

Replacing an item in a list should be a relative common operation.
2026-01-12 11:58:42 +00:00
Shelvacu
5018a33b62 lib/strings.escapeNixIdentifier: quote keywords
Previously `lib.strings.escapeNixIdentifier "assert"` would return `"assert"`; This is wrong, `assert` is not a valid identifier unless quoted.\

This also fixes `lib.generators.toPretty`: `toPretty {} { "assert" = false; }` would previously return an invalid expression.
2026-01-02 13:47:32 -08:00
Robert Hensing
34b2f6702f lib.makeOverridable: preserve constructor override and metadata attributes (#461032) 2025-12-22 13:20:41 +00:00
Robert Hensing
649a766e35 lib/tests: move throwTestFailures tests to standalone script
The throwTestFailures tests in misc.nix produce confusing trace output
that looks like test failures even when tests pass.

This commit moves these tests to a new lib/tests/debug.sh script,
following the pattern from modules.sh, sources.sh, and filesystem.sh.

The confusing trace output does not appear in any logs anymore.

Example of the confusing log:

trace: FAIL "testDerivation":
Expected:
<derivation a>

Result:
<derivation b>

evaluation warning: Using `lib.generators.toPlist` without `escape = true` is deprecated
[ ]
2025-11-26 23:51:59 +01:00
Johannes Kirschbauer
3ae8192162 lib: deprecate fold (#456532) 2025-11-25 14:15:41 +00:00
Yueh-Shun Li
21021adbe9 lib.makeOverrdable: preserve constructor override and metadata attributes 2025-11-13 18:01:44 +08:00
Rebecca Turner
dc4cf16993 lib.debug.throwTestFailures: init
`lib.debug.runTests` provides a unit test evaluator for Nix, but its
results are returned in a raw and difficult-to-read form.

Currently, different callers output the results in various ways:
`builtins.throw (builtins.toJSON failures)` and `builtins.throw ("Tests
failed: " + lib.generators.toPretty { } failures)` are both used.

This change adds a new `lib.debug.throwTestFailures` function which
displays the results nicely before throwing an exception (or returns
`null` if no failures are given), unifying these disparate call-sites.

First, each failing test is pretty-printed in a `trace` message:

```
trace: FAIL testDerivation:
  Expected: <derivation a>
    Result: <derivation b>
```

Then, an exception is thrown containing the number of tests that failed
(and their names), followed by the raw JSON of the results (for parity
with previous usage, and because `lib.generators.toPretty` sometimes
omits information that `builins.toJSON` includes):

```
error:
       … while evaluating the file '...':

       … caused by explicit throw
         at /nix/store/.../lib/debug.nix:528:7:
          527|       in
          528|       throw (
             |       ^
          529|         builtins.seq traceFailures (

       error: 1 tests failed:
       - testDerivation

       [{"expected":"/nix/store/xh7kyqp69mxkwspmi81a94m9xx74r8dr-a","name":"testDerivation","result":"/nix/store/503l84nir4zw57d1shfhai25bxxn16c6-b"}]
```
2025-11-10 10:41:31 -08:00
Robert Hensing
acb6191c6e lib: Add splicing utilities (#426889) 2025-10-30 21:22:54 +00:00
Aliaksandr
f4d36941eb lib: deprecate fold 2025-10-30 16:56:49 +02:00
Lukas Wurzinger
73e8a483e6 lib/cli: add toCommandLine 2025-10-21 21:01:32 +02:00
Robert Hensing
6a5f6f1a4e lib: init strings.join (#446278) 2025-10-02 15:34:34 +00:00
NAHO
e8f998c3bc lib: add builtins prefixes for prelude functions to improve clarity 2025-09-30 09:38:12 +02:00
NAHO
6177c4ad72 lib: remove optional builtins prefixes from prelude functions
Remove optional builtins prefixes from prelude functions by running:

    builtins=(
      abort
      baseNameOf
      break
      derivation
      derivationStrict
      dirOf
      false
      fetchGit
      fetchMercurial
      fetchTarball
      fetchTree
      fromTOML
      import
      isNull
      map
      null
      placeholder
      removeAttrs
      scopedImport
      throw
      toString
      true
    )

    fd --type file . lib --exec-batch sed --in-place --regexp-extended "
      s/\<builtins\.($(
        printf '%s\n' "${builtins[@]}" |
          paste --delimiter '|' --serial -
      ))\>/\1/g
    "

    nix fmt
2025-09-30 09:02:59 +02:00
Johannes Kirschbauer
b37ac6a920 lib: init strings.join 2025-09-27 09:18:37 +02:00
Matt Sturgeon
6c20442479 lib.options.optionAttrSetToDocList: add visible = "transparent"
Allows marking an option as invisible, without excluding its sub-options.

In practice, this is similar to `visible = true; internal = true;`,
however it is more explicit and less reliant on implementation details.
2025-09-11 17:00:17 +01:00
Matt Sturgeon
1efdbe8f2f lib/tests/misc: add option visibility test 2025-09-10 14:34:59 +01:00
Robert Hensing
7ba464154f lib: Introduce Cross Index concept
A Cross Index, short for Cross Platform Pair Index, is the essential
shape of a splice, without the invoking the more mind bending concept
of adding variations of for these 6 pairings to an existing thing so
that it can be switched out for something else.

So the purpose of a Cross Index is to contain the result of `f`\
(which may be reified in code, or just an abstract concept):
 - f "build" "build"
 - f "build" "host"
 - ...

Splicing on the other hand refers not just to these six variants, but
to the idea of tacking them onto one of the variants. (hostTarget,
I believe)

Cross Indexes are a necessity for making cross compilation work, but
splicing is more than necessary.
2025-09-09 04:42:37 +02:00
Robert Hensing
ca4beaaf1c lib: Add splice structure utilities 2025-09-09 04:42:36 +02:00
Wolfgang Walther
399136b3d7 lib/attrsets: add mapAttrsToListRecursive(Cond) function (#395160) 2025-09-08 19:12:59 +00:00
Plume
0477b30ab6 lib.attrsets.genAttrs': init (#436434)
* lib.attrsets.genAttrs': init

* lib.attrsets.genAttrs: rewrite with lib.attrsets.genAttrs'

* lib.attrsets.{genAttrs,genAttrs'}: add tests

* Update lib/attrsets.nix

Co-authored-by: Johannes Kirschbauer <hsjobeki+github@gmail.com>

* Update lib/attrsets.nix

Co-authored-by: Johannes Kirschbauer <hsjobeki+github@gmail.com>

* lib.attrsets.genAttrs': document and test attrName collision

---------

Co-authored-by: Johannes Kirschbauer <hsjobeki+github@gmail.com>
2025-09-01 11:58:28 +02:00
Mikael Voss
e5ac84e29c lib/attrsets: add mapAttrsToListRecursive(Cond) function 2025-08-31 12:11:43 +02:00
Martin Weinelt
4bd0b9c7ab Reapply "Merge remote-tracking branch 'origin/master' into staging-next"
This reverts commit 106b1418bc.

Restores the commits lost during the revert of a merge on staging-next.
2025-08-23 16:06:41 +02:00
nixpkgs-ci[bot]
6870d2a5fa Merge master into staging-next 2025-08-17 18:05:03 +00:00
Emily
922b6476c5 lib: deprecate fromHexString on dodgy inputs (#434218) 2025-08-17 17:17:43 +01:00
nixpkgs-ci[bot]
2bdc906674 Merge master into staging-next 2025-08-16 18:05:04 +00:00
Emily
3c7d67da23 lib: deprecate fromHexString on dodgy inputs
See <https://github.com/NixOS/nixpkgs/pull/433710>.
2025-08-16 13:36:05 +01:00
Emily
677ccc5446 lib: fix overflowing fromHexString tests and example (#433710) 2025-08-16 13:25:31 +01:00
Martin Weinelt
106b1418bc Revert "Merge remote-tracking branch 'origin/master' into staging-next"
This reverts commit 28cafe5795, reversing
changes made to 281c9189d4.

Broken merge due to mergiraf removing many newlines from
python-packages.nix.
2025-08-15 13:58:54 +02:00
Johannes Kirschbauer
36bd116f11 lib: init lists.uniqueStrings (#355616) 2025-08-15 12:03:26 +02:00