Commit graph

181 commits

Author SHA1 Message Date
Plume 678ff5cf1c 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>
(cherry picked from commit 0477b30ab6)
2025-09-07 12:46:11 +00:00
Sergei Zimmerman 0e04293d59 lib/tests/misc: don't hardcode store directory
(cherry picked from commit 76a7b54083)
2025-08-18 16:43:17 +00:00
Sergei Zimmerman e888a09931 lib/tests/misc: don't import nixpkgs
(cherry picked from commit 74799dd97c)
2025-08-18 16:43:16 +00:00
Emily fce01f7d71 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.

(cherry picked from commit 449ad44f16)
2025-08-16 12:26:14 +00:00
Emily b591001250 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.

(cherry picked from commit 6673e05ad0)
2025-08-16 12:26:13 +00:00
Gutyina Gergő feb1a72ead lib/strings: init replaceString
(cherry picked from commit b5720e2b3f)
2025-07-09 16:41:16 +00: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
Johannes Kirschbauer d504a1e680
lib.types.attrsWith: add placeholder parameter 2024-12-09 15:25:18 +01:00
Yueh-Shun Li b1371135b5 lib.strings.concatMapAttrsStringSep: init 2024-12-06 11:02:26 +08:00
h7x4 83e1488c6d
lib/tests: test filterAttrs 2024-10-25 15:08:15 +02:00
Yueh-Shun Li 11c20cd390 lib.toExtension: init
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
2024-09-14 20:56:47 +08:00
Yueh-Shun Li 6749b1c4bc lib/tests/misc.nix: move testFix into new category FIXED-POINTS 2024-09-14 20:54:10 +08:00
Gabriella Gonzalez 75c122699a
lib.cli.escapeShellArg{,s}: Only escape when necessary (#333744)
These utilities will now leave the string undisturbed if it doesn't need to be quoted (because it doesn't have any special characters). This can help generate nicer-looking command lines.

This also transitively improves the output of `lib.toGNUCommandLine` which uses `escapeShellArg` internally
2024-08-16 16:26:08 +02:00
Matt Sturgeon aad87c2aa8
lib.strings: add trim and trimWith
`strings.trim` returns a copy of the string with all leading and trailing
whitespace removed.

`strings.trimWith` does the same thing, but calling code can decide
whether to trim the start and/or end of the string.
2024-07-25 23:43:33 +01:00
Silvan Mosberger 7c6692836e
Merge pull request #149990 from ShamrockLee/lib-spdx-license 2024-07-25 22:27:10 +02:00
Yueh-Shun Li 10d2e6c906 lib.getLicenseFromSpdxIdOr: init
Add lib.meta.getLicenseFromSpdxIdOr as a variant of
lib.meta.getLicenseFromSpdxId that explicitly state the default
(fallback) value if there's no license matching the given SPDX ID.
2024-06-30 02:59:58 +08:00
lucasew 52cc703bba lib: add fromHexString
Co-authored-by: lucasew <lucas59356@gmail.com>
2024-06-28 20:53:53 +03:00
Rob Pilling 9c9c3848d4 cli.nix: permit separators between args -> -a=b, --xyz=abc, etc 2024-05-22 07:58:26 +01:00
Silvan Mosberger 6861ef7707 lib.lists.ifilter0: init 2024-04-22 18:33:14 +02:00
Gabriel Volpe 7b687a59cd
lib/tests: apply lints 2024-04-15 19:18:41 +02:00
Gabriel Volpe fe2bead78b
lib/attrsets: introduce mapCartesianProduct 2024-04-15 19:16:15 +02:00
Gabriel Volpe 10517cf9ab
tree-wide: use cartesianProduct 2024-04-15 19:13:22 +02:00
Silvan Mosberger a448a21521
Merge pull request #301556 from edef1c/lib-xor
lib: add xor
2024-04-04 22:03:53 +02:00
edef 725bb4e48c lib: add xor
This gets clumsily reimplemented in various places, to no useful end.
2024-04-04 19:46:58 +00:00
Johannes Kirschbauer c0f5f271d1
doc: migrate trivial files to doc-comment format (#299986)
* doc: migrate trivial files to doc-comment format

* fix: revert some comments

* Apply suggestions from code review

Thanks @danielSidhion

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

* Update lib/types.nix

---------

Co-authored-by: Daniel Sidhion <DanielSidhion@users.noreply.github.com>
Co-authored-by: Silvan Mosberger <github@infinisil.com>
2024-04-04 16:36:07 +02:00
Florian Richer 3b883d3cdf
lib/strings: Add makeIncludePath (#296237)
* Update strings.nix

* Fix typo in docs

* Update lib/strings.nix

Co-authored-by: lolbinarycat <dogedoge61+github@gmail.com>

* Update lib/strings.nix

Co-authored-by: lolbinarycat <dogedoge61+github@gmail.com>

* Add unit test with strings

* Move test to strings

* Add unit test with package structure

* testMakeIncludePathWithPkgs: use real pkgs

* Revert "testMakeIncludePathWithPkgs: use real pkgs"

This reverts commit fb1850c069.

* Update lib/tests/misc.nix

Co-authored-by: lolbinarycat <dogedoge61+github@gmail.com>

* Update lib/tests/misc.nix

Co-authored-by: Silvan Mosberger <github@infinisil.com>

---------

Co-authored-by: lolbinarycat <dogedoge61+github@gmail.com>
Co-authored-by: Silvan Mosberger <github@infinisil.com>
2024-04-02 19:01:05 +02:00
Philip Taron c8a8550c28
Avoid top-level with in lib/tests/misc.nix 2024-03-11 16:30:31 -07:00
Silvan Mosberger fc3cc21d2a
Merge pull request #292209 from hercules-ci/lazyDerivation-multi-output
lib.lazyDerivation: Support multi-output derivations
2024-03-06 17:14:33 +01:00
Robert Hensing 612dcbe11e lib.lazyDerivation: Support multi-output derivations 2024-02-29 12:37:52 +01:00
Andrew Childs 741377b300 lib/customization: propagate function arguments in callPackagesWith
makeOverridable is very careful to ensure the arguments to the
overridden function are the same as the input function. As a result,
the arguments of hello.override are exactly the same as the original
arguments of the hello function that produced the derivation.

However, callPackagesWith calls makeOverridable with a lambda that
does not propagate the arguments. The override function for a package
instantiated with callPackagesWith will not have the original
arguments.

For example:

    nix-repl> lib.functionArgs hello.override
    { callPackage = false; fetchurl = false; hello = false; lib = false; nixos = false; stdenv = false; testers = false; }

    nix-repl> lib.functionArgs openssl.override
    { }

By copying the arguments onto the inner lambda before passing it to
makeOverridable, we can make callPackage and callPackages behave the
same.

    nix-repl> lib.functionArgs openssl.override
    { buildPackages = false; coreutils = false; cryptodev = false; enableSSL2 = true; enableSSL3 = true; fetchurl = false; lib = false; perl = false; removeReferencesTo = false; static = true; stdenv = false; withCryptodev = true; withPerl = true; }
2024-02-28 15:29:08 +02:00
Shea Levy ca1262a483
lib: Add optionalDrvAttr to conditionally set drv attributes.
This allows for adding new, conditionally set, derivation attributes
to an existing derivation without changing any output paths in the
case where the condition is not met.
2024-02-02 16:27:30 -05:00