projects/guix·blob master · refs

236 lines · 8.3 KiB

1# GNU Guix --- Functional package management for GNU
2# Copyright © 2021-2023 Andrew Tropin <[email protected]>
3# Copyright © 2021 Oleg Pykhalov <[email protected]>
4# Copyright © 2022-2023, 2025 Ludovic Courtès <[email protected]>
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
25set -e
26
27guix home --version
28
29container_supported ()
30{
31 if guile -c '((@ (guix scripts environment) assert-container-features))'
32 then
33 return 0
34 else
35 return 1
36 fi
37}
38
39localstatedir="$(guile -c '(use-modules (guix config))(display %localstatedir)')"
40NIX_STORE_DIR="$(guile -c '(use-modules (guix config))(display %storedir)')"
41GUIX_DAEMON_SOCKET="$localstatedir/guix/daemon-socket/socket"
42GUIX_BUILD_OPTIONS="--timeout=`guile -c '(use-modules (guix tests))(display %tests-build-timeout)'`"
43export NIX_STORE_DIR GUIX_DAEMON_SOCKET GUIX_BUILD_OPTIONS
44
45# Run tests only when a "real" daemon is available.
46if ! guile -c '(use-modules (guix)) (exit (false-if-exception (open-connection)))'
47then
48 exit 77
49fi
50
51STORE_PARENT="$(dirname "$NIX_STORE_DIR")"
52export STORE_PARENT
53if test "$STORE_PARENT" = "/"; then exit 77; fi
54
55test_directory="$(mktemp -d)"
56trap 'chmod -Rf +w "$test_directory"; rm -rf "$test_directory"' EXIT
57
58(
59 cd "$test_directory" || exit 77
60
61 cat > "home.scm" <<'EOF'
62(use-modules (guix gexp)
63 (gnu home)
64 (gnu home services)
65 (gnu home services shells)
66 (gnu packages bash)
67 (gnu services))
68
69(home-environment
70 (services
71 (list
72 (simple-service 'test-config
73 home-files-service-type
74 (list `(".config/test.conf"
75 ,(plain-file
76 "tmp-file.txt"
77 "the content of ~/.config/test.conf"))
78
79 `("symlink" ,(symlink-to "<test_directory>"))))
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 'add-environment-variable
87 home-environment-variables-service-type
88 `(("TODAY" . "26 messidor")
89 ("SHELL" . ,(file-append bash "/bin/bash"))
90 ("BUILDHOST_TIME" . ,#~(strftime "%c"
91 (localtime (current-time))))
92 ("STRING_WITH_ESCAPES" . "chars: \" /\\")
93 ("LITERAL" . ,(literal-string "${abc}"))))
94
95 (simple-service 'home-bash-service-extension-test
96 home-bash-service-type
97 (home-bash-extension
98 (environment-variables
99 '(("PS1" . "$GUIX_ENVIRONMENT λ ")))
100 (aliases
101 `(("run" . "guix shell")
102 ("path" . ,(literal-string "echo $PATH"))))
103 (bashrc
104 (list
105 (plain-file
106 "bashrc-test-config.sh"
107 "# the content of bashrc-test-config.sh"))))))))
108EOF
109
110 sed -i "s,<test_directory>,$test_directory," home.scm
111 echo -n "# dot-bashrc test file for guix home" > "dot-bashrc"
112
113 # Check whether the graph commands work as expected.
114 guix home extension-graph "home.scm" | grep 'label = "home-activation"'
115 guix home extension-graph "home.scm" | grep 'label = "home-symlink-manager"'
116 guix home extension-graph "home.scm" | grep 'label = "home"'
117
118 # There are no Shepherd services so the one below must fail.
119 guix home shepherd-graph "home.scm" && false
120
121 if container_supported
122 then
123 # Run the home in a container. Always use bash inside container for
124 # reproducibility of the tests.
125 # TODO: Make container independent from external environment variables.
126 SHELL=bash
127 guix home container home.scm -- true
128 guix home container home.scm -- false && false
129 test "$(guix home container home.scm -- echo '$HOME')" = "$HOME"
130 guix home container home.scm -- cat '~/.config/test.conf' | \
131 grep "the content of"
132 guix home container home.scm -- test -h '~/.bashrc'
133 guix home container home.scm -- test -h '~/symlink'
134 test "$(guix home container home.scm -- id -u)" = 1000
135 guix home container home.scm -- test -f '$HOME/sample/home.scm' && false
136 guix home container home.scm --expose="$PWD=$HOME/sample" -- \
137 test -f '$HOME/sample/home.scm'
138 guix home container home.scm --expose="$PWD=$HOME/sample" -- \
139 rm -v '$HOME/sample/home.scm' && false
140 guix home container home.scm -- touch /whatever && false
141 else
142 echo "'guix home container' test SKIPPED" >&2
143 fi
144
145 HOME="$test_directory"
146 export HOME
147
148 #
149 # Test 'guix home reconfigure'.
150 #
151
152 echo "# This file will be overridden and backed up." > "$HOME/.bashrc"
153 mkdir "$HOME/.config"
154 echo "This file will be overridden too." > "$HOME/.config/test.conf"
155 echo "This file will stay around." > "$HOME/.config/random-file"
156
157 guix home reconfigure "${test_directory}/home.scm"
158 test -d "${HOME}/.guix-home"
159 test -h "${HOME}/.bash_profile"
160 test -h "${HOME}/.bashrc"
161 test -h "${HOME}/symlink"
162 test "$(readlink -f $HOME/symlink)" == "$test_directory"
163 grep 'alias run="guix shell"' "$HOME/.bashrc"
164 grep "alias path='echo \$PATH'" "$HOME/.bashrc"
165 test "$(tail -n 2 "${HOME}/.bashrc")" == "\
166# dot-bashrc test file for guix home
167# the content of bashrc-test-config.sh"
168 grep -q "the content of ~/.config/test.conf" "${HOME}/.config/test.conf"
169 grep '^export PS1="\$GUIX_ENVIRONMENT λ "$' "${HOME}/.bash_profile"
170
171 ( . "${HOME}/.guix-home/setup-environment"; test "$TODAY" = "26 messidor" )
172 ( . "${HOME}/.guix-home/setup-environment"; test "$LITERAL" = '${abc}' )
173 ( . "${HOME}/.guix-home/setup-environment";
174 test "$STRING_WITH_ESCAPES" = "chars: \" /\\")
175 ( . "${HOME}/.guix-home/setup-environment";
176 echo "$SHELL" | grep "/gnu/store/.*/bin/bash" )
177
178 # This one should still be here.
179 grep "stay around" "$HOME/.config/random-file"
180
181 # Make sure preexisting files were backed up.
182 grep "overridden" "$HOME"/*guix-home*backup/.bashrc
183 grep "overridden" "$HOME"/*guix-home*backup/.config/test.conf
184 rm -r "$HOME"/*guix-home*backup
185
186 #
187 # Test 'guix home describe'.
188 #
189
190 configuration_file()
191 {
192 guix home describe \
193 | grep 'configuration file:' \
194 | cut -d : -f 2 \
195 | xargs echo
196 }
197 test "$(cat "$(configuration_file)")" == "$(cat home.scm)"
198
199 canonical_file_name()
200 {
201 guix home describe \
202 | grep 'canonical file name:' \
203 | cut -d : -f 2 \
204 | xargs echo
205 }
206 test "$(canonical_file_name)" == "$(readlink "${HOME}/.guix-home")"
207
208 #
209 # Configure a new generation.
210 #
211
212 # Change the bashrc snippet content and comment out one service.
213 sed -i "home.scm" -e's/the content of/the NEW content of/g'
214 sed -i "home.scm" -e"s/(simple-service 'test-config/#;(simple-service 'test-config/g"
215
216 guix home reconfigure "${test_directory}/home.scm"
217 test "$(tail -n 2 "${HOME}/.bashrc")" == "\
218# dot-bashrc test file for guix home
219# the NEW content of bashrc-test-config.sh"
220
221 # This file must have been removed and not backed up.
222 test ! -e "$HOME/.config/test.conf"
223 test ! -e "$HOME"/*guix-home*backup/.config/test.conf
224
225 test "$(cat "$(configuration_file)")" == "$(cat home.scm)"
226 test "$(canonical_file_name)" == "$(readlink "${HOME}/.guix-home")"
227
228 test $(guix home list-generations | grep "^Generation" | wc -l) -eq 2
229
230 #
231 # Test 'guix home search'.
232 #
233
234 guix home search mcron | grep "^name: home-mcron"
235 guix home search scheduling daemon | grep "^name: home-mcron"
236)