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