guix-home.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # GNU Guix --- Functional package management for GNU
  2. # Copyright © 2021 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. NIX_STORE_DIR="$(guile -c '(use-modules (guix config))(display %storedir)')"
  35. localstatedir="$(guile -c '(use-modules (guix config))(display %localstatedir)')"
  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 services))
  56. (home-environment
  57. (services
  58. (list
  59. (simple-service 'test-config
  60. home-files-service-type
  61. (list `("config/test.conf"
  62. ,(plain-file
  63. "tmp-file.txt"
  64. "the content of ~/.config/test.conf"))))
  65. (service home-bash-service-type
  66. (home-bash-configuration
  67. (guix-defaults? #t)
  68. (bashrc (list (local-file "dot-bashrc")))))
  69. (simple-service 'home-bash-service-extension-test
  70. home-bash-service-type
  71. (home-bash-extension
  72. (bashrc
  73. (list
  74. (plain-file
  75. "bashrc-test-config.sh"
  76. "# the content of bashrc-test-config.sh"))))))))
  77. EOF
  78. echo -n "# dot-bashrc test file for guix home" > "dot-bashrc"
  79. # Check whether the graph commands work as expected.
  80. guix home extension-graph "home.scm" | grep 'label = "home-activation"'
  81. guix home extension-graph "home.scm" | grep 'label = "home-symlink-manager"'
  82. guix home extension-graph "home.scm" | grep 'label = "home"'
  83. # There are no Shepherd services so the one below must fail.
  84. ! guix home shepherd-graph "home.scm"
  85. if container_supported
  86. then
  87. # Run the home in a container.
  88. guix home container home.scm -- true
  89. ! guix home container home.scm -- false
  90. test "$(guix home container home.scm -- echo '$HOME')" = "$HOME"
  91. guix home container home.scm -- cat '~/.config/test.conf' | \
  92. grep "the content of"
  93. guix home container home.scm -- test -h '~/.bashrc'
  94. test "$(guix home container home.scm -- id -u)" = 1000
  95. ! guix home container home.scm -- test -f '$HOME/sample/home.scm'
  96. guix home container home.scm --expose="$PWD=$HOME/sample" -- \
  97. test -f '$HOME/sample/home.scm'
  98. ! guix home container home.scm --expose="$PWD=$HOME/sample" -- \
  99. rm -v '$HOME/sample/home.scm'
  100. else
  101. echo "'guix home container' test SKIPPED" >&2
  102. fi
  103. HOME="$test_directory"
  104. export HOME
  105. #
  106. # Test 'guix home reconfigure'.
  107. #
  108. echo "# This file will be overridden and backed up." > "$HOME/.bashrc"
  109. mkdir "$HOME/.config"
  110. echo "This file will be overridden too." > "$HOME/.config/test.conf"
  111. echo "This file will stay around." > "$HOME/.config/random-file"
  112. guix home reconfigure "${test_directory}/home.scm"
  113. test -d "${HOME}/.guix-home"
  114. test -h "${HOME}/.bash_profile"
  115. test -h "${HOME}/.bashrc"
  116. test "$(tail -n 2 "${HOME}/.bashrc")" == "\
  117. # dot-bashrc test file for guix home
  118. # the content of bashrc-test-config.sh"
  119. grep -q "the content of ~/.config/test.conf" "${HOME}/.config/test.conf"
  120. # This one should still be here.
  121. grep "stay around" "$HOME/.config/random-file"
  122. # Make sure preexisting files were backed up.
  123. grep "overridden" "$HOME"/*guix-home*backup/.bashrc
  124. grep "overridden" "$HOME"/*guix-home*backup/.config/test.conf
  125. rm -r "$HOME"/*guix-home*backup
  126. #
  127. # Test 'guix home describe'.
  128. #
  129. configuration_file()
  130. {
  131. guix home describe \
  132. | grep 'configuration file:' \
  133. | cut -d : -f 2 \
  134. | xargs echo
  135. }
  136. test "$(cat "$(configuration_file)")" == "$(cat home.scm)"
  137. canonical_file_name()
  138. {
  139. guix home describe \
  140. | grep 'canonical file name:' \
  141. | cut -d : -f 2 \
  142. | xargs echo
  143. }
  144. test "$(canonical_file_name)" == "$(readlink "${HOME}/.guix-home")"
  145. #
  146. # Configure a new generation.
  147. #
  148. # Change the bashrc snippet content and comment out one service.
  149. sed -i "home.scm" -e's/the content of/the NEW content of/g'
  150. sed -i "home.scm" -e"s/(simple-service 'test-config/#;(simple-service 'test-config/g"
  151. guix home reconfigure "${test_directory}/home.scm"
  152. test "$(tail -n 2 "${HOME}/.bashrc")" == "\
  153. # dot-bashrc test file for guix home
  154. # the NEW content of bashrc-test-config.sh"
  155. # This file must have been removed and not backed up.
  156. ! test -e "$HOME/.config/test.conf"
  157. ! test -e "$HOME"/*guix-home*backup/.config/test.conf
  158. test "$(cat "$(configuration_file)")" == "$(cat home.scm)"
  159. test "$(canonical_file_name)" == "$(readlink "${HOME}/.guix-home")"
  160. test $(guix home list-generations | grep "^Generation" | wc -l) -eq 2
  161. #
  162. # Test 'guix home search'.
  163. #
  164. guix home search mcron | grep "^name: home-mcron"
  165. guix home search job manager | grep "^name: home-mcron"
  166. )