1 # Testers {#chap-testers}
3 This chapter describes several testing builders which are available in the `testers` namespace.
5 ## `hasPkgConfigModules` {#tester-hasPkgConfigModules}
7 <!-- Old anchor name so links still work -->
8 []{#tester-hasPkgConfigModule}
9 Checks whether a package exposes a given list of `pkg-config` modules.
10 If the `moduleNames` argument is omitted, `hasPkgConfigModules` will use `meta.pkgConfigModules`.
12 :::{.example #ex-haspkgconfigmodules-defaultvalues}
14 # Check that `pkg-config` modules are exposed using default values
18 passthru.tests.pkg-config = testers.hasPkgConfigModules {
19 package = finalAttrs.finalPackage;
22 meta.pkgConfigModules = [ "libfoo" ];
28 :::{.example #ex-haspkgconfigmodules-explicitmodules}
30 # Check that `pkg-config` modules are exposed using explicit module names
34 passthru.tests.pkg-config = testers.hasPkgConfigModules {
35 package = finalAttrs.finalPackage;
36 moduleNames = [ "libfoo" ];
43 ## `lycheeLinkCheck` {#tester-lycheeLinkCheck}
45 Check a packaged static site's links with the [`lychee` package](https://search.nixos.org/packages?show=lychee&type=packages&query=lychee).
47 You may use Nix to reproducibly build static websites, such as for software documentation.
48 Some packages will install documentation in their `out` or `doc` outputs, or maybe you have dedicated package where you've made your static site reproducible by running a generator, such as [Hugo](https://gohugo.io/) or [mdBook](https://rust-lang.github.io/mdBook/), in a derivation.
50 If you have a static site that can be built with Nix, you can use `lycheeLinkCheck` to check that the hyperlinks in your site are correct, and do so as part of your Nix workflow and CI.
52 :::{.example #ex-lycheelinkcheck}
54 # Check hyperlinks in the `nix` documentation
57 testers.lycheeLinkCheck {
58 site = nix.doc + "/share/doc/nix/manual";
64 ### Return value {#tester-lycheeLinkCheck-return}
66 This tester produces a package that does not produce useful outputs, but only succeeds if the hyperlinks in your site are correct. The build log will list the broken links.
70 - Build the returned derivation; its build process will check that internal hyperlinks are correct. This runs in the sandbox, so it will not check external hyperlinks, but it is quick and reliable.
72 - Invoke the `.online` attribute with [`nix run`](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-run) ([experimental](https://nixos.org/manual/nix/stable/contributing/experimental-features#xp-feature-nix-command)). This runs outside the sandbox, and checks that both internal and external hyperlinks are correct.
76 nix run nixpkgs#lychee.tests.ok.online
79 ### Inputs {#tester-lycheeLinkCheck-inputs}
81 `site` (path or derivation) {#tester-lycheeLinkCheck-param-site}
83 : The path to the files to check.
85 `remap` (attribe set, optional) {#tester-lycheeLinkCheck-param-remap}
87 : An attribute set where the attribute names are regular expressions.
88 The values should be strings, derivations, or path values.
90 In the returned check's default configuration, external URLs are only checked when you run the `.online` attribute.
92 By adding remappings, you can check offline that URLs to external resources are correct, by providing a stand-in from the file system.
94 Before checking the existence of a URL, the regular expressions are matched and replaced by their corresponding values.
100 "https://nix\\.dev/manual/nix/[a-z0-9.-]*" = "${nix.doc}/share/doc/nix/manual";
101 "https://nixos\\.org/manual/nix/(un)?stable" = "${emptyDirectory}/placeholder-to-disallow-old-nix-docs-urls";
105 Store paths in the attribute values are automatically prefixed with `file://`, because lychee requires this for paths in the file system.
106 If this is a problem, or if you need to control the order in which replacements are performed, use `extraConfig.remap` instead.
108 `extraConfig` (attribute set) {#tester-lycheeLinkCheck-param-extraConfig}
110 : Extra configuration to pass to `lychee` in its [configuration file](https://github.com/lycheeverse/lychee/blob/master/lychee.example.toml).
111 It is automatically [translated](https://nixos.org/manual/nixos/stable/index.html#sec-settings-nix-representable) to TOML.
113 Example: `{ "include_verbatim" = true; }`
115 `lychee` (derivation, optional) {#tester-lycheeLinkCheck-param-lychee}
117 : The `lychee` package to use.
119 ## `shellcheck` {#tester-shellcheck}
121 Runs files through `shellcheck`, a static analysis tool for shell scripts.
123 :::{.example #ex-shellcheck}
124 # Run `testers.shellcheck`
139 inherit (lib) fileset;
143 src = fileset.toSource {
145 fileset = fileset.unions [
155 ### Inputs {#tester-shellcheck-inputs}
157 [`src` (path or string)]{#tester-shellcheck-param-src}
159 : The path to the shell script(s) to check.
160 This can be a single file or a directory containing shell files.
161 All files in `src` will be checked, so you may want to provide `fileset`-based source instead of a whole directory.
163 ### Return value {#tester-shellcheck-return}
165 A derivation that runs `shellcheck` on the given script(s).
166 The build will fail if `shellcheck` finds any issues.
168 ## `testVersion` {#tester-testVersion}
170 Checks that the output from running a command contains the specified version string in it as a whole word.
172 NOTE: In most cases, [`versionCheckHook`](#versioncheckhook) should be preferred, but this function is provided and documented here anyway. The motivation for adding either tests would be:
174 - Catch dynamic linking errors and such and missing environment variables that should be added by wrapping.
175 - Probable protection against accidentally building the wrong version, for example when using an "old" hash in a fixed-output derivation.
177 By default, the command to be run will be inferred from the given `package` attribute:
178 it will check `meta.mainProgram` first, and fall back to `pname` or `name`.
179 The default argument to the command is `--version`, and the version to be checked will be inferred from the given `package` attribute as well.
181 :::{.example #ex-testversion-hello}
183 # Check a program version using all the default values
185 This example will run the command `hello --version`, and then check that the version of the `hello` package is in the output of the command.
189 passthru.tests.version = testers.testVersion { package = hello; };
195 :::{.example #ex-testversion-different-commandversion}
197 # Check the program version using a specified command and expected version string
199 This example will run the command `leetcode -V`, and then check that `leetcode 0.4.2` is in the output of the command as a whole word (separated by whitespaces).
200 This means that an output like "leetcode 0.4.21" would fail the tests, and an output like "You're running leetcode 0.4.2" would pass the tests.
202 A common usage of the `version` attribute is to specify `version = "v${version}"`.
208 passthru.tests.version = testers.testVersion {
209 package = leetcode-cli;
210 command = "leetcode -V";
211 version = "leetcode ${version}";
218 ## `testBuildFailure` {#tester-testBuildFailure}
220 Make sure that a build does not succeed. This is useful for testing testers.
222 This returns a derivation with an override on the builder, with the following effects:
224 - Fail the build when the original builder succeeds
225 - Move `$out` to `$out/result`, if it exists (assuming `out` is the default output)
226 - Save the build log to `$out/testBuildFailure.log` (same)
228 While `testBuildFailure` is designed to keep changes to the original builder's environment to a minimum, some small changes are inevitable:
230 - The file `$TMPDIR/testBuildFailure.log` is present. It should not be deleted.
231 - `stdout` and `stderr` are a pipe instead of a tty. This could be improved.
232 - One or two extra processes are present in the sandbox during the original builder's execution.
233 - The derivation and output hashes are different, but not unusual.
234 - The derivation includes a dependency on `buildPackages.bash` and `expect-failure.sh`, which is built to include a transitive dependency on `buildPackages.coreutils` and possibly more.
235 These are not added to `PATH` or any other environment variable, so they should be hard to observe.
237 :::{.example #ex-testBuildFailure-showingenvironmentchanges}
239 # Check that a build fails, and verify the changes made during build
242 runCommand "example" {
243 failed = testers.testBuildFailure (runCommand "fail" {} ''
249 grep -F 'ok-ish' $failed/result
250 grep -F 'failing though' $failed/testBuildFailure.log
251 [[ 3 = $(cat $failed/testBuildFailure.exit) ]]
258 ## `testEqualContents` {#tester-testEqualContents}
260 Check that two paths have the same contents.
262 :::{.example #ex-testEqualContents-toyexample}
264 # Check that two paths have the same contents
267 testers.testEqualContents {
268 assertion = "sed -e performs replacement";
269 expected = writeText "expected" ''
272 actual = runCommand "actual" {
273 # not really necessary for a package that's in stdenv
274 nativeBuildInputs = [ gnused ];
275 base = writeText "base" ''
279 sed -e 's/bar/baz/g' $base >$out
286 ## `testEqualDerivation` {#tester-testEqualDerivation}
288 Checks that two packages produce the exact same build instructions.
290 This can be used to make sure that a certain difference of configuration, such as the presence of an overlay does not cause a cache miss.
292 When the derivations are equal, the return value is an empty file.
293 Otherwise, the build log explains the difference via `nix-diff`.
295 :::{.example #ex-testEqualDerivation-hello}
297 # Check that two packages produce the same derivation
300 testers.testEqualDerivation
301 "The hello package must stay the same when enabling checks."
303 (hello.overrideAttrs(o: { doCheck = true; }))
308 ## `invalidateFetcherByDrvHash` {#tester-invalidateFetcherByDrvHash}
310 Use the derivation hash to invalidate the output via name, for testing.
312 Type: `(a@{ name, ... } -> Derivation) -> a -> Derivation`
314 Normally, fixed output derivations can and should be cached by their output hash only, but for testing we want to re-fetch everytime the fetcher changes.
316 Changes to the fetcher become apparent in the drvPath, which is a hash of how to fetch, rather than a fixed store path.
317 By inserting this hash into the name, we can make sure to re-run the fetcher every time the fetcher changes.
319 This relies on the assumption that Nix isn't clever enough to reuse its database of local store contents to optimize fetching.
321 You might notice that the "salted" name derives from the normal invocation, not the final derivation.
322 `invalidateFetcherByDrvHash` has to invoke the fetcher function twice:
323 once to get a derivation hash, and again to produce the final fixed output derivation.
325 :::{.example #ex-invalidateFetcherByDrvHash-nix}
327 # Prevent nix from reusing the output of a fetcher
331 tests.fetchgit = testers.invalidateFetcherByDrvHash fetchgit {
333 url = "https://github.com/NixOS/nix";
334 rev = "9d9dbe6ed05854e03811c361a3380e09183f4f4a";
335 hash = "sha256-7DszvbCNTjpzGRmpIVAWXk20P0/XTrWZ79KSOGLrUWY=";
342 ## `runCommand` {#tester-runCommand}
344 `runCommand :: { name, script, stdenv ? stdenvNoCC, hash ? "...", ... } -> Derivation`
346 This is a wrapper around `pkgs.runCommandWith`, which
347 - produces a fixed-output derivation, enabling the command(s) to access the network ;
348 - salts the derivation's name based on its inputs, ensuring the command is re-run whenever the inputs changes.
350 It accepts the following attributes:
351 - the derivation's `name` ;
352 - the `script` to be executed ;
353 - `stdenv`, the environment to use, defaulting to `stdenvNoCC` ;
354 - the derivation's output `hash`, defaulting to the empty file's.
355 The derivation's `outputHashMode` is set by default to recursive, so the `script` can output a directory as well.
357 All other attributes are passed through to [`mkDerivation`](#sec-using-stdenv),
358 including `nativeBuildInputs` to specify dependencies available to the `script`.
360 :::{.example #ex-tester-runCommand-nix}
362 # Run a command with network access
366 name = "access-the-internet";
368 curl -o /dev/null https://example.com
371 nativeBuildInputs = with pkgs; [ cacert curl ];
377 ## `runNixOSTest` {#tester-runNixOSTest}
379 A helper function that behaves exactly like the NixOS `runTest`, except it also assigns this Nixpkgs package set as the `pkgs` of the test and makes the `nixpkgs.*` options read-only.
381 If your test is part of the Nixpkgs repository, or if you need a more general entrypoint, see ["Calling a test" in the NixOS manual](https://nixos.org/manual/nixos/stable/index.html#sec-calling-nixos-tests).
383 :::{.example #ex-runNixOSTest-hello}
385 # Run a NixOS test using `runNixOSTest`
388 pkgs.testers.runNixOSTest ({ lib, ... }: {
390 nodes.machine = { pkgs, ... }: {
391 environment.systemPackages = [ pkgs.hello ];
394 machine.succeed("hello")
401 ## `nixosTest` {#tester-nixosTest}
403 Run a NixOS VM network test using this evaluation of Nixpkgs.
405 NOTE: This function is primarily for external use. NixOS itself uses `make-test-python.nix` directly. Packages defined in Nixpkgs [reuse NixOS tests via `nixosTests`, plural](#ssec-nixos-tests-linking).
407 It is mostly equivalent to the function `import ./make-test-python.nix` from the [NixOS manual](https://nixos.org/nixos/manual/index.html#sec-nixos-tests), except that the current application of Nixpkgs (`pkgs`) will be used, instead of letting NixOS invoke Nixpkgs anew.
409 If a test machine needs to set NixOS options under `nixpkgs`, it must set only the `nixpkgs.pkgs` option.
411 ### Parameter {#tester-nixosTest-parameter}
413 A [NixOS VM test network](https://nixos.org/nixos/manual/index.html#sec-nixos-tests), or path to it. Example:
419 machine1 = { lib, pkgs, nodes, ... }: {
420 environment.systemPackages = [ pkgs.hello ];
421 services.foo.enable = true;
427 machine1.wait_for_unit("foo.service")
428 machine1.succeed("hello | foo-send")
433 ### Result {#tester-nixosTest-result}
435 A derivation that runs the VM test.
439 * `nodes`: the evaluated NixOS configurations. Useful for debugging and exploring the configuration.
441 * `driverInteractive`: a script that launches an interactive Python session in the context of the `testScript`.