guix-environment.sh 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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"
  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. rm "$gcroot"
  104. # Try '-r' with a relative file name.
  105. (cd "$tmpdir"; mkdir "gc-root";
  106. guix environment --bootstrap -r "gc-root/r" --ad-hoc guile-bootstrap \
  107. -- guile -c 1;
  108. rm "gc-root/r"; rmdir "gc-root")
  109. # Same with an absolute file name.
  110. guix environment --bootstrap -r "$PWD/$gcroot" --ad-hoc guile-bootstrap \
  111. -- guile -c 1
  112. test `readlink "$gcroot"` = "$expected"
  113. case "`uname -m`" in
  114. x86_64)
  115. # On x86_64, we should be able to create a 32-bit environment.
  116. guix environment --bootstrap --ad-hoc guile-bootstrap --pure \
  117. -- guile -c '(exit (string-prefix? "x86_64" %host-type))'
  118. guix environment --bootstrap --ad-hoc guile-bootstrap --pure \
  119. -s i686-linux \
  120. -- guile -c '(exit (string-prefix? "i686" %host-type))'
  121. ;;
  122. *)
  123. echo "nothing to do" >&2
  124. ;;
  125. esac
  126. # Make sure we can build the environment of 'guix'. There may be collisions
  127. # in its profile (e.g., for 'gzip'), but we have to accept them.
  128. guix environment guix --bootstrap -n
  129. # Try program transformation options.
  130. mkdir "$tmpdir/emacs-36.8"
  131. drv="`guix environment --ad-hoc emacs -n 2>&1 | grep 'emacs.*\.drv'`"
  132. transformed_drv="`guix environment --ad-hoc emacs --with-source="$tmpdir/emacs-36.8" -n 2>&1 | grep 'emacs.*\.drv'`"
  133. test -n "$drv"
  134. test "$drv" != "$transformed_drv"
  135. case "$transformed_drv" in
  136. *-emacs-36.8.drv) true;;
  137. *) false;;
  138. esac
  139. rmdir "$tmpdir/emacs-36.8"
  140. # Transformation options without '--ad-hoc'.
  141. drv="`guix environment -n emacs-geiser 2>&1 | grep '\.drv$'`"
  142. transformed_drv="`guix environment -n emacs-geiser \
  143. --with-input=emacs-minimal=vim 2>&1 | grep '\.drv$'`"
  144. test "$drv" != "$transformed_drv"
  145. case "$drv" in
  146. *-emacs-minimal*.drv*) true;;
  147. *) false;;
  148. esac
  149. case "$transformed_drv" in
  150. *-emacs-minimal*.drv*) false;;
  151. *) true;;
  152. esac
  153. case "$transformed_drv" in
  154. *-vim*.drv*) true;;
  155. *) false;;
  156. esac
  157. if guile -c '(getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)' 2> /dev/null
  158. then
  159. # Compute the build environment for the initial GNU Make.
  160. guix environment --bootstrap --no-substitutes --search-paths --pure \
  161. -e '(@ (guix tests) gnu-make-for-tests)' > "$tmpdir/a"
  162. # Make sure bootstrap binaries are in the profile.
  163. profile=`grep "^export PATH" "$tmpdir/a" | sed -r 's|^.*="(.*)/bin"|\1|'`
  164. # Make sure the bootstrap binaries are all listed where they belong.
  165. grep -E "^export PATH=\"$profile/bin\"" "$tmpdir/a"
  166. grep -E "^export C_INCLUDE_PATH=\"$profile/include\"" "$tmpdir/a"
  167. grep -E "^export LIBRARY_PATH=\"$profile/lib\"" "$tmpdir/a"
  168. for dep in bootstrap-binaries-0 gcc-bootstrap-0 glibc-bootstrap-0
  169. do
  170. guix gc --references "$profile" | grep "$dep"
  171. done
  172. # 'make-boot0' itself must not be listed.
  173. ! guix gc --references "$profile" | grep make-boot0
  174. # Make sure that the shell spawned with '--exec' sees the same environment
  175. # as returned by '--search-paths'.
  176. guix environment --bootstrap --no-substitutes --pure \
  177. -e '(@ (guix tests) gnu-make-for-tests)' \
  178. -- /bin/sh -c 'echo $PATH $C_INCLUDE_PATH $LIBRARY_PATH' > "$tmpdir/b"
  179. ( . "$tmpdir/a" ; echo $PATH $C_INCLUDE_PATH $LIBRARY_PATH ) > "$tmpdir/c"
  180. cmp "$tmpdir/b" "$tmpdir/c"
  181. rm "$tmpdir"/*
  182. # The following test assumes 'make-boot0' has a "debug" output.
  183. make_boot0_debug="`guix build -e '(@ (guix tests) gnu-make-for-tests)' | grep -e -debug`"
  184. test "x$make_boot0_debug" != "x"
  185. # Make sure the "debug" output is not listed.
  186. ! guix gc --references "$profile" | grep "$make_boot0_debug"
  187. # Compute the build environment for the initial GNU Make, but add in the
  188. # bootstrap Guile as an ad-hoc addition.
  189. guix environment --bootstrap --no-substitutes --search-paths --pure \
  190. -e '(@ (guix tests) gnu-make-for-tests)' \
  191. --ad-hoc guile-bootstrap > "$tmpdir/a"
  192. profile=`grep "^export PATH" "$tmpdir/a" | sed -r 's|^.*="(.*)/bin"|\1|'`
  193. # Make sure the bootstrap binaries are all listed where they belong.
  194. grep -E "^export PATH=\"$profile/bin\"" "$tmpdir/a"
  195. grep -E "^export C_INCLUDE_PATH=\"$profile/include\"" "$tmpdir/a"
  196. grep -E "^export LIBRARY_PATH=\"$profile/lib\"" "$tmpdir/a"
  197. for dep in bootstrap-binaries-0 gcc-bootstrap-0 glibc-bootstrap-0 \
  198. guile-bootstrap
  199. do
  200. guix gc --references "$profile" | grep "$dep"
  201. done
  202. # Make sure a package list with plain package objects and package+output
  203. # tuples can be used with -e.
  204. expr_list_test_code="
  205. (list (@ (guix tests) gnu-make-for-tests)
  206. (list (@ (gnu packages bootstrap) %bootstrap-guile) \"out\"))"
  207. guix environment --bootstrap --ad-hoc --no-substitutes --search-paths \
  208. --pure -e "$expr_list_test_code" > "$tmpdir/a"
  209. profile=`grep "^export PATH" "$tmpdir/a" | sed -r 's|^.*="(.*)/bin"|\1|'`
  210. for dep in make-test-boot0 guile-bootstrap
  211. do
  212. guix gc --references "$profile" | grep "$dep"
  213. done
  214. fi