Merge branch '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 cat > "$module_dir/foo.scm"<<EOF
81 (define-module (foo)
82 #:use-module (guix tests)
83 #:use-module (guix packages)
84 #:use-module (guix download)
85 #:use-module (guix build-system trivial))
86
87 (define-public foo
88 (package
89 (name "foo")
90 (version "42")
91 (source (origin
92 (method url-fetch)
93 (uri "http://www.example.com/foo.tar.gz")
94 (sha256
95 (base32
96 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"))))
97 (build-system trivial-build-system)
98 (inputs
99 (quasiquote (("bar" ,bar))))
100 (home-page "www.example.com")
101 (synopsis "Dummy package")
102 (description "foo is a dummy package for testing.")
103 (license #f)))
104
105 (define-public bar
106 (package
107 (name "bar")
108 (version "9001")
109 (source (origin
110 (method url-fetch)
111 (uri "http://www.example.com/bar.tar.gz")
112 (sha256
113 (base32
114 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"))))
115 (build-system trivial-build-system)
116 (inputs
117 (quasiquote
118 (("data" ,(origin
119 (method url-fetch)
120 (uri "http://www.example.com/bar.dat")
121 (sha256
122 (base32
123 "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz")))))))
124 (home-page "www.example.com")
125 (synopsis "Dummy package")
126 (description "bar is a dummy package for testing.")
127 (license #f)))
128
129 (define-public baz
130 (dummy-package "baz" (replacement foo)))
131
132 (define-public superseded
133 (deprecated-package "superseded" bar))
134
135 EOF
136
137 GUIX_PACKAGE_PATH="$module_dir"
138 export GUIX_PACKAGE_PATH
139
140 # foo.tar.gz
141 guix build -d -S foo
142 guix build -d -S foo | grep -e 'foo\.tar\.gz'
143
144 # 'baz' has a replacement so we should be getting the replacement's source.
145 (unset GUIX_BUILD_OPTIONS;
146 test "`guix build -d -S baz`" = "`guix build -d -S foo`")
147
148 guix build -d --sources=package foo
149 guix build -d --sources=package foo | grep -e 'foo\.tar\.gz'
150
151 # bar.tar.gz and bar.dat
152 guix build -d --sources bar
153 test `guix build -d --sources bar \
154 | grep -e 'bar\.tar\.gz' -e 'bar\.dat' \
155 | wc -l` -eq 2
156
157 # bar.tar.gz and bar.dat
158 guix build -d --sources=all bar
159 test `guix build -d --sources bar \
160 | grep -e 'bar\.tar\.gz' -e 'bar\.dat' \
161 | wc -l` -eq 2
162
163 # Should include foo.tar.gz, bar.tar.gz, and bar.dat
164 guix build -d --sources=transitive foo
165 test `guix build -d --sources=transitive foo \
166 | grep -e 'foo\.tar\.gz' -e 'bar\.tar\.gz' -e 'bar\.dat' \
167 | wc -l` -eq 3
168
169
170 # Unbound variable in thunked field.
171 cat > "$module_dir/foo.scm" <<EOF
172 (define-module (foo)
173 #:use-module (guix tests)
174 #:use-module (guix build-system trivial))
175
176 (define-public foo
177 (dummy-package "package-with-something-wrong"
178 (build-system trivial-build-system)
179 (inputs (quasiquote (("sed" ,sed)))))) ;unbound variable
180 EOF
181
182 ! guix build package-with-something-wrong -n
183 guix build package-with-something-wrong -n 2> "$module_dir/err" || true
184 grep "unbound" "$module_dir/err" # actual error
185 grep "forget.*(gnu packages base)" "$module_dir/err" # hint
186
187 # Unbound variable at the top level.
188 cat > "$module_dir/foo.scm" <<EOF
189 (define-module (foo)
190 #:use-module (guix tests))
191
192 (define-public foo
193 (dummy-package "package-with-something-wrong"
194 (build-system gnu-build-system))) ;unbound variable
195 EOF
196
197 guix build sed -n 2> "$module_dir/err"
198 grep "unbound" "$module_dir/err" # actual error
199 grep "forget.*(guix build-system gnu)" "$module_dir/err" # hint
200
201 rm -f "$module_dir"/*
202
203 # Unbound variable: don't suggest modules that do not export the variable.
204 cat > "$module_dir/aa-private.scm" <<EOF
205 (define-module (aa-private))
206 (define make-thing #f)
207 (set! make-thing make-thing) ;don't inline
208 EOF
209
210 cat > "$module_dir/bb-public.scm" <<EOF
211 (define-module (bb-public) #:export (make-thing))
212 (define make-thing identity)
213 EOF
214
215 cat > "$module_dir/cc-user.scm" <<EOF
216 ;; Make those module available in the global name space.
217 (load-from-path "aa-private.scm")
218 (load-from-path "bb-public.scm")
219
220 (define-module (cc-user))
221 (make-thing 42)
222 EOF
223 ! guix build -f "$module_dir/cc-user.scm" -n 2> "$module_dir/err"
224 cat "$module_dir/err"
225 grep "make-thing.*unbound" "$module_dir/err" # actual error
226 grep "forget.*(bb-public)" "$module_dir/err" # hint
227
228 rm -f "$module_dir"/*
229
230 # Wrong 'define-module' clause reported by 'warn-about-load-error'.
231 cat > "$module_dir/foo.scm" <<EOF
232 (define-module (something foo)
233 #:use-module (guix)
234 #:use-module (gnu))
235 EOF
236 guix build guile-bootstrap -n 2> "$module_dir/err"
237 grep "does not match file name" "$module_dir/err"
238
239 rm "$module_dir"/*
240
241 # Should all return valid log files.
242 drv="`guix build -d -e '(@@ (gnu packages bootstrap) %bootstrap-guile)'`"
243 out="`guix build -e '(@@ (gnu packages bootstrap) %bootstrap-guile)'`"
244 log="`guix build --log-file $drv`"
245 echo "$log" | grep log/.*guile.*drv
246 test -f "$log"
247 test "`guix build -e '(@@ (gnu packages bootstrap) %bootstrap-guile)' --log-file`" \
248 = "$log"
249 test "`guix build --log-file guile-bootstrap`" = "$log"
250 test "`guix build --log-file $out`" = "$log"
251
252 # Should fail because the name/version combination could not be found.
253 ! guix build hello-0.0.1 -n
254
255 # Keep a symlink to the result, registered as a root.
256 result="t-result-$$"
257 guix build -r "$result" \
258 -e '(@@ (gnu packages bootstrap) %bootstrap-guile)'
259 test -x "$result/bin/guile"
260
261 # Should fail, because $result already exists.
262 ! guix build -r "$result" -e '(@@ (gnu packages bootstrap) %bootstrap-guile)'
263
264 rm -f "$result"
265
266 # Check relative file name canonicalization: <https://bugs.gnu.org/35271>.
267 mkdir "$result"
268 guix build -r "$result/x" -e '(@@ (gnu packages bootstrap) %bootstrap-guile)'
269 test -x "$result/x/bin/guile"
270 rm "$result/x"
271 rmdir "$result"
272
273 # Cross building.
274 guix build coreutils --target=mips64el-linux-gnu --dry-run --no-substitutes
275
276 # Likewise, but with '-e' (see <https://bugs.gnu.org/38093>).
277 guix build --target=arm-linux-gnueabihf --dry-run \
278 -e '(@ (gnu packages base) coreutils)'
279
280 # Replacements.
281 drv1=`guix build guix --with-input=guile-zstd=idutils -d`
282 drv2=`guix build guix -d`
283 test "$drv1" != "$drv2"
284
285 drv1=`guix build guile -d`
286 drv2=`guix build guile --with-input=gimp=ruby -d`
287 test "$drv1" = "$drv2"
288
289 # See <https://bugs.gnu.org/42156>.
290 drv1=`guix build glib -d`
291 drv2=`guix build glib -d --with-input=libreoffice=inkscape`
292 test "$drv1" = "$drv2"
293
294 # '--with-graft' should have no effect when using '--no-grafts'.
295 # See <https://bugs.gnu.org/43890>.
296 drv1=`guix build inkscape -d --no-grafts`
297 drv2=`guix build inkscape -d --no-grafts --with-graft=glib=glib-networking`
298 test "$drv1" = "$drv2"
299
300 # Rewriting implicit inputs.
301 drv1=`guix build hello -d`
302 drv2=`guix build hello -d --with-input=gcc=gcc-toolchain`
303 test "$drv1" != "$drv2"
304 guix gc -R "$drv2" | grep `guix build -d gcc-toolchain`
305
306 ! guix build guile --with-input=libunistring=something-really-silly
307
308 # Deprecated/superseded packages.
309 test "`guix build superseded -d`" = "`guix build bar -d`"
310
311 # Parsing package names and versions.
312 guix build -n time # PASS
313 guix build -n time@1.9 # PASS, version found
314 ! guix build -n time@3.2 # FAIL, version not found
315 ! guix build -n something-that-will-never-exist # FAIL
316
317 # Invoking a monadic procedure.
318 guix build -e "(begin
319 (use-modules (guix gexp))
320 (lambda ()
321 (gexp->derivation \"test\"
322 (gexp (mkdir (ungexp output))))))" \
323 --dry-run
324
325 # Running a gexp.
326 guix build -e '#~(mkdir #$output)' -d
327 guix build -e '#~(mkdir #$output)' -d | grep 'gexp\.drv'
328
329 # Same with a file-like object.
330 guix build -e '(computed-file "foo" #~(mkdir #$output))' -d
331 guix build -e '(computed-file "foo" #~(mkdir #$output))' -d | grep 'foo\.drv'
332
333 # Building from a package file.
334 cat > "$module_dir/package.scm"<<EOF
335 (use-modules (gnu))
336 (use-package-modules bootstrap)
337
338 %bootstrap-guile
339 EOF
340 guix build --file="$module_dir/package.scm"
341
342 # Building from a monadic procedure file.
343 cat > "$module_dir/proc.scm"<<EOF
344 (use-modules (guix gexp))
345 (lambda ()
346 (gexp->derivation "test"
347 (gexp (mkdir (ungexp output)))))
348 EOF
349 guix build --file="$module_dir/proc.scm" --dry-run
350
351 # Building from a gexp file.
352 cat > "$module_dir/gexp.scm"<<EOF
353 (use-modules (guix gexp))
354
355 (gexp (mkdir (ungexp output)))
356 EOF
357 guix build --file="$module_dir/gexp.scm" -d
358 guix build --file="$module_dir/gexp.scm" -d | grep 'gexp\.drv'
359
360 # Building from a manifest file.
361 cat > "$module_dir/manifest.scm"<<EOF
362 (specifications->manifest '("hello" "guix"))
363 EOF
364 test `guix build -d --manifest="$module_dir/manifest.scm" \
365 | grep -e '-hello-' -e '-guix-' \
366 | wc -l` -eq 2
367
368 # Building from a manifest that contains a non-package object.
369 cat > "$module_dir/manifest.scm"<<EOF
370 (manifest
371 (list (manifest-entry (name "foo") (version "0")
372 (item (computed-file "computed-thingie"
373 #~(mkdir (ungexp output)))))))
374 EOF
375 guix build -d -m "$module_dir/manifest.scm" \
376 | grep 'computed-thingie\.drv$'
377
378 rm "$module_dir"/*.scm
379
380 # Using 'GUIX_BUILD_OPTIONS'.
381 GUIX_BUILD_OPTIONS="--dry-run --no-grafts"
382 export GUIX_BUILD_OPTIONS
383
384 guix build emacs
385
386 GUIX_BUILD_OPTIONS="--something-completely-crazy"
387 ! guix build emacs