1{
2 config,
3 hostPkgs,
4 lib,
5 options,
6 ...
7}:
8let
9 inherit (lib) types mkOption;
10 inherit (hostPkgs.stdenv.hostPlatform) isDarwin isLinux;
11
12 # TODO (lib): Also use lib equivalent in nodes.nix
13 /**
14 Create a module system definition that overrides an existing option from a different module evaluation.
15
16 Type: Option a -> (a -> a) -> Definition a
17 */
18 mkOneUp =
19 /**
20 Option from an existing module evaluation, e.g.
21 - `(lib.evalModules ...).options.x` when invoking `evalModules` again,
22 - or `{ options, ... }:` when invoking `extendModules`.
23 */
24 opt:
25 /**
26 Function from the old value to the new definition, which will be wrapped with `mkOverride`.
27 */
28 f:
29 lib.mkOverride (opt.highestPrio - 1) (f opt.value);
30in
31{
32 options = {
33 passthru = mkOption {
34 type = types.lazyAttrsOf types.raw;
35 description = ''
36 Attributes to add to the returned derivations,
37 which are not necessarily part of the build.
38
39 This is a bit like doing `drv // { myAttr = true; }` (which would be lost by `overrideAttrs`).
40 It does not change the actual derivation, but adds the attribute nonetheless, so that
41 consumers of what would be `drv` have more information.
42 '';
43 };
44
45 enableDebugHook = lib.mkEnableOption "" // {
46 description = ''
47 Halt test execution after any test fail and provide the possibility to
48 hook into the sandbox to connect with either the test driver via
49 `telnet localhost 4444` or with the VMs via SSH and vsocks (see also
50 `sshBackdoor.enable`).
51 '';
52 };
53
54 rawTestDerivation = mkOption {
55 type = types.package;
56 description = ''
57 Unfiltered version of `test`, for troubleshooting the test framework and `testBuildFailure` in the test framework's test suite.
58 This is not intended for general use. Use `test` instead.
59 '';
60 internal = true;
61 };
62
63 rawTestDerivationArg = mkOption {
64 type = types.functionTo types.raw;
65 description = ''
66 Argument passed to `mkDerivation` to create the `rawTestDerivation`.
67 '';
68 };
69
70 test = mkOption {
71 type = types.package;
72 # TODO: can the interactive driver be configured to access the network?
73 description = ''
74 Derivation that runs the test as its "build" process.
75
76 This implies that NixOS tests run isolated from the network, making them
77 more dependable.
78 '';
79 };
80 };
81
82 config = {
83 rawTestDerivation = hostPkgs.stdenv.mkDerivation config.rawTestDerivationArg;
84 rawTestDerivationArg =
85 finalAttrs:
86 assert lib.assertMsg (
87 config.sshBackdoor.enable -> isLinux
88 ) "The SSH backdoor is not supported for macOS host systems!";
89
90 assert lib.assertMsg (
91 config.enableDebugHook -> isLinux
92 ) "The debugging hook is not supported for macOS host systems!";
93 {
94 name = "vm-test-run-${config.name}";
95
96 requiredSystemFeatures = [
97 "nixos-test"
98 ]
99 ++ lib.optional isLinux "kvm"
100 ++ lib.optional isDarwin "apple-virt";
101
102 nativeBuildInputs = lib.optionals config.enableDebugHook [
103 hostPkgs.openssh
104 hostPkgs.inetutils
105 ];
106
107 buildCommand = ''
108 mkdir -p $out
109
110 # effectively mute the XMLLogger
111 export LOGFILE=/dev/null
112
113 ${lib.optionalString config.enableDebugHook ''
114 ln -sf \
115 ${hostPkgs.systemd}/lib/systemd/ssh_config.d/20-systemd-ssh-proxy.conf \
116 ssh_config
117 ''}
118
119 ${config.driver}/bin/nixos-test-driver \
120 -o $out \
121 ${lib.optionalString config.enableDebugHook "--debug-hook=${hostPkgs.breakpointHook.attach}"}
122 '';
123
124 passthru = config.passthru;
125
126 meta = config.meta;
127 };
128 test = lib.lazyDerivation {
129 # lazyDerivation improves performance when only passthru items and/or meta are used.
130 derivation = config.rawTestDerivation;
131 inherit (config) passthru meta;
132 };
133
134 # useful for inspection (debugging / exploration)
135 passthru.config = config;
136
137 /**
138 For discoverTests only. Deprecated. Will be removed when discoverTests can be removed from NixOS all-tests.nix.
139 */
140 passthru.test = config.test;
141
142 # Docs: nixos/doc/manual/development/writing-nixos-tests.section.md
143 /**
144 See https://nixos.org/manual/nixos/unstable#sec-override-nixos-test
145 */
146 passthru.overrideTestDerivation =
147 f:
148 config.passthru.extend {
149 modules = [
150 {
151 rawTestDerivationArg = mkOneUp options.rawTestDerivationArg (lib.extends (lib.toExtension f));
152 }
153 ];
154 };
155 };
156}