358 Commits

Author SHA1 Message Date
Eman Resu
3ec1e90b10 lib/modules: remove warnIf usage
warnIf sends our warning message through a function call, even if the
warning condition doesn't trigger. This requires a lot of thunk
allocation that can be easily avoided.
2026-05-28 00:19:32 -04:00
Robert Hensing
e29bf2412b lib.modules.mapDefinitionValue: init 2026-05-24 15:32:04 +02:00
Johannes Kirschbauer
0bb05b5bd0 lib/modules: document limitations of 'mkRenamedOptionModule' (#470160) 2026-05-14 13:35:43 +00:00
Eman Resu
4a63856f02 lib.modules: avoid override filtering on singleton lists 2026-05-12 11:22:11 -04:00
Eman Resu
b707b446ba lib.modules: use concatMap for override filtering 2026-05-12 11:22:00 -04:00
Lin Jian
e0101cb9cf lib/modules: make comment more clear 2026-05-10 15:47:59 +08:00
Ben Siraphob
b787ee3d40 lib/modules: reduce allocations in evalModules and mergeModules
- When `evalModules` is called without deprecated `args`/`check` parameters (the common case), avoid `modules ++ []` which copies the entire module list for no reason.
- `collectModules` was calling `collectStructuredModules` twice with identical arguments — once for `filterModules` and once for `toGraph`. Share the result via a `let` binding.
- Replace `zipAttrsWith (n: v: v)` with `zipAttrs`
- Replace `concatMap` with `filter`+`map` in `filterOverrides'`

NixOS minimal eval improvement: -5.0% list_concats, -1.2%
nrFunctionCalls, -0.7% nrThunks, -1.0% envs, -1.5% list_bytes.
2026-04-19 09:52:40 -07:00
Ben Siraphob
67c22bba37 lib/modules: skip optional + ++ in evalOptionValue when no default (#510028) 2026-04-14 17:42:41 +00:00
Ben Siraphob
1bec8475a6 lib/modules: skip optional + ++ in evalOptionValue when no default
Replace `(optional (opt ? default) { ... }) ++ defs` with a direct
if/then/else. When the option has no default, this skips both the
list allocation and the ++ concat entirely. Most options do have
defaults, but even then we avoid the `optional` function call overhead
(closure + env allocation).

On NixOS minimal eval: -45K function calls (-0.93%), -3.7K list
concats (-1.93%).
2026-04-14 10:25:34 -07:00
Jörg Thalheim
a3b13a82d4 lib/modules: short-circuit mergeDefinitions for plain singletons
The vast majority of option definitions reaching mergeDefinitions are
singletons whose value carries no _type wrapper (mkIf/mkMerge/
mkOverride/mkOrder). For those, the dischargeProperties ->
filterOverrides' -> sortProperties pipeline is a no-op yet still costs
~125k function calls and ~70k thunks on a minimal NixOS system eval.

Detect this case up front and reuse the input list as
defsFinal'.values, keeping the addErrorContext around the value probe
so error traces still point at the defining file. highestPrio is
defaultOverridePriority by construction.

A single-fold filterOverrides' was prototyped but allocates an attrset
per definition and regressed gc.totalBytes; with the fast path in
place that function is no longer hot enough to matter.

NIX_SHOW_STATS, minimal `nix-instantiate ./nixos -A system`:
  nrFunctionCalls  5490285 -> 5365172  (-125113)
  nrThunks         8182569 -> 8112150   (-70419)
  gc.totalBytes     640.0M -> 635.2M     (-4.8M)
2026-04-12 09:27:11 +02:00
Robert Hensing
ffe82c6a3c lib/modules: deprecate 'mkAliasIfDef' (#470154) 2026-04-07 09:05:53 +00:00
Robert Hensing
ff011b00be lib/modules: Improve errors involving pushDownProperties (#502117) 2026-04-01 16:00:01 +00:00
cinereal
ac0ef82504 lib.modules: default to emptyValue
Signed-off-by: cinereal <cinereal@riseup.net>
2026-03-23 16:04:32 +01:00
dramforever
413f4666cd lib/modules: Improve errors involving pushDownProperties
If an attrset option was given a definition like:

    { system = lib.mkIf true false; }

Before this change, we get the non-explanatory error message:

    error: expected a set but found a Boolean: true

With the stack trace having nothing to do with the module involved.
After this change, we get the better:

    error: In module `[...]', you're trying to define a value of type `bool' [...]

Which names the actual module involved.
2026-03-22 13:53:50 +08: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
Silvan Mosberger
58b187378d lib/modules: add suggestions to invalid option name errors (#442263) 2026-02-03 19:10:35 +00:00
Johannes Kirschbauer
6ca8927eb0 lib/modules: deprecate 'mkAliasIfDef' 2025-12-14 21:44:28 +01:00
Johannes Kirschbauer
95afd78e86 lib/modules: inline trivial 'mkAliasIfDef' 2025-12-14 21:34:12 +01:00
Johannes Kirschbauer
2916db79f3 lib/modules: remove silent failure of 'mkAliasIfDef' 2025-12-14 21:30:54 +01:00
Johannes Kirschbauer
023e9300bb lib/modules: document limitations of 'mkRenamedOptionModule' 2025-12-12 15:14:38 +01:00
Robert Hensing
a01aeeb9b1 lib/modules: Add false positive info to check coherence assertion
Unfortunately, we don't have a good enough way to discern this
possibility. Technically we could add yet another attribute, but
that does not seem to be worth the cost at this point, in terms
of complexity and possibly even performance.

I take the blame for this one. I identified the need for the second
merge commit in review but didn't require it in the first merge.
These changes really should have been done in a single merge.
That would have prevented this situation.
2025-11-09 19:30:17 +01:00
Robert Hensing
586961172a lib/modules: Report error for unsupported t // { check = ...; } (#454964) 2025-10-28 16:40:39 +00:00
Robert Hensing
60072f71ab lib/modules: Clarify variable names in suggestions logic 2025-10-28 15:27:17 +01:00
Robert Hensing
143ca6688b lib/modules: Fix suggestions in submodules 2025-10-28 15:22:17 +01:00
Robert Hensing
cdefdba743 lib/modules: Support multiple suggestions, optimize 2025-10-28 15:17:08 +01:00
Aliaksandr
dc7095a86e treewide: remove deprecated lib functions that had warning for more than 2 years 2025-10-24 06:30:16 +03:00
Robert Hensing
93ea59f66d lib/modules: Report error for unsupported // { check }
`check` can have a new place since the introduction of
merge.v2. This makes the // { check = ... } idiom unreliable.

In this PR we add checks to detect and report this.

merge.v2 introduced in:
https://github.com/NixOS/nixpkgs/pull/391544

Real world case:
https://hercules-ci.com/github/hercules-ci/hercules-ci-effects/jobs/875
2025-10-23 19:06:05 +02:00
Aliaksandr
def801fb09 lib: deprecate mkAliasOptionModuleMD 2025-10-21 16:00:52 +03:00
Johannes Kirschbauer
bb9da33c62 lib/*: fix docs to use "returns" instead of "return" (#442388) 2025-10-06 08:09:54 +00:00
Johannes Kirschbauer
1c960d4bcf lib/modules: rename defs''' into human friendly names 2025-09-24 09:22:29 +02:00
Felix Buehler
06ac3fbebd lib/*: fix docs to use "returns" instead of "return" 2025-09-12 16:16:41 +02:00
Winter
0f54833b4a lib/modules: add suggestions to invalid option name errors
This change introduces the suggesting of possible valid option names for
a given invalid option, based on the keys of said invalid option's
parent attrset. That is, `foo.bar.baz` will only be suggested keys from
`foo.bar`, while `(config).baz` will only be suggested keys from the
toplevel.
2025-09-11 23:12:45 -04:00
Johannes Kirschbauer
bb0bd3d413 lib/modules: add _internal to valueMeta of checkedAndMerged 2025-08-20 21:55:19 +02:00
Johannes Kirschbauer
4f802d935c lib/modules: fix typo 2025-08-18 08:01:24 +02:00
Johannes Kirschbauer
50bef19448 lib/modules: add nested 'headError.message'
This should make headError extensible other information needs to be passed
This seems to improve performance slightly
2025-08-13 15:09:20 +02:00
Johannes Kirschbauer
ebafc3eb74 lib/modules: optimize performance by inlining bindings 2025-08-13 15:09:20 +02:00
Johannes Kirschbauer
70ab11c2f2 lib/modules: add new merge.v2 for 'types.{either,coercedTo}' 2025-08-13 15:09:20 +02:00
Robert Hensing
644527dd57 lib.modules: init types checkAndMerge to allow adding 'valueMeta' attributes
This allows individual types to add attributes that would be discarded during normal evaluation.
Some examples:

types.submodule performs a submodule evluation which yields an 'evalModules' result.
It returns '.config' but makes the original result accessible via 'valueMeta' allowing introspection of '.options' and all other kinds of module evaluation results

types.attrsOf returns an attribute set of the nestedType.
It makes each valueMeta available under the corresponding attribute name.
2025-08-13 15:09:20 +02:00
Shahar "Dawn" Or
5186921ded lib.evalModules: add graph attribute
Co-authored-by: Ali Jamadi <jamadi1377@gmail.com>
2025-08-07 11:38:16 +07:00
Ali Jamadi
2c1ec14d12 lib/modules: extract isDisabled from filterModules
Co-authored-by: Shahar "Dawn" Or <mightyiampresence@gmail.com>
2025-08-07 11:27:10 +07:00
Robert Hensing
5620fc678e lib.modules: Improve infinite recursion hint
hsjobeki: Using config in imports is possible in general. But its not possible to do conditional imports where the condition depends on config. Thats two different statements.

Co-authored-by: Johannes Kirschbauer <hsjobeki+github@gmail.com>
2025-08-04 10:05:26 +02:00
Robert Hensing
c34e08489e lib.modules: Adjust error message
- Lower case error trace for consistency
- Be more explicit about the condition under which the hint applies,
  and the resolution.
2025-08-02 11:17:26 +02:00
Robert Hensing
3d15b12d8f lib.modules: Make _module.args evaluation explicit in trace 2025-08-02 11:17:26 +02:00
Robert Hensing
9dad048f21 lib.modules: Generalize the import hint to _module.args 2025-08-02 10:51:09 +02:00
49016
7a787e122b lib.modules: Add hint when using config in imports 2025-08-01 22:09:02 +02:00
Wolfgang Walther
5a0711127c treewide: run nixfmt 1.0.0 2025-07-24 13:55:40 +02:00
Johannes Kirschbauer
1849ee507e modules: Add _prefix module argument, improve error, add docs (#398839) 2025-06-19 16:09:07 +02:00
Matt Sturgeon
1fb1446380 lib.modules.importApply: Fix doc typo; _keykey
The `importApply` docs reference using the `_key` attr along with
`importApply` or `_file`, however the actual attr name used by the
module system is `key`.
2025-05-08 17:48:50 +01:00
Robert Hensing
38bb05d169 lib.modules: Add prefix to imports type check error 2025-04-15 13:42:29 +02:00