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