import: crate: Use guile-semver to resolve module versions.
[jackhill/guix/guix.git] / tests / guix-package.sh
1 # GNU Guix --- Functional package management for GNU
2 # Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 # Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
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 package' command-line utility.
22 #
23
24 guix package --version
25
26 readlink_base ()
27 {
28 basename `readlink "$1"`
29 }
30
31 module_dir="t-guix-package-$$"
32 profile="t-profile-$$"
33 tmpfile="t-guix-package-file-$$"
34 rm -f "$profile" "$tmpfile"
35
36 trap 'rm -f "$profile" "$profile.lock" "$profile-"[0-9]* "$tmpfile"; rm -rf "$module_dir" t-home-'"$$" EXIT
37
38 # Use `-e' with a non-package expression.
39 ! guix package --bootstrap -e +
40
41 # Install a store item and make sure the version and output in the manifest
42 # are correct.
43 guix package --bootstrap -p "$profile" -i `guix build guile-bootstrap`
44 test "`guix package -A guile-bootstrap | cut -f 1-2`" \
45 = "`guix package -p "$profile" -I | cut -f 1-2`"
46 test "`guix package -p "$profile" -I | cut -f 3`" = "out"
47 rm "$profile"
48
49 guix package --bootstrap -p "$profile" -i guile-bootstrap
50 test -L "$profile" && test -L "$profile-1-link"
51 test -f "$profile/bin/guile"
52
53 # Make sure the profile is a GC root.
54 guix gc --list-live | grep "`readlink "$profile-1-link"`"
55
56 # Installing the same package a second time does nothing.
57 guix package --bootstrap -p "$profile" -i guile-bootstrap
58 test -L "$profile" && test -L "$profile-1-link"
59 ! test -f "$profile-2-link"
60 test -f "$profile/bin/guile"
61
62 # Collisions are properly flagged (in this case, 'g-wrap' propagates
63 # guile@2.2, which conflicts with guile@2.0.)
64 ! guix package --bootstrap -n -p "$profile" -i g-wrap guile@2.0
65
66 guix package --bootstrap -n -p "$profile" -i g-wrap guile@2.0 \
67 --allow-collisions
68
69 # No search path env. var. here.
70 guix package -p "$profile" --search-paths
71 guix package -p "$profile" --search-paths | grep '^export PATH='
72 test "`guix package -p "$profile" --search-paths | wc -l`" = 1 # $PATH
73 ( set -e; set -x; \
74 eval `guix package --search-paths=prefix -p "$PWD/$profile"`; \
75 test "`type -P guile`" = "$PWD/$profile/bin/guile" ; \
76 type -P rm )
77
78 # Exit with 1 when a generation does not exist.
79 ! guix package -p "$profile" --delete-generations=42
80
81 # Exit with 0 when trying to delete the zeroth generation.
82 guix package -p "$profile" --delete-generations=0
83
84 # Make sure multiple arguments to -i works.
85 guix package --bootstrap -i guile zile -p "$profile" -n
86
87 # Make sure the `:' syntax works.
88 guix package --bootstrap -i "glibc:debug" -p "$profile" -n
89
90 # Make sure nonexistent outputs are reported.
91 guix package --bootstrap -i "guile-bootstrap:out" -p "$profile" -n
92 ! guix package --bootstrap -i "guile-bootstrap:does-not-exist" -p "$profile" -n
93 ! guix package --bootstrap -i "guile-bootstrap:does-not-exist" -p "$profile"
94
95 # Make sure we get an error when trying to remove something that's not
96 # installed.
97 ! guix package --bootstrap -r something-not-installed -p "$profile"
98
99 # Check whether `--list-available' returns something sensible.
100 guix package -p "$profile" -A 'gui.*e' | grep guile
101
102 # Check whether `--show' returns something sensible.
103 guix package --show=guile | grep "^name: guile"
104
105 # Ensure `--show' doesn't fail for packages with non-package inputs.
106 guix package --show=texlive
107
108 # Fail for non-existent packages or package/version pairs.
109 ! guix package --show=does-not-exist
110 ! guix package --show=emacs@42
111
112 # Search.
113 LC_MESSAGES=C
114 export LC_MESSAGES
115 test "`guix package -s "An example GNU package" | grep ^name:`" = \
116 "name: hello"
117 test -z "`guix package -s "n0t4r341p4ck4g3"`"
118
119 # Search with one and then two regexps.
120 # First we get printed circuit boards *and* board games.
121 guix package -s '\<board\>' > "$tmpfile"
122 grep '^name: pcb' "$tmpfile"
123 grep '^name: gnubg' "$tmpfile"
124
125 # Second we get only board games.
126 guix package -s '\<board\>' -s game > "$tmpfile"
127 grep -v '^name: pcb' "$tmpfile" > /dev/null
128 grep '^name: gnubg' "$tmpfile"
129
130 rm -f "$tmpfile"
131
132 # Make sure deprecated packages don't show up: <https://bugs.gnu.org/30566>.
133 mkdir "$module_dir"
134 cat > "$module_dir/foo.scm"<<EOF
135 (define-module (foo)
136 #:use-module (guix packages)
137 #:use-module (gnu packages base))
138
139 (define-public deprecated
140 (deprecated-package "fileutils" coreutils))
141 EOF
142
143 guix build -L "$module_dir" -e '(@ (foo) deprecated)' -n
144 test "`guix package -L "$module_dir" -s ^fileutils$ | grep ^name:`" = ""
145
146 rm -rf "$module_dir"
147
148 # Make sure `--search' can display all the packages.
149 guix package --search="" > /dev/null
150
151 # There's no generation older than 12 months, so the following command should
152 # have no effect.
153 generation="`readlink_base "$profile"`"
154 ! guix package -p "$profile" --delete-generations=12m
155 test "`readlink_base "$profile"`" = "$generation"
156
157 # The following command should not delete the current generation, even though
158 # it matches the given pattern (see <http://bugs.gnu.org/19978>.) And since
159 # there's nothing else to delete, it should just fail.
160 guix package --list-generations -p "$profile"
161 ! guix package --bootstrap -p "$profile" --delete-generations=1..
162 test "`readlink_base "$profile"`" = "$generation"
163
164 # Make sure $profile is a GC root at this point.
165 real_profile="`readlink -f "$profile"`"
166 ! guix gc -d "$real_profile"
167 test -d "$real_profile"
168
169 # Now, let's remove all the symlinks to $real_profile, and make sure
170 # $real_profile is no longer a GC root.
171 rm "$profile" "$profile"-[0-9]-link
172 guix gc -d "$real_profile"
173 [ ! -d "$real_profile" ]
174
175 # Package transformations.
176
177 # Make sure we get the right version number when using '--with-source'.
178 mkdir "$module_dir"
179 emacs_tarball="$module_dir/emacs-42.5.9rc7.tar.gz"
180 touch "$emacs_tarball"
181 guix package -p "$profile" -i emacs --with-source="$emacs_tarball" -n \
182 2> "$tmpfile"
183 grep -E 'emacs[[:blank:]]+42\.5\.9rc7' "$tmpfile"
184 rm "$emacs_tarball" "$tmpfile"
185 rmdir "$module_dir"
186
187 # Install with package transformations.
188 guix install --bootstrap -p "$profile" sed --with-input=sed=guile-bootstrap
189 grep "sed=guile-bootstrap" "$profile/manifest"
190 test "$(readlink -f "$profile/bin/guile")" \
191 = "$(guix build guile-bootstrap)/bin/guile"
192 test ! -f "$profile/bin/sed"
193
194 # Make sure the package transformation is preserved.
195 guix package --bootstrap -p "$profile" -u
196 grep "sed=guile-bootstrap" "$profile/manifest"
197 test "$(readlink -f "$profile/bin/guile")" \
198 = "$(guix build guile-bootstrap)/bin/guile"
199 test ! -f "$profile/bin/sed"
200 rm "$profile" "$profile"-[0-9]-link
201
202 # Profiles with a relative file name. Make sure we don't create dangling
203 # symlinks--see bug report at
204 # <https://lists.gnu.org/archive/html/guix-devel/2018-07/msg00036.html>.
205 mkdir -p "$module_dir/foo"
206 ( cd "$module_dir" ; \
207 guix package --bootstrap -i guile-bootstrap -p foo/prof )
208 test -f "$module_dir/foo/prof/bin/guile"
209 rm "$module_dir/foo"/*
210 rmdir "$module_dir/foo"
211 rmdir "$module_dir"
212
213 #
214 # Try with the default profile.
215 #
216
217 XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
218 export XDG_CACHE_HOME
219 HOME="$PWD/t-home-$$"
220 export HOME
221
222 mkdir -p "$HOME"
223
224 # Get the canonical directory name so that 'guix package' recognizes it.
225 HOME="`cd $HOME; pwd -P`"
226
227 guix package --bootstrap -i guile-bootstrap
228 test -L "$HOME/.guix-profile"
229 test -f "$HOME/.guix-profile/bin/guile"
230
231 # Move to the empty profile.
232 default_profile="`readlink "$HOME/.guix-profile"`"
233 for i in `seq 1 3`
234 do
235 # Make sure the current generation is a GC root.
236 profile_link="`readlink "$default_profile"`"
237 guix gc --list-live | grep "`readlink "$profile_link"`"
238
239 guix package --bootstrap --roll-back
240 ! test -f "$HOME/.guix-profile/bin"
241 ! test -f "$HOME/.guix-profile/lib"
242 test "`readlink "$default_profile"`" = "`basename $default_profile-0-link`"
243 done
244
245 # Check whether '-p ~/.guix-profile' makes any difference.
246 # See <http://bugs.gnu.org/17939>.
247 ! test -e "$HOME/.guix-profile-0-link"
248 ! test -e "$HOME/.guix-profile-1-link"
249 guix package --bootstrap -p "$HOME/.guix-profile" -i guile-bootstrap
250 ! test -e "$HOME/.guix-profile-1-link"
251 guix package --bootstrap --roll-back -p "$HOME/.guix-profile"
252 ! test -e "$HOME/.guix-profile-0-link"
253
254 # Extraneous argument.
255 ! guix package install foo-bar
256
257 # Make sure the "broken pipe" doesn't yield an error.
258 # Note: 'pipefail' is a Bash-specific option.
259 set -o pipefail || true
260 guix package -A g | head -1 2> "$HOME/err1"
261 guix package -I | head -1 2> "$HOME/err2"
262 test "`cat "$HOME/err1" "$HOME/err2"`" = ""
263
264 # Make sure '-L' extends the package module search path.
265 mkdir "$module_dir"
266
267 cat > "$module_dir/foo.scm"<<EOF
268 (define-module (foo)
269 #:use-module (guix packages)
270 #:use-module (gnu packages emacs))
271
272 (define-public x
273 (package (inherit emacs)
274 (name "emacs-foo-bar")
275 (version "42.77.0")))
276 EOF
277
278 guix package -A emacs-foo-bar -L "$module_dir" | grep 42
279 guix package -i emacs-foo-bar@42 -n -L "$module_dir"
280
281 # Same thing using the 'GUIX_PACKAGE_PATH' environment variable.
282 GUIX_PACKAGE_PATH="$module_dir"
283 export GUIX_PACKAGE_PATH
284 guix package -A emacs-foo-bar | grep 42
285 guix package -i emacs-foo-bar@42 -n
286
287 # Make sure GUIX_PACKAGE_PATH/'-L' takes precedence in case of duplicate packages.
288 cat > "$module_dir/bar.scm"<<EOF
289 (define-module (bar)
290 #:use-module (guix packages))
291
292 (define-public hello
293 (package (inherit (@@ (gnu packages base) hello))
294 (synopsis "an overridden version of GNU hello")))
295 EOF
296
297 guix package -i hello -n 2>&1 | grep choosing.*bar.scm
298 ( unset GUIX_PACKAGE_PATH; \
299 guix package -i hello -n -L "$module_dir" 2>&1 | grep choosing.*bar.scm )
300
301 # Make sure patches that live under $GUIX_PACKAGE_PATH are found.
302 cat > "$module_dir/emacs.patch"<<EOF
303 This is a fake patch.
304 EOF
305 cat > "$module_dir/foo.scm"<<EOF
306 (define-module (foo)
307 #:use-module (guix packages)
308 #:use-module (gnu packages)
309 #:use-module (gnu packages emacs))
310
311 (define-public x
312 (package (inherit emacs)
313 (source (origin (inherit (package-source emacs))
314 (patches (list (search-patch "emacs.patch")))))
315 (name "emacs-foo-bar-patched")
316 (version "42.42.42")))
317
318 (define-public y
319 (package (inherit emacs)
320 (name "super-non-portable-emacs")
321 (supported-systems '("foobar64-hurd"))))
322 EOF
323 guix package -i emacs-foo-bar-patched -n
324
325 # Same when -L is used.
326 ( unset GUIX_PACKAGE_PATH; \
327 guix package -L "$module_dir" -i emacs-foo-bar-patched -n )
328
329 # Make sure installing from a file works.
330 cat > "$module_dir/package.scm"<<EOF
331 (use-modules (gnu))
332 (use-package-modules bootstrap)
333
334 %bootstrap-guile
335 EOF
336 guix package --bootstrap --install-from-file="$module_dir/package.scm"
337
338 # Make sure an error is raised if the file doesn't return a package.
339 cat > "$module_dir/package.scm"<<EOF
340 (use-modules (gnu packages base))
341
342 (define my-package coreutils) ;returns *unspecified*
343 EOF
344 ! guix package --bootstrap --install-from-file="$module_dir/package.scm"
345
346 rm "$module_dir/package.scm"
347
348 # This one should not show up in searches since it's no supported on the
349 # current system.
350 test "`guix package -A super-non-portable-emacs`" = ""
351 test "`guix package -s super-non-portable-emacs | grep ^systems:`" = "systems: "
352
353 # Don't upgrade packages marked for removal: <http://bugs.gnu.org/27262>.
354 guix package --bootstrap -p "$profile" -i guile-bootstrap
355
356 cat > "$module_dir/foo.scm"<<EOF
357 (define-module (foo)
358 #:use-module (guix)
359 #:use-module (gnu packages bootstrap))
360
361 (define-public x
362 (package (inherit %bootstrap-guile) (version "42")))
363 EOF
364
365 guix package --bootstrap -p "$profile" -r guile-bootstrap -u guile
366 test ! -f "$profile/bin/guile"
367 guix package --bootstrap -p "$profile" --roll-back
368 test -f "$profile/bin/guile"
369 rm "$profile-2-link"
370
371 unset GUIX_PACKAGE_PATH
372
373
374 # Using 'GUIX_BUILD_OPTIONS'.
375 available="`guix package -A | sort`"
376 GUIX_BUILD_OPTIONS="--dry-run --no-grafts"
377 export GUIX_BUILD_OPTIONS
378
379 # Make sure $GUIX_BUILD_OPTIONS is not simply appended to the command-line,
380 # which would break 'guix package -A' and similar.
381 available2="`guix package -A | sort`"
382 test "$available2" = "$available"
383 guix package -I
384
385 # Restore '--no-grafts', which makes sure we don't end up building stuff when
386 # '--dry-run' is passed.
387 GUIX_BUILD_OPTIONS="--no-grafts"
388
389 # Applying a manifest file.
390 cat > "$module_dir/manifest.scm"<<EOF
391 (use-package-modules bootstrap)
392
393 (packages->manifest (list %bootstrap-guile))
394 EOF
395 guix package --bootstrap -m "$module_dir/manifest.scm"
396 guix package -I | grep guile
397 test `guix package -I | wc -l` -eq 1
398 guix package --rollback --bootstrap
399
400 # Applying two manifests.
401 cat > "$module_dir/manifest2.scm"<<EOF
402 (use-modules (gnu packages bootstrap) (guix))
403 (define p (package (inherit %bootstrap-guile) (name "eliug")))
404 (packages->manifest (list p))
405 EOF
406 guix package --bootstrap \
407 -m "$module_dir/manifest.scm" -m "$module_dir/manifest2.scm"
408 guix package -I | grep guile
409 guix package -I | grep eliug
410 test `guix package -I | wc -l` -eq 2
411 guix package --rollback --bootstrap
412
413 # Applying a manifest file with inferior packages.
414 cat > "$module_dir/manifest.scm"<<EOF
415 (use-modules (guix inferior))
416
417 (define i
418 (open-inferior "$abs_top_srcdir" #:command "scripts/guix"))
419
420 (let ((guile (car (lookup-inferior-packages i "guile-bootstrap"))))
421 (packages->manifest (list guile)))
422 EOF
423 guix package --bootstrap -m "$module_dir/manifest.scm"
424 guix package -I | grep guile
425 test `guix package -I | wc -l` -eq 1
426
427 # Error reporting.
428 cat > "$module_dir/manifest.scm"<<EOF
429 (use-package-modules bootstrap)
430 (packages->manifest
431 (list %bootstrap-guile
432 wonderful-package-that-does-not-exist))
433 EOF
434 if guix package --bootstrap -n -m "$module_dir/manifest.scm" \
435 2> "$module_dir/stderr"
436 then false
437 else
438 cat "$module_dir/stderr"
439 grep "manifest.scm:[1-3]:.*wonderful-package.*: unbound variable" \
440 "$module_dir/stderr"
441 fi
442
443 # Verify that package outputs are included in search results.
444 rm -rf "$module_dir"
445 mkdir "$module_dir"
446 cat > "$module_dir/foo.scm"<<EOF
447 (define-module (foo)
448 #:use-module (guix packages)
449 #:use-module (guix build-system trivial))
450
451 (define-public dummy-package
452 (package
453 (name "dummy-package")
454 (version "dummy-version")
455 (outputs '("out" "dummy-output"))
456 (source #f)
457 ;; Without a real build system, the "guix package -s" command will fail.
458 (build-system trivial-build-system)
459 (synopsis "dummy-synopsis")
460 (description "dummy-description")
461 (home-page "https://dummy-home-page")
462 (license #f)))
463 EOF
464 guix package -L "$module_dir" -s dummy-output > /tmp/out
465 test "`guix package -L "$module_dir" -s dummy-output | grep ^name:`" = "name: dummy-package"
466 rm -rf "$module_dir"
467
468 # Make sure we can see user profiles.
469 guix package --list-profiles | grep "$profile"
470 guix package --list-profiles | grep '\.guix-profile'
471
472 # Make sure we can properly lock a profile.
473 mkdir "$module_dir"
474 echo "(open-output-file \"$module_dir/ready\") (sleep 60)" \
475 > "$module_dir/manifest.scm"
476 guix package -m "$module_dir/manifest.scm" -p "$module_dir/profile" &
477 pid=$!
478 while [ ! -f "$module_dir/ready" ] ; do sleep 0.5 ; done
479 if guix install emacs -p "$module_dir/profile"; then kill $pid; false; else true; fi
480 kill $pid