guix-home.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 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. (bashrc
  84. (list
  85. (plain-file
  86. "bashrc-test-config.sh"
  87. "# the content of bashrc-test-config.sh"))))))))
  88. EOF
  89. echo -n "# dot-bashrc test file for guix home" > "dot-bashrc"
  90. # Check whether the graph commands work as expected.
  91. guix home extension-graph "home.scm" | grep 'label = "home-activation"'
  92. guix home extension-graph "home.scm" | grep 'label = "home-symlink-manager"'
  93. guix home extension-graph "home.scm" | grep 'label = "home"'
  94. # There are no Shepherd services so the one below must fail.
  95. guix home shepherd-graph "home.scm" && false
  96. if container_supported
  97. then
  98. # Run the home in a container. Always use bash inside container for
  99. # reproducibility of the tests.
  100. # TODO: Make container independent from external environment variables.
  101. SHELL=bash
  102. guix home container home.scm -- true
  103. guix home container home.scm -- false && false
  104. test "$(guix home container home.scm -- echo '$HOME')" = "$HOME"
  105. guix home container home.scm -- cat '~/.config/test.conf' | \
  106. grep "the content of"
  107. guix home container home.scm -- test -h '~/.bashrc'
  108. test "$(guix home container home.scm -- id -u)" = 1000
  109. guix home container home.scm -- test -f '$HOME/sample/home.scm' && false
  110. guix home container home.scm --expose="$PWD=$HOME/sample" -- \
  111. test -f '$HOME/sample/home.scm'
  112. guix home container home.scm --expose="$PWD=$HOME/sample" -- \
  113. rm -v '$HOME/sample/home.scm' && false
  114. else
  115. echo "'guix home container' test SKIPPED" >&2
  116. fi
  117. HOME="$test_directory"
  118. export HOME
  119. #
  120. # Test 'guix home reconfigure'.
  121. #
  122. echo "# This file will be overridden and backed up." > "$HOME/.bashrc"
  123. mkdir "$HOME/.config"
  124. echo "This file will be overridden too." > "$HOME/.config/test.conf"
  125. echo "This file will stay around." > "$HOME/.config/random-file"
  126. guix home reconfigure "${test_directory}/home.scm"
  127. test -d "${HOME}/.guix-home"
  128. test -h "${HOME}/.bash_profile"
  129. test -h "${HOME}/.bashrc"
  130. test "$(tail -n 2 "${HOME}/.bashrc")" == "\
  131. # dot-bashrc test file for guix home
  132. # the content of bashrc-test-config.sh"
  133. grep -q "the content of ~/.config/test.conf" "${HOME}/.config/test.conf"
  134. grep '^export PS1="\$GUIX_ENVIRONMENT λ "$' "${HOME}/.bash_profile"
  135. ( . "${HOME}/.guix-home/setup-environment"; test "$TODAY" = "26 messidor" )
  136. ( . "${HOME}/.guix-home/setup-environment"; test "$LITERAL" = '${abc}' )
  137. ( . "${HOME}/.guix-home/setup-environment";
  138. test "$STRING_WITH_ESCAPES" = "chars: \" /\\")
  139. ( . "${HOME}/.guix-home/setup-environment";
  140. echo "$SHELL" | grep "/gnu/store/.*/bin/bash" )
  141. # This one should still be here.
  142. grep "stay around" "$HOME/.config/random-file"
  143. # Make sure preexisting files were backed up.
  144. grep "overridden" "$HOME"/*guix-home*backup/.bashrc
  145. grep "overridden" "$HOME"/*guix-home*backup/.config/test.conf
  146. rm -r "$HOME"/*guix-home*backup
  147. #
  148. # Test 'guix home describe'.
  149. #
  150. configuration_file()
  151. {
  152. guix home describe \
  153. | grep 'configuration file:' \
  154. | cut -d : -f 2 \
  155. | xargs echo
  156. }
  157. test "$(cat "$(configuration_file)")" == "$(cat home.scm)"
  158. canonical_file_name()
  159. {
  160. guix home describe \
  161. | grep 'canonical file name:' \
  162. | cut -d : -f 2 \
  163. | xargs echo
  164. }
  165. test "$(canonical_file_name)" == "$(readlink "${HOME}/.guix-home")"
  166. #
  167. # Configure a new generation.
  168. #
  169. # Change the bashrc snippet content and comment out one service.
  170. sed -i "home.scm" -e's/the content of/the NEW content of/g'
  171. sed -i "home.scm" -e"s/(simple-service 'test-config/#;(simple-service 'test-config/g"
  172. guix home reconfigure "${test_directory}/home.scm"
  173. test "$(tail -n 2 "${HOME}/.bashrc")" == "\
  174. # dot-bashrc test file for guix home
  175. # the NEW content of bashrc-test-config.sh"
  176. # This file must have been removed and not backed up.
  177. test ! -e "$HOME/.config/test.conf"
  178. test ! -e "$HOME"/*guix-home*backup/.config/test.conf
  179. test "$(cat "$(configuration_file)")" == "$(cat home.scm)"
  180. test "$(canonical_file_name)" == "$(readlink "${HOME}/.guix-home")"
  181. test $(guix home list-generations | grep "^Generation" | wc -l) -eq 2
  182. #
  183. # Test 'guix home search'.
  184. #
  185. guix home search mcron | grep "^name: home-mcron"
  186. guix home search job manager | grep "^name: home-mcron"
  187. )