at 23.11-pre 2.9 kB view raw
1testModuleArgs@{ config, lib, hostPkgs, nodes, moduleType, ... }: 2let 3 inherit (lib) mkOption types mdDoc; 4 inherit (types) either str functionTo; 5in 6{ 7 options = { 8 testScript = mkOption { 9 type = either str (functionTo str); 10 description = mdDoc '' 11 A series of python declarations and statements that you write to perform 12 the test. 13 ''; 14 }; 15 testScriptString = mkOption { 16 type = str; 17 readOnly = true; 18 internal = true; 19 }; 20 21 includeTestScriptReferences = mkOption { 22 type = types.bool; 23 default = true; 24 internal = true; 25 }; 26 withoutTestScriptReferences = mkOption { 27 type = moduleType; 28 description = mdDoc '' 29 A parallel universe where the testScript is invalid and has no references. 30 ''; 31 internal = true; 32 visible = false; 33 }; 34 }; 35 config = { 36 withoutTestScriptReferences.includeTestScriptReferences = false; 37 withoutTestScriptReferences.testScript = lib.mkForce "testscript omitted"; 38 39 testScriptString = 40 if lib.isFunction config.testScript 41 then 42 config.testScript 43 { 44 nodes = 45 lib.mapAttrs 46 (k: v: 47 if v.virtualisation.useNixStoreImage 48 then 49 # prevent infinite recursion when testScript would 50 # reference v's toplevel 51 config.withoutTestScriptReferences.nodesCompat.${k} 52 else 53 # reuse memoized config 54 v 55 ) 56 config.nodesCompat; 57 } 58 else config.testScript; 59 60 defaults = { config, name, ... }: { 61 # Make sure all derivations referenced by the test 62 # script are available on the nodes. When the store is 63 # accessed through 9p, this isn't important, since 64 # everything in the store is available to the guest, 65 # but when building a root image it is, as all paths 66 # that should be available to the guest has to be 67 # copied to the image. 68 virtualisation.additionalPaths = 69 lib.optional 70 # A testScript may evaluate nodes, which has caused 71 # infinite recursions. The demand cycle involves: 72 # testScript --> 73 # nodes --> 74 # toplevel --> 75 # additionalPaths --> 76 # hasContext testScript' --> 77 # testScript (ad infinitum) 78 # If we don't need to build an image, we can break this 79 # cycle by short-circuiting when useNixStoreImage is false. 80 (config.virtualisation.useNixStoreImage && builtins.hasContext testModuleArgs.config.testScriptString && testModuleArgs.config.includeTestScriptReferences) 81 (hostPkgs.writeStringReferencesToFile testModuleArgs.config.testScriptString); 82 }; 83 }; 84}