tests: Check 'guix home reconfigure' for a second generation.
[jackhill/guix/guix.git] / tests / guix-home.sh
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 #
22 # Test the 'guix home' using the external store, if any.
23 #
24
25 set -e
26
27 guix home --version
28
29 NIX_STORE_DIR="$(guile -c '(use-modules (guix config))(display %storedir)')"
30 localstatedir="$(guile -c '(use-modules (guix config))(display %localstatedir)')"
31 GUIX_DAEMON_SOCKET="$localstatedir/guix/daemon-socket/socket"
32 export NIX_STORE_DIR GUIX_DAEMON_SOCKET
33
34 # Run tests only when a "real" daemon is available.
35 if ! guile -c '(use-modules (guix)) (exit (false-if-exception (open-connection)))'
36 then
37 exit 77
38 fi
39
40 STORE_PARENT="$(dirname "$NIX_STORE_DIR")"
41 export STORE_PARENT
42 if test "$STORE_PARENT" = "/"; then exit 77; fi
43
44 test_directory="$(mktemp -d)"
45 trap 'chmod -Rf +w "$test_directory"; rm -rf "$test_directory"' EXIT
46
47 (
48 cd "$test_directory" || exit 77
49
50 HOME="$test_directory"
51 export HOME
52
53 #
54 # Test 'guix home reconfigure'.
55 #
56
57 echo "# This file will be overridden and backed up." > "$HOME/.bashrc"
58 mkdir "$HOME/.config"
59 echo "This file will be overridden too." > "$HOME/.config/test.conf"
60 echo "This file will stay around." > "$HOME/.config/random-file"
61
62 echo -n "# dot-bashrc test file for guix home" > "dot-bashrc"
63
64 cat > "home.scm" <<'EOF'
65 (use-modules (guix gexp)
66 (gnu home)
67 (gnu home services)
68 (gnu home services shells)
69 (gnu services))
70
71 (home-environment
72 (services
73 (list
74 (simple-service 'test-config
75 home-files-service-type
76 (list `("config/test.conf"
77 ,(plain-file
78 "tmp-file.txt"
79 "the content of ~/.config/test.conf"))))
80
81 (service home-bash-service-type
82 (home-bash-configuration
83 (guix-defaults? #t)
84 (bashrc (list (local-file "dot-bashrc")))))
85
86 (simple-service 'home-bash-service-extension-test
87 home-bash-service-type
88 (home-bash-extension
89 (bashrc
90 (list
91 (plain-file
92 "bashrc-test-config.sh"
93 "# the content of bashrc-test-config.sh"))))))))
94 EOF
95
96 guix home reconfigure "${test_directory}/home.scm"
97 test -d "${HOME}/.guix-home"
98 test -h "${HOME}/.bash_profile"
99 test -h "${HOME}/.bashrc"
100 test "$(tail -n 2 "${HOME}/.bashrc")" == "\
101 # dot-bashrc test file for guix home
102 # the content of bashrc-test-config.sh"
103 grep -q "the content of ~/.config/test.conf" "${HOME}/.config/test.conf"
104
105 # This one should still be here.
106 grep "stay around" "$HOME/.config/random-file"
107
108 # Make sure preexisting files were backed up.
109 grep "overridden" "$HOME"/*guix-home*backup/.bashrc
110 grep "overridden" "$HOME"/*guix-home*backup/.config/test.conf
111 rm -r "$HOME"/*guix-home*backup
112
113 #
114 # Test 'guix home describe'.
115 #
116
117 configuration_file()
118 {
119 guix home describe \
120 | grep 'configuration file:' \
121 | cut -d : -f 2 \
122 | xargs echo
123 }
124 test "$(cat "$(configuration_file)")" == "$(cat home.scm)"
125
126 canonical_file_name()
127 {
128 guix home describe \
129 | grep 'canonical file name:' \
130 | cut -d : -f 2 \
131 | xargs echo
132 }
133 test "$(canonical_file_name)" == "$(readlink "${HOME}/.guix-home")"
134
135 #
136 # Configure a new generation.
137 #
138
139 # Change the bashrc snippet content and comment out one service.
140 sed -i "home.scm" -e's/the content of/the NEW content of/g'
141 sed -i "home.scm" -e"s/(simple-service 'test-config/#;(simple-service 'test-config/g"
142
143 guix home reconfigure "${test_directory}/home.scm"
144 test "$(tail -n 2 "${HOME}/.bashrc")" == "\
145 # dot-bashrc test file for guix home
146 # the NEW content of bashrc-test-config.sh"
147
148 # This file must have been removed and not backed up.
149 ! test -e "$HOME/.config/test.conf"
150 ! test -e "$HOME"/*guix-home*backup/.config/test.conf
151
152 test "$(cat "$(configuration_file)")" == "$(cat home.scm)"
153 test "$(canonical_file_name)" == "$(readlink "${HOME}/.guix-home")"
154
155 test $(guix home list-generations | grep "^Generation" | wc -l) -eq 2
156
157 #
158 # Test 'guix home search'.
159 #
160
161 guix home search mcron | grep "^name: home-mcron"
162 guix home search job manager | grep "^name: home-mcron"
163 )