Commit Graph

144 Commits

Author SHA1 Message Date
Robert Hensing
89b4bd8b24 lib/options: Add showOptionWithDefLocs 2022-06-21 14:22:33 +02:00
pennae
320aa2a791 treewide: attempt at markdown option docs 2022-06-12 12:44:38 +02:00
Robert Hensing
b37801bc3f
Merge pull request #157480 from MatthewCroughan/in-pure-eval-mode
lib: add inPureEvalMode
2022-06-10 16:31:31 +02:00
Robert Scott
da9162f667 add mechanism for handling meta.sourceProvenance attributes
heavily based on patterns used by licenses infrastructure, so may
appear overengineered for its initial level of use
2022-05-30 16:27:34 +08:00
adisbladis
161776ec1e Revert "lib: init flakes.nix"
This reverts commit PR #167947.

Flakes aren't standardised and the `lib` namespace shouldn't be
polluted with utilities that serve only experimental uses.
2022-05-15 22:39:46 +12:00
matthewcroughan
4d9801a78f lib: add inPureEvalMode
This makes a value that is true if builtins does not contain the
currentSystem function, but false if it does.
2022-05-06 15:42:52 +01:00
Artturi
a5357d06e4
Merge pull request #167947 from MatthewCroughan/mc/callLocklessFlake
lib: add callLocklessFlake
2022-05-01 23:06:17 +03:00
Robert Hensing
5f8cb21011
Merge pull request #170583 from ncfavier/mkShellVars
lib/strings: add toShellVars
2022-04-27 23:33:28 +02:00
Naïm Favier
226bc99659
lib/strings: add toShellVars
A straightforward piece of plumbing to safely inject Nix variables into
shell scripts:

''
  ${lib.toShellVars { inherit foo bar; }}
  cmd "$foo" --bar "$bar"
''
2022-04-27 16:04:17 +02:00
Artturin
379b9c8be3 lib/meta: add getExe to get the main program of a drv 2022-04-24 04:19:49 +03:00
matthewcroughan
cad8bbe589 lib: init flakes.nix
This commit creates flakes.nix, which is a library containing functions
which relate to interacting with flakes. It also moves related functions
from trivial.nix into it.
2022-04-12 19:28:23 +01:00
matthewcroughan
c190b08bb7 lib: add callLocklessFlake
This is essentially a copy of the function of the same name, from
flake-compat. callLocklessFlake is useful when trying to utilise a
flake.nix without a lock file, often for when you want to create a
subflake from within a parent flake.

Co-authored-by: Tom Bereknyei <tomberek@gmail.com>
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
2022-04-12 19:27:24 +01:00
Naïm Favier
9f5796be49
lib/trivial: actually expose warnIfNot and throwIf 2022-03-27 16:48:33 +02:00
Robert Hensing
6c469679f6 Merge remote-tracking branch 'upstream/master' into tests-restrict-arguments 2022-03-21 23:17:17 +01:00
Robert Hensing
84274cbc95 lib: Add toFunction 2022-03-21 23:14:10 +01:00
Robert Hensing
6a0b24b276 lib: applyIfFunction -> applyModuleArgsIfFunction 2022-03-21 23:14:10 +01:00
Kevin Cox
8ce16fcf62
Merge pull request #163451 from hercules-ci/stop-premature-warnings
Stop premature warnings, including `nix.settings` migration
2022-03-21 10:10:40 -04:00
Silvan Mosberger
85003ecdbb lib.attrsets: Introduce updateManyAttrsByPath 2022-03-18 00:08:29 +01:00
Silvan Mosberger
71b130c581 lib.attrsets: Introduce showAttrPath 2022-03-18 00:05:08 +01:00
Robert Hensing
7377ea57ff lib: Add mkRenamedOptionModuleWith
Adds support for sinceRelease
2022-03-09 14:50:51 +01:00
0x4A6F
3362ff0f7b
Merge pull request #157301 from pennae/lib-add-mkPackageOption
lib: add mkPackageOption to default.nix
2022-02-28 20:37:59 +01:00
pennae
1c0918d828 lib: add mkPackageOption to default.nix
this was forgotten in #155669
2022-01-29 18:28:20 +01:00
Naïm Favier
5a07097772
lib/modules: introduce setDefaultModuleLocation
Wraps a module with a default location for reporting errors.
2022-01-27 22:12:53 +01:00
Robert Hensing
ba3e91ed43 lib.types: Add unique like uniq, but custom errors
Couldn't extend types.uniq and it had a silly name anyway.
Now we can have better error messages.
2022-01-24 16:14:55 +01:00
José Romildo
4fc67da841 lib.checkListOfEnum: init 2022-01-09 19:21:08 -03:00
Robert Hensing
f2c5c706f4 lib.throwIfNot: init 2021-12-22 13:13:50 +01:00
Shamrock Lee
60950f739e lib/meta: add getLicenseFromSpdxId function
Move function spdxLicense, internally used in yarn2nix
to lib/meta.nix, and
rename to getLicenseFromSpdxId

