Commit graph

147 commits

Author SHA1 Message Date
Robert Hensing
d572d95502
lib.types.anything: remove custom logic for lists (#340202) 2024-09-18 17:08:15 +02:00
HexoKnight
cf5a46368b
lib/types: fix toCoerced's typeMerge 2024-09-10 22:45:27 +01:00
Alexis Destrez
e5e69b748f lib.types.anything: remove custom logic for lists (default to 'mergeEqualOption')
Previously, for values of type list, the merge function would only retain the value
if the number of option definitions was less than or equal to 1, and would throw an
error for conflicting definitions to avoid potentially unwanted list merges.

This change removes that logic, defaulting to the 'mergeEqualOption' function for
values of type list. This approach maintains the same safeguard against merging
different lists while allowing lists with identical values to be merged.
2024-09-07 01:15:32 +02:00
Robert Hensing
92f4ef78de
lib/modules: improve error message when option is being accessed that isn't defined (#338362) 2024-09-02 10:08:49 +02:00
Sandro Jäckel
ab8bf8c35b
lib/modules: improve error message when option is being accessed that isn't defined 2024-09-01 23:29:21 +02:00
Robert Hensing
0abfc619bc
lib.importApply: init (#230588)
* lib.modules.importApply: init

Brings variables from rich scopes to modules defined in separate files.

A helper for functions in files that return a module.

* lib.modules.importApply: Edit doc

Generally improve the quality. Notes:

- Not rendered to the manual yet, so probably the syntax could be
  improved, but I have no way to test this now.

- The docs use `arg` vs `staticArg` in the code. This is intentional,
  because the doc is pretty clear about the role of `arg` whereas
  the code exists in a context where ambiguities are more harmful.

* Format
2024-08-31 01:12:43 +02:00
Silvan Mosberger
8f425c25ad lib/tests: Format a file with nixfmt
And fix locations to not break the test.
This is a rare case where another change is required after formatting.
We do this in a separate commit so that we don't need to do it in the
treewide reformatting PR.
2024-08-25 05:07:51 +02:00
Robert Hensing
7f838d4c54 lib/tests/modules.sh: Improve failure log format
- Clear separation between failures
- Move error regex close to error message, which is at the bottom
  of a fairly long trace
- Move most relevant and consistent info to bottom of terminal:
  the location of the failure.
  Some editors including vscode heuristically resolve file paths
  on Ctrl+click.
- Less wordy - easy to glance
- Capitalized prefixes to distinguish from Nix's own logging
2024-08-12 13:48:25 +02:00
Robert Hensing
6fa24da815 lib/tests/modules.sh: Add loc <depth> optional parameter 2024-08-12 13:47:41 +02:00
Robert Hensing
c516c03bf4 lib/tests/modules.sh: Do not redirect diagnostics to stdout
It still prints its own diagnostics to stdout, but it's always done that.
2024-08-12 13:47:36 +02:00
Robert Hensing
8a6c85d886 lib/tests/modules.sh: Report failure source location 2024-08-12 13:46:38 +02:00
Robert Hensing
4a014ed92e lib/modules: Use fixed lib instead of args.lib
The practical use for this should be very limited because I don't
think anyone should change `lib`, let alone change `lib.functionArgs`,
but, but it would be even stranger to rely on `args.lib` (or really
`specialArgs.lib` for what's clearly a behavior of the current
`evalModules`, which uses its own ambient lib for basically everything.

The shadowing of `lib` by `args.lib` here seems to be a small mistake,
which is easy to make.
2024-07-03 11:16:31 +02:00
Robert Hensing
47e4a18d01 types.attrTagWith: remove
Keep it simple for now.
2024-04-04 12:00:54 +02:00
Robert Hensing
1465777b63 lib.types.attrTag: Custom error when passing bare type 2024-04-04 11:54:46 +02:00
Robert Hensing
475a55b2f0 lib.types.attrTag: Remove tags from description
You can find them in the sub-options now.
2024-04-04 11:54:46 +02:00
Robert Hensing
ca81a89839 lib.types.attrTag: init 2024-02-11 18:44:52 +01:00
Silvan Mosberger
f37ba19765
Merge pull request #284512 from hercules-ci/lib-types-unique-merge
lib.types.unique: Check inner type deeply
2024-02-10 02:52:45 +01:00
Robert Hensing
29c7665003 lib.modules.doRename: Add condition parameter
This is to support single-to-multi service migrations, so that the
`to` (e.g. `foos.""`) isn't defined unconditionally. See test cases.
2024-02-02 07:31:16 +01:00
Danila Danko
7dea495d34 feat: add test for nonEmptyListOf submodule 2024-01-30 21:32:34 +03:00
Robert Hensing
b78ba9bc68 lib.types.unique: Check inner type deeply
This doesn't change uniq. Why not?

- In NixOS it seems that uniq is only used with
  simple types that are fully checked by t.check.

- It exists for much longer and is used more widely.

- I believe we should deprecate it, because unique was
  already better.

- unique can be a proving ground.
2024-01-28 14:09:27 +01:00
Rebecca Turner
fa9727cf1e
lib: modules.sh should check JSON output for predictability
Currently, the `lib/tests/modules.sh` test checks the output of
`nix-instantiate --eval` without `--json`, which outputs an unspecified
human-readable format.

This patch modifies `modules.sh` to use the `--json` output instead, to
be robust against future changes to `nix-instantiate` output.
2023-12-19 09:23:09 -08:00
Maximilian Bosch
d56f942e43
Merge pull request #272709 from hercules-ci/module-system-test-pr-131205
lib/modules: Test optionless module errors from #131205
2023-12-10 13:49:02 +01:00
Robert Hensing
83712164e6 lib/modules: Clarify test assertions 2023-12-09 20:38:29 +01:00
Robert Hensing
afb1a2e376 lib/modules: Test optionless module errors from #131205 2023-12-09 14:15:49 +01:00
Silvan Mosberger
8d3978c149 lib.types.boolByOr: init
This type is necessary to have correct merging behavior for
`allowUnfreePredicate` and `allowInsecurePredicate`

Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
2023-12-08 21:50:29 +01:00
Anselm Schüler
55ab538abf
lib/tests: add test for pkgsText parameter and package set selection of mkPackageOption 2023-10-10 13:13:38 +02:00
Anselm Schüler
d27d38c13a
lib/tests: add more tests for mkPackageOption 2023-10-06 14:08:46 +02:00
linsui
a50cea84be lib.gvariant: make the tests really work 2023-09-28 16:30:05 +02:00
Robert Hensing
00e5487906
Merge pull request #249243 from lf-/jade/declarationsWithLocations
nixos/modules: Add declarationPositions
2023-09-17 19:43:07 +02:00
Jade Lovelace
a1d3882307 nixos/modules: Add declarationPositions
What it does: line and column level *declaration* position information:

$ nix repl .
nix-repl> :p nixosConfigurations.micro.options.environment.systemPackages.declarationPositions
[ { column = 7; file = "/nix/store/24aj3k7fgqv3ly7qkbf98qvphasrw9nb-source/nixos/modules/config/system-path.nix"; line = 63; } ]

Use cases:
- ctags over NixOS options, as will be presented at NixCon 2023 ;)
- improving the documentation pages to go to the exact line of the
  declarations.

Related work:
- https://github.com/NixOS/nixpkgs/pull/65024

  This one does it for all *definitions* rather than declarations, and
  it was not followed through with due to performance worries.
- https://github.com/NixOS/nixpkgs/pull/208173

  The basis for this change. This change is just a rebase of that one.
  I split it out to add the capability before adding users of it, in
  order to simplify review. However, the ctags script in there is a
  sample user of this feature.

Benchmarks: conducted by evaluating my own reasonably complex NixOS
configuration with the command:
`hyperfine -S none -w 1 -- "nix eval .#nixosConfigurations.snowflake.config.system.build.toplevel.outPath"`

```
Benchmark 1: nix eval .#nixosConfigurations.snowflake.config.system.build.toplevel.outPath
  Time (mean ± σ):      8.971 s ±  0.254 s    [User: 5.872 s, System: 1.388 s]
  Range (min … max):    8.574 s …  9.327 s    10 runs

Benchmark 1: nix eval .#nixosConfigurations.snowflake.config.system.build.toplevel.outPath
  Time (mean ± σ):      8.766 s ±  0.160 s    [User: 5.873 s, System: 1.346 s]
  Range (min … max):    8.496 s …  9.033 s    10 runs
```

Summary of results: it seems to be in the noise, this does not cause any
visible regression in times.
2023-09-08 11:48:40 +02:00
Robert Hensing
5ad01f1b6f lib/modules: Report a better error when option tree has bare type
Improves on 0d472a6201
- https://github.com/NixOS/nixpkgs/pull/242339

We actually do have the file name.

Thanks Shawn8901 for the [feedback]!

feedback: https://github.com/NixOS/nixpkgs/pull/242339#issuecomment-1683107055
2023-08-18 11:36:51 +02:00
Robert Hensing
0d472a6201 lib/modules: Report a good error when option tree has bare type
Note that this removes the possibility of declaring an option
named `_type`.
2023-08-14 10:44:56 +02:00
Robert Hensing
53b289836b
Merge pull request #243139 from hercules-ci/modules-test-default-argument
lib/tests/modules: Test that _module.args works when a default argume…
2023-08-02 20:12:37 +02:00
Robert Hensing
19f1d7da06
Merge pull request #245271 from sternenseemann/module-system-merge-no-type
lib/modules: handle typeless options in mergeModules
2023-07-27 10:55:08 +02:00
sternenseemann
9c35f44999 lib/modules: handle typeless options in mergeModules
mkOption does not require a `type` argument and does not set the
resulting attribute if it is not given. Consequently, we need to be
prepared to merge options that have no type information.
2023-07-26 23:58:04 +02:00
Robert Hensing
0665253b86
Merge pull request #244044 from tweag/lib-readme
Create a Readme in `lib`
2023-07-19 16:34:36 +02:00
Silvan Mosberger
581d7c88be lib/tests: Unify documentation of individual testable files 2023-07-18 17:22:45 +02:00
Robert Hensing
72f2c8d6c6 lib/tests/modules: Test that _module.args works when a default argument is set 2023-07-12 21:54:57 +02:00
Robert Hensing
8014460c4d lib.mergeModules: Add context to error message 2023-07-11 13:03:52 +02:00
Naïm Favier
f66b401fa3
lib/tests: invalidate hashes
Having the current bash hash present in the nixpkgs tree makes Nix
detect bash as a runtime dependency of nixpkgs, which in turns messes up
`fetchFromGitHub` due to https://github.com/NixOS/nix/issues/6660
2023-07-05 11:31:58 +02:00
Robert Hensing
18111335ed
lib/tests/modules.sh: Test types.pathInStore
Add missing test cases. I think the .links case should be rejected
even though it's technically a path in the store.
2023-06-27 11:48:53 +02:00
Robert Hensing
36ea2bbfe8 lib.modules: Add mergeAttrDefinitionsWithPrio
This will let us make assertions involving _module.args.pkgs, which
is not an option but a value attribute, and therefore doesn't have
its own highestPrio to inspect. The new function gives us that info.
2023-06-16 22:08:16 +02:00
Silvan Mosberger
0b6021ee53 lib/tests: Fix when run with Nix 2.3 2023-06-01 18:07:33 +02:00
Naïm Favier
ac9915b1ea
lib/tests: add mkPackageOption tests 2023-05-20 18:23:41 +02:00
Robert Hensing
eab660d91e lib.modules: configurationClass -> class
This simplifies the documentation. `configuration` is implied by `_type`.
2023-05-06 18:32:59 +02:00
Robert Hensing
89491bef8d lib.modules: in evalModules return move _module.class -> configurationClass 2023-05-06 18:32:59 +02:00
Robert Hensing
7459c02495 lib/tests/modules.sh: Add submodule + class tests 2023-05-06 18:32:59 +02:00
Robert Hensing
84b1b01702 lib/modules: Only interpret class declaration in non-shorthand mode
This is to avoid stealing keys from submodules. `class` might be
common enough that reinterpreting existing `class` attributes in
configurations as a declaration leads to fairly widespread problems.
2023-05-06 18:29:04 +02:00
Robert Hensing
9714487f74 lib/modules: Explain that a configuration can't be loaded as a module 2023-05-06 18:29:04 +02:00
Robert Hensing
2e689d58cb lib/modules: Improve error when a configuration is imported
This is appears to be a fairly common mistake for beginners who want
to build larger things from the system configurations, such as NixOps
networks, etc. Further explanation seems appropriate.
2023-05-06 18:29:03 +02:00