Commit Graph

227 Commits

Author SHA1 Message Date
Robert Hensing
a2c29561e7 lib.types.deferredModule: Improve reported location 2022-06-14 23:01:26 +02:00
Robert Hensing
4746f6d03e lib.types: Add deferredModule 2022-06-14 23:01:23 +02:00
Robert Hensing
9dead5565a lib/types, lib/modules: Remove unused extensionOffset 2022-06-14 17:25:06 +02:00
Robert Hensing
d4a84aeeca lib/types: Use map instead of imap1 in submoduleWith 2022-06-14 17:09:05 +02:00
Silvan Mosberger
907627f656 lib/types: Simplify submoduleWith shorthandOnlyDefinesConfig handling
The module system already uses the parent module's _file as a fallback,
so we don't need to inject the file in a weird way
2022-06-14 17:07:33 +02:00
Robert Hensing
ec82f36ab6
Merge pull request #173621 from ncfavier/submodule-description
lib/types: allow custom `submoduleWith` descriptions
2022-06-10 11:10:20 +02:00
Naïm Favier
ceebdcfc2c
lib/types: allow custom submoduleWith descriptions
Currently the only way to set the description for a submodule type is to
use `freeformType`. This is not ideal as it requires setting a
freeform type, and evaluates the submodule config unnecessarily.

Instead, add a `description` argument to `submoduleWith`.
2022-05-19 17:06:39 +02:00
Robert Hensing
2d1a34b8cc
Merge pull request #172813 from hercules-ci/functionTo-properly
`lib.types.functionTo` type merging and docs
2022-05-17 22:22:21 +02:00
Robert Hensing
062bc5e74a lib.types.functionTo: Add pseudo-attr to generated docs 2022-05-13 09:01:05 +02:00
Robert Hensing
06da97fc3a lib.types.functionTo: Support type merging 2022-05-13 09:01:05 +02:00
Klemens Nanni
7bec3e60ef lib/types: Drop misleading plural from type descriptions #170561
Probably being the most prominent document demonstrating the problem,
configuration.nix(5) describes various types in plural, e.g.
- `           Type: list of strings`
- `           Type: list of systemd options`

However, there are other cases where appending "s" to the inner type
effectively changes the type, e.g.
- ```
           Type: list of string matching the pattern
           [a-zA-Z0-9@%:_.\-]+[.](service|socket|device|mount|automount|swap|target|path|timer|scope|slice)s
```

This should've read "list of string[s]..." but instead changes the
regular expression.

Simply drop the best-effort plural in favour of correctness and
simplicity rather than adding more grammar related logic/trying to fix
this.
2022-04-27 05:59:46 +02:00
Robert Hensing
224426ba6d lib.types.submoduleWith: Avoid _key collisions after extendModules 2022-04-24 00:07:59 +02:00
Silvan Mosberger
96698efe0c lib/modules: Finally remove deprecated types.optionSet
types.optionSet has been deprecated for almost 10 years now
(0e333688ce)! A removal
was already attempted in 2019
(27982b408e), but it was promptly
reinstantiated since some third-party uses were discovered
(f531ce75e4178c6867cc1d0f7fec96b2d5c3f1cb).

It's finally time to remove it for good :)
2022-03-18 04:51:27 +01:00
Silvan Mosberger
b97742c66c
Merge pull request #156533 from hercules-ci/issue-146882-transparent-submodule-options
lib.modules: Let module declare options directly in bare submodule
2022-03-16 21:44:35 +01:00
Robert Hensing
a15fbdb88f
Merge pull request #163443 from ncfavier/fix-types-package
lib.types.package: only call toDerivation when necessary
2022-03-11 11:56:44 +01:00
Naïm Favier
9c2266c031
lib.types.package: only call toDerivation when necessary
The current logic assumes that everything that isn't a derivation is a
store path, but it can also be something that's *coercible* to a store
path, like a flake input.

