1#!/usr/bin/env bash
2
3# This script is used to test that the module system is working as expected.
4# Executing it runs tests for `lib.modules`, `lib.options` and `lib.types`.
5# By default it test the version of nixpkgs which is defined in the NIX_PATH.
6#
7# Run:
8# [nixpkgs]$ lib/tests/modules.sh
9# or:
10# [nixpkgs]$ nix-build lib/tests/release.nix
11
12set -o errexit -o noclobber -o nounset -o pipefail
13shopt -s failglob inherit_errexit
14
15# https://stackoverflow.com/a/246128/6605742
16DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
17
18cd "$DIR"/modules
19
20pass=0
21fail=0
22
23evalConfig() {
24 local attr=$1
25 shift
26 local script="import ./default.nix { modules = [ $* ];}"
27 nix-instantiate --timeout 1 -E "$script" -A "$attr" --eval-only --show-trace --read-write-mode --json
28}
29
30reportFailure() {
31 local attr=$1
32 shift
33 local script="import ./default.nix { modules = [ $* ];}"
34 echo 2>&1 "$ nix-instantiate -E '$script' -A '$attr' --eval-only --json"
35 evalConfig "$attr" "$@" || true
36 ((++fail))
37}
38
39checkConfigOutput() {
40 local outputContains=$1
41 shift
42 if evalConfig "$@" 2>/dev/null | grep -E --silent "$outputContains" ; then
43 ((++pass))
44 else
45 echo 2>&1 "error: Expected result matching '$outputContains', while evaluating"
46 reportFailure "$@"
47 fi
48}
49
50checkConfigError() {
51 local errorContains=$1
52 local err=""
53 shift
54 if err="$(evalConfig "$@" 2>&1 >/dev/null)"; then
55 echo 2>&1 "error: Expected error code, got exit code 0, while evaluating"
56 reportFailure "$@"
57 else
58 if echo "$err" | grep -zP --silent "$errorContains" ; then
59 ((++pass))
60 else
61 echo 2>&1 "error: Expected error matching '$errorContains', while evaluating"
62 reportFailure "$@"
63 fi
64 fi
65}
66
67# Shorthand meta attribute does not duplicate the config
68checkConfigOutput '^"one two"$' config.result ./shorthand-meta.nix
69
70checkConfigOutput '^true$' config.result ./test-mergeAttrDefinitionsWithPrio.nix
71
72# Check that a module argument is passed, also when a default is available
73# (but not needed)
74#
75# When the default is needed, we currently fail to do what the users expect, as
76# we pass our own argument anyway, even if it *turns out* not to exist.
77#
78# The reason for this is that we don't know at invocation time what is in the
79# _module.args option. That value is only available *after* all modules have been
80# invoked.
81#
82# Hypothetically, Nix could help support this by giving access to the default
83# values, through a new built-in function.
84# However the default values are allowed to depend on other arguments, so those
85# would have to be passed in somehow, making this not just a getter but
86# something more complicated.
87#
88# At that point we have to wonder whether the extra complexity is worth the cost.
89# Another - subjective - reason not to support it is that default values
90# contradict the notion that an option has a single value, where _module.args
91# is the option.
92checkConfigOutput '^true$' config.result ./module-argument-default.nix
93
94# gvariant
95checkConfigOutput '^true$' config.assertion ./gvariant.nix
96
97# https://github.com/NixOS/nixpkgs/pull/131205
98# We currently throw this error already in `config`, but throwing in `config.wrong1` would be acceptable.
99checkConfigError 'It seems as if you.re trying to declare an option by placing it into .config. rather than .options.' config.wrong1 ./error-mkOption-in-config.nix
100# We currently throw this error already in `config`, but throwing in `config.nest.wrong2` would be acceptable.
101checkConfigError 'It seems as if you.re trying to declare an option by placing it into .config. rather than .options.' config.nest.wrong2 ./error-mkOption-in-config.nix
102checkConfigError 'The option .sub.wrong2. does not exist. Definition values:' config.sub ./error-mkOption-in-submodule-config.nix
103checkConfigError '.*This can happen if you e.g. declared your options in .types.submodule.' config.sub ./error-mkOption-in-submodule-config.nix
104checkConfigError '.*A definition for option .bad. is not of type .non-empty .list of .submodule...\.' config.bad ./error-nonEmptyListOf-submodule.nix
105
106# types.attrTag
107checkConfigOutput '^true$' config.okChecks ./types-attrTag.nix
108checkConfigError 'A definition for option .intStrings\.syntaxError. is not of type .attribute-tagged union' config.intStrings.syntaxError ./types-attrTag.nix
109checkConfigError 'A definition for option .intStrings\.syntaxError2. is not of type .attribute-tagged union' config.intStrings.syntaxError2 ./types-attrTag.nix
110checkConfigError 'A definition for option .intStrings\.syntaxError3. is not of type .attribute-tagged union' config.intStrings.syntaxError3 ./types-attrTag.nix
111checkConfigError 'A definition for option .intStrings\.syntaxError4. is not of type .attribute-tagged union' config.intStrings.syntaxError4 ./types-attrTag.nix
112checkConfigError 'A definition for option .intStrings\.mergeError. is not of type .attribute-tagged union' config.intStrings.mergeError ./types-attrTag.nix
113checkConfigError 'A definition for option .intStrings\.badTagError. is not of type .attribute-tagged union' config.intStrings.badTagError ./types-attrTag.nix
114checkConfigError 'A definition for option .intStrings\.badTagTypeError\.left. is not of type .signed integer.' config.intStrings.badTagTypeError.left ./types-attrTag.nix
115checkConfigError 'A definition for option .nested\.right\.left. is not of type .signed integer.' config.nested.right.left ./types-attrTag.nix
116checkConfigError 'In attrTag, each tag value must be an option, but tag int was a bare type, not wrapped in mkOption.' config.opt.int ./types-attrTag-wrong-decl.nix
117
118# types.pathInStore
119checkConfigOutput '".*/store/0lz9p8xhf89kb1c1kk6jxrzskaiygnlh-bash-5.2-p15.drv"' config.pathInStore.ok1 ./types.nix
120checkConfigOutput '".*/store/0fb3ykw9r5hpayd05sr0cizwadzq1d8q-bash-5.2-p15"' config.pathInStore.ok2 ./types.nix
121checkConfigOutput '".*/store/0fb3ykw9r5hpayd05sr0cizwadzq1d8q-bash-5.2-p15/bin/bash"' config.pathInStore.ok3 ./types.nix
122checkConfigError 'A definition for option .* is not of type .path in the Nix store.. Definition values:\n\s*- In .*: ""' config.pathInStore.bad1 ./types.nix
123checkConfigError 'A definition for option .* is not of type .path in the Nix store.. Definition values:\n\s*- In .*: ".*/store"' config.pathInStore.bad2 ./types.nix
124checkConfigError 'A definition for option .* is not of type .path in the Nix store.. Definition values:\n\s*- In .*: ".*/store/"' config.pathInStore.bad3 ./types.nix
125checkConfigError 'A definition for option .* is not of type .path in the Nix store.. Definition values:\n\s*- In .*: ".*/store/.links"' config.pathInStore.bad4 ./types.nix
126checkConfigError 'A definition for option .* is not of type .path in the Nix store.. Definition values:\n\s*- In .*: "/foo/bar"' config.pathInStore.bad5 ./types.nix
127
128# Check boolean option.
129checkConfigOutput '^false$' config.enable ./declare-enable.nix
130checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*: true' config.enable ./define-enable.nix
131checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*' config.enable ./define-enable-throw.nix
132checkConfigError 'while evaluating a definition from `.*/define-enable-abort.nix' config.enable ./define-enable-abort.nix
133checkConfigError 'while evaluating the error message for definitions for .enable., which is an option that does not exist' config.enable ./define-enable-abort.nix
134
135# Check boolByOr type.
136checkConfigOutput '^false$' config.value.falseFalse ./boolByOr.nix
137checkConfigOutput '^true$' config.value.trueFalse ./boolByOr.nix
138checkConfigOutput '^true$' config.value.falseTrue ./boolByOr.nix
139checkConfigOutput '^true$' config.value.trueTrue ./boolByOr.nix
140
141checkConfigOutput '^1$' config.bare-submodule.nested ./declare-bare-submodule.nix ./declare-bare-submodule-nested-option.nix
142checkConfigOutput '^2$' config.bare-submodule.deep ./declare-bare-submodule.nix ./declare-bare-submodule-deep-option.nix
143checkConfigOutput '^42$' config.bare-submodule.nested ./declare-bare-submodule.nix ./declare-bare-submodule-nested-option.nix ./declare-bare-submodule-deep-option.nix ./define-bare-submodule-values.nix
144checkConfigOutput '^420$' config.bare-submodule.deep ./declare-bare-submodule.nix ./declare-bare-submodule-nested-option.nix ./declare-bare-submodule-deep-option.nix ./define-bare-submodule-values.nix
145checkConfigOutput '^2$' config.bare-submodule.deep ./declare-bare-submodule.nix ./declare-bare-submodule-deep-option.nix ./define-shorthandOnlyDefinesConfig-true.nix
146checkConfigError 'The option .bare-submodule.deep. in .*/declare-bare-submodule-deep-option.nix. is already declared in .*/declare-bare-submodule-deep-option-duplicate.nix' config.bare-submodule.deep ./declare-bare-submodule.nix ./declare-bare-submodule-deep-option.nix ./declare-bare-submodule-deep-option-duplicate.nix
147
148# Check integer types.
149# unsigned
150checkConfigOutput '^42$' config.value ./declare-int-unsigned-value.nix ./define-value-int-positive.nix
151checkConfigError 'A definition for option .* is not of type.*unsigned integer.*. Definition values:\n\s*- In .*: -23' config.value ./declare-int-unsigned-value.nix ./define-value-int-negative.nix
152# positive
153checkConfigError 'A definition for option .* is not of type.*positive integer.*. Definition values:\n\s*- In .*: 0' config.value ./declare-int-positive-value.nix ./define-value-int-zero.nix
154# between
155checkConfigOutput '^42$' config.value ./declare-int-between-value.nix ./define-value-int-positive.nix
156checkConfigError 'A definition for option .* is not of type.*between.*-21 and 43.*inclusive.*. Definition values:\n\s*- In .*: -23' config.value ./declare-int-between-value.nix ./define-value-int-negative.nix
157
158# Check either types
159# types.either
160checkConfigOutput '^42$' config.value ./declare-either.nix ./define-value-int-positive.nix
161checkConfigOutput '^"24"$' config.value ./declare-either.nix ./define-value-string.nix
162# types.oneOf
163checkConfigOutput '^42$' config.value ./declare-oneOf.nix ./define-value-int-positive.nix
164checkConfigOutput '^\[\]$' config.value ./declare-oneOf.nix ./define-value-list.nix
165checkConfigOutput '^"24"$' config.value ./declare-oneOf.nix ./define-value-string.nix
166
167# Check mkForce without submodules.
168set -- config.enable ./declare-enable.nix ./define-enable.nix
169checkConfigOutput '^true$' "$@"
170checkConfigOutput '^false$' "$@" ./define-force-enable.nix
171checkConfigOutput '^false$' "$@" ./define-enable-force.nix
172
173# Check mkForce with option and submodules.
174checkConfigError 'attribute .*foo.* .* not found' config.attrsOfSub.foo.enable ./declare-attrsOfSub-any-enable.nix
175checkConfigOutput '^false$' config.attrsOfSub.foo.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo.nix
176set -- config.attrsOfSub.foo.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo-enable.nix
177checkConfigOutput '^true$' "$@"
178checkConfigOutput '^false$' "$@" ./define-force-attrsOfSub-foo-enable.nix
179checkConfigOutput '^false$' "$@" ./define-attrsOfSub-force-foo-enable.nix
180checkConfigOutput '^false$' "$@" ./define-attrsOfSub-foo-force-enable.nix
181checkConfigOutput '^false$' "$@" ./define-attrsOfSub-foo-enable-force.nix
182
183# Check overriding effect of mkForce on submodule definitions.
184checkConfigError 'attribute .*bar.* .* not found' config.attrsOfSub.bar.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo.nix
185checkConfigOutput '^false$' config.attrsOfSub.bar.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo.nix ./define-attrsOfSub-bar.nix
186set -- config.attrsOfSub.bar.enable ./declare-attrsOfSub-any-enable.nix ./define-attrsOfSub-foo.nix ./define-attrsOfSub-bar-enable.nix
187checkConfigOutput '^true$' "$@"
188checkConfigError 'attribute .*bar.* .* not found' "$@" ./define-force-attrsOfSub-foo-enable.nix
189checkConfigError 'attribute .*bar.* .* not found' "$@" ./define-attrsOfSub-force-foo-enable.nix
190checkConfigOutput '^true$' "$@" ./define-attrsOfSub-foo-force-enable.nix
191checkConfigOutput '^true$' "$@" ./define-attrsOfSub-foo-enable-force.nix
192
193# Check mkIf with submodules.
194checkConfigError 'attribute .*foo.* .* not found' config.attrsOfSub.foo.enable ./declare-enable.nix ./declare-attrsOfSub-any-enable.nix
195set -- config.attrsOfSub.foo.enable ./declare-enable.nix ./declare-attrsOfSub-any-enable.nix
196checkConfigError 'attribute .*foo.* .* not found' "$@" ./define-if-attrsOfSub-foo-enable.nix
197checkConfigError 'attribute .*foo.* .* not found' "$@" ./define-attrsOfSub-if-foo-enable.nix
198checkConfigError 'attribute .*foo.* .* not found' "$@" ./define-attrsOfSub-foo-if-enable.nix
199checkConfigOutput '^false$' "$@" ./define-attrsOfSub-foo-enable-if.nix
200checkConfigOutput '^true$' "$@" ./define-enable.nix ./define-if-attrsOfSub-foo-enable.nix
201checkConfigOutput '^true$' "$@" ./define-enable.nix ./define-attrsOfSub-if-foo-enable.nix
202checkConfigOutput '^true$' "$@" ./define-enable.nix ./define-attrsOfSub-foo-if-enable.nix
203checkConfigOutput '^true$' "$@" ./define-enable.nix ./define-attrsOfSub-foo-enable-if.nix
204
205# Check disabledModules with config definitions and option declarations.
206set -- config.enable ./define-enable.nix ./declare-enable.nix
207checkConfigOutput '^true$' "$@"
208checkConfigOutput '^false$' "$@" ./disable-define-enable.nix
209checkConfigOutput '^false$' "$@" ./disable-define-enable-string-path.nix
210checkConfigError "The option .*enable.* does not exist. Definition values:\n\s*- In .*: true" "$@" ./disable-declare-enable.nix
211checkConfigError "attribute .*enable.* in selection path .*config.enable.* not found" "$@" ./disable-define-enable.nix ./disable-declare-enable.nix
212checkConfigError "attribute .*enable.* in selection path .*config.enable.* not found" "$@" ./disable-enable-modules.nix
213
214checkConfigOutput '^true$' 'config.positive.enable' ./disable-module-with-key.nix
215checkConfigOutput '^false$' 'config.negative.enable' ./disable-module-with-key.nix
216checkConfigError 'Module ..*disable-module-bad-key.nix. contains a disabledModules item that is an attribute set, presumably a module, that does not have a .key. attribute. .*' 'config.enable' ./disable-module-bad-key.nix
217
218# Not sure if we want to keep supporting module keys that aren't strings, paths or v?key, but we shouldn't remove support accidentally.
219checkConfigOutput '^true$' 'config.positive.enable' ./disable-module-with-toString-key.nix
220checkConfigOutput '^false$' 'config.negative.enable' ./disable-module-with-toString-key.nix
221
222# Check _module.args.
223set -- config.enable ./declare-enable.nix ./define-enable-with-custom-arg.nix
224checkConfigError 'while evaluating the module argument .*custom.* in .*define-enable-with-custom-arg.nix.*:' "$@"
225checkConfigOutput '^true$' "$@" ./define-_module-args-custom.nix
226
227# Check that using _module.args on imports cause infinite recursions, with
228# the proper error context.
229set -- "$@" ./define-_module-args-custom.nix ./import-custom-arg.nix
230checkConfigError 'while evaluating the module argument .*custom.* in .*import-custom-arg.nix.*:' "$@"
231checkConfigError 'infinite recursion encountered' "$@"
232
233# Check _module.check.
234set -- config.enable ./declare-enable.nix ./define-enable.nix ./define-attrsOfSub-foo.nix
235checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*' "$@"
236checkConfigOutput '^true$' "$@" ./define-module-check.nix
237
238# Check coerced value.
239set --
240checkConfigOutput '^"42"$' config.value ./declare-coerced-value.nix
241checkConfigOutput '^"24"$' config.value ./declare-coerced-value.nix ./define-value-string.nix
242checkConfigError 'A definition for option .* is not.*string or signed integer convertible to it.*. Definition values:\n\s*- In .*: \[ \]' config.value ./declare-coerced-value.nix ./define-value-list.nix
243
244# Check coerced value with unsound coercion
245checkConfigOutput '^12$' config.value ./declare-coerced-value-unsound.nix
246checkConfigError 'A definition for option .* is not of type .*. Definition values:\n\s*- In .*: "1000"' config.value ./declare-coerced-value-unsound.nix ./define-value-string-bigint.nix
247checkConfigError 'toInt: Could not convert .* to int' config.value ./declare-coerced-value-unsound.nix ./define-value-string-arbitrary.nix
248
249# Check mkAliasOptionModule.
250checkConfigOutput '^true$' config.enable ./alias-with-priority.nix
251checkConfigOutput '^true$' config.enableAlias ./alias-with-priority.nix
252checkConfigOutput '^false$' config.enable ./alias-with-priority-can-override.nix
253checkConfigOutput '^false$' config.enableAlias ./alias-with-priority-can-override.nix
254
255# Check mkPackageOption
256checkConfigOutput '^"hello"$' config.package.pname ./declare-mkPackageOption.nix
257checkConfigOutput '^"hello"$' config.namedPackage.pname ./declare-mkPackageOption.nix
258checkConfigOutput '^".*Hello.*"$' options.namedPackage.description ./declare-mkPackageOption.nix
259checkConfigOutput '^"hello"$' config.pathPackage.pname ./declare-mkPackageOption.nix
260checkConfigOutput '^"pkgs\.hello\.override \{ stdenv = pkgs\.clangStdenv; \}"$' options.packageWithExample.example.text ./declare-mkPackageOption.nix
261checkConfigOutput '^".*Example extra description\..*"$' options.packageWithExtraDescription.description ./declare-mkPackageOption.nix
262checkConfigError 'The option .undefinedPackage. is used but not defined' config.undefinedPackage ./declare-mkPackageOption.nix
263checkConfigOutput '^null$' config.nullablePackage ./declare-mkPackageOption.nix
264checkConfigOutput '^"null or package"$' options.nullablePackageWithDefault.type.description ./declare-mkPackageOption.nix
265checkConfigOutput '^"myPkgs\.hello"$' options.packageWithPkgsText.defaultText.text ./declare-mkPackageOption.nix
266checkConfigOutput '^"hello-other"$' options.packageFromOtherSet.default.pname ./declare-mkPackageOption.nix
267
268# submoduleWith
269
270## specialArgs should work
271checkConfigOutput '^"foo"$' config.submodule.foo ./declare-submoduleWith-special.nix
272
273## shorthandOnlyDefines config behaves as expected
274checkConfigOutput '^true$' config.submodule.config ./declare-submoduleWith-shorthand.nix ./define-submoduleWith-shorthand.nix
275checkConfigError 'is not of type `boolean' config.submodule.config ./declare-submoduleWith-shorthand.nix ./define-submoduleWith-noshorthand.nix
276checkConfigError "In module ..*define-submoduleWith-shorthand.nix., you're trying to define a value of type \`bool'\n\s*rather than an attribute set for the option" config.submodule.config ./declare-submoduleWith-noshorthand.nix ./define-submoduleWith-shorthand.nix
277checkConfigOutput '^true$' config.submodule.config ./declare-submoduleWith-noshorthand.nix ./define-submoduleWith-noshorthand.nix
278
279## submoduleWith should merge all modules in one swoop
280checkConfigOutput '^true$' config.submodule.inner ./declare-submoduleWith-modules.nix
281checkConfigOutput '^true$' config.submodule.outer ./declare-submoduleWith-modules.nix
282# Should also be able to evaluate the type name (which evaluates freeformType,
283# which evaluates all the modules defined by the type)
284checkConfigOutput '^"submodule"$' options.submodule.type.description ./declare-submoduleWith-modules.nix
285
286## submodules can be declared using (evalModules {...}).type
287checkConfigOutput '^true$' config.submodule.inner ./declare-submodule-via-evalModules.nix
288checkConfigOutput '^true$' config.submodule.outer ./declare-submodule-via-evalModules.nix
289# Should also be able to evaluate the type name (which evaluates freeformType,
290# which evaluates all the modules defined by the type)
291checkConfigOutput '^"submodule"$' options.submodule.type.description ./declare-submodule-via-evalModules.nix
292
293## Paths should be allowed as values and work as expected
294checkConfigOutput '^true$' config.submodule.enable ./declare-submoduleWith-path.nix
295
296## deferredModule
297# default module is merged into nodes.foo
298checkConfigOutput '"beta"' config.nodes.foo.settingsDict.c ./deferred-module.nix
299# errors from the default module are reported with accurate location
300checkConfigError 'In `the-file-that-contains-the-bad-config.nix, via option default'\'': "bogus"' config.nodes.foo.bottom ./deferred-module.nix
301checkConfigError '.*lib/tests/modules/deferred-module-error.nix, via option deferred [(]:anon-1:anon-1:anon-1[)] does not look like a module.' config.result ./deferred-module-error.nix
302
303# Check the file location information is propagated into submodules
304checkConfigOutput the-file.nix config.submodule.internalFiles.0 ./submoduleFiles.nix
305
306
307# Check that disabledModules works recursively and correctly
308checkConfigOutput '^true$' config.enable ./disable-recursive/main.nix
309checkConfigOutput '^true$' config.enable ./disable-recursive/{main.nix,disable-foo.nix}
310checkConfigOutput '^true$' config.enable ./disable-recursive/{main.nix,disable-bar.nix}
311checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*: true' config.enable ./disable-recursive/{main.nix,disable-foo.nix,disable-bar.nix}
312
313# Check that imports can depend on derivations
314checkConfigOutput '^true$' config.enable ./import-from-store.nix
315
316# Check that configs can be conditional on option existence
317checkConfigOutput '^true$' config.enable ./define-option-dependently.nix ./declare-enable.nix ./declare-int-positive-value.nix
318checkConfigOutput '^360$' config.value ./define-option-dependently.nix ./declare-enable.nix ./declare-int-positive-value.nix
319checkConfigOutput '^7$' config.value ./define-option-dependently.nix ./declare-int-positive-value.nix
320checkConfigOutput '^true$' config.set.enable ./define-option-dependently-nested.nix ./declare-enable-nested.nix ./declare-int-positive-value-nested.nix
321checkConfigOutput '^360$' config.set.value ./define-option-dependently-nested.nix ./declare-enable-nested.nix ./declare-int-positive-value-nested.nix
322checkConfigOutput '^7$' config.set.value ./define-option-dependently-nested.nix ./declare-int-positive-value-nested.nix
323
324# Check attrsOf and lazyAttrsOf. Only lazyAttrsOf should be lazy, and only
325# attrsOf should work with conditional definitions
326# In addition, lazyAttrsOf should honor an options emptyValue
327checkConfigError "is not lazy" config.isLazy ./declare-attrsOf.nix ./attrsOf-lazy-check.nix
328checkConfigOutput '^true$' config.isLazy ./declare-lazyAttrsOf.nix ./attrsOf-lazy-check.nix
329checkConfigOutput '^true$' config.conditionalWorks ./declare-attrsOf.nix ./attrsOf-conditional-check.nix
330checkConfigOutput '^false$' config.conditionalWorks ./declare-lazyAttrsOf.nix ./attrsOf-conditional-check.nix
331checkConfigOutput '^"empty"$' config.value.foo ./declare-lazyAttrsOf.nix ./attrsOf-conditional-check.nix
332
333
334# Even with multiple assignments, a type error should be thrown if any of them aren't valid
335checkConfigError 'A definition for option .* is not of type .*' \
336 config.value ./declare-int-unsigned-value.nix ./define-value-list.nix ./define-value-int-positive.nix
337
338## Freeform modules
339# Assigning without a declared option should work
340checkConfigOutput '^"24"$' config.value ./freeform-attrsOf.nix ./define-value-string.nix
341# Shorthand modules interpret `meta` and `class` as config items
342checkConfigOutput '^true$' options._module.args.value.result ./freeform-attrsOf.nix ./define-freeform-keywords-shorthand.nix
343# No freeform assignments shouldn't make it error
344checkConfigOutput '^{}$' config ./freeform-attrsOf.nix
345# but only if the type matches
346checkConfigError 'A definition for option .* is not of type .*' config.value ./freeform-attrsOf.nix ./define-value-list.nix
347# and properties should be applied
348checkConfigOutput '^"yes"$' config.value ./freeform-attrsOf.nix ./define-value-string-properties.nix
349# Options should still be declarable, and be able to have a type that doesn't match the freeform type
350checkConfigOutput '^false$' config.enable ./freeform-attrsOf.nix ./define-value-string.nix ./declare-enable.nix
351checkConfigOutput '^"24"$' config.value ./freeform-attrsOf.nix ./define-value-string.nix ./declare-enable.nix
352# and this should work too with nested values
353checkConfigOutput '^false$' config.nest.foo ./freeform-attrsOf.nix ./freeform-nested.nix
354checkConfigOutput '^"bar"$' config.nest.bar ./freeform-attrsOf.nix ./freeform-nested.nix
355# Check whether a declared option can depend on an freeform-typed one
356checkConfigOutput '^null$' config.foo ./freeform-attrsOf.nix ./freeform-str-dep-unstr.nix
357checkConfigOutput '^"24"$' config.foo ./freeform-attrsOf.nix ./freeform-str-dep-unstr.nix ./define-value-string.nix
358# Check whether an freeform-typed value can depend on a declared option, this can only work with lazyAttrsOf
359checkConfigError 'infinite recursion encountered' config.foo ./freeform-attrsOf.nix ./freeform-unstr-dep-str.nix
360checkConfigError 'The option .* is used but not defined' config.foo ./freeform-lazyAttrsOf.nix ./freeform-unstr-dep-str.nix
361checkConfigOutput '^"24"$' config.foo ./freeform-lazyAttrsOf.nix ./freeform-unstr-dep-str.nix ./define-value-string.nix
362# submodules in freeformTypes should have their locations annotated
363checkConfigOutput '/freeform-submodules.nix"$' config.fooDeclarations.0 ./freeform-submodules.nix
364# freeformTypes can get merged using `types.type`, including submodules
365checkConfigOutput '^10$' config.free.xxx.foo ./freeform-submodules.nix
366checkConfigOutput '^10$' config.free.yyy.bar ./freeform-submodules.nix
367
368## types.anything
369# Check that attribute sets are merged recursively
370checkConfigOutput '^null$' config.value.foo ./types-anything/nested-attrs.nix
371checkConfigOutput '^null$' config.value.l1.foo ./types-anything/nested-attrs.nix
372checkConfigOutput '^null$' config.value.l1.l2.foo ./types-anything/nested-attrs.nix
373checkConfigOutput '^null$' config.value.l1.l2.l3.foo ./types-anything/nested-attrs.nix
374# Attribute sets that are coercible to strings shouldn't be recursed into
375checkConfigOutput '^"foo"$' config.value.outPath ./types-anything/attrs-coercible.nix
376# Multiple lists aren't concatenated together
377checkConfigError 'The option .* has conflicting definitions' config.value ./types-anything/lists.nix
378# Check that all equalizable atoms can be used as long as all definitions are equal
379checkConfigOutput '^0$' config.value.int ./types-anything/equal-atoms.nix
380checkConfigOutput '^false$' config.value.bool ./types-anything/equal-atoms.nix
381checkConfigOutput '^""$' config.value.string ./types-anything/equal-atoms.nix
382checkConfigOutput '^"/[^"]+"$' config.value.path ./types-anything/equal-atoms.nix
383checkConfigOutput '^null$' config.value.null ./types-anything/equal-atoms.nix
384checkConfigOutput '^0.1$' config.value.float ./types-anything/equal-atoms.nix
385# Functions can't be merged together
386checkConfigError "The option .value.multiple-lambdas.<function body>. has conflicting option types" config.applied.multiple-lambdas ./types-anything/functions.nix
387checkConfigOutput '^true$' config.valueIsFunction.single-lambda ./types-anything/functions.nix
388checkConfigOutput '^null$' config.applied.merging-lambdas.x ./types-anything/functions.nix
389checkConfigOutput '^null$' config.applied.merging-lambdas.y ./types-anything/functions.nix
390# Check that all mk* modifiers are applied
391checkConfigError 'attribute .* not found' config.value.mkiffalse ./types-anything/mk-mods.nix
392checkConfigOutput '^{}$' config.value.mkiftrue ./types-anything/mk-mods.nix
393checkConfigOutput '^1$' config.value.mkdefault ./types-anything/mk-mods.nix
394checkConfigOutput '^{}$' config.value.mkmerge ./types-anything/mk-mods.nix
395checkConfigOutput '^true$' config.value.mkbefore ./types-anything/mk-mods.nix
396checkConfigOutput '^1$' config.value.nested.foo ./types-anything/mk-mods.nix
397checkConfigOutput '^"baz"$' config.value.nested.bar.baz ./types-anything/mk-mods.nix
398
399## types.functionTo
400checkConfigOutput '^"input is input"$' config.result ./functionTo/trivial.nix
401checkConfigOutput '^"a b"$' config.result ./functionTo/merging-list.nix
402checkConfigError 'A definition for option .fun.<function body>. is not of type .string.. Definition values:\n\s*- In .*wrong-type.nix' config.result ./functionTo/wrong-type.nix
403checkConfigOutput '^"b a"$' config.result ./functionTo/list-order.nix
404checkConfigOutput '^"a c"$' config.result ./functionTo/merging-attrs.nix
405checkConfigOutput '^"a bee"$' config.result ./functionTo/submodule-options.nix
406checkConfigOutput '^"fun.<function body>.a fun.<function body>.b"$' config.optionsResult ./functionTo/submodule-options.nix
407
408# moduleType
409checkConfigOutput '^"a b"$' config.resultFoo ./declare-variants.nix ./define-variant.nix
410checkConfigOutput '^"a b y z"$' config.resultFooBar ./declare-variants.nix ./define-variant.nix
411checkConfigOutput '^"a b c"$' config.resultFooFoo ./declare-variants.nix ./define-variant.nix
412
413## emptyValue's
414checkConfigOutput "\[\]" config.list.a ./emptyValues.nix
415checkConfigOutput "{}" config.attrs.a ./emptyValues.nix
416checkConfigOutput "null" config.null.a ./emptyValues.nix
417checkConfigOutput "{}" config.submodule.a ./emptyValues.nix
418# These types don't have empty values
419checkConfigError 'The option .int.a. is used but not defined' config.int.a ./emptyValues.nix
420checkConfigError 'The option .nonEmptyList.a. is used but not defined' config.nonEmptyList.a ./emptyValues.nix
421
422# types.unique
423# requires a single definition
424checkConfigError 'The option .examples\.merged. is defined multiple times while it.s expected to be unique' config.examples.merged.a ./types-unique.nix
425# user message is printed
426checkConfigError 'We require a single definition, because seeing the whole value at once helps us maintain critical invariants of our system.' config.examples.merged.a ./types-unique.nix
427# let the inner merge function check the values (on demand)
428checkConfigError 'A definition for option .examples\.badLazyType\.a. is not of type .string.' config.examples.badLazyType.a ./types-unique.nix
429# overriding still works (unlike option uniqueness)
430checkConfigOutput '^"bee"$' config.examples.override.b ./types-unique.nix
431
432## types.raw
433checkConfigOutput '^true$' config.unprocessedNestingEvaluates.success ./raw.nix
434checkConfigOutput "10" config.processedToplevel ./raw.nix
435checkConfigError "The option .multiple. is defined multiple times" config.multiple ./raw.nix
436checkConfigOutput "bar" config.priorities ./raw.nix
437
438## Option collision
439checkConfigError \
440 'The option .set. in module .*/declare-set.nix. would be a parent of the following options, but its type .attribute set of signed integer. does not support nested options.\n\s*- option[(]s[)] with prefix .set.enable. in module .*/declare-enable-nested.nix.' \
441 config.set \
442 ./declare-set.nix ./declare-enable-nested.nix
443
444# Options: accidental use of an option-type instead of option (or other tagged type; unlikely)
445checkConfigError 'In module .*/options-type-error-typical.nix: expected an option declaration at option path .result. but got an attribute set with type option-type' config.result ./options-type-error-typical.nix
446checkConfigError 'In module .*/options-type-error-typical-nested.nix: expected an option declaration at option path .result.here. but got an attribute set with type option-type' config.result.here ./options-type-error-typical-nested.nix
447checkConfigError 'In module .*/options-type-error-configuration.nix: expected an option declaration at option path .result. but got an attribute set with type configuration' config.result ./options-type-error-configuration.nix
448
449# Check that that merging of option collisions doesn't depend on type being set
450checkConfigError 'The option .group..*would be a parent of the following options, but its type .<no description>. does not support nested options.\n\s*- option.s. with prefix .group.enable..*' config.group.enable ./merge-typeless-option.nix
451
452# Test that types.optionType merges types correctly
453checkConfigOutput '^10$' config.theOption.int ./optionTypeMerging.nix
454checkConfigOutput '^"hello"$' config.theOption.str ./optionTypeMerging.nix
455
456# Test that types.optionType correctly annotates option locations
457checkConfigError 'The option .theOption.nested. in .other.nix. is already declared in .optionTypeFile.nix.' config.theOption.nested ./optionTypeFile.nix
458
459# Test that types.optionType leaves types untouched as long as they don't need to be merged
460checkConfigOutput 'ok' config.freeformItems.foo.bar ./adhoc-freeformType-survives-type-merge.nix
461
462# Anonymous submodules don't get nixed by import resolution/deduplication
463# because of an `extendModules` bug, issue 168767.
464checkConfigOutput '^1$' config.sub.specialisation.value ./extendModules-168767-imports.nix
465
466# Class checks, evalModules
467checkConfigOutput '^{}$' config.ok.config ./class-check.nix
468checkConfigOutput '"nixos"' config.ok.class ./class-check.nix
469checkConfigError 'The module .*/module-class-is-darwin.nix was imported into nixos instead of darwin.' config.fail.config ./class-check.nix
470checkConfigError 'The module foo.nix#darwinModules.default was imported into nixos instead of darwin.' config.fail-anon.config ./class-check.nix
471
472# Class checks, submoduleWith
473checkConfigOutput '^{}$' config.sub.nixosOk ./class-check.nix
474checkConfigError 'The module .*/module-class-is-darwin.nix was imported into nixos instead of darwin.' config.sub.nixosFail.config ./class-check.nix
475
476# submoduleWith type merge with different class
477checkConfigError 'A submoduleWith option is declared multiple times with conflicting class values "darwin" and "nixos".' config.sub.mergeFail.config ./class-check.nix
478
479# _type check
480checkConfigError 'Could not load a value as a module, because it is of type "flake", in file .*/module-imports-_type-check.nix' config.ok.config ./module-imports-_type-check.nix
481checkConfigOutput '^true$' "$@" config.enable ./declare-enable.nix ./define-enable-with-top-level-mkIf.nix
482checkConfigError 'Could not load a value as a module, because it is of type "configuration", in file .*/import-configuration.nix.*please only import the modules that make up the configuration.*' config ./import-configuration.nix
483
484# doRename works when `warnings` does not exist.
485checkConfigOutput '^1234$' config.c.d.e ./doRename-basic.nix
486# doRename adds a warning.
487checkConfigOutput '^"The option `a\.b. defined in `.*/doRename-warnings\.nix. has been renamed to `c\.d\.e.\."$' \
488 config.result \
489 ./doRename-warnings.nix
490checkConfigOutput "^true$" config.result ./doRename-condition.nix ./doRename-condition-enable.nix
491checkConfigOutput "^true$" config.result ./doRename-condition.nix ./doRename-condition-no-enable.nix
492checkConfigOutput "^true$" config.result ./doRename-condition.nix ./doRename-condition-migrated.nix
493
494# Anonymous modules get deduplicated by key
495checkConfigOutput '^"pear"$' config.once.raw ./merge-module-with-key.nix
496checkConfigOutput '^"pear\\npear"$' config.twice.raw ./merge-module-with-key.nix
497
498# Declaration positions
499# Line should be present for direct options
500checkConfigOutput '^10$' options.imported.line10.declarationPositions.0.line ./declaration-positions.nix
501checkConfigOutput '/declaration-positions.nix"$' options.imported.line10.declarationPositions.0.file ./declaration-positions.nix
502# Generated options may not have line numbers but they will at least get the
503# right file
504checkConfigOutput '/declaration-positions.nix"$' options.generated.line18.declarationPositions.0.file ./declaration-positions.nix
505checkConfigOutput '^null$' options.generated.line18.declarationPositions.0.line ./declaration-positions.nix
506# Submodules don't break it
507checkConfigOutput '^39$' config.submoduleLine34.submodDeclLine39.0.line ./declaration-positions.nix
508checkConfigOutput '/declaration-positions.nix"$' config.submoduleLine34.submodDeclLine39.0.file ./declaration-positions.nix
509# New options under freeform submodules get collected into the parent submodule
510# (consistent with .declarations behaviour, but weird; notably appears in system.build)
511checkConfigOutput '^34|23$' options.submoduleLine34.declarationPositions.0.line ./declaration-positions.nix
512checkConfigOutput '^34|23$' options.submoduleLine34.declarationPositions.1.line ./declaration-positions.nix
513# nested options work
514checkConfigOutput '^30$' options.nested.nestedLine30.declarationPositions.0.line ./declaration-positions.nix
515
516cat <<EOF
517====== module tests ======
518$pass Pass
519$fail Fail
520EOF
521
522if [ "$fail" -ne 0 ]; then
523 exit 1
524fi
525exit 0