Commit graph

189 commits

Author SHA1 Message Date
Robert Hensing 02de37ca07 lib/modules.nix: Optimize optionSet legacy compat code
It's still in the hot path.
2022-03-07 11:07:50 +01:00
Silvan Mosberger 023fa7b923 lib.modules: Use types.optionType for _module.freeformType
This ensures that the module file locations are propagated to the
freeform type, which makes it so that submodules in freeform types now
have their declaration location shown in the manual, fixing
https://github.com/NixOS/nixpkgs/issues/132085.

In addition, this also newly allows freeformTypes to be declared
multiple times and all declarations being merged together according to
normal option merging.

This also removes some awkwardness regarding the type of `freeformType`
2022-03-01 19:31:02 +01:00
Silvan Mosberger ff13cd5d3e lib/modules: Use types.raw for _module.args
Fixes https://github.com/NixOS/nixpkgs/issues/53458, as types.raw
doesn't allow setting multiple values
2022-02-22 15:54:45 +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
pennae 0de4ecff8c lib/modules: extract multiply-used value in byName
module.${attr} is used at least twice, so it must be evaluated at least
twice (and since it's a function argument, be turned into a thunk
twice).
2021-12-28 16:53:50 +01:00
pennae afecbb2f75 lib/modules: optimize byName
the foldl is equivalent to a zip with concat. list concatenation in nix
is an O(n) operation, which makes this operation extremely inefficient
when large numbers of modules are involved.

this change reduces the number of list elements by 7 million on the
system used to write this, total memory spent on lists by 58MB, and
total memory allocated on the GC heap by almost 100MB (with a similar
reduction in GC heap size). it's also slightly faster.
2021-12-25 00:19:44 +01:00
Graham Christensen 06edb74413
Merge pull request #148785 from pennae/more-option-doc-staticizing
treewide: more defaultText for options
2021-12-17 11:14:08 -05:00
Silvan Mosberger 6d80d3c964 nixos/modules: Allow options to be coerced to a string for convenience 2021-12-08 21:41:17 +01:00
Silvan Mosberger ae0b7d6db0
Merge pull request #144022 from hercules-ci/lib-modules-optimize-unmatchedDefns
lib/modules: Short-circuit unmatchedDefns when configs is empty
2021-12-07 19:38:43 +01:00
Robert Hensing 260b941dd0 lib/modules: Deprecate args and check 2021-12-02 18:23:43 +00:00
Robert Hensing b37f099ae7 lib/modules: Pass legacy args argument along through extendModules 2021-12-02 17:45:48 +00:00
Robert Hensing d464ccfdd9 modules: Add moduleType to module arguments 2021-11-22 16:50:50 +01:00
Robert Hensing 426ab31fde modules: Document that extendModules is also a module argument 2021-11-22 16:10:18 +01: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 844a9e746f lib/modules: Use strict fold' as recursiveUpdate is also strict
recursiveUpdate does not produce an attrset until it has evaluated
both its arguments to weak head normal form.

    nix-repl> lib.recursiveUpdate (throw "a") (throw "b")
    error: b

    nix-repl> lib.recursiveUpdate (throw "a") {}
    error: a
2021-11-03 19:47:03 +01:00
Robert Hensing 541ce53a3b lib/modules: Fix import* comments
Very confusing otherwise.
2021-11-03 19:39:31 +01:00
Robert Hensing 8b584158a5 lib/modules: Remove a lib.flip
In hot code, the overhead (envs, applies) can matter.
2021-11-03 19:34:27 +01:00
Robert Hensing bfaa9426c0 lib/modules: Short-circuit unmatchedDefns earlier 2021-11-03 19:05:26 +01:00
Robert Hensing 86f5136baf modules: Update evalModules doc 2021-11-01 09:38:51 +01:00
Robert Hensing 27644a82a9 modules: Add extendModules to module args 2021-11-01 09:34:07 +01:00
Robert Hensing dece37b83a lib.evalModules: Add extendModules and type to result
Allows the simultaneous construction of top-level invocations and
submodule types.