Unnecessary uses of `lib.toDerivation` result in errors in pure evaluation
mode when `builtins.storePath` is disabled.

Also document what a `package` is.
2022-03-11 11:26:34 +01:00
Robert Hensing
55ee7ab4a6 lib.types.optionType: Only merge when necessary 2022-03-10 20:25:49 +01:00
Robert Hensing
2050669922 lib.modules: Inline a private function
This should save about four calls per module.
2022-03-03 00:29:15 +01:00
Robert Hensing
28aeae2126 lib.modules: Default shorthandOnlyDefinesConfig to true when null 2022-03-03 00:29:14 +01:00
Robert Hensing
8baea8b82c lib.modules: Make option injection work when shorthandOnlyDefinesConfig 2022-03-03 00:29:14 +01:00
Robert Hensing
58a8a48e9d lib.types.submodule: Remove redundant isSubmodule attr 2022-03-03 00:28:35 +01:00
Robert Hensing
d030e2109f lib.modules: Let module declare options directly in bare submodule
... where a bare submodule is an option that has a type like
`submoduleWith x`, as opposed to `attrsOf (submoduleWith x)`.

This makes migration unnecessary when introducing a freeform type
in an existing option tree.

Closes #146882
2022-03-03 00:28:35 +01:00
Silvan Mosberger
6bcfd61109
Merge pull request #162283 from Infinisil/functionTo.description
lib/types: Fix functionTo description
2022-03-02 19:42:48 +01:00
Silvan Mosberger
5cbeddfde4 lib.types: Introduce types.optionType
This type correctly merges multiple option types together while also
annotating them with file information. In a future commit this will be
used for `_module.freeformType`
2022-03-01 19:31:00 +01:00
Silvan Mosberger
fb023114ed lib/types: Fix functionTo description 2022-03-01 02:04:35 +01:00
Silvan Mosberger
665344f148 lib/types: Introduce types.raw for unprocessed values 2022-02-22 15:54:44 +01:00
Silvan Mosberger
382289027f lib/types: Fix emptyValue of listOf and nonEmptyListOf
An empty list is [], not {}!

Also, non-empty lists shouldn't have a default of an empty list!
2022-02-17 18:48:27 +01:00
Robert Hensing
ba3e91ed43 lib.types: Add unique like uniq, but custom errors
Couldn't extend types.uniq and it had a silly name anyway.
Now we can have better error messages.
2022-01-24 16:14:55 +01:00
Jules Aguillon
1394bfc32a types.singleLineStr: Improve description
Co-authored-by: pennae <82953136+pennae@users.noreply.github.com>
2022-01-21 09:16:56 +01:00
Jules Aguillon
4baf8548fb types.singleLineStr: Allow and trim trailing \n
Allow a \n character at the end of the string and remove it during the
merge function.

An option of this type will resolve to the value "foo" whether it is set
to "foo" or "foo\n".

This is useful when using 'builtins.readFile' or ''-strings, which might
add an unintended newline (for example, bash trim the final newline from
a subshell).
2022-01-20 18:49:54 +01:00
Jules Aguillon
f25a13212b types.singleLineStr: Disallow \r 2022-01-18 23:56:50 +01:00
Jules Aguillon
df590070b0 types.singleLineStr: strings that don't contain '\n'
Add a new type, inheriting 'types.str' but checking whether the value
doesn't contain any newline characters.

The motivation comes from a problem with the
'users.users.${u}.openssh.authorizedKeys' option.
It is easy to unintentionally insert a newline character at the end of a
string, or even in the middle, for example:

    restricted_ssh_keys = command: keys:
      let
        prefix = ''
          command="${command}",no-pty,no-agent-forwarding,no-port-forwarding,no-X11-forwarding
        '';
      in map (key: "${prefix} ${key}") keys;

The 'prefix' string ends with a newline, which ends up in the middle of
a key entry after a few manipulations.

