135 Commits

Author SHA1 Message Date
Philip Taron
e03d0a783d various: reduce // merges, optimize ++ chains across hot paths (#506793) 2026-05-27 22:06:37 +00:00
Eman Resu
01272740d8 lib.extendMkDerivation: save several function calls 2026-05-19 20:59:24 -04:00
Eman Resu
a50c77cc33 lib.extendMkDerivation: use a single attrset instead of merging 2026-05-19 20:45:42 -04:00
Aliaksandr
20b7f2410b lib/customisation: reduce // merges in extendDerivation
Fold the 'all' attribute into the listToAttrs call alongside the outputs, so commonAttrs builds from drv // listToAttrs(...) // passthru // { drvPath; outPath; } instead of four separate merges with a standalone 'all' attrset.

Also convert remaining optionalAttrs to nullable attr names in extendDerivation, overrideDerivation, and makeOverridable.

Per-derivation // merges drop from 4 to 3.
2026-05-18 03:54:26 +03:00
Eman Resu
0b29c3289e lib.callPackageWith: check if args are already empty before handling defaults 2026-05-03 08:23:34 -04:00
Eman Resu
efd9c79a80 lib.callPackageWith: filter names instead of attributes 2026-05-03 08:23:33 -04:00
Eman Resu
54cbe35bf9 lib.callPackageWith: use custom version of filterAttrs
Avoids a double-negation and passing the name every time.
2026-05-03 08:23:33 -04:00
Eman Resu
caab17b274 lib.callPackageWith: move error message variables out of happy path 2026-05-03 08:23:32 -04:00
Eman Resu
6a07020e3c lib.overrideDerivation: rewrite to not be utterly ridiculous
Why are we running a `flip` on arguments we can control? Why are we
using `or` on one line only to use a full if/else on the next line? Why
are we merging with {}, and why have we been doing it for three years?
THese are the questions I ask myself.
2026-05-03 08:23:32 -04:00
Eman Resu
a72cd26ceb lib.makeOverridable: avoid some function calls 2026-05-03 08:23:26 -04:00
Eman Resu
cb1edb8183 lib.makeOverridable: inline helper function into its usages
Also perform some manual beta reductions.
2026-04-28 13:59:32 -04:00
Eman Resu
c33811716d lib.makeOverridable: avoid optionalAttrs merge 2026-04-28 13:59:32 -04:00
Eman Resu
1e022acbf5 lib.makeOverridable: inline several variables 2026-04-28 13:59:32 -04:00
Eman Resu
8ccea3bf6c lib.extendDerivation: avoid optionalAttrs merge 2026-04-28 13:59:32 -04:00
Eman Resu
6325e7ff14 lib.extendDerivation: inline variables 2026-04-28 13:59:31 -04:00
quantenzitrone
f08c835bf9 lib.newScope: allow overriding callPackage 2026-03-17 16:30:00 +01:00
İlkecan Bozdoğan
6acae46c84 lib: don't inherit unused functions 2026-03-04 00:10:00 +03:00
İlkecan Bozdoğan
e394a579b0 lib: update type signatures
- concrete types start with uppercase: Int, String, Bool, Derivation,
  etc.
- type variables start with lowercase: a, b, etc.
- list:
  - use `[x]` for homogeneous lists instead of `List x` or `[ x ]`
  - use `List` for heterogeneous lists (not that common in `lib`)
- attr:
  - use `AttrSet` for a generic attribute set type
  - use `{ key1 :: Type1; key2 :: Type2; ... }` for adding signatures
    for known attribute names and types
  - use `{ key1 = value1; key2 = value2; ... }` for adding attributes
    with known literals
  - end with an ellipsis (`...`) if the set can contain unknown
    attributes
  - use `{ [String] :: x }` if all the attributes has the same type `x`
- prefer `Any` over `a` if the latter is not reused
2026-03-04 00:10:00 +03:00
Robert Hensing
34b2f6702f lib.makeOverridable: preserve constructor override and metadata attributes (#461032) 2025-12-22 13:20:41 +00:00
Anton Mosich
367d102c09 docs/lib: use backticks for nix names 2025-11-29 21:46:54 +01:00
Yueh-Shun Li
21021adbe9 lib.makeOverrdable: preserve constructor override and metadata attributes 2025-11-13 18:01:44 +08:00
Yueh-Shun Li
3026703280 lib.extendMkDerivation: add argument excludeFunctionArgNames 2025-11-06 23:42:14 +08:00
Yueh-Shun Li
d75b203514 lib.extendMkDerivation: document the typical usage of inheritFunctionArgs 2025-11-06 23:37:18 +08:00
Yueh-Shun Li
7f6ce9097f lib.extendMkDerivation: fix argument documentation layout 2025-11-06 23:35:08 +08:00
Yueh-Shun Li
918e017d64 lib.extendMkDerivation: adjust default value documentation 2025-11-06 19:34:30 +08:00
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