Commit graph

205 commits

Author SHA1 Message Date
Robert Hensing acb6191c6e
lib: Add splicing utilities (#426889) 2025-10-30 21:22:54 +00: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
Johannes Kirschbauer 78ac637056
lib: init lists.uniqueStrings 2025-08-15 11:45:50 +02:00
Emily 449ad44f16 lib: fix overflowing fromHexString tests and example
`fromHexString` is backed by `builtins.fromTOML`. Per [the TOML
v1.0.0 specification]:

> Arbitrary 64-bit signed integers (from −2^63 to 2^63−1) should be
> accepted and handled losslessly. If an integer cannot be represented
> losslessly, an error must be thrown.

[the TOML v1.0.0 specification]: <https://toml.io/en/v1.0.0#integer>

The saturating behaviour of the toml11 version currently used
by Nix is not lossless, and is therefore a violation of the TOML
specification. We should not be relying on it. This blocks the update
of toml11, as it became stricter about reporting this condition.

This, yes, is arguably an evaluation compatibility break. However,
integer overflow was recently explicitly defined as an error by
both Nix and Lix, as opposed to the C++ undefined behaviour it was
previously implemented as:

* <https://nix.dev/manual/nix/stable/release-notes/rl-2.25>
* <https://docs.lix.systems/manual/lix/stable/release-notes/rl-2.91.html#fixes>

This included changing `builtins.fromJSON` to explicitly
reject overflowing integer literals. I believe that the case for
`builtins.fromTOML` is comparable, and that we are effectively testing
undefined behaviour in TOML and the Nix language here, in the same way
that we would have been if we had tests relying on overflowing integer
arithmetic. I am not aware of any use of this behaviour outside of
these tests; the reverted toml11 bump in Nix did not break the 23.11
evaluation regression test, for example.

C++ undefined behaviour is not involved here, as toml11 used the C++
formatted input functions that are specified to saturate on invalid
values. But it’s still a violation of the TOML specification caused
by insufficient error checking in the old version of the library,
and inconsistent with the handling of overflowing literals in the
rest of Nix.

Let’s fix this so that Nix implementations can correctly flag up
this error and we can unblock the toml11 update.
2025-08-14 21:02:11 +01:00
Emily 6673e05ad0 lib: add fromHexString tests for distressing behaviour
This was cherry‐picked from
<https://github.com/NixOS/nixpkgs/pull/266705> and merged as part of
<https://github.com/NixOS/nixpkgs/pull/318712>, despite there being
a blocking review on the former pointing out these kinds of issues.

This documents some of the dodgy behaviour. It also can’t handle
negative literals. It might be worth considering deprecating and
dropping this, by inlining it into `lib.network.ipv6.fromString`,
its only in‐tree user.
2025-08-14 21:02:11 +01:00
Sergei Zimmerman 76a7b54083
lib/tests/misc: don't hardcode store directory 2025-08-14 19:18:08 +03:00
Sergei Zimmerman 74799dd97c
lib/tests/misc: don't import nixpkgs 2025-08-14 19:10:08 +03:00
Rebecca Turner 4a81a5e556
lib.packagesFromDirectoryRecursive: Allow non-"path" directory
As initially designed, `lib.packagesFromDirectoryRecursive` allowed
passing a string for the `directory` argument. This is necessary for
several reasons:

- `outPath` on derivations and Flake inputs is not a path.
- Derivations can be coerced to their `outPath` in string interpolation,
  but that produces strings, not paths.
- `builtins.path`, bizarrely, returns a string instead of a path (not
  that the documentation makes this clear).

If a path is used instead of a string here, then Nix will dutifully copy
the entire directory into a new path in the Nix store (ignored as
WONTFIX by Eelco in https://github.com/NixOS/nix/issues/9428). For
industrial use cases, this can result in an extra 10-15 seconds on every
single eval just to copy files from one spot in the Nix store to another
spot in the Nix store.

In #361424, this was changed so that `directory` must be a path,
breaking these use-cases.

I'm not really sure what happened here -- #361424 has very little
justification for why it exists, only a reference to a previous version
of the PR (#359941), which itself had very little justification given.
The description on #359941 explained that it would "Shrink the
function's code by ~2/3rd 🎉", but 60% of the reduction in size was just
deleting comments (!) and bindings like `directoryEntryIsPackage` that
helped clarify the intent of the implementation. As a result, the new
implementation is (to my eyes) more challenging to read and understand.
I think the whole thing was in service of #392800, which adds a
`newScope` argument in order "to create nested scopes for each
(sub)directory (not just the top-level one) when `newScope` is given."

Nobody noticed this regression until after the commit was merged. After
@phanirithvij pointed out the regression, @nbraud said they would
"shortly prepare a PR to fix this" [1] but did not. Later, they would
explain that they were "quite ill the last month(s)" [2], which explains
why this got forgotten about. @nbraud also requested a review from
@Gabriella439 [3], as she had reviewed the original PR adding
`lib.packagesFromDirectoryRecursive`, but not from me, the original
author of that PR. @Gabriella439 did not review the "refactor" PR, and
no attempt to contact her or myself was made after that initial request.
This behavior is admittedly rather subtle, so I'm not sure either
Gabriella or myself would have noticed the change (especially since the
relevant PR restructures the entire implementation).

While I find this a bit frustrating, I should have added a test for this
use-case in my original PR; if there was a test that relied on passing
paths in as a string, perhaps the authors modifying this code would have
noticed that the implementation was not an accident.

[1]: https://github.com/NixOS/nixpkgs/pull/361424#discussion_r1912407693
[2]: https://github.com/NixOS/nixpkgs/pull/359984#issuecomment-2775768808
[3]: https://github.com/NixOS/nixpkgs/pull/361424#issuecomment-2521308983
2025-07-10 16:13:38 -07:00
Robert Hensing ecc039f327 lib.filesystem.resolveDefaultNix: init 2025-06-21 21:43:37 +02:00
Gutyina Gergő b5720e2b3f
lib/strings: init replaceString 2025-05-28 00:20:56 +02:00
Heitor Augusto 24e8a790b7
lib.strings: init toCamelCase 2025-05-11 20:24:56 -03:00
Johannes Kirschbauer be393db73e
lib.takeEnd: init (#398222) 2025-04-23 14:20:47 +02:00
Pol Dellaiera a9320986b3
lib.strings: init splitStringBy (#385643) 2025-04-23 04:51:38 +00:00
Mikael Voss 487a002c63
lib/strings: Zero‐pad hex digits in escapeC
lib.strings.escapeC produces single‐digit hexadecimal strings for
character values ≤ 15, which results in an ambiguity. If the following
character is a hex digit, it will be interpreted as being part of the
escape sequence.

systemd, which also relies on C‐style escape sequences, does not
decode single‐digit sequences at all, even if unambiguous.

Padding the hexadecimal string with "0" avoids this problem.
2025-04-18 13:41:53 +02:00
azahi 9fee9e7371
lib.takeEnd: init 2025-04-13 17:37:35 +03:00
Robert Hensing e09cf9f066 lib.types.submodule: Disable check during docs generation
Put simply docs generation is a weird eval, and we should avoid
generating any unnecessary errors.
2025-04-11 00:32:01 +02:00
Heitor Augusto bb4be9a474
lib.strings: init splitStringBy 2025-04-07 15:51:18 -03:00
Silvan Mosberger 374e6bcc40 treewide: Format all Nix files
Format all Nix files using the officially approved formatter,
making the CI check introduced in the previous commit succeed:

  nix-build ci -A fmt.check

This is the next step of the of the [implementation](https://github.com/NixOS/nixfmt/issues/153)
of the accepted [RFC 166](https://github.com/NixOS/rfcs/pull/166).

This commit will lead to merge conflicts for a number of PRs,
up to an estimated ~1100 (~33%) among the PRs with activity in the past 2
months, but that should be lower than what it would be without the previous
[partial treewide format](https://github.com/NixOS/nixpkgs/pull/322537).

Merge conflicts caused by this commit can now automatically be resolved while rebasing using the
[auto-rebase script](8616af08d9/maintainers/scripts/auto-rebase).

If you run into any problems regarding any of this, please reach out to the
[formatting team](https://nixos.org/community/teams/formatting/) by
pinging @NixOS/nix-formatting.
2025-04-01 20:10:43 +02:00
nicoo 32235deeb8 lib.packagesFromDirectoryRecursive: default to creating new scopes when newScope is provided
Co-authored-by: Rebecca Turner <rbt@sent.as>
2025-03-26 12:11:44 +01:00
nicoo 41f219d1d9 lib.packagesFromDirectoryRecursive: add tests for nested scopes 2025-03-26 12:11:43 +01:00
nicoo fdd2c6f80f lib/tests: move packages-from-directory/* into a subdir
in preparation for adding more tests for `lib.packagesFromDirectoryRecursive`
2025-03-24 18:35:26 +01:00
Mikael Voss 7a07cc0da9
lib/strings: allow CA paths in isStorePath 2025-03-05 13:08:41 +01:00
Wroclaw 0de1d18795 lib.types.functionTo: preserve functionArgs
Previously if function in defs had set pattern in argument,
this information would be lost.

This keeps functionArgs in set pattern,
by using functor and `__functionArgs`
that is later used by `lib.functionArgs`.
2025-03-02 00:04:19 +01:00
Johannes Kirschbauer 0d52202380
lib.strings: init toSentenceCase (#381802) 2025-02-20 07:58:44 +07:00
isabel b109863c25 lib.strings: init toSentenceCase 2025-02-19 12:10:39 +00:00
Johannes Kirschbauer 9d3e649939
lib.types: init mergeTypes (#364620) 2025-02-10 22:10:30 +07:00
Johannes Kirschbauer 3c911b2aca
lib.types: init mergeTypes 2025-02-10 21:23:12 +07:00
Robert Hensing 782a0e6581 lib.dropEnd: init
Naming borrowed from Haskell
- `base`: [`dropWhileEnd`]
- `extra`, `mono-traversable`, ...: [`dropEnd`]

[`dropEnd`]: https://hackage.haskell.org/package/mono-traversable-1.0.21.0/docs/Data-Sequences.html#v:dropEnd
[`dropWhileEnd`]: https://hackage.haskell.org/package/base-4.21.0.0/docs/Data-List.html#v:dropWhileEnd
2025-01-03 12:04:06 +01:00
Robert Hensing c96e907825 lib/tests/misc: Test lib.drop 2025-01-03 12:03:33 +01:00
Silvan Mosberger d510f60a4d
lib.generators.toPlist: escape XML syntax in strings & keys (#356502) 2024-12-23 13:47:44 +01:00
Linnnus d1cb670ee6 lib.generators.toPlist: escape XML syntax in strings & keys
Before this patch, code like this would break generate invalid XML:

    lib.generators.toPlist {} "ab<cd"

That's obviously bad, since the call to toPlist often happens through
indirection, as is the case in e.g. the nix-darwin project. A user might
not realize that they have to escape the strings.

This patch adds the argument 'escape' to lib.generators.plist and emits
a warning if it is not set to true. In a future release, this behavior
should become the default.

I have also added a note for future maintainers, in case I forget to
actually remove the deprecated functionality in a future release.
2024-12-23 12:29:20 +01:00