This is problematic because the key file is built by concatenating all
the keys with 'concatStringsSep "\n"', with result in two entries for
the faulty key:

    ''
      command="...",options...
      MY_KEY
    ''

This is hard to debug and might be dangerous. This is now caught at
build time.
2022-01-18 22:06:34 +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
Silvan Mosberger
48293bd6b6 lib/types: Make types.anything merge functions
Previously it would give an error if there were multiple function
definitions.
2021-10-02 16:37:22 +02:00
Robert Hensing
98352288bd
Merge pull request #128032 from Artturin/add-swap-options
nixos/swap: add options option
2021-07-23 10:45:53 +02:00
Artturin
a3c5f0cba8 lib/types.nix: add nonEmptyStr 2021-07-20 20:51:27 +03:00
Robert Hensing
285632320d lib.types.enum: Improve description for lengths 0 and 1 2021-05-25 10:53:04 +02:00
Silvan Mosberger
fb1969806f
Merge pull request #117888 from kvtb/patch-3
types.nix: fix outdated comment
2021-05-08 05:46:10 +02:00
Silvan Mosberger
68955fe612 lib/types: Introduce mkOptionType occurringTypes argument
This will be used to issue deprecation warnings recursively in the next
commit

In addition, this allows easily getting nested types of other options, which
is useful when you want to create an option that aliases a part of
another one.
2021-05-03 22:15:30 +02:00
sternenseemann
8a83d611f5 lib/types: only accept derivations for shellPackage
Since shellPackage actually requires the value to be an attribute set
(i. e. an derivation in this case), we cannot re-use the package.check
type checker since it also allows strings or things that are coercible
to strings as long as they look like store paths.
2021-04-26 20:43:43 +02:00
kvtb
1c0d31d042
types.nix: types.ints.s32 and types.ints.u32 do work 2021-03-30 15:03:53 +00:00
kvtb
3131cdf05b
types.nix: fix outdated comment
Nix's `int` is always 64-bit
2021-03-28 14:04:39 +00:00
Fritz Otlinghaus
e0215b3175
lib/types: add description for functionTo 2021-01-31 15:59:13 +01:00
Silvan Mosberger
e9101d4a3b
lib/types: Improved functionTo merging
Now type checks the resulting function values and allows mkMerge and co.
Also indicates that the type check is done in the function body

Co-Authored-By: Robert Hensing <robert@roberthensing.nl>
2021-01-27 00:16:23 +01:00
Bas van Dijk
b454af298d
Revert "Remove types.functionTo."
This reverts commit 4ff1ab5a56.

We need this to type options like:
services.xserver.windowManager.xmonad.extraPackages that specify functions that
take an attribute set containing packages / plugins and return a list containing
a selection of the values in this set.

The reason we need a dedicated type for this is to have the correct merge
behaviour. Without the functionTo type merging multiple function option
definitions results in an evaluation error. The functionTo type merges
definitions by returning a new function that applies the functions of all the
definitions to the given input and merges the result.

(cherry picked from commit 7ed41ff5e7e633dd64866398ee497ac02736a3a5)
2021-01-24 16:56:33 +01:00
Keshav Kini
a456ccb0c8 lib/types.nix: fix missing inherit
I think there was a silent (i.e. semantic) merge conflict between PR #101139 and
PR #100456.  This commit should fix the error, which manifests as follows:

  error: undefined variable 'boolToString' at /home/kkini/src/nixpkgs/lib/types.nix:552:42
2020-10-26 00:50:06 -07:00
Robert Hensing
7102388834
Merge pull request #101139 from roberth/lib-use-static-scope-checking
lib: Use Nix's static scope checking, fix error message, optimize
2020-10-26 06:59:17 +01:00
Robert Hensing
afa6c51f27 lib: Use Nix's static scope checking, fix error message, optimize
Nix can perform static scope checking, but whenever code is inside
a `with` expression, the analysis breaks down, because it can't
know statically what's in the attribute set whose attributes were
brought into scope. In those cases, Nix has to assume that
everything works out.