This helps structure configuration systems integration code.
2021-11-01 09:34:07 +01:00
Robert Hensing d6ebd537e5 lib/modules: Short-circuit unmatchedDefns when configs is empty 2021-10-31 22:28:42 +01:00
Maximilian Bosch b96101a35f
lib/modules: grammar fix in error msg 2021-08-26 00:37:33 +02:00
Maximilian Bosch b6d3c9f821
lib/modules: fix error-message when declaring an option inside `config'
The message I originally implemented here was to catch a mixup of
`config' and `options' in a `types.submodule'[1]. However it looks
rather weird for a wrongly declared top-level option.

So I decided to throw

    error: The option `foo' does not exist. Definition values:
           - In `<unknown-file>':
               {
                 bar = {
                   _type = "option";
                   type = {
                     _type = "option-type";
               ...

           It seems as you're trying to declare an option by placing it into `config' rather than `options'!

for an expression like

    with import ./lib;

    evalModules {
      modules = [
        {
          foo.bar = mkOption {
            type = types.str;
          };
        }
      ];
    }

[1]  fa30c9abed
2021-08-25 23:18:27 +02:00
David Arnold 9e42d02047
lib/modules: add mkImageMediaOverride
so the underlaying use case of the preceding commit is so
generic, that we gain a lot in reasoning to give it an
appropriate name.

As the comment states:
image media needs to override host config short of mkForce
2021-08-03 18:28:14 -05: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
Robert Hensing 0633b6aa74
Merge pull request #121870 from Pacman99/pass-specialargs
lib/modules: pass specialArgs to modules
2021-05-07 01:54:48 +02:00
Pacman99 c949e60220 lib/modules: pass specialArgs as a module argument 2021-05-06 16:04:08 -07:00
Silvan Mosberger 98c77a0b2d lib/modules: Small optimization 2021-05-06 04:59:27 +02:00
Silvan Mosberger f445acbe0a
Merge pull request #114955 from berbiche/fix/modules-imports-list
lib/modules: provide a better error message when "imports" contains a list
2021-05-05 23:20:39 +02:00
Nicolas Berbiche 810c9c6a0e
lib/modules: provide error message when imports contains a list 2021-05-05 14:15:04 -04:00
Robert Hensing a36e6760e9 Revert "lib/modules: Issue type deprecation warnings recursively"
This reverts commit 4b54aedee5.
2021-05-05 18:53:34 +02:00
Silvan Mosberger 4b54aedee5 lib/modules: Issue type deprecation warnings recursively
Previously, an option of type

  attrsOf string

wouldn't throw a deprecation warning, even though the string type is
deprecated. This was because the deprecation warning trigger only looked
at the type of the option itself, not any of its subtypes.

This commit fixes this, causing each of the types deprecationMessages to
trigger for the option. This relies on the subtypes mkOptionType
attribute introduced in 26607a5a2e06653fec453c83d063cdfc4b59185f
2021-05-03 22:16:02 +02:00
Alyssa Ross a8afbb45c1 treewide: use lib.warnIf where appropriate 2021-04-28 21:44:21 +00:00
Maximilian Bosch e878fc4aac
lib/modules: better error message if an attr-set of options is expected
I recently wrote some Nix code where I wrongly set a value to an option
which wasn't an actual option, but an attr-set of options. The mistake I
made can be demonstrated with an expression like this:

    {
      foo = { lib, pkgs, config, ... }: with lib; {
        options.foo.bar.baz = mkOption {
          type = types.str;
        };
        config.foo.bar = 23;
      };
    }

