home: services: environment-variables: Add support for literal strings.
[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 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
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
60 cat > "home.scm" <<'EOF'
61 (use-modules (guix gexp)
62 (gnu home)
63 (gnu home services)
64 (gnu home services shells)
65 (gnu services))
66
67 (home-environment
68 (services
69 (list
70 (simple-service 'test-config
71 home-files-service-type
72 (list `(".config/test.conf"
73 ,(plain-file
74 "tmp-file.txt"
75 "the content of ~/.config/test.conf"))))
76
77 (service home-bash-service-type
78 (home-bash-configuration
79 (guix-defaults? #t)
80 (bashrc (list (local-file "dot-bashrc")))))
81
82 (simple-service 'add-environment-variable
83 home-environment-variables-service-type
84 `(("TODAY" . "26 messidor")
85 ("LITERAL" . ,(literal-string "${abc}"))))
86
87 (simple-service 'home-bash-service-extension-test
88 home-bash-service-type
89 (home-bash-extension
90 (environment-variables
91 '(("PS1" . "$GUIX_ENVIRONMENT λ ")))
92 (bashrc
93 (list
94 (plain-file
95 "bashrc-test-config.sh"
96 "# the content of bashrc-test-config.sh"))))))))
97 EOF
98
99 echo -n "# dot-bashrc test file for guix home" > "dot-bashrc"
100
101 # Check whether the graph commands work as expected.
102 guix home extension-graph "home.scm" | grep 'label = "home-activation"'
103 guix home extension-graph "home.scm" | grep 'label = "home-symlink-manager"'
104 guix home extension-graph "home.scm" | grep 'label = "home"'
105
106 # There are no Shepherd services so the one below must fail.
107 ! guix home shepherd-graph "home.scm"
108
109 if container_supported
110 then
111 # Run the home in a container. Always use bash inside container for
112 # reproducibility of the tests.
113 # TODO: Make container independent from external environment variables.
114 SHELL=bash
115 guix home container home.scm -- true
116 ! guix home container home.scm -- false
117 test "$(guix home container home.scm -- echo '$HOME')" = "$HOME"
118 guix home container home.scm -- cat '~/.config/test.conf' | \
119 grep "the content of"
120 guix home container home.scm -- test -h '~/.bashrc'
121 test "$(guix home container home.scm -- id -u)" = 1000
122 ! guix home container home.scm -- test -f '$HOME/sample/home.scm'
123 guix home container home.scm --expose="$PWD=$HOME/sample" -- \
124 test -f '$HOME/sample/home.scm'
125 ! guix home container home.scm --expose="$PWD=$HOME/sample" -- \
126 rm -v '$HOME/sample/home.scm'
127 else
128 echo "'guix home container' test SKIPPED" >&2
129 fi
130
131 HOME="$test_directory"
132 export HOME
133
134 #
135 # Test 'guix home reconfigure'.
136 #
137
138 echo "# This file will be overridden and backed up." > "$HOME/.bashrc"
139 mkdir "$HOME/.config"
140 echo "This file will be overridden too." > "$HOME/.config/test.conf"
141 echo "This file will stay around." > "$HOME/.config/random-file"
142
143 guix home reconfigure "${test_directory}/home.scm"
144 test -d "${HOME}/.guix-home"
145 test -h "${HOME}/.bash_profile"
146 test -h "${HOME}/.bashrc"
147 test "$(tail -n 2 "${HOME}/.bashrc")" == "\
148 # dot-bashrc test file for guix home
149 # the content of bashrc-test-config.sh"
150 grep -q "the content of ~/.config/test.conf" "${HOME}/.config/test.conf"
151 grep '^export PS1="\$GUIX_ENVIRONMENT λ "$' "${HOME}/.bash_profile"
152 ( . "${HOME}/.guix-home/setup-environment"; test "$TODAY" = "26 messidor" )
153 ( . "${HOME}/.guix-home/setup-environment"; test "$LITERAL" = '${abc}' )
154
155 # This one should still be here.
156 grep "stay around" "$HOME/.config/random-file"
157
158 # Make sure preexisting files were backed up.
159 grep "overridden" "$HOME"/*guix-home*backup/.bashrc
160 grep "overridden" "$HOME"/*guix-home*backup/.config/test.conf
161 rm -r "$HOME"/*guix-home*backup
162
163 #
164 # Test 'guix home describe'.
165 #
166
167 configuration_file()
168 {
169 guix home describe \
170 | grep 'configuration file:' \
171 | cut -d : -f 2 \
172 | xargs echo
173 }
174 test "$(cat "$(configuration_file)")" == "$(cat home.scm)"
175
176 canonical_file_name()
177 {
178 guix home describe \
179 | grep 'canonical file name:' \
180 | cut -d : -f 2 \
181 | xargs echo
182 }
183 test "$(canonical_file_name)" == "$(readlink "${HOME}/.guix-home")"
184
185 #
186 # Configure a new generation.
187 #
188
189 # Change the bashrc snippet content and comment out one service.
190 sed -i "home.scm" -e's/the content of/the NEW content of/g'
191 sed -i "home.scm" -e"s/(simple-service 'test-config/#;(simple-service 'test-config/g"
192
193 guix home reconfigure "${test_directory}/home.scm"
194 test "$(tail -n 2 "${HOME}/.bashrc")" == "\
195 # dot-bashrc test file for guix home
196 # the NEW content of bashrc-test-config.sh"
197
198 # This file must have been removed and not backed up.
199 ! test -e "$HOME/.config/test.conf"
200 ! test -e "$HOME"/*guix-home*backup/.config/test.conf
201
202 test "$(cat "$(configuration_file)")" == "$(cat home.scm)"
203 test "$(canonical_file_name)" == "$(readlink "${HOME}/.guix-home")"
204
205 test $(guix home list-generations | grep "^Generation" | wc -l) -eq 2
206
207 #
208 # Test 'guix home search'.
209 #
210
211 guix home search mcron | grep "^name: home-mcron"
212 guix home search job manager | grep "^name: home-mcron"
213 )