Except it doesnt. Removing `with` from lib/ revealed an undefined
variable in an error message.

If that doesn't convince you that we're better off without `with`,
I can tell you that this PR results in a 3% evaluation performance
improvement because Nix can look up local variables by index.
This adds up with applications like the module system.

Furthermore, removing `with` makes the binding site of each
variable obvious, which helps with comprehension.
2020-10-22 13:46:47 +02:00
Robert Hensing
5aa2a98dfa lib/types.nix: Use // instead of mergeAttrs 2020-10-22 13:46:47 +02:00
Malte Brandy
cebf9198f3
treewide: De-inline uses of lib.boolToString
This commit should not change eval results
2020-10-14 01:46:17 +02:00
Silvan Mosberger
de71ca2ebc
lib/types: Fix type description of bool enum values
Previously bool values would show as <bool>
2020-10-03 15:15:04 +02:00
Silvan Mosberger
2ff7c3e2e1
lib/types: Remove unreachable if branch
The type's check function already ensured that it can't be passed
non-lists
2020-09-21 17:13:06 +02:00
Robert Hensing
f3893d8b53
Merge pull request #97119 from Infinisil/types.anything
Introduce `types.anything`
2020-09-21 08:49:24 +02:00
Silvan Mosberger
67551f46fb
lib/types: Introduce types.anything
This new type has unsurprising merge behavior: Only attribute sets are
merged together (recursively), and only if they don't conflict.

This is in contrast to the existing types:
- types.attrs is problematic because later definitions completely
  override attributes of earlier definitions, and it doesn't support
  mkIf and co.
- types.unspecified is very similar to types.attrs, but it has smart
  merging behavior that often doesn't make sense, and it doesn't support
  all types
2020-09-15 21:01:07 +02:00
Silvan Mosberger
ed5a07c0ef
Merge pull request #97114 from Infinisil/type-deprecation
Better type deprecation messages
2020-09-07 19:33:19 +02:00
Silvan Mosberger
f73b762aac
Merge pull request #97042 from Infinisil/freeform-option-docs
Show sub options of freeform types
2020-09-07 19:31:01 +02:00
Silvan Mosberger
a582f6adde
lib/types: Set deprecationMessage for types.optionSet 2020-09-07 13:17:19 +02:00
Silvan Mosberger
3b7aca47e0
lib/types: Set deprecationMessage for types.loaOf 2020-09-07 13:17:19 +02:00
Silvan Mosberger
2bed3b2ad7
lib/types: Set deprecationMessage for types.string 2020-09-07 13:17:19 +02:00
Silvan Mosberger
14095f8f48
lib/types: Remove types.list, it's been deprecated long enough
Has been deprecated since fd803fce60
(2013-08-22)
2020-09-07 13:17:18 +02:00
Silvan Mosberger
1d4656225d
lib/types: Allow types to emit a deprecation warning
Previously the only way to deprecate a type was using

  theType = lib.warn "deprecated" (mkOptionType ...)

This caused the warning to be emitted when the type was evaluated, but
the error didn't include which option actually used that type.