While it wasn't too hard to find the cause of the mistake for me, it was
necessary to have some practice in reading stack traces from the module
system since the eval-error I got was not very helpful:

    error: --- TypeError --------------------------------------------------------- nix-build
    at: (323:25) in file: /nix/store/3nm31brdz95pj8gch5gms6xwqh0xx55c-source/lib/modules.nix

       322|         foldl' (acc: module:
       323|                 acc // (mapAttrs (n: v:
          |                         ^
       324|                                    (acc.${n} or []) ++ f module v

    value is an integer while a set was expected
    (use '--show-trace' to show detailed location information)

I figured that such an error can be fairly confusing for someone who's
new to NixOS, so I decided to catch this case in th `byName` function in
`lib/modules.nix` by checking if the value to map through is an actual
attr-set. If not, a different error will be thrown.
2021-03-11 14:55:56 +01:00
Silvan Mosberger 7f2fcc45f7
lib/modules: Set submodule type for renamed option sets
For renames like

  mkAliasOptionModule [ "services" "compton" ] [ "services" "picom" ]

where the target is an option set (like services.picom) instead of a single
option (like services.picom.enable), previously the renamed option type
was unset, leading to it being `types.unspecified`.

This changes it to be `types.submodule {}` instead, which makes more
sense.
2021-01-21 21:57:48 +01:00
Silvan Mosberger 9e6737710c Revert "Module-builtin assertions, disabling assertions and submodule assertions" 2020-12-18 16:44:37 +01:00
Silvan Mosberger a6a70d14a9
lib/modules: Prefix mkRemovedOptionModule & co. check names
To avoid name clashes

Co-authored-by: Robert Hensing <robert@roberthensing.nl>
2020-12-18 03:34:39 +01:00
Silvan Mosberger 767d80099c
lib/modules: Introduce _module.checks.*.check
Previously the .enable option was used to encode the condition as well,
which lead to some oddness:
- In order to encode an assertion, one had to invert it
- To disable a check, one had to mkForce it

By introducing a separate .check option this is solved because:
- It can be used to encode assertions
- Disabling is done separately with .enable option, whose default can be
  overridden without a mkForce
2020-12-17 21:52:24 +01:00
Silvan Mosberger 991dfccbd1
lib/modules: _module.check should always be internal
Honestly this option should probably just be removed
2020-11-30 23:51:42 +01:00
Silvan Mosberger 8dea4df903
lib/modules: Remove _module.checks.*.triggerPath as it's not necessary
Previously this option was thought to be necessary to avoid infinite
recursion, but it actually isn't, since the check evaluation isn't fed
back into the module fixed-point.
2020-11-30 23:51:42 +01:00
Silvan Mosberger c9cc8969b4
lib/modules: Rename _module.assertions to _module.checks 2020-11-30 23:51:41 +01:00
Silvan Mosberger 3759a77fcd
nixos/modules: Expose the internal module in the top-level documentation 2020-11-30 23:51:23 +01:00
Silvan Mosberger 1e6a84b7af
nixos/modules: Allow options to be coerced to a string for convenience 2020-11-30 23:51:23 +01:00
Silvan Mosberger 20131348db
lib/modules: Use module-builtin assertions for mkRemovedOptionModule and co. 2020-11-30 23:51:23 +01:00
Silvan Mosberger 9523df7eb6
nixos/assertions: Use module-builtin assertion implementation 2020-11-30 23:51:22 +01:00
Silvan Mosberger df5ba82f74
lib/modules: Implement module-builtin assertions
This implements assertions/warnings supported by the module system directly,
instead of just being a NixOS option (see
nixos/modules/misc/assertions.nix).

This has the following benefits:
- It allows cleanly redoing the user interface. The new
  implementation specifically allows disabling assertions or
  converting them to warnings instead.
- Assertions/warnings can now be thrown easily from within
  submodules, which previously wasn't possible and needed workarounds.
2020-11-30 23:51:19 +01:00
Silvan Mosberger e08182020c
Merge pull request #99115 from Infinisil/toString-module-files
lib/modules: Make sure to not import module _file's into the store
2020-11-30 21:00:59 +01:00