guix-environment.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. # GNU Guix --- Functional package management for GNU
  2. # Copyright © 2015, 2016, 2017, 2018, 2019, 2021 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. # Test 'guix environment'.
  20. #
  21. set -e
  22. guix environment --version
  23. tmpdir="t-guix-environment-$$"
  24. gcroot="t-guix-environment-gc-root-$$"
  25. trap 'rm -r "$tmpdir"; rm -f "$gcroot"' EXIT
  26. mkdir "$tmpdir"
  27. # 'guix environment' launches /bin/sh if 'SHELL' is unset, so export 'SHELL'
  28. # since we know it's valid (build environments lack /bin/sh.)
  29. export SHELL
  30. # Check the environment variables for the bootstrap Guile.
  31. guix environment --bootstrap --ad-hoc guile-bootstrap --pure \
  32. --search-paths > "$tmpdir/a"
  33. guix environment --bootstrap --ad-hoc guile-bootstrap:out --pure \
  34. --search-paths > "$tmpdir/b"
  35. # $PATH must appear in the search paths, and nothing else.
  36. grep -E '^export PATH=.*profile/bin' "$tmpdir/a"
  37. test "`wc -l < "$tmpdir/a"`" = 1
  38. # Guile must be on $PATH.
  39. test -x `sed -r 's/^export PATH="(.*)"/\1/' "$tmpdir/a"`/guile
  40. cmp "$tmpdir/a" "$tmpdir/b"
  41. # Check '--preserve'.
  42. GUIX_TEST_ABC=1
  43. GUIX_TEST_DEF=2
  44. GUIX_TEST_XYZ=3
  45. export GUIX_TEST_ABC GUIX_TEST_DEF GUIX_TEST_XYZ
  46. guix environment --bootstrap --ad-hoc guile-bootstrap --pure \
  47. --preserve='^GUIX_TEST_A' --preserve='^GUIX_TEST_D' \
  48. -- "$SHELL" -c set > "$tmpdir/a"
  49. grep '^PATH=' "$tmpdir/a"
  50. grep '^GUIX_TEST_ABC=' "$tmpdir/a"
  51. grep '^GUIX_TEST_DEF=' "$tmpdir/a"
  52. grep '^GUIX_TEST_XYZ=' "$tmpdir/a" && false
  53. # Make sure the exit value is preserved.
  54. if guix environment --bootstrap --ad-hoc guile-bootstrap --pure \
  55. -- guile -c '(exit 42)'
  56. then
  57. false
  58. else
  59. test $? = 42
  60. fi
  61. # Make sure 'GUIX_ENVIRONMENT' points to the profile.
  62. guix environment --bootstrap --ad-hoc guile-bootstrap --pure \
  63. -- "$SHELL" -c 'test -f "$GUIX_ENVIRONMENT/bin/guile"'
  64. # Make sure 'GUIX_ENVIRONMENT' points to the profile when building from a
  65. # manifest.
  66. echo "(use-modules (guix profiles) (gnu packages bootstrap))
  67. (packages->manifest (list %bootstrap-guile))
  68. " > $tmpdir/manifest.scm
  69. guix environment --bootstrap --manifest=$tmpdir/manifest.scm --pure \
  70. -- "$SHELL" -c 'test -f "$GUIX_ENVIRONMENT/bin/guile"'
  71. # Make sure '--manifest' can be specified multiple times.
  72. cat > "$tmpdir/manifest2.scm" <<EOF
  73. (use-modules (guix) (guix profiles)
  74. (guix build-system trivial)
  75. (gnu packages bootstrap))
  76. (packages->manifest
  77. (list (package
  78. (inherit %bootstrap-guile)
  79. (name "eliug")
  80. (build-system trivial-build-system)
  81. (arguments
  82. (quasiquote
  83. (#:guile ,%bootstrap-guile
  84. #:builder
  85. (begin
  86. (mkdir %output)
  87. (mkdir (string-append %output "/eliug")))))))))
  88. EOF
  89. guix environment --bootstrap -m "$tmpdir/manifest.scm" \
  90. -m "$tmpdir/manifest2.scm" --pure \
  91. -- "$SHELL" -c 'test -f "$GUIX_ENVIRONMENT/bin/guile" && test -d "$GUIX_ENVIRONMENT/eliug"'
  92. # Make sure '-r' works as expected.
  93. rm -f "$gcroot"
  94. expected="`guix environment --bootstrap --ad-hoc guile-bootstrap \
  95. -- "$SHELL" -c 'echo $GUIX_ENVIRONMENT'`"
  96. guix environment --bootstrap -r "$gcroot" --ad-hoc guile-bootstrap \
  97. -- guile -c 1
  98. test `readlink "$gcroot"` = "$expected"
  99. # Make sure '-r' is idempotent.
  100. guix environment --bootstrap -r "$gcroot" --ad-hoc guile-bootstrap \
  101. -- guile -c 1
  102. test `readlink "$gcroot"` = "$expected"
  103. # Make sure '-p' works as expected.
  104. test $(guix environment -p "$gcroot" -- "$SHELL" -c 'echo $GUIX_ENVIRONMENT') = "$expected"
  105. paths1="$(guix environment -p "$gcroot" --search-paths)"
  106. paths2="$(guix environment --bootstrap --ad-hoc guile-bootstrap --search-paths)"
  107. test "$paths1" = "$paths2"
  108. rm "$gcroot"
  109. # Try '-r' with a relative file name.
  110. (cd "$tmpdir"; mkdir "gc-root";
  111. guix environment --bootstrap -r "gc-root/r" --ad-hoc guile-bootstrap \
  112. -- guile -c 1;
  113. rm "gc-root/r"; rmdir "gc-root")
  114. # Same with an absolute file name.
  115. guix environment --bootstrap -r "$PWD/$gcroot" --ad-hoc guile-bootstrap \
  116. -- guile -c 1
  117. test `readlink "$gcroot"` = "$expected"
  118. case "`uname -m`" in
  119. x86_64)
  120. # On x86_64, we should be able to create a 32-bit environment.
  121. guix environment --bootstrap --ad-hoc guile-bootstrap --pure \
  122. -- guile -c '(exit (string-prefix? "x86_64" %host-type))'
  123. guix environment --bootstrap --ad-hoc guile-bootstrap --pure \
  124. -s i686-linux \
  125. -- guile -c '(exit (string-prefix? "i686" %host-type))'
  126. ;;
  127. *)
  128. echo "nothing to do" >&2
  129. ;;
  130. esac
  131. # Make sure we can build the environment of 'guix'. There may be collisions
  132. # in its profile (e.g., for 'gzip'), but we have to accept them.
  133. guix environment guix --bootstrap -n
  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. # Transformation options without '--ad-hoc'.
  146. drv="`guix environment -n emacs-geiser 2>&1 | grep '\.drv$'`"
  147. transformed_drv="`guix environment -n emacs-geiser \
  148. --with-input=emacs-minimal=vim 2>&1 | grep '\.drv$'`"
  149. test "$drv" != "$transformed_drv"
  150. case "$drv" in
  151. *-emacs-minimal*.drv*) true;;
  152. *) false;;
  153. esac
  154. case "$transformed_drv" in
  155. *-emacs-minimal*.drv*) false;;
  156. *) true;;
  157. esac
  158. case "$transformed_drv" in
  159. *-vim*.drv*) true;;
  160. *) false;;
  161. esac
  162. if guile -c '(getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)' 2> /dev/null
  163. then
  164. # Compute the build environment for the initial GNU Make.
  165. guix environment --bootstrap --no-substitutes --search-paths --pure \
  166. -e '(@ (guix tests) gnu-make-for-tests)' > "$tmpdir/a"
  167. # Make sure bootstrap binaries are in the profile.
  168. profile=`grep "^export PATH" "$tmpdir/a" | sed -r 's|^.*="(.*)/bin"|\1|'`
  169. # Make sure the bootstrap binaries are all listed where they belong.
  170. grep -E "^export PATH=\"$profile/bin\"" "$tmpdir/a"
  171. grep -E "^export C_INCLUDE_PATH=\"$profile/include\"" "$tmpdir/a"
  172. grep -E "^export LIBRARY_PATH=\"$profile/lib\"" "$tmpdir/a"
  173. for dep in bootstrap-binaries-0 gcc-bootstrap-0 glibc-bootstrap-0
  174. do
  175. guix gc --references "$profile" | grep "$dep"
  176. done
  177. # 'make-boot0' itself must not be listed.
  178. guix gc --references "$profile" | grep make-boot0 && false
  179. # Make sure that the shell spawned with '--exec' sees the same environment
  180. # as returned by '--search-paths'.
  181. guix environment --bootstrap --no-substitutes --pure \
  182. -e '(@ (guix tests) gnu-make-for-tests)' \
  183. -- /bin/sh -c 'echo $PATH $C_INCLUDE_PATH $LIBRARY_PATH' > "$tmpdir/b"
  184. ( . "$tmpdir/a" ; echo $PATH $C_INCLUDE_PATH $LIBRARY_PATH ) > "$tmpdir/c"
  185. cmp "$tmpdir/b" "$tmpdir/c"
  186. rm "$tmpdir"/*
  187. # The following test assumes 'make-boot0' has a "debug" output.
  188. make_boot0_debug="`guix build -e '(@ (guix tests) gnu-make-for-tests)' | grep -e -debug`"
  189. test "x$make_boot0_debug" != "x"
  190. # Make sure the "debug" output is not listed.
  191. guix gc --references "$profile" | grep "$make_boot0_debug" && false
  192. # Compute the build environment for the initial GNU Make, but add in the
  193. # bootstrap Guile as an ad-hoc addition.
  194. guix environment --bootstrap --no-substitutes --search-paths --pure \
  195. -e '(@ (guix tests) gnu-make-for-tests)' \
  196. --ad-hoc guile-bootstrap > "$tmpdir/a"
  197. profile=`grep "^export PATH" "$tmpdir/a" | sed -r 's|^.*="(.*)/bin"|\1|'`
  198. # Make sure the bootstrap binaries are all listed where they belong.
  199. grep -E "^export PATH=\"$profile/bin\"" "$tmpdir/a"
  200. grep -E "^export C_INCLUDE_PATH=\"$profile/include\"" "$tmpdir/a"
  201. grep -E "^export LIBRARY_PATH=\"$profile/lib\"" "$tmpdir/a"
  202. for dep in bootstrap-binaries-0 gcc-bootstrap-0 glibc-bootstrap-0 \
  203. guile-bootstrap
  204. do
  205. guix gc --references "$profile" | grep "$dep"
  206. done
  207. # Make sure a package list with plain package objects and package+output
  208. # tuples can be used with -e.
  209. expr_list_test_code="
  210. (list (@ (guix tests) gnu-make-for-tests)
  211. (list (@ (gnu packages bootstrap) %bootstrap-guile) \"out\"))"
  212. guix environment --bootstrap --ad-hoc --no-substitutes --search-paths \
  213. --pure -e "$expr_list_test_code" > "$tmpdir/a"
  214. profile=`grep "^export PATH" "$tmpdir/a" | sed -r 's|^.*="(.*)/bin"|\1|'`
  215. for dep in make-test-boot0 guile-bootstrap
  216. do
  217. guix gc --references "$profile" | grep "$dep"
  218. done
  219. fi