guix-home.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # GNU Guix --- Functional package management for GNU
  2. # Copyright © 2021-2023 Andrew Tropin <andrew@trop.in>
  3. # Copyright © 2021 Oleg Pykhalov <go.wigust@gmail.com>
  4. # Copyright © 2022, 2023 Ludovic Courtès <ludo@gnu.org>
  5. #
  6. # This file is part of GNU Guix.
  7. #
  8. # GNU Guix is free software; you can redistribute it and/or modify it
  9. # under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 3 of the License, or (at
  11. # your option) any later version.
  12. #
  13. # GNU Guix is distributed in the hope that it will be useful, but
  14. # WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. # Test the 'guix home' using the external store, if any.
  22. #
  23. set -e
  24. guix home --version
  25. container_supported ()
  26. {
  27. if guile -c '((@ (guix scripts environment) assert-container-features))'
  28. then
  29. return 0
  30. else
  31. return 1
  32. fi
  33. }
  34. localstatedir="$(guile -c '(use-modules (guix config))(display %localstatedir)')"
  35. NIX_STORE_DIR="$(guile -c '(use-modules (guix config))(display %storedir)')"
  36. GUIX_DAEMON_SOCKET="$localstatedir/guix/daemon-socket/socket"
  37. export NIX_STORE_DIR GUIX_DAEMON_SOCKET
  38. # Run tests only when a "real" daemon is available.
  39. if ! guile -c '(use-modules (guix)) (exit (false-if-exception (open-connection)))'
  40. then
  41. exit 77
  42. fi
  43. STORE_PARENT="$(dirname "$NIX_STORE_DIR")"
  44. export STORE_PARENT
  45. if test "$STORE_PARENT" = "/"; then exit 77; fi
  46. test_directory="$(mktemp -d)"
  47. trap 'chmod -Rf +w "$test_directory"; rm -rf "$test_directory"' EXIT
  48. (
  49. cd "$test_directory" || exit 77
  50. cat > "home.scm" <<'EOF'
  51. (use-modules (guix gexp)
  52. (gnu home)
  53. (gnu home services)
  54. (gnu home services shells)
  55. (gnu packages bash)
  56. (gnu services))
  57. (home-environment
  58. (services
  59. (list
  60. (simple-service 'test-config
  61. home-files-service-type
  62. (list `(".config/test.conf"
  63. ,(plain-file
  64. "tmp-file.txt"
  65. "the content of ~/.config/test.conf"))))
  66. (service home-bash-service-type
  67. (home-bash-configuration
  68. (guix-defaults? #t)
  69. (bashrc (list (local-file "dot-bashrc")))))
  70. (simple-service 'add-environment-variable
  71. home-environment-variables-service-type
  72. `(("TODAY" . "26 messidor")
  73. ("SHELL" . ,(file-append bash "/bin/bash"))
  74. ("BUILDHOST_TIME" . ,#~(strftime "%c"
  75. (localtime (current-time))))
  76. ("STRING_WITH_ESCAPES" . "chars: \" /\\")
  77. ("LITERAL" . ,(literal-string "${abc}"))))
  78. (simple-service 'home-bash-service-extension-test
  79. home-bash-service-type
  80. (home-bash-extension
  81. (environment-variables
  82. '(("PS1" . "$GUIX_ENVIRONMENT λ ")))
  83. (aliases
  84. `(("run" . "guix shell")
  85. ("path" . ,(literal-string "echo $PATH"))))
  86. (bashrc
  87. (list
  88. (plain-file
  89. "bashrc-test-config.sh"
  90. "# the content of bashrc-test-config.sh"))))))))
  91. EOF
  92. echo -n "# dot-bashrc test file for guix home" > "dot-bashrc"
  93. # Check whether the graph commands work as expected.
  94. guix home extension-graph "home.scm" | grep 'label = "home-activation"'
  95. guix home extension-graph "home.scm" | grep 'label = "home-symlink-manager"'
  96. guix home extension-graph "home.scm" | grep 'label = "home"'
  97. # There are no Shepherd services so the one below must fail.
  98. guix home shepherd-graph "home.scm" && false
  99. if container_supported
  100. then
  101. # Run the home in a container. Always use bash inside container for
  102. # reproducibility of the tests.
  103. # TODO: Make container independent from external environment variables.
  104. SHELL=bash
  105. guix home container home.scm -- true
  106. guix home container home.scm -- false && false
  107. test "$(guix home container home.scm -- echo '$HOME')" = "$HOME"
  108. guix home container home.scm -- cat '~/.config/test.conf' | \
  109. grep "the content of"
  110. guix home container home.scm -- test -h '~/.bashrc'
  111. test "$(guix home container home.scm -- id -u)" = 1000
  112. guix home container home.scm -- test -f '$HOME/sample/home.scm' && false
  113. guix home container home.scm --expose="$PWD=$HOME/sample" -- \
  114. test -f '$HOME/sample/home.scm'
  115. guix home container home.scm --expose="$PWD=$HOME/sample" -- \
  116. rm -v '$HOME/sample/home.scm' && false
  117. else
  118. echo "'guix home container' test SKIPPED" >&2
  119. fi
  120. HOME="$test_directory"
  121. export HOME
  122. #
  123. # Test 'guix home reconfigure'.
  124. #
  125. echo "# This file will be overridden and backed up." > "$HOME/.bashrc"
  126. mkdir "$HOME/.config"
  127. echo "This file will be overridden too." > "$HOME/.config/test.conf"
  128. echo "This file will stay around." > "$HOME/.config/random-file"
  129. guix home reconfigure "${test_directory}/home.scm"
  130. test -d "${HOME}/.guix-home"
  131. test -h "${HOME}/.bash_profile"
  132. test -h "${HOME}/.bashrc"
  133. grep 'alias run="guix shell"' "$HOME/.bashrc"
  134. grep "alias path='echo \$PATH'" "$HOME/.bashrc"
  135. test "$(tail -n 2 "${HOME}/.bashrc")" == "\
  136. # dot-bashrc test file for guix home
  137. # the content of bashrc-test-config.sh"
  138. grep -q "the content of ~/.config/test.conf" "${HOME}/.config/test.conf"
  139. grep '^export PS1="\$GUIX_ENVIRONMENT λ "$' "${HOME}/.bash_profile"
  140. ( . "${HOME}/.guix-home/setup-environment"; test "$TODAY" = "26 messidor" )
  141. ( . "${HOME}/.guix-home/setup-environment"; test "$LITERAL" = '${abc}' )
  142. ( . "${HOME}/.guix-home/setup-environment";
  143. test "$STRING_WITH_ESCAPES" = "chars: \" /\\")
  144. ( . "${HOME}/.guix-home/setup-environment";
  145. echo "$SHELL" | grep "/gnu/store/.*/bin/bash" )
  146. # This one should still be here.
  147. grep "stay around" "$HOME/.config/random-file"
  148. # Make sure preexisting files were backed up.
  149. grep "overridden" "$HOME"/*guix-home*backup/.bashrc
  150. grep "overridden" "$HOME"/*guix-home*backup/.config/test.conf
  151. rm -r "$HOME"/*guix-home*backup
  152. #
  153. # Test 'guix home describe'.
  154. #
  155. configuration_file()
  156. {
  157. guix home describe \
  158. | grep 'configuration file:' \
  159. | cut -d : -f 2 \
  160. | xargs echo
  161. }
  162. test "$(cat "$(configuration_file)")" == "$(cat home.scm)"
  163. canonical_file_name()
  164. {
  165. guix home describe \
  166. | grep 'canonical file name:' \
  167. | cut -d : -f 2 \
  168. | xargs echo
  169. }
  170. test "$(canonical_file_name)" == "$(readlink "${HOME}/.guix-home")"
  171. #
  172. # Configure a new generation.
  173. #
  174. # Change the bashrc snippet content and comment out one service.
  175. sed -i "home.scm" -e's/the content of/the NEW content of/g'
  176. sed -i "home.scm" -e"s/(simple-service 'test-config/#;(simple-service 'test-config/g"
  177. guix home reconfigure "${test_directory}/home.scm"
  178. test "$(tail -n 2 "${HOME}/.bashrc")" == "\
  179. # dot-bashrc test file for guix home
  180. # the NEW content of bashrc-test-config.sh"
  181. # This file must have been removed and not backed up.
  182. test ! -e "$HOME/.config/test.conf"
  183. test ! -e "$HOME"/*guix-home*backup/.config/test.conf
  184. test "$(cat "$(configuration_file)")" == "$(cat home.scm)"
  185. test "$(canonical_file_name)" == "$(readlink "${HOME}/.guix-home")"
  186. test $(guix home list-generations | grep "^Generation" | wc -l) -eq 2
  187. #
  188. # Test 'guix home search'.
  189. #
  190. guix home search mcron | grep "^name: home-mcron"
  191. guix home search scheduling daemon | grep "^name: home-mcron"
  192. )