nixos/manual: Must disable secure boot on UEFI installs (#364406)
[NixPkgs.git] / pkgs / build-support / trivial-builders / test / references / default.nix
blobbc9f8913e99afceb99aa46c820a6bd6f13c59c46
2   lib,
3   stdenvNoCC,
4   testers,
5   callPackage,
6   writeText,
7   # nativeBuildInputs
8   shellcheck-minimal,
9   # Samples
10   samples ? cleanSamples (callPackage ./samples.nix { }),
11   # Filter out the non-string-like attributes such as <pkg>.override added by
12   # callPackage.
13   cleanSamples ? lib.filterAttrs (n: lib.isStringLike),
14   # Test targets
15   writeDirectReferencesToFile,
16   writeClosure,
19 # -------------------------------------------------------------------------- #
21 #                         trivial-builders test
23 # -------------------------------------------------------------------------- #
25 # Execute this build script directly (quick):
27 # * Classic
28 #   $ NIX_PATH="nixpkgs=$PWD" nix-shell -p tests.trivial-builders.references.testScriptBin --run references-test
30 # * Flake-based
31 #   $ nix run .#tests.trivial-builders.references.testScriptBin
33 # or in the build sandbox with a ~20s VM overhead:
35 # * Classic
36 #   $ nix-build --no-out-link -A tests.trivial-builders.references
38 # * Flake-based
39 #   $ nix build -L --no-link .#tests.trivial-builders.references
41 # -------------------------------------------------------------------------- #
43 let
44   # Map each attribute to an element specification of Bash associative arrary
45   # and concatenate them with white spaces, to be used to define a
46   # one-line Bash associative array.
47   samplesToString =
48     attrs:
49     lib.concatMapStringsSep " " (name: "[${name}]=${lib.escapeShellArg "${attrs.${name}}"}") (
50       builtins.attrNames attrs
51     );
53   closures = lib.mapAttrs (n: v: writeClosure [ v ]) samples;
54   directReferences = lib.mapAttrs (n: v: writeDirectReferencesToFile v) samples;
55   collectiveClosure = writeClosure (lib.attrValues samples);
57   testScriptBin = stdenvNoCC.mkDerivation (finalAttrs: {
58     name = "references-test";
60     src = ./references-test.sh;
61     dontUnpack = true;
62     dontBuild = true;
64     installPhase = ''
65       runHook preInstall
66       mkdir -p "$out/bin"
67       substitute "$src" "$out/bin/${finalAttrs.meta.mainProgram}" \
68         --replace "@SAMPLES@" ${lib.escapeShellArg (samplesToString samples)} \
69         --replace "@CLOSURES@" ${lib.escapeShellArg (samplesToString closures)} \
70         --replace "@DIRECT_REFS@" ${lib.escapeShellArg (samplesToString directReferences)} \
71         --replace "@COLLECTIVE_CLOSURE@" ${lib.escapeShellArg collectiveClosure}
72       runHook postInstall
73       chmod +x "$out/bin/${finalAttrs.meta.mainProgram}"
74     '';
76     doInstallCheck = true;
77     nativeInstallCheckInputs = [
78       shellcheck-minimal
79     ];
80     installCheckPhase = ''
81       runHook preInstallCheck
82       shellcheck "$out/bin/${finalAttrs.meta.mainProgram}"
83       runHook postInstallCheck
84     '';
86     passthru = {
87       inherit
88         collectiveClosure
89         directReferences
90         closures
91         samples
92         ;
93     };
95     meta = with lib; {
96       mainProgram = "references-test";
97     };
98   });
100 testers.runNixOSTest (
101   { config, lib, ... }:
102   let
103     # Use the testScriptBin from guest pkgs.
104     # The attribute path to access the guest version of testScriptBin is
105     # tests.trivial-builders.references.config.node.pkgs.tests.trivial-builders.references.testScriptBin
106     # which is why passthru.guestTestScriptBin is provided.
107     guestTestScriptBin = config.node.pkgs.tests.trivial-builders.references.testScriptBin;
108   in
109   {
110     name = "nixpkgs-trivial-builders-references";
111     nodes.machine =
112       {
113         config,
114         lib,
115         pkgs,
116         ...
117       }:
118       {
119         virtualisation.writableStore = true;
121         # Test runs without network, so we don't substitute and prepare our deps
122         nix.settings.substituters = lib.mkForce [ ];
123         system.extraDependencies = [ guestTestScriptBin ];
124       };
125     testScript = ''
126       machine.succeed("""
127         ${lib.getExe guestTestScriptBin} 2>/dev/console
128       """)
129     '';
130     passthru = {
131       inherit
132         collectiveClosure
133         directReferences
134         closures
135         samples
136         testScriptBin
137         ;
138       inherit guestTestScriptBin;
139     };
140     meta = {
141       maintainers = with lib.maintainers; [
142         roberth
143         ShamrockLee
144       ];
145     };
146   }