Merge remote-tracking branch 'origin/master' into core-updates-frozen
[jackhill/guix/guix.git] / tests / guix-build.sh
1 # GNU Guix --- Functional package management for GNU
2 # Copyright © 2012, 2013, 2014, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
3 # Copyright © 2020 Marius Bakke <mbakke@fastmail.com>
4 # Copyright © 2021 Chris Marusich <cmmarusich@gmail.com>
5 #
6 # This file is part of GNU Guix.
7 #
8 # GNU Guix is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or (at
11 # your option) any later version.
12 #
13 # GNU Guix is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 #
22 # Test the `guix build' command-line utility.
23 #
24
25 guix build --version
26
27 # Should fail.
28 ! guix build -e +
29
30 # Source-less packages are accepted; they just return nothing.
31 guix build -e '(@ (gnu packages bootstrap) %bootstrap-glibc)' -S
32 test "`guix build -e '(@ (gnu packages bootstrap) %bootstrap-glibc)' -S`" = ""
33
34 # Should pass.
35 guix build -e '(@@ (gnu packages bootstrap) %bootstrap-guile)' | \
36 grep -e '-guile-'
37 guix build hello -d | \
38 grep -e '-hello-[0-9\.]\+\.drv$'
39
40 # Passing a .drv.
41 drv="`guix build -e '(@@ (gnu packages bootstrap) %bootstrap-guile)' -d`"
42 out="`guix build "$drv"`"
43 out2="`guix build -e '(@@ (gnu packages bootstrap) %bootstrap-guile)'`"
44 test "$out" = "$out2"
45
46 # Passing the name of a .drv that doesn't exist. The daemon should try to
47 # substitute the .drv. Here we just look for the "cannot build missing
48 # derivation" error that indicates that the daemon did try to substitute the
49 # .drv.
50 guix build "$NIX_STORE_DIR/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo.drv" 2>&1 \
51 | grep "missing derivation"
52
53 # Passing a URI.
54 GUIX_DAEMON_SOCKET="file://$GUIX_STATE_DIRECTORY/daemon-socket/socket" \
55 guix build -e '(@@ (gnu packages bootstrap) %bootstrap-guile)'
56
57 ( if GUIX_DAEMON_SOCKET="weird://uri" \
58 guix build -e '(@@ (gnu packages bootstrap) %bootstrap-guile)'; \
59 then exit 1; fi )
60
61 # Passing one '-s' flag.
62 test `guix build sed -s x86_64-linux -d | wc -l` = 1
63
64 # Passing multiple '-s' flags.
65 all_systems="-s x86_64-linux -s i686-linux -s armhf-linux -s aarch64-linux \
66 -s powerpc64le-linux"
67 test `guix build sed $all_systems -d | sort -u | wc -l` = 5
68
69 # Check there's no weird memoization effect leading to erroneous results.
70 # See <https://bugs.gnu.org/40482>.
71 drv1="`guix build sed -s x86_64-linux -s armhf-linux -d | sort`"
72 drv2="`guix build sed -s armhf-linux -s x86_64-linux -d | sort`"
73 test "$drv1" = "$drv2"
74
75 # Check --sources option with its arguments
76 module_dir="t-guix-build-$$"
77 mkdir "$module_dir"
78 trap "rm -rf $module_dir" EXIT
79
80 # Check error reporting for '-f'.
81 cat > "$module_dir/foo.scm" <<EOF
82 (use-modules (guix))
83 ) ;extra closing paren
84 EOF
85 ! guix build -f "$module_dir/foo.scm" 2> "$module_dir/stderr"
86 grep "read error" "$module_dir/stderr"
87 rm "$module_dir/stderr" "$module_dir/foo.scm"
88
89 # Check 'GUIX_PACKAGE_PATH' & co.
90 cat > "$module_dir/foo.scm"<<EOF
91 (define-module (foo)
92 #:use-module (guix tests)
93 #:use-module (guix packages)
94 #:use-module (guix download)
95 #:use-module (guix build-system trivial))
96
97 (define-public foo
98 (package
99 (name "foo")
100 (version "42")
101 (source (origin
102 (method url-fetch)
103 (uri "http://www.example.com/foo.tar.gz")
104 (sha256
105 (base32
106 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"))))
107 (build-system trivial-build-system)
108 (inputs
109 (quasiquote (("bar" ,bar))))
110 (home-page "www.example.com")
111 (synopsis "Dummy package")
112 (description "foo is a dummy package for testing.")
113 (license #f)))
114
115 (define-public bar
116 (package
117 (name "bar")
118 (version "9001")
119 (source (origin
120 (method url-fetch)
121 (uri "http://www.example.com/bar.tar.gz")
122 (sha256
123 (base32
124 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"))))
125 (build-system trivial-build-system)
126 (inputs
127 (quasiquote
128 (("data" ,(origin
129 (method url-fetch)
130 (uri "http://www.example.com/bar.dat")
131 (sha256
132 (base32
133 "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz")))))))
134 (home-page "www.example.com")
135 (synopsis "Dummy package")
136 (description "bar is a dummy package for testing.")
137 (license #f)))
138
139 (define-public baz
140 (dummy-package "baz" (replacement foo)))
141
142 (define-public superseded
143 (deprecated-package "superseded" bar))
144
145 EOF
146
147 GUIX_PACKAGE_PATH="$module_dir"
148 export GUIX_PACKAGE_PATH
149
150 # foo.tar.gz
151 guix build -d -S foo
152 guix build -d -S foo | grep -e 'foo\.tar\.gz'
153
154 # 'baz' has a replacement so we should be getting the replacement's source.
155 (unset GUIX_BUILD_OPTIONS;
156 test "`guix build -d -S baz`" = "`guix build -d -S foo`")
157
158 guix build -d --sources=package foo
159 guix build -d --sources=package foo | grep -e 'foo\.tar\.gz'
160
161 # bar.tar.gz and bar.dat
162 guix build -d --sources bar
163 test `guix build -d --sources bar \
164 | grep -e 'bar\.tar\.gz' -e 'bar\.dat' \
165 | wc -l` -eq 2
166
167 # bar.tar.gz and bar.dat
168 guix build -d --sources=all bar
169 test `guix build -d --sources bar \
170 | grep -e 'bar\.tar\.gz' -e 'bar\.dat' \
171 | wc -l` -eq 2
172
173 # Should include foo.tar.gz, bar.tar.gz, and bar.dat
174 guix build -d --sources=transitive foo
175 test `guix build -d --sources=transitive foo \
176 | grep -e 'foo\.tar\.gz' -e 'bar\.tar\.gz' -e 'bar\.dat' \
177 | wc -l` -eq 3
178
179
180 # Unbound variable in thunked field.
181 cat > "$module_dir/foo.scm" <<EOF
182 (define-module (foo)
183 #:use-module (guix tests)
184 #:use-module (guix build-system trivial))
185
186 (define-public foo
187 (dummy-package "package-with-something-wrong"
188 (build-system trivial-build-system)
189 (inputs (quasiquote (("sed" ,sed)))))) ;unbound variable
190 EOF
191
192 ! guix build package-with-something-wrong -n
193 guix build package-with-something-wrong -n 2> "$module_dir/err" || true
194 grep "unbound" "$module_dir/err" # actual error
195 grep "forget.*(gnu packages base)" "$module_dir/err" # hint
196
197 # Unbound variable at the top level.
198 cat > "$module_dir/foo.scm" <<EOF
199 (define-module (foo)
200 #:use-module (guix tests))
201
202 (define-public foo
203 (dummy-package "package-with-something-wrong"
204 (build-system gnu-build-system))) ;unbound variable
205 EOF
206
207 guix build sed -n 2> "$module_dir/err"
208 grep "unbound" "$module_dir/err" # actual error
209 grep "forget.*(guix build-system gnu)" "$module_dir/err" # hint
210
211 rm -f "$module_dir"/*
212
213 # Unbound variable: don't suggest modules that do not export the variable.
214 cat > "$module_dir/aa-private.scm" <<EOF
215 (define-module (aa-private))
216 (define make-thing #f)
217 (set! make-thing make-thing) ;don't inline
218 EOF
219
220 cat > "$module_dir/bb-public.scm" <<EOF
221 (define-module (bb-public) #:export (make-thing))
222 (define make-thing identity)
223 EOF
224
225 cat > "$module_dir/cc-user.scm" <<EOF
226 ;; Make those module available in the global name space.
227 (load-from-path "aa-private.scm")
228 (load-from-path "bb-public.scm")
229
230 (define-module (cc-user))
231 (make-thing 42)
232 EOF
233 ! guix build -f "$module_dir/cc-user.scm" -n 2> "$module_dir/err"
234 cat "$module_dir/err"
235 grep "make-thing.*unbound" "$module_dir/err" # actual error
236 grep "forget.*(bb-public)" "$module_dir/err" # hint
237
238 rm -f "$module_dir"/*
239
240 # Wrong 'define-module' clause reported by 'warn-about-load-error'.
241 cat > "$module_dir/foo.scm" <<EOF
242 (define-module (something foo)
243 #:use-module (guix)
244 #:use-module (gnu))
245 EOF
246 guix build guile-bootstrap -n 2> "$module_dir/err"
247 grep "does not match file name" "$module_dir/err"
248
249 rm "$module_dir"/*
250
251 # Should all return valid log files.
252 drv="`guix build -d -e '(@@ (gnu packages bootstrap) %bootstrap-guile)'`"
253 out="`guix build -e '(@@ (gnu packages bootstrap) %bootstrap-guile)'`"
254 log="`guix build --log-file $drv`"
255 echo "$log" | grep log/.*guile.*drv
256 test -f "$log"
257 test "`guix build -e '(@@ (gnu packages bootstrap) %bootstrap-guile)' --log-file`" \
258 = "$log"
259 test "`guix build --log-file guile-bootstrap`" = "$log"
260 test "`guix build --log-file $out`" = "$log"
261
262 # Should fail because the name/version combination could not be found.
263 ! guix build hello-0.0.1 -n
264
265 # Keep a symlink to the result, registered as a root.
266 result="t-result-$$"
267 guix build -r "$result" \
268 -e '(@@ (gnu packages bootstrap) %bootstrap-guile)'
269 test -x "$result/bin/guile"
270
271 # Should fail, because $result already exists.
272 ! guix build -r "$result" -e '(@@ (gnu packages bootstrap) %bootstrap-guile)'
273
274 rm -f "$result"
275
276 # Check relative file name canonicalization: <https://bugs.gnu.org/35271>.
277 mkdir "$result"
278 guix build -r "$result/x" -e '(@@ (gnu packages bootstrap) %bootstrap-guile)'
279 test -x "$result/x/bin/guile"
280 rm "$result/x"
281 rmdir "$result"
282
283 # Cross building.
284 guix build coreutils --target=mips64el-linux-gnu --dry-run --no-substitutes
285
286 # Likewise, but with '-e' (see <https://bugs.gnu.org/38093>).
287 guix build --target=arm-linux-gnueabihf --dry-run \
288 -e '(@ (gnu packages base) coreutils)'
289
290 # Replacements.
291 drv1=`guix build guix --with-input=guile-zstd=idutils -d`
292 drv2=`guix build guix -d`
293 test "$drv1" != "$drv2"
294
295 drv1=`guix build guile -d`
296 drv2=`guix build guile --with-input=gimp=ruby -d`
297 test "$drv1" = "$drv2"
298
299 # See <https://bugs.gnu.org/42156>.
300 drv1=`guix build glib -d`
301 drv2=`guix build glib -d --with-input=libreoffice=inkscape`
302 test "$drv1" = "$drv2"
303
304 # '--with-graft' should have no effect when using '--no-grafts'.
305 # See <https://bugs.gnu.org/43890>.
306 drv1=`guix build inkscape -d --no-grafts`
307 drv2=`guix build inkscape -d --no-grafts --with-graft=glib=glib-networking`
308 test "$drv1" = "$drv2"
309
310 # Rewriting implicit inputs.
311 drv1=`guix build hello -d`
312 drv2=`guix build hello -d --with-input=gcc=gcc-toolchain`
313 test "$drv1" != "$drv2"
314 guix gc -R "$drv2" | grep `guix build -d gcc-toolchain`
315
316 ! guix build guile --with-input=libunistring=something-really-silly
317
318 # Deprecated/superseded packages.
319 test "`guix build superseded -d`" = "`guix build bar -d`"
320
321 # Parsing package names and versions.
322 guix build -n time # PASS
323 guix build -n time@1.9 # PASS, version found
324 ! guix build -n time@3.2 # FAIL, version not found
325 ! guix build -n something-that-will-never-exist # FAIL
326
327 # Invoking a monadic procedure.
328 guix build -e "(begin
329 (use-modules (guix gexp))
330 (lambda ()
331 (gexp->derivation \"test\"
332 (gexp (mkdir (ungexp output))))))" \
333 --dry-run
334
335 # Running a gexp.
336 guix build -e '#~(mkdir #$output)' -d
337 guix build -e '#~(mkdir #$output)' -d | grep 'gexp\.drv'
338
339 # Same with a file-like object.
340 guix build -e '(computed-file "foo" #~(mkdir #$output))' -d
341 guix build -e '(computed-file "foo" #~(mkdir #$output))' -d | grep 'foo\.drv'
342
343 # Building from a package file.
344 cat > "$module_dir/package.scm"<<EOF
345 (use-modules (gnu))
346 (use-package-modules bootstrap)
347
348 %bootstrap-guile
349 EOF
350 guix build --file="$module_dir/package.scm"
351
352 # Building from a monadic procedure file.
353 cat > "$module_dir/proc.scm"<<EOF
354 (use-modules (guix gexp))
355 (lambda ()
356 (gexp->derivation "test"
357 (gexp (mkdir (ungexp output)))))
358 EOF
359 guix build --file="$module_dir/proc.scm" --dry-run
360
361 # Building from a gexp file.
362 cat > "$module_dir/gexp.scm"<<EOF
363 (use-modules (guix gexp))
364
365 (gexp (mkdir (ungexp output)))
366 EOF
367 guix build --file="$module_dir/gexp.scm" -d
368 guix build --file="$module_dir/gexp.scm" -d | grep 'gexp\.drv'
369
370 # Building from a manifest file.
371 cat > "$module_dir/manifest.scm"<<EOF
372 (specifications->manifest '("hello" "guix"))
373 EOF
374 test `guix build -d --manifest="$module_dir/manifest.scm" \
375 | grep -e '-hello-' -e '-guix-' \
376 | wc -l` -eq 2
377
378 # Building from a manifest that contains a non-package object.
379 cat > "$module_dir/manifest.scm"<<EOF
380 (manifest
381 (list (manifest-entry (name "foo") (version "0")
382 (item (computed-file "computed-thingie"
383 #~(mkdir (ungexp output)))))))
384 EOF
385 guix build -d -m "$module_dir/manifest.scm" \
386 | grep 'computed-thingie\.drv$'
387
388 rm "$module_dir"/*.scm
389
390 # Using 'GUIX_BUILD_OPTIONS'.
391 GUIX_BUILD_OPTIONS="--dry-run --no-grafts"
392 export GUIX_BUILD_OPTIONS
393
394 guix build emacs
395
396 GUIX_BUILD_OPTIONS="--something-completely-crazy"
397 ! guix build emacs