With this commit, types can specify a deprecationMessage, which when
non-null, is printed along with the option that uses the type
2020-09-07 13:17:14 +02:00
Silvan Mosberger
ea5ba6e13d
lib/types: Show sub options of freeform types
Previously if you set the freeform type to e.g. attrsOf (submodule ..),
those submodule options wouldn't be shown in the manual.
2020-09-03 21:16:29 +02:00
rnhmjoj
20d491a317
treewide: completely remove types.loaOf 2020-09-02 00:42:50 +02:00
Silvan Mosberger
2d45a62899
lib/types: Make submodules use the freeform type description
Submodules that have a freeform type set behave as if that was the type
of the option itself (for values that don't have an option). Since the
submodules options are shown as separate entries in the manual, it makes
sense to show the freeform type as the submodule type.
2020-08-03 22:37:01 +02:00
Drew Hess
370efefd8c
Replace short URL from #63103 with full URL.
The error message is already helpfully verbose, so there's little
reason to shorten the informational URL.
2020-01-15 13:47:49 +00:00
arcnmx
92b464d57d lib/types: prioritise coercedType in coercedTo
This more intuitively matches `types.either` and allows paths to be
coerced to submodules again, which was inhibited by #76861
2020-01-14 09:30:53 -08:00
worldofpeace
265da70e58 lib/types: dont warn loaOf for home-manager namespace
This option namespace is not a part of NixOS
so we shouldn't provide this warning for it.
2020-01-12 15:14:08 -05:00
Jan Tojnar
61cf52bc17
Merge pull request #77501 from jtojnar/more-loaof-fxes
tree-wide: fix more warning related to loaOf deprecation
2020-01-12 18:47:34 +01:00
Robert Hensing
9884cb3ed0
Merge pull request #76861 from Infinisil/paths-as-submodules
lib/types: Allow paths as submodule values
2020-01-12 14:19:04 +01:00
Jan Tojnar
13633bd21a
lib/types: improve loaOf message even more
Now we suggest correct names for all options in Nixpkgs and also home-manager at the time of writing.
2020-01-11 15:02:57 +01:00
Jan Tojnar
6fc46fbb17
lib/types: only show ... in loaOf warning when necessary 2020-01-11 13:32:30 +01:00
Jan Tojnar
b0c2c96cbe
lib/types: improve loaOf warning
Not all modules use name attribute as the name of the submodule, for example,
environment.etc uses target. We will need to maintain a list of exceptions.
2020-01-11 08:56:29 +01:00
Silvan Mosberger
b48717d1eb
lib/types: Introduce lazyAttrsOf
The standard attrsOf is strict in its *values*, meaning it's impossible to
access only one attribute value without evaluating all others as well.
lazyAttrsOf is a version that doesn't have that problem, at the expense
of conditional definitions not properly working anymore.
2020-01-10 16:19:55 +01:00
Silvan Mosberger
4268b4f9cf
lib/types: Add emptyValue attribute to types
Co-Authored-By: Robert Hensing <roberth@users.noreply.github.com>
2020-01-10 16:19:54 +01:00
Silvan Mosberger
6525da6321
lib/types: Allow paths as submodule values 2020-01-08 23:54:45 +01:00
Silvan Mosberger
65872f407e
Merge pull request #77133 from Infinisil/fix-path-check
lib/types: Fix path type check
2020-01-07 16:43:50 +01:00
Silvan Mosberger
d7a109b59f
lib/types: Fix path type check
Previously when this function was called without a value coercible to a
string it would throw an error instead of returning false. Now it does.

As a result this now allows the use of a type like `either path attrs`
without it erroring out when a definition is an attribute set.

The warning about there not being a isPath primop was removed because
this is not the case anymore, there is builtins.isPath. But also there
always was `builtins.typeOf x == "path"` that could've been used
instead. However the path type now stands for more than just path types,
but absolute paths in general.
2020-01-06 22:09:56 +01:00
rnhmjoj
03309899eb lib/types: warn loaOf is deprecated 2020-01-06 10:39:19 -05:00
Silvan Mosberger
c7ade6844f
Revert "lib/types: Allow paths as submodule values"
This reverts commit eec83d41e3.

This broke hydra evaluation because with this commit submodule values
are allowed to be paths, however the certmgr module uses `either
(submodule ...) path` in its type, meaning it already used paths for
something else which would now be interpreted as a submodule.
2020-01-03 05:21:01 +01:00
Silvan Mosberger
eec83d41e3
lib/types: Allow paths as submodule values 2020-01-02 09:59:35 +01:00
Silvan Mosberger
5002e6afbc
lib/types: Add types.submoduleWith for more flexibility than types.submodule 2020-01-01 01:13:01 +01:00
Vladimír Čunát
f21211ebfe
Merge branch 'master' into staging 2019-09-02 23:25:24 +02:00
Silvan Mosberger
03391cd336
lib/types: Make usage of types.string emit a warning 2019-08-31 18:19:15 +02:00
volth
08f68313a4 treewide: remove redundant rec 2019-08-28 11:07:32 +00:00
volth
35d68ef143 treewide: remove redundant quotes 2019-08-26 21:40:19 +00:00
Aaron Andersen
6f6468bef3
Merge pull request #65728 from Infinisil/types-eithers
lib/types: Add oneOf, extension of either to a list of types
2019-08-13 11:48:42 -04:00
Silvan Mosberger
9a44f44d4c
lib/types: Add oneOf, extension of either to a list of types 2019-08-06 14:08:42 +02:00
danbst
795383204e lib/types: change merge strategy for str, int, float and enum
Change to `mergeEqualOption`.
2019-07-25 18:12:15 +03:00
Danylo Hlynskyi
60e8fcf0e5
module system: revert "remove types.optionSet", just deprecate (#56857)
The explicit remove helped to uncover some hidden uses of `optionSet`
in NixOps. However it makes life harder for end-users of NixOps - it will
be impossible to deploy 19.03 systems with old NixOps, but there is no
new release of NixOps with `optionSet` fixes.

Also, "deprecation" process isn't well defined. Even that `optionSet` was
declared "deprecated" for many years, it was never announced. Hence, I
leave "deprecation" announce. Then, 3 releases after announce,
we can announce removal of this feature.

This type has to be removed, not `throw`-ed in runtime, because it makes
some perfectly fine code to fail. For example:
```
$ nix-instantiate --eval -E '(import <nixpkgs/lib>).types' --strict
trace: `types.list` is deprecated; use `types.listOf` instead
error: types.optionSet is deprecated; use types.submodule instead
(use '--show-trace' to show detailed location information)
```
2019-03-07 21:28:09 +02:00
danbst
27982b408e types.optionSet: deprecate and remove last usages 2019-01-31 00:41:10 +02:00
danbst
f32987d451 lib/types.nix: small eval optimization (listToAttrs + mapAttrsToList -> mapAttrs) 2019-01-30 15:26:44 +02:00
Daniel Rutz
161c0765ad Add type port as an alias to u16 2018-10-18 23:39:13 +02:00
Samuel Dionne-Riel
0808c7cd31 lib/types: enhances separatedString's description
The previous description "string" is misleading in the full options
manual pages; they are actually concatenated strings, with a specific
character.

The empty string version ("types.string") has been special-cased to
provide a better message.
2018-10-12 19:11:16 -04:00
Profpatsch
efdf618330 lib: move assertMsg and assertOneOf to their own library file
Since the `assertOneOf` uses `lib.generators`, they are not really trivial
anymore and should go into their own library file.
2018-09-06 18:14:27 +02:00
Profpatsch
3e45b61a99 lib/trivial: add a few examples of usage of assertMsg/assertOneOf 2018-09-06 18:14:27 +02:00
volth
6d2857a311 [bot] treewide: remove unused 'inherit' in let blocks 2018-07-20 19:38:19 +00:00
volth
87f5930c3f [bot]: remove unreferenced code 2018-07-20 18:48:37 +00:00
Richard Marko
4a310a0404 lib: add float option type 2018-07-03 00:11:11 +02:00
Samuel Dionne-Riel
136f1c4706 doc: Fixes documented default option for <name> for submodules. (#40464)
Fixes #40463

This is related to change 1d56d0c8a7
2018-05-16 18:11:36 +02:00
Robert Helgesson
08e8701673
lib.types: fix loaOf behavior for long lists
Assigning a list of 10 or more elements to an option having the type
`loaOf a` produces a configuration value that is not honoring the
order of the original list. This commit fixes this and a related issue
arising when 10 or more lists are merged into this type of option.
2018-05-07 20:23:52 +02:00