Commit Graph

110 Commits

Author SHA1 Message Date
Robert Hensing
acb6191c6e lib: Add splicing utilities (#426889) 2025-10-30 21:22:54 +00:00
Aliaksandr
dc7095a86e treewide: remove deprecated lib functions that had warning for more than 2 years 2025-10-24 06:30:16 +03:00
Jeremy Fleischman
775ce27666 lib.customisation: fix error message when running in nix repl
This code was more careful before
<dd435697b3>
(it didn't assume that `unsafeGetAttrPos` always returns a non-null
location). Unfortunately, `unsafeGetAttrPos` *does* return `null` when
dealing with `nix repl`:

```
nix-repl> f = {foo}: foo

nix-repl> builtins.unsafeGetAttrPos "foo" (builtins.functionArgs f)
null
```

Here's how to reproduce the issue.

*Before* this fix:

```
nix-repl> f = {foo}: foo

nix-repl> myCallPackage = lib.callPackageWith {}

nix-repl> myCallPackage f {}
error:
       … while calling the 'abort' builtin
         at /home/jeremy/src/github.com/NixOS/nixpkgs/lib/customisation.nix:323:7:
          322|     else
          323|       abort "lib.customisation.callPackageWith: ${error}";
             |       ^
          324|

       … while selecting an attribute
         at /home/jeremy/src/github.com/NixOS/nixpkgs/lib/customisation.nix:310:14:
          309|         "Function called without required argument \"${arg}\" at "
          310|         + "${loc.file}:${toString loc.line}${prettySuggestions (getSuggestions arg)}";
             |              ^
          311|

       error: expected a set but found null: null

```

*After*:

```
nix-repl> f = {foo}: foo

nix-repl> myCallPackage = lib.callPackageWith {}

nix-repl> myCallPackage f {}
error:
       … while calling the 'abort' builtin
         at /home/jeremy/src/github.com/NixOS/nixpkgs/lib/customisation.nix:332:7:
          331|     # Inputs
          332|
             |       ^
          333|     `autoArgs`

       error: evaluation aborted with the following error message: 'lib.customisation.callPackageWith: Function called without required argument "foo" at <unknown location>'
```
2025-10-16 08:27:01 -04:00
Wolfgang Walther
91a8fee3aa treewide: remove redundant parentheses
Auto-fixed by nixf-diagnose.
2025-10-05 10:52:03 +02:00
Yueh-Shun Li
4fa63ffcca lib.customisation: inherit unsafeGetAttrsPos in let-in 2025-09-28 03:05:49 +08: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
Philip Taron
01df068cc8 lib, mkDerivation: Document overriding functions (#428563) 2025-08-27 06:13:54 -07:00
Matt Sturgeon
5a07f707c5 lib.customisation.callPackageWith: fix line number interpolation
We need to use `toString` on `loc.line` because it is an int.
2025-08-22 18:48:43 +01:00
Wolfgang Walther
dd435697b3 lib.customisation: remove unsafeGetAttrPos workaround
This could have been done a while ago, minver has been > 2.3.4 for quite
some time already.
2025-08-12 15:52:37 +02:00
Robert Hensing
b3255718ac lib, mkDerivation: Document overriding functions
These doc comments are mainly for consumption in the repl.
2025-07-27 21:44:18 +02:00
Wolfgang Walther
5a0711127c treewide: run nixfmt 1.0.0 2025-07-24 13:55:40 +02:00
Robert Hensing
66016feb83 lib.callPackageWith: Use resolveDefaultNix
Tested with:
1. Replace the callPackageWith call by `null`, to simulate an ancient Nix
2. Run the following commands in a terminal in nixpkgs:

    $ mkdir test/

    $ echo '{ asdfasdfasdf }: null' >test/default.nix

    $ nix repl -f .

    nix-repl> callPackage ./test { }
    error:
          … while calling the 'abort' builtin
            at /home/user/src/nixpkgs/lib/customisation.nix:312:7:
              311|     else
              312|       abort "lib.customisation.callPackageWith: ${error}";
                |       ^
              313|

          error: evaluation aborted with the following error message: 'lib.customisation.callPackageWith: Function called without required argument "asdfasdfasdf" at /home/user/src/nixpkgs/test/default.nix'
2025-06-21 21:43:37 +02:00
Yueh-Shun Li
2439ead791 lib.extendMkDerivation: init
Add functions extendMkDerivation to lib.customisation.

Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
Co-authored-by: Lin Jian <me@linj.tech>
Co-authored-by: Philip Taron <philip.taron@gmail.com>
2025-01-18 14:20:45 +08:00
Yueh-Shun Li
e5a9567c3a lib.customisation: remove dead code
Remove unneeded function inheritance in the let-in block.
2024-12-20 21:02:54 +08:00
Yueh-Shun Li
8788c4bff9 lib/customisation.nix: format with nixfmt-rfc-style 2024-12-20 20:57:46 +08:00
Wolfgang Walther
af10dd2014 lib/customisation: remove overrideScope'
Let's follow through on the comment.
2024-11-24 21:09:32 +01:00
Kazuki Okamoto
55eedcc6dc just fix markup 2024-08-17 16:47:58 +09:00
Johannes Kirschbauer
3dfd61965e doc: migrate lib.customisation to use doc-comments 2024-03-22 10:02:09 +01:00
Valentin Gagarin
41298a0dff doc: actually document lib.customisation.makeScope (#294194)
* doc: actually document `lib.customisation.makeScope`
2024-03-09 19:29:13 +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
Silvan Mosberger
56df668386 lib.callPackageWith: Use abort, not throw
This reverts f8ea911f7c, see also https://github.com/NixOS/nixpkgs/pull/271123#discussion_r1442134594
2024-01-07 23:31:11 +01:00
Robert Hensing
016993237f lib.callPackageWith: Optimize levenshtein sort
Probably not significant because of the limits already applied.
This is mostly cleanup.
2023-12-08 22:15:29 +01:00
Someone Serge
a3006991c3 lib/customisation: fix eval error (attribute "levenshtein" missing) 2023-12-03 03:49:22 +00:00
K900
37445f3c5c lib/customisation: fix callPackage error messages 2023-12-03 01:28:17 +03:00
Adam Joseph
f8ea911f7c lib.customisation.callPackageWith: use throw, not abort 2023-11-30 07:43:16 +01:00
adisbladis
8d162ec7b8 lib.customisation: Don't allocate intermediate list for missing args 2023-11-27 21:06:42 +13:00
adisbladis
7903613b0b lib.customisation: Inherit lib/builtins into scope
It makes the code more readable if we have less nested attrsets being accessed.
2023-11-27 21:06:41 +13:00
Yueh-Shun Li
6a8b6b8f72 lib.makeOverridable: simplify function arguments preservation
Rename temporary variable copyArgs -> mirrorArgs.
Use lib.mirrorFunctionArgs to define `mirrorArgs`.
Apply mirrorArgs also to the returned function.
2023-11-07 01:58:48 +00:00
Artturin
dd48cb0b34 lib.makeScopeWithSplicing': add comments
I didn't add these arguments, so these comments are from my understading of the arguments.
2023-11-02 17:47:02 +02:00
Yueh-Shun Li
0695d3e8fe lib.overrideDerivation: inter-link the documentation
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-10-21 16:39:12 +08:00
Yueh-Shun Li
93b8f14a2d doc: lib.customization: add Type and Example tags
Add the "Type:" blocks.
Move the examples below the descriptions whenever possibles
Add "Example:" tags before the examples moved below the descriptions.
2023-10-21 16:39:12 +08:00
Robert Hensing
0a10279342 lib.makeOverridable: fix functionArgs on returned function 2023-10-11 22:56:53 +02:00
nicoo
fe138d36c9 doc: Replace sha256 with hash where appropriate 2023-09-13 17:24:49 +00:00
Artturin
724cc0cba3 makeScopeWithSplicing: fix makeScopeWithSplicing' call
makeScopeWithSplicing: fix comment
2023-08-21 19:57:34 +03:00
Artturi
bde196dde9 Merge pull request #245957 from amjoseph-nixpkgs/pr/lib/customization/makeScopeWithSplicing2 2023-08-20 18:57:05 +03:00
Artturin
3c1f82f99e lib.customisation.makeScope: Make overrideScope consistent with makeScopeWithSplicing
Right now converting `makeScope` to `makeScopeWithSplicing` is not
transparent to users and requires adding a warning for `overrideScope'`
in the set itself.

Warning and `overrideScope'` were added in 2018 b9dce11712 and there should be no users left after 5 years.
2023-08-14 18:46:47 +03:00
Adam Joseph
a1fdbae706 lib.customisation: add uncurried form of makeScopeWithSplicing
Deeply-curried functions are pretty error-prone in untyped languages
like Nix.  This is a particularly bad case because
`top-level/splice.nix` *also* declares a makeScopeWithSplicing, but
it takes *two fewer arguments*.

Let's add a version that uses attrset-passing form, to provide some
minimal level of sanity-checking.

This also provides defaults for keep and extra (these are often
unneeded by the user).
2023-08-14 02:50:32 +03:00
Silvan Mosberger
a0b8caf3bc Revert "lib.customisation: uncurry makeScopeWithSplicing" 2023-07-28 23:04:09 +02:00
Artturin
3716ef19d8 lib.makeScopeWithSplicing: provide default for keep,extra
These are often unneeded by the user.
2023-07-28 17:41:01 +03:00
Adam Joseph
cb13669b00 lib.customisation: uncurry makeScopeWithSplicing
Deeply-curried functions are pretty error-prone in untyped languages
like Nix.  This is a particularly bad case because
`top-level/splice.nix` *also* declares a makeScopeWithSplicing, but
it takes *two fewer arguments*.

Let's switch to attrset-passing form, to provide some minimal level
of sanity-checking.
2023-07-27 21:31:59 -07:00
Artturin
ec7d5c518c splice.nix: finish nativeDrv,crossDrv removal 2023-06-22 23:11:08 +03:00
Cole Helbling
d9f767600f lib/customisation: callPackageWith should abort with errors
ofborg relies on the behavior that existed prior to
1c00bf3948, where evaluation would
immediately abort due to a missing argument (whether it be an aliased
package when `allowAliases = false;` or a typo'd or otherwise
nonexistent package).

If `callPackageWith` `throw`s instead of `abort`s, the following
`nix-env` invocation does not fail fast but instead silently skips the
attribute (assuming there is a package that has an aliased package in
its `autoArgs`):

    $ nix-env -qa --json --file . --arg config '{ allowAliases = false; }' &>/dev/null
    $ echo $?
    0

This does change the error output when there is a missing package (for
any of the reasons mentioned above), though. Before this change, the
errors looked like this:

    $ nix-build -A hello --arg config '{ allowAliases = false; }'
    error:
           … while calling the 'throw' builtin

             at /home/vin/workspace/vcs/nixpkgs/master/lib/customisation.nix:179:65:

              178|
              179|     in if missingArgs == [] then makeOverridable f allArgs else throw error;
                 |                                                                 ^
              180|

           error: Function called without required argument "bash_5" at /home/vin/workspace/vcs/nixpkgs/master/pkgs/applications/misc/hello/default.nix:8, did you mean "bash" or "bashdb"?

And the errors now look like this:

    $ nix-build -A hello --arg config '{ allowAliases = false; }'
    error:
           … while calling the 'abort' builtin

             at /home/vin/workspace/vcs/nixpkgs/master/lib/customisation.nix:179:65:

              178|
              179|     in if missingArgs == [] then makeOverridable f allArgs else abort error;
                 |                                                                 ^
              180|

           error: evaluation aborted with the following error message: 'Function called without required argument "bash_5" at /home/vin/workspace/vcs/nixpkgs/master/pkgs/applications/misc/hello/default.nix:8, did you mean "bash" or "bashdb"?'
2023-03-24 14:22:11 -07:00
Artturi
dcc7df7fe6 Merge pull request #211685 from Artturin/splicingstuff1-split 2023-02-03 12:49:08 +02:00
Robert Hensing
473ac9692e lib.hydraJob: Tolerate null
By allowing null, we allow code to avoid filterAttrs, improving
laziness in real world use cases.
Specifically, this strategy prevents infinite recursion errors,
performance issues and possibly other errors that are unrelated to
the user's code.
2023-01-30 00:35:34 +01:00
Robert Hensing
5b2f597b11 lib.extendDerivation: Fix interaction between output selection and overrideAttrs 2023-01-20 02:45:34 +02:00
YoshiRulz
7fba83890c lib: Fix mismatched quotes in lib.* doc comments
caused problems for automated rich text generation such as
https://teu5us.github.io/nix-lib.html#customisation-functions
2023-01-02 08:25:17 +10:00
Naïm Favier
4df10debe7 lib/customisation.overrideDerivation: propagate evaluation condition
The new derivation should evaluate only if the old derivation does.

Sadly this means that the old derivation cannot depend on the new one
any more, which was used by xorgserver on Darwin. But this is not a
problem as `overrideAttrs` can (and should) usually be used instead.

This change allowed catching an invalid `meta.platforms` in the linux_rpi
kernels, which use `overrideDerivation`.
2023-01-01 14:10:42 +01:00
Artturin
341e6fd558 splice.nix: start deprecating nativeDrv and crossDrv 2022-11-19 00:04:54 +02:00
Artturin
7022556622 lib.overrideDerivation: override attrs in __spliced 2022-11-18 18:44:12 +02:00