Merge branch 'master' into staging
[jackhill/guix/guix.git] / tests / guix-environment.sh
1 # GNU Guix --- Functional package management for GNU
2 # Copyright © 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 #
4 # This file is part of GNU Guix.
5 #
6 # GNU Guix is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or (at
9 # your option) any later version.
10 #
11 # GNU Guix is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 #
20 # Test 'guix environment'.
21 #
22
23 set -e
24
25 guix environment --version
26
27 tmpdir="t-guix-environment-$$"
28 gcroot="t-guix-environment-gc-root-$$"
29 trap 'rm -r "$tmpdir"; rm -f "$gcroot"' EXIT
30
31 mkdir "$tmpdir"
32
33 # 'guix environment' launches /bin/sh if 'SHELL' is unset, so export 'SHELL'
34 # since we know it's valid (build environments lack /bin/sh.)
35 export SHELL
36
37 # Check the environment variables for the bootstrap Guile.
38 guix environment --bootstrap --ad-hoc guile-bootstrap --pure \
39 --search-paths > "$tmpdir/a"
40 guix environment --bootstrap --ad-hoc guile-bootstrap:out --pure \
41 --search-paths > "$tmpdir/b"
42
43 # $PATH must appear in the search paths, and nothing else.
44 grep -E '^export PATH=.*profile/bin' "$tmpdir/a"
45 test "`wc -l < "$tmpdir/a"`" = 1
46
47 # Guile must be on $PATH.
48 test -x `sed -r 's/^export PATH="(.*)"/\1/' "$tmpdir/a"`/guile
49
50 cmp "$tmpdir/a" "$tmpdir/b"
51
52 # Check '--inherit'.
53 GUIX_TEST_ABC=1
54 GUIX_TEST_DEF=2
55 GUIX_TEST_XYZ=3
56 export GUIX_TEST_ABC GUIX_TEST_DEF GUIX_TEST_XYZ
57 guix environment --bootstrap --ad-hoc guile-bootstrap --pure \
58 --inherit='^GUIX_TEST_A' --inherit='^GUIX_TEST_D' \
59 -- "$SHELL" -c set > "$tmpdir/a"
60 grep '^PATH=' "$tmpdir/a"
61 grep '^GUIX_TEST_ABC=' "$tmpdir/a"
62 grep '^GUIX_TEST_DEF=' "$tmpdir/a"
63 if grep '^GUIX_TEST_XYZ=' "$tmpdir/a"; then false; else true; fi
64
65 # Make sure the exit value is preserved.
66 if guix environment --bootstrap --ad-hoc guile-bootstrap --pure \
67 -- guile -c '(exit 42)'
68 then
69 false
70 else
71 test $? = 42
72 fi
73
74 # Make sure 'GUIX_ENVIRONMENT' points to the profile.
75 guix environment --bootstrap --ad-hoc guile-bootstrap --pure \
76 -- "$SHELL" -c 'test -f "$GUIX_ENVIRONMENT/bin/guile"'
77
78 # Make sure 'GUIX_ENVIRONMENT' points to the profile when building from a
79 # manifest.
80 echo "(use-modules (guix profiles) (gnu packages bootstrap))
81
82 (packages->manifest (list %bootstrap-guile))
83 " > $tmpdir/manifest.scm
84 guix environment --bootstrap --manifest=$tmpdir/manifest.scm --pure \
85 -- "$SHELL" -c 'test -f "$GUIX_ENVIRONMENT/bin/guile"'
86
87 # Make sure '-r' works as expected.
88 rm -f "$gcroot"
89 expected="`guix environment --bootstrap --ad-hoc guile-bootstrap \
90 -- "$SHELL" -c 'echo $GUIX_ENVIRONMENT'`"
91 guix environment --bootstrap -r "$gcroot" --ad-hoc guile-bootstrap \
92 -- guile -c 1
93 test `readlink "$gcroot"` = "$expected"
94
95 # Make sure '-r' is idempotent.
96 guix environment --bootstrap -r "$gcroot" --ad-hoc guile-bootstrap \
97 -- guile -c 1
98 test `readlink "$gcroot"` = "$expected"
99 rm "$gcroot"
100
101 # Same with an absolute file name.
102 guix environment --bootstrap -r "$PWD/$gcroot" --ad-hoc guile-bootstrap \
103 -- guile -c 1
104 test `readlink "$gcroot"` = "$expected"
105
106 case "`uname -m`" in
107 x86_64)
108 # On x86_64, we should be able to create a 32-bit environment.
109 guix environment --bootstrap --ad-hoc guile-bootstrap --pure \
110 -- guile -c '(exit (string-prefix? "x86_64" %host-type))'
111 guix environment --bootstrap --ad-hoc guile-bootstrap --pure \
112 -s i686-linux \
113 -- guile -c '(exit (string-prefix? "i686" %host-type))'
114 ;;
115 *)
116 echo "nothing to do" >&2
117 ;;
118 esac
119
120
121 # Same as above, but with deprecated -E flag.
122 if guix environment --bootstrap --ad-hoc guile-bootstrap --pure \
123 -E "guile -c '(exit 42)'"
124 then
125 false
126 else
127 test $? = 42
128 fi
129
130 # Make sure we can build the environment of 'guix'. There may be collisions
131 # in its profile (e.g., for 'gzip'), but we have to accept them.
132 guix environment guix --bootstrap -n
133
134 # Try program transformation options.
135 mkdir "$tmpdir/emacs-36.8"
136 drv="`guix environment --ad-hoc emacs -n 2>&1 | grep 'emacs.*\.drv'`"
137 transformed_drv="`guix environment --ad-hoc emacs --with-source="$tmpdir/emacs-36.8" -n 2>&1 | grep 'emacs.*\.drv'`"
138 test -n "$drv"
139 test "$drv" != "$transformed_drv"
140 case "$transformed_drv" in
141 *-emacs-36.8.drv) true;;
142 *) false;;
143 esac
144 rmdir "$tmpdir/emacs-36.8"
145
146 if guile -c '(getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)' 2> /dev/null
147 then
148 # Compute the build environment for the initial GNU Make.
149 guix environment --bootstrap --no-substitutes --search-paths --pure \
150 -e '(@@ (gnu packages commencement) gnu-make-boot0)' > "$tmpdir/a"
151
152 # Make sure bootstrap binaries are in the profile.
153 profile=`grep "^export PATH" "$tmpdir/a" | sed -r 's|^.*="(.*)/bin"|\1|'`
154
155 # Make sure the bootstrap binaries are all listed where they belong.
156 grep -E "^export PATH=\"$profile/bin\"" "$tmpdir/a"
157 grep -E "^export CPATH=\"$profile/include\"" "$tmpdir/a"
158 grep -E "^export LIBRARY_PATH=\"$profile/lib\"" "$tmpdir/a"
159 for dep in bootstrap-binaries-0 gcc-bootstrap-0 glibc-bootstrap-0
160 do
161 guix gc --references "$profile" | grep "$dep"
162 done
163
164 # 'make-boot0' itself must not be listed.
165 if guix gc --references "$profile" | grep make-boot0
166 then false; else true; fi
167
168 # Make sure that the shell spawned with '--exec' sees the same environment
169 # as returned by '--search-paths'.
170 guix environment --bootstrap --no-substitutes --pure \
171 -e '(@@ (gnu packages commencement) gnu-make-boot0)' \
172 -- /bin/sh -c 'echo $PATH $CPATH $LIBRARY_PATH' > "$tmpdir/b"
173 ( . "$tmpdir/a" ; echo $PATH $CPATH $LIBRARY_PATH ) > "$tmpdir/c"
174 cmp "$tmpdir/b" "$tmpdir/c"
175
176 rm "$tmpdir"/*
177
178 # Compute the build environment for the initial GNU Findutils.
179 guix environment --bootstrap --no-substitutes --search-paths --pure \
180 -e '(@@ (gnu packages commencement) findutils-boot0)' > "$tmpdir/a"
181 profile=`grep "^export PATH" "$tmpdir/a" | sed -r 's|^.*="(.*)/bin"|\1|'`
182
183 # Make sure the bootstrap binaries are all listed where they belong.
184 grep -E "^export PATH=\"$profile/bin\"" "$tmpdir/a"
185 grep -E "^export CPATH=\"$profile/include\"" "$tmpdir/a"
186 grep -E "^export LIBRARY_PATH=\"$profile/lib\"" "$tmpdir/a"
187 for dep in bootstrap-binaries-0 gcc-bootstrap-0 glibc-bootstrap-0 \
188 make-boot0
189 do
190 guix gc --references "$profile" | grep "$dep"
191 done
192
193 # The following test assumes 'make-boot0' has a "debug" output.
194 make_boot0_debug="`guix build -e '(@@ (gnu packages commencement) gnu-make-boot0)' | grep -e -debug`"
195 test "x$make_boot0_debug" != "x"
196
197 # Make sure the "debug" output is not listed.
198 if guix gc --references "$profile" | grep "$make_boot0_debug"
199 then false; else true; fi
200
201 # Compute the build environment for the initial GNU Make, but add in the
202 # bootstrap Guile as an ad-hoc addition.
203 guix environment --bootstrap --no-substitutes --search-paths --pure \
204 -e '(@@ (gnu packages commencement) gnu-make-boot0)' \
205 --ad-hoc guile-bootstrap > "$tmpdir/a"
206 profile=`grep "^export PATH" "$tmpdir/a" | sed -r 's|^.*="(.*)/bin"|\1|'`
207
208 # Make sure the bootstrap binaries are all listed where they belong.
209 grep -E "^export PATH=\"$profile/bin\"" "$tmpdir/a"
210 grep -E "^export CPATH=\"$profile/include\"" "$tmpdir/a"
211 grep -E "^export LIBRARY_PATH=\"$profile/lib\"" "$tmpdir/a"
212 for dep in bootstrap-binaries-0 gcc-bootstrap-0 glibc-bootstrap-0 \
213 guile-bootstrap
214 do
215 guix gc --references "$profile" | grep "$dep"
216 done
217
218 # Make sure a package list with plain package objects and package+output
219 # tuples can be used with -e.
220 expr_list_test_code="
221 (list (@@ (gnu packages commencement) gnu-make-boot0)
222 (list (@ (gnu packages bootstrap) %bootstrap-guile) \"out\"))"
223
224 guix environment --bootstrap --ad-hoc --no-substitutes --search-paths \
225 --pure -e "$expr_list_test_code" > "$tmpdir/a"
226 profile=`grep "^export PATH" "$tmpdir/a" | sed -r 's|^.*="(.*)/bin"|\1|'`
227
228 for dep in make-boot0 guile-bootstrap
229 do
230 guix gc --references "$profile" | grep "$dep"
231 done
232 fi