Commit Graph

167 Commits

Author SHA1 Message Date
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