A similar function is implemented in poetry2nix,
but the one originally in yarn2nix seems beter.
since it falls back to an license-like attrset
for mismatched case
instead of a plain string
2021-12-02 18:33:48 +08:00
Taeer Bar-Yam
0bef0c38f7 lib.modules: add mkDerivedConfig
mkDerivedConfig : Option a -> (a -> Definition b) -> Definition b

Create config definitions with the same priority as the definition of another option.
This should be used for option definitions where one option sets the value of another as a convenience.
For instance a config file could be set with a `text` or `source` option, where text translates to a `source`
value using `mkDerivedConfig options.text (pkgs.writeText "filename.conf")`.

It takes care of setting the right priority using `mkOverride`.
2021-11-15 07:03:41 -05:00
Robert Hensing
0699530f08
Merge pull request #136909 from ncfavier/cleanup-defaults-examples
nixos/doc: clean up defaults and examples
2021-10-04 20:37:42 +02:00
Naïm Favier
52a2e4136e
lib/options: add literalExpression and literalDocBook, deprecate literalExample 2021-10-03 17:19:19 +02:00
Robert Helgesson
e75f346aa3
lib: add function escapeXML
Given a string, this function returns a string that can be inserted
verbatim in an XML document.
2021-10-03 11:28:03 +02:00
Harrison Houghton
9d71711aec lib: export strings/escapeRegex
I see no reason why I should be disallowed from using it.
2021-08-22 00:01:38 -04:00
lassulus
229ff549e6 lib: inherit mkImageMediaOverride 2021-08-18 14:21:16 +02:00
Robert Hensing
cad20d8983 lib.mkFixStrictness: Deprecate 2021-07-12 07:31:29 +02:00
Robert Hensing
99bc203025 Partially revert "lib/modules: Drop mkStrict and mkFixStrictness"
mkFixStrictness was never properly deprecated and should only be
removed after warning for some time.

This partially reverts commit 8fb9984690.
2021-07-12 07:25:52 +02:00
Janne Heß
8fb9984690
lib/modules: Drop mkStrict and mkFixStrictness
This was deprecated in 2014 and is not used anywhere in the tree.
2021-06-06 20:53:05 +02:00
Alyssa Ross
81e1e68eaf lib.trivial.warnIf: init
It's a common pattern in Nixpkgs to want to emit a warning in certain
cases, but not actually change behaviours.

This is often expressed as either
	if cond then lib.warn "Don't do that thing" x else x
Or
	(if cond then lib.warn "Don't do that thing" else lib.id) x

Neither of which really expresses the intent here, because it looks
like 'x' is being chosen conditionally.

To make this clearer, I introduce a "warnIf" function, which makes it
clear that the only thing being affected by the condition is whether
the warning is generated, not the value being returned.
2021-04-28 21:44:21 +00:00
Jacek Galowicz
123045a570 lib/attrsets: add cartesianProductOfSets function 2021-01-28 23:08:59 +01:00
Profpatsch
41e13149f7 lib/debug: add traceFnSeqN
Immensely helpful when you want to see the changes a function makes to
its value as it passes through.

Example:

```
$ nix-instantiate --strict --eval -E '(with import ./lib; traceFnSeqN 2 "id" (x: x) { a.b.c = 3; })'
trace: {
  fn = "id";
  from = {
    a = {
      b = {…};
    };
  };
  to = {
    a = {
      b = {…};
    };
  };
}
{ a = { b = { c = 3; }; }; }
```
2021-01-25 19:25:50 +01:00
John Ericson
a6218c058b lib: Create makeScopeWithSplicing
It's ugly as hell, but I suppose it is needed to codify how to make
spliced package sets.
2020-11-19 00:07:14 -05:00
Joe Hermaszewski
c3b35f21f7 lib: Add composeManyExtensions 2020-11-13 21:37:57 +01:00
Robert Hensing
fe4a58eec0 lib: Add lib.trace for consistency
This puts it among the trace* family of functions derived from it.
2020-10-22 13:46:48 +02:00
Robert Hensing
9a4bed1a80 lib: Add lib.isFloat for consistency
Unlike the other three is* functions in lib.trivial, it was only
available as lib.trivial.isFloat
2020-10-22 13:46:48 +02:00
Robert Hensing
afa6c51f27 lib: Use Nix's static scope checking, fix error message, optimize
Nix can perform static scope checking, but whenever code is inside
a `with` expression, the analysis breaks down, because it can't
know statically what's in the attribute set whose attributes were
brought into scope. In those cases, Nix has to assume that
everything works out.

Except it doesnt. Removing `with` from lib/ revealed an undefined
variable in an error message.

If that doesn't convince you that we're better off without `with`,
I can tell you that this PR results in a 3% evaluation performance
improvement because Nix can look up local variables by index.
This adds up with applications like the module system.

Furthermore, removing `with` makes the binding site of each
variable obvious, which helps with comprehension.
2020-10-22 13:46:47 +02:00
zimbatm
947a7d33f9
lib: add importTOML
Complements the `lib.importJSON`. `builtins.readTOML` has been
introduced in Nix 2.1.
2020-09-12 16:37:50 +02:00
Bas van Dijk
6e7822b8f3 lib: toHex -> toHexString & toBase -> toBaseDigits
This makes the type of these functions more apparent from the name.
2020-07-20 13:14:19 +02:00
Bas van Dijk
00022fbeda lib: add the toHex and toBase utility functions
`toHex` converts the given positive integer to a string of the hexadecimal
representation of that integer. For example:

```
toHex 0 => "0"

toHex 16 => "10"

toHex 250 => "FA"
```

`toBase base i` converts the positive integer `i` to a list of it
digits in the given `base`. For example:

```
toBase 10 123 => [ 1 2 3 ]

toBase 2 6 => [ 1 1 0 ]

toBase 16 250 => [ 15 10 ]
```
2020-07-20 13:09:26 +02:00
rnhmjoj
986079275b
lib/attrsets: add getMan function 2020-06-18 10:16:57 +02:00
Emily
aff9979170 lib.fake{Sri => Hash}: fix and rename
The previous hash was too short and caused evaluation-time errors like:

    invalid SRI hash 'sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA='

Additionally, since the fact that this is broken implies that nobody
could have been using it, "SRI" is a bit of a vague and obscure term,
`fakeSriHash` would be somewhat of a mouthful, and the relevant fetcher
parameters are just called `hash`, rename it to `fakeHash`.
2020-05-11 23:11:12 +01:00
Tony Olagbaiye
d2ba3fba69 lib: add shortcut for fake SRI hash 2020-04-05 01:48:20 +01:00