guix-home.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. NIX_STORE_DIR="$(guile -c '(use-modules (guix config))(display %storedir)')"
  26. localstatedir="$(guile -c '(use-modules (guix config))(display %localstatedir)')"
  27. GUIX_DAEMON_SOCKET="$localstatedir/guix/daemon-socket/socket"
  28. export NIX_STORE_DIR GUIX_DAEMON_SOCKET
  29. # Run tests only when a "real" daemon is available.
  30. if ! guile -c '(use-modules (guix)) (exit (false-if-exception (open-connection)))'
  31. then
  32. exit 77
  33. fi
  34. STORE_PARENT="$(dirname "$NIX_STORE_DIR")"
  35. export STORE_PARENT
  36. if test "$STORE_PARENT" = "/"; then exit 77; fi
  37. test_directory="$(mktemp -d)"
  38. trap 'chmod -Rf +w "$test_directory"; rm -rf "$test_directory"' EXIT
  39. (
  40. cd "$test_directory" || exit 77
  41. HOME="$test_directory"
  42. export HOME
  43. #
  44. # Test 'guix home reconfigure'.
  45. #
  46. echo "# This file will be overridden and backed up." > "$HOME/.bashrc"
  47. mkdir "$HOME/.config"
  48. echo "This file will be overridden too." > "$HOME/.config/test.conf"
  49. echo "This file will stay around." > "$HOME/.config/random-file"
  50. echo -n "# dot-bashrc test file for guix home" > "dot-bashrc"
  51. cat > "home.scm" <<'EOF'
  52. (use-modules (guix gexp)
  53. (gnu home)
  54. (gnu home services)
  55. (gnu home services shells)
  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 'home-bash-service-extension-test
  71. home-bash-service-type
  72. (home-bash-extension
  73. (bashrc
  74. (list
  75. (plain-file
  76. "bashrc-test-config.sh"
  77. "# the content of bashrc-test-config.sh"))))))))
  78. EOF
  79. guix home reconfigure "${test_directory}/home.scm"
  80. test -d "${HOME}/.guix-home"
  81. test -h "${HOME}/.bash_profile"
  82. test -h "${HOME}/.bashrc"
  83. test "$(tail -n 2 "${HOME}/.bashrc")" == "\
  84. # dot-bashrc test file for guix home
  85. # the content of bashrc-test-config.sh"
  86. grep -q "the content of ~/.config/test.conf" "${HOME}/.config/test.conf"
  87. # This one should still be here.
  88. grep "stay around" "$HOME/.config/random-file"
  89. # Make sure preexisting files were backed up.
  90. grep "overridden" "$HOME"/*guix-home*backup/.bashrc
  91. grep "overridden" "$HOME"/*guix-home*backup/.config/test.conf
  92. rm -r "$HOME"/*guix-home*backup
  93. #
  94. # Test 'guix home describe'.
  95. #
  96. configuration_file()
  97. {
  98. guix home describe \
  99. | grep 'configuration file:' \
  100. | cut -d : -f 2 \
  101. | xargs echo
  102. }
  103. test "$(cat "$(configuration_file)")" == "$(cat home.scm)"
  104. canonical_file_name()
  105. {
  106. guix home describe \
  107. | grep 'canonical file name:' \
  108. | cut -d : -f 2 \
  109. | xargs echo
  110. }
  111. test "$(canonical_file_name)" == "$(readlink "${HOME}/.guix-home")"
  112. #
  113. # Configure a new generation.
  114. #
  115. # Change the bashrc snippet content and comment out one service.
  116. sed -i "home.scm" -e's/the content of/the NEW content of/g'
  117. sed -i "home.scm" -e"s/(simple-service 'test-config/#;(simple-service 'test-config/g"
  118. guix home reconfigure "${test_directory}/home.scm"
  119. test "$(tail -n 2 "${HOME}/.bashrc")" == "\
  120. # dot-bashrc test file for guix home
  121. # the NEW content of bashrc-test-config.sh"
  122. # This file must have been removed and not backed up.
  123. ! test -e "$HOME/.config/test.conf"
  124. ! test -e "$HOME"/*guix-home*backup/.config/test.conf
  125. test "$(cat "$(configuration_file)")" == "$(cat home.scm)"
  126. test "$(canonical_file_name)" == "$(readlink "${HOME}/.guix-home")"
  127. test $(guix home list-generations | grep "^Generation" | wc -l) -eq 2
  128. #
  129. # Test 'guix home search'.
  130. #
  131. guix home search mcron | grep "^name: home-mcron"
  132. guix home search job manager | grep "^name: home-mcron"
  133. )