1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2025 Ludovic Courtès <ludo@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
(define-module (gnu tests foreign)
#:use-module (guix download)
#:use-module (guix gexp)
#:use-module (guix modules)
#:use-module (guix monads)
#:use-module (guix packages)
#:use-module (guix profiles)
#:autoload (guix store) (%store-prefix %store-monad %graft?)
#:use-module (gnu compression)
#:use-module (gnu tests)
#:use-module (gnu packages base)
#:use-module (gnu packages bootstrap)
#:use-module (gnu packages guile)
#:use-module (gnu packages make-bootstrap)
#:use-module (gnu packages package-management)
#:use-module (gnu packages virtualization)
#:use-module (gnu system vm)
#:use-module ((guix scripts pack) #:prefix pack:)
#:use-module (srfi srfi-9)
#:export (%test-debian-install))
(define marionette-systemd-service
;; Definition of the marionette service for systemd.
(plain-file "marionette.service" "
[Unit]
Description=Guix marionette service
[Install]
WantedBy=multi-user.target
[Service]
ExecStart=/opt/guix/bin/guile --no-auto-compile \\
/opt/guix/share/guix/marionette-repl.scm\n"))
(define* (qcow-image-with-marionette image
#:key
(name "image-with-marionette.qcow2")
(device "/dev/vdb1"))
"Instrument IMAGE, returning a new image that contains a statically-linked
Guile under /opt/guix and a marionette systemd service. The relevant file
system is expected to be on DEVICE."
(define vm
(virtual-machine
(marionette-operating-system %simple-os)))
(define build
(with-imported-modules (source-module-closure
'((guix build utils)
(gnu build marionette)))
#~(begin
(use-modules (guix build utils)
(gnu build marionette))
(define target-image
#$output)
(invoke #+(file-append qemu "/bin/qemu-img")
"create" "-b" #$image
"-F" "qcow2" "-f" "qcow2" target-image
"10G")
;; Run a VM that will mount IMAGE and populate it. This is somewhat
;; more convenient to set up than 'guestfish' from libguestfs.
(let ((marionette
(make-marionette
(list #$vm "-drive"
(string-append "file=" target-image
",format=qcow2,if=virtio,"
"cache=writeback,werror=report,readonly=off")))))
(marionette-eval '(system* "mount" #$device "/mnt")
marionette)
(marionette-eval '(system* "ls" "-la" "/mnt")
marionette)
(marionette-eval '(begin
(use-modules (guix build utils))
(mkdir-p "/mnt/opt/guix")
(copy-recursively #$%guile-static-initrd
"/mnt/opt/guix"
#:log (%make-void-port "w")
#:keep-mtime? #t))
marionette)
(marionette-eval '(system* "/mnt/opt/guix/bin/guile" "--version")
marionette)
(unless (= 42 (status:exit-val
(marionette-eval '(system* "/mnt/opt/guix/bin/guile"
"-c" "(exit 42)")
marionette)))
(error "statically-linked Guile is broken"))
;; Install the marionette systemd service and activate it.
(marionette-eval '(begin
(mkdir-p "/mnt/opt/guix/share/guix")
(copy-file #$(marionette-program)
"/mnt/opt/guix/share/guix/marionette-repl.scm")
(mkdir-p "/mnt/etc/systemd/system")
(copy-file #$marionette-systemd-service
"/mnt/etc/systemd/system/marionette.service")
;; Activate the service, as per 'systemctl
;; enable marionette.service'.
(symlink
"/etc/systemd/system/marionette.service"
"/mnt/etc/systemd/system/multi-user.target.wants/marionette.service"))
marionette)
(unless (zero? (marionette-eval '(system* "umount" "/mnt")
marionette))
(error "failed to unmount device"))))))
(computed-file name build))
(define (manifest-entry-without-grafts entry)
"Return ENTRY with grafts disabled on its contents."
(manifest-entry
(inherit entry)
(item (with-parameters ((%graft? #f))
(manifest-entry-item entry)))))
(define %installation-tarball-manifest
;; Manifest of the Guix installation tarball.
(concatenate-manifests
(list (packages->manifest (list guix))
;; Include the dependencies of 'hello' in addition to 'guix' so that
;; we can test 'guix build hello'.
(map-manifest-entries
manifest-entry-without-grafts
(package->development-manifest hello))
;; Add the source of 'hello'.
(manifest
(list (manifest-entry
(name "hello-source")
(version (package-version hello))
(item (let ((file (origin-actual-file-name
(package-source hello))))
(computed-file
"hello-source"
#~(begin
;; Put the tarball in a subdirectory since
;; profile union crashes otherwise.
(mkdir #$output)
(mkdir (in-vicinity #$output "src"))
(symlink #$(package-source hello)
(in-vicinity #$output
(string-append "src/"
#$file))))))))))
;; Include 'guile-final', which is needed when building derivations
;; such as that of 'hello' but missing from the development manifest.
;; Add '%bootstrap-guile', used by 'guix install --bootstrap'.
(map-manifest-entries
manifest-entry-without-grafts
(packages->manifest (list (canonical-package guile-3.0)
%bootstrap-guile))))))
(define %guix-install-script
;; The 'guix-install.sh' script.
;;
;; To test local changes, replace the expression below with:
;;
;; (local-file "../../etc/guix-install.sh")
;;
;; This cannot be done unconditionally since that file does not exists in
;; inferiors.
(file-append (package-source guix) "/etc/guix-install.sh"))
(define (run-foreign-install-test image name)
"Run an installation of Guix in IMAGE, the QCOW2 image of a systemd-based
GNU/Linux distro, and check that the installation is functional."
(define instrumented-image
(qcow-image-with-marionette image
#:name (string-append name ".qcow2")))
(define (test tarball)
(with-imported-modules (source-module-closure
'((gnu build marionette)
(gnu system file-systems)))
#~(begin
(use-modules (gnu build marionette)
(gnu system file-systems)
(srfi srfi-64)
(ice-9 match))
(define marionette
(make-marionette
(list (string-append #$qemu-minimal "/bin/" (qemu-command))
#$@(common-qemu-options instrumented-image
(list (%store-prefix))
#:image-format "qcow2"
#:rw-image? #t)
"-m" "512"
"-snapshot")))
(test-runner-current (system-test-runner #$output))
(test-begin "foreign-install")
(test-equal "marionette works"
"Linux"
(marionette-eval '(utsname:sysname (uname))
marionette))
(test-assert "/etc/os-release"
(marionette-eval '(begin
(use-modules (ice-9 textual-ports))
(call-with-input-file "/etc/os-release"
get-string-all))
marionette))
(test-equal "mount host file store"
0
(marionette-eval
'(begin
(mkdir "/host")
(system* "mount" "-t" "9p"
"-o" "trans=virtio,cache=loose,ro"
#$(file-system->mount-tag (%store-prefix))
"/host"))
marionette))
(test-assert "screenshot before"
(marionette-control (string-append "screendump " #$output
"/before-install.ppm")
marionette))
(test-assert "install fake dependencies"
;; The installation script insists on checking for the
;; availability of 'wget' and 'gpg' but does not actually use them
;; when 'GUIX_BINARY_FILE_NAME' is set. Provide fake binaries.
(marionette-eval '(begin
(symlink "/bin/true" "/bin/wget")
(symlink "/bin/true" "/bin/gpg")
#t)
marionette))
(test-assert "run install script"
(marionette-eval '(system
(string-append
"yes '' | GUIX_BINARY_FILE_NAME="
(in-vicinity "/host"
(basename #$tarball))
" sh "
(in-vicinity
"/host"
(string-drop #$%guix-install-script
#$(string-length
(%store-prefix))))))
marionette))
(test-equal "hello not already built"
#f
;; Check that the next test will really build 'hello'.
(marionette-eval '(file-exists?
#$(with-parameters ((%graft? #f))
hello))
marionette))
(test-equal "guix build hello"
0
;; Check that guix-daemon is up and running and that the build
;; environment is properly set up (build users, etc.).
(marionette-eval '(system* "guix" "build" "hello" "--no-grafts")
marionette))
(test-assert "hello indeed built"
(marionette-eval '(file-exists?
#$(with-parameters ((%graft? #f))
hello))
marionette))
(test-equal "guix install hello"
0
;; Check that ~/.guix-profile & co. are properly created.
(marionette-eval '(let ((pw (getpwuid (getuid))))
(setenv "USER" (passwd:name pw))
(setenv "HOME" (pk 'home (passwd:dir pw)))
(system* "guix" "install" "hello"
"--no-grafts" "--bootstrap"))
marionette))
(test-equal "user profile created"
0
(marionette-eval '(system "ls -lad ~/.guix-profile")
marionette))
(test-equal "hello"
0
(marionette-eval '(system "~/.guix-profile/bin/hello")
marionette))
(test-equal "create user account"
0
(marionette-eval '(system* "useradd" "-d" "/home/user" "-m"
"user")
marionette))
(test-equal "guix install hello, unprivileged user"
0
;; Check that 'guix' is in $PATH for new users and that
;; ~user/.guix-profile also gets created.
(marionette-eval '(system "su - user -c \
'guix install hello --no-grafts --bootstrap'")
marionette))
(test-equal "user hello"
0
(marionette-eval '(system "~user/.guix-profile/bin/hello")
marionette))
(test-equal "unprivileged user profile created"
0
(marionette-eval '(system "ls -lad ~user/.guix-profile")
marionette))
(test-equal "store is read-only"
EROFS
(marionette-eval '(catch 'system-error
(lambda ()
(mkdir (in-vicinity #$(%store-prefix)
"whatever"))
0)
(lambda args
(system-error-errno args)))
marionette))
(test-assert "screenshot after"
(marionette-control (string-append "screendump " #$output
"/after-install.ppm")
marionette))
(test-end))))
(mlet* %store-monad ((profile (profile-derivation
%installation-tarball-manifest))
(tarball (pack:self-contained-tarball
"guix-binary" profile
#:compressor (lookup-compressor "zstd")
#:profile-name "current-guix"
#:localstatedir? #t)))
(gexp->derivation name (test tarball))))
(define debian-12-qcow2
;; Image taken from <https://www.debian.org/distrib/>.
;; XXX: Those images are periodically removed from debian.org.
(origin
(uri
"https://cloud.debian.org/images/cloud/bookworm/20250210-2019/debian-12-nocloud-amd64-20250210-2019.qcow2")
(method url-fetch)
(sha256
(base32
"06vlcq2dzgczlyp9lfkkdf3dgvfjp22lh5xz0mnl0bdgzq61sykb"))))
(define %test-debian-install
(system-test
(name "debian-install")
(description
"Test installation of Guix on Debian using the @file{guix-install.sh}
script.")
(value (run-foreign-install-test debian-12-qcow2 name))))
|