nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 bash,
3 coreutils,
4 devShellTools,
5 emptyFile,
6 gnugrep,
7 lib,
8 runCommand,
9 stdenv,
10 hello,
11 writeText,
12 zlib,
13 nixosTests,
14}:
15let
16 inherit (lib)
17 concatLines
18 escapeShellArg
19 isString
20 mapAttrsToList
21 ;
22in
23lib.recurseIntoAttrs {
24
25 # nix-build -A tests.devShellTools.nixos
26 nixos = nixosTests.docker-tools-nix-shell;
27
28 # nix-build -A tests.devShellTools.valueToString
29 valueToString =
30 let
31 inherit (devShellTools) valueToString;
32 in
33
34 stdenv.mkDerivation {
35 name = "devShellTools-valueToString-built-tests";
36
37 # Test inputs
38 inherit emptyFile hello;
39 one = 1;
40 boolTrue = true;
41 boolFalse = false;
42 foo = "foo";
43 list = [
44 1
45 2
46 3
47 ];
48 pathDefaultNix = ./default.nix;
49 packages = [
50 hello
51 emptyFile
52 ];
53 # TODO: nested lists
54
55 buildCommand = ''
56 touch $out
57 ( set -x
58 [[ "$one" = ${escapeShellArg (valueToString 1)} ]]
59 [[ "$boolTrue" = ${escapeShellArg (valueToString true)} ]]
60 [[ "$boolFalse" = ${escapeShellArg (valueToString false)} ]]
61 [[ "$foo" = ${escapeShellArg (valueToString "foo")} ]]
62 [[ "$hello" = ${escapeShellArg (valueToString hello)} ]]
63 [[ "$list" = ${
64 escapeShellArg (valueToString [
65 1
66 2
67 3
68 ])
69 } ]]
70 [[ "$packages" = ${
71 escapeShellArg (valueToString [
72 hello
73 emptyFile
74 ])
75 } ]]
76 [[ "$pathDefaultNix" = ${escapeShellArg (valueToString ./default.nix)} ]]
77 [[ "$emptyFile" = ${escapeShellArg (valueToString emptyFile)} ]]
78 ) >log 2>&1 || { cat log; exit 1; }
79 '';
80 };
81
82 # nix-build -A tests.devShellTools.valueToString
83 unstructuredDerivationInputEnv =
84 let
85 inherit (devShellTools) unstructuredDerivationInputEnv;
86
87 drvAttrs = {
88 one = 1;
89 boolTrue = true;
90 boolFalse = false;
91 foo = "foo";
92 list = [
93 1
94 2
95 3
96 ];
97 pathDefaultNix = ./default.nix;
98 stringWithDep = "Exe: ${hello}/bin/hello";
99 aPackageAttrSet = hello;
100 anOutPath = hello.outPath;
101 anAnAlternateOutput = zlib.dev;
102 args = [
103 "args must not be added to the environment"
104 "Nix doesn't do it."
105 ];
106
107 passAsFile = [ "bar" ];
108 bar = ''
109 bar
110 ${writeText "qux" "yea"}
111 '';
112 };
113 result = unstructuredDerivationInputEnv { inherit drvAttrs; };
114 in
115 assert
116 result // { barPath = "<check later>"; } == {
117 one = "1";
118 boolTrue = "1";
119 boolFalse = "";
120 foo = "foo";
121 list = "1 2 3";
122 pathDefaultNix = "${./default.nix}";
123 stringWithDep = "Exe: ${hello}/bin/hello";
124 aPackageAttrSet = "${hello}";
125 anOutPath = "${hello.outPath}";
126 anAnAlternateOutput = "${zlib.dev}";
127
128 passAsFile = "bar";
129 barPath = "<check later>";
130 };
131
132 # Not runCommand, because it alters `passAsFile`
133 stdenv.mkDerivation (
134 {
135 name = "devShellTools-unstructuredDerivationInputEnv-built-tests";
136
137 exampleBarPathString =
138 assert isString result.barPath;
139 result.barPath;
140
141 dontUnpack = true;
142 dontBuild = true;
143 dontFixup = true;
144 doCheck = true;
145
146 installPhase = "touch $out";
147
148 checkPhase = ''
149 fail() {
150 echo "$@" >&2
151 exit 1
152 }
153 checkAttr() {
154 echo checking attribute $1...
155 if [[ "$2" != "$3" ]]; then
156 echo "expected: $3"
157 echo "actual: $2"
158 exit 1
159 fi
160 }
161 ${concatLines (
162 mapAttrsToList (name: value: "checkAttr ${name} \"\$${name}\" ${escapeShellArg value}") (
163 removeAttrs result [
164 "args"
165
166 # Nix puts it in workdir, which is not a concept for
167 # unstructuredDerivationInputEnv, so we have to put it in the
168 # store instead. This means the full path won't match.
169 "barPath"
170 ]
171 )
172 )}
173 (
174 set -x
175
176 diff $exampleBarPathString $barPath
177
178 ${lib.optionalString (builtins ? convertHash) ''
179 [[ "$(basename $exampleBarPathString)" = "$(basename $barPath)" ]]
180 ''}
181 )
182
183 ''${args:+fail "args should not be set by Nix. We don't expect it to and unstructuredDerivationInputEnv removes it."}
184 if [[ "''${builder:-x}" == x ]]; then
185 fail "builder should be set by Nix. We don't remove it in unstructuredDerivationInputEnv."
186 fi
187 '';
188 }
189 // removeAttrs drvAttrs [
190 # This would break the derivation. Instead, we have a check in the derivation to make sure Nix doesn't set it.
191 "args"
192 ]
193 );
194
195 # derivation: Call it directly so that we get a minimal environment to run in
196 buildHelloInShell =
197 derivation {
198 inherit (stdenv.buildPlatform) system;
199 name = "buildHelloInShell";
200 builder = "${bash}/bin/bash";
201 PATH = lib.makeBinPath [
202 coreutils
203 gnugrep
204 ];
205 args = [
206 "-euo"
207 "pipefail"
208 "-c"
209 ''
210 ${lib.getExe hello.devShell} -c 'genericBuild && ln -s $out "'"$PWD"'/result"'
211 ls -al
212 ./result/bin/hello | grep -i 'hello, world'
213 (set -x; [[ ! -e $out ]])
214 touch $out
215 ''
216 ];
217 }
218 // {
219 # Required package attributes for Nixpkgs (CI)
220 meta = { };
221 };
222
223 various =
224 let
225 hello2 = hello.overrideAttrs (o: {
226 shellHook = ''
227 echo 'shellHook says hi :)'
228 '';
229 });
230 inherit (hello2) devShell;
231 in
232 derivation {
233 inherit (stdenv.buildPlatform) system;
234 name = "various";
235 builder = "${bash}/bin/bash";
236 PATH = lib.makeBinPath [
237 coreutils
238 gnugrep
239 ];
240 args = [
241 "-euo"
242 "pipefail"
243 "-c"
244 ''
245 ${lib.getExe devShell} -c 'echo "hello, world"' \
246 | grep -F 'hello, world'
247 ${lib.getExe devShell} -c 'echo "hello, world"' \
248 | grep -F 'hello, world'
249
250 ls -al
251 (set -x; [[ ! -e $out ]])
252 touch $out
253 ''
254 ];
255 }
256 // {
257 # Required package attributes for Nixpkgs (CI)
258 meta = { };
259 };
260}