~ruther/guix-local

ref: 7c52959ae4dd735ad0e2f67236fca4497f6af791 guix-local/etc/teams/release/artifacts-manifest.scm -rw-r--r-- 15.9 KiB
7c52959a — Rutherther Make sure only Guix channel is used to produce release artifacts. a month ago
                                                                                
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
;;; GNU Guix --- Functional package management for GNU
;;;
;;; 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/>.

;;; This manifest returns build artfacts for all supported systems.  This can be
;;; controlled by SUPPORTED_SYSTEMS environment variable.  For the list of
;;; artifacts produced, see artifacts-for-system and the `<thing>-for-system?`
;;; procedures.  NOTE: the --system argument does not change the system for which
;;; the resulting package is built. They return different definitions of the
;;; images. To change the system, pass different SUPPORTED_SYSTEMS.

(use-modules (gnu compression)
             (gnu image)
             (gnu packages graphviz)
             (gnu packages imagemagick)
             (gnu packages package-management)
             (gnu packages perl)
             (gnu services)
             (gnu system image)
             (gnu system install)
             (gnu system)
             (guix build-system gnu)
             (guix build-system trivial)
             (guix channels)
             (guix gexp)
             (guix git)
             (guix grafts)
             (guix memoization)
             (guix monads)
             (guix packages)
             (guix profiles)
             (guix records)
             (guix scripts pack)
             (guix store)
             (guix ui)
             (guix utils)
             (ice-9 format)
             (ice-9 match)
             (srfi srfi-9)
             (srfi srfi-26)
             (srfi srfi-35))

;; For easier testing, use (snapshot) guix package from (gnu packages
;; package-management). Otherwise, the package is updated to current commit and
;; might not be substitutable, leading to longer build times.
(define %use-snapshot-package?
  (string=? (or (getenv "GUIX_USE_SNAPSHOT_PACKAGE") "no") "yes"))

(define (%guix-version)
  ;; NOTE: while package-version guix is not correct in general,
  ;; it is correct for the release itself. At that time, the
  ;; guix package is updated to vX.Y.Z and it's the version
  ;; we want to use.
  (package-version guix))

(define (%vm-image-path)
  (search-path %load-path "gnu/system/examples/vm-image.tmpl"))

(define (%vm-image-efi-path)
  (search-path %load-path "gnu/system/examples/vm-image-efi.tmpl"))

;; monadic record and gexp-compiler
;; taken from Inria
;; https://gitlab.inria.fr/numpex-pc5/wp3/guix-images/-/blob/17bf4585abc2d637faa5d339436e778b7c9fb1ce/modules/guix-hpc/packs.scm

;; XXX: The <monadic> hack below will hopefully become unnecessary once the
;; (guix scripts pack) interface switches to declarative style--i.e.,
;; file-like objects.

(define-record-type <monadic>
  (monadic->declarative mvalue)
  monadic?
  (mvalue monadic-value))

(define-gexp-compiler (monadic-compiler (monadic <monadic>) system target)
  (monadic-value monadic))

;; The tarball should be the same for every system.
;; Still, we need to decide what system to build it
;; for, so use the one that CI has most resources for.
(define (source-tarball-for-system? system)
  (member system
        '("x86_64-linux")))

(define (iso-for-system? system)
  (member system
        '("x86_64-linux" "i686-linux" "aarch64-linux")))

(define (qcow2-for-system? system)
  (member system
        '("x86_64-linux" "aarch64-linux")))

(define* (qcow2-gpt-for-system? system)
  (string=? system "aarch64-linux"))

(define (copy-/etc/config.scm config)
  "Copy the configuration.scm of the operating system to /etc/config.scm, for
user's convenience. The file has to be writable, not a link to the store, so
etc-service-type can't be used here. CONFIG is a pair of strings, (FROM . TO).
The config will be copied from FROM to TO."
  (match config
    ((from . to)
     (with-imported-modules '((guix build utils))
       #~(begin
           (use-modules (guix build utils))
           (when (not (file-exists? #$to))
             (copy-file #$from #$to)
             (make-file-writable #$to)))))
    (_ (raise
        (formatted-message-string
         (G_ "unexpected config parameter, should be pair of strings: ~a"
             config))))))

(define copy-/etc/config.scm-service-type
  (service-type (name 'copy-/etc/config.scm)
                (description
                 "Copy the system configuration file to /etc/config.scm.")
                (extensions (list (service-extension activation-service-type
                                                     copy-/etc/config.scm)))
                (default-value (cons "/run/current-system/configuration.scm"
                                     "/etc/config.scm"))))

(define (operating-system-with-/etc/config.scm os)
  "Copy the system configuration file to writable /etc/config.scm on first boot."
  (operating-system
    (inherit os)
    (services (cons (service copy-/etc/config.scm-service-type)
                    (operating-system-user-services os)))))

(define (simple-provenance-entry config-file)
  "Return system entries describing the operating system config, provided
through CONFIG-FILE."
  (mbegin %store-monad
    (return `(("configuration.scm"
               ,(local-file (assume-valid-file-name config-file)
                            "configuration.scm"))))))

;; This is mostly taken from provenance-service-type from (gnu services),
;; but it provides only configuration.scm, not channels.scm. This is
;; to get the same derivations for both Cuirass and local builds.
;; In the future, provenance-service-type could be adapted to support
;; this use case as well.
(define simple-provenance-service-type
  (service-type (name 'provenance)
                (extensions
                 (list (service-extension system-service-type
                                          simple-provenance-entry)))
                (default-value #f)                ;the OS config file
                (description
                 "Store configuration.scm of the system in the system
itself.")))

(define* (operating-system-with-simple-provenance
          os
          #:optional
          (config-file
           (operating-system-configuration-file
            os)))
  "Return a variant of OS that stores its CONFIG-FILE.  This is similar to
`operating-system-with-provenance`, but it does copy only the
configuration.scm."
  (operating-system
    (inherit os)
    (services (cons (service simple-provenance-service-type config-file)
                    (operating-system-user-services os)))))

(define (guix-package-commit guix)
  ;; Extract the commit of the GUIX package.
  (match (package-source guix)
    ((? channel? source)
     (channel-commit source))
    (_
     (apply (lambda* (#:key commit #:allow-other-keys) commit)
            (package-arguments guix)))))

;; NOTE: Normally, we would use (current-guix), along with url
;; overriden to the upstream repository to not leak our local checkout.
;; But currently, the (current-guix) derivation has to be computed through
;; QEMU for systems other than your host system. This takes a lot of time,
;; it takes at least half an hour to get the derivations.
(define (guix-package/with-commit guix commit)
  "Use the guix from (gnu packages package-management),
but override its commit to the specified version. Make sure
to also override the channel commit to have the correct
provenance."
  (let ((scm-version (car (string-split (package-version guix) #\-))))
    (package
      (inherit guix)
      (version (string-append scm-version "." (string-take commit 7)))
      (source (git-checkout
                (url (channel-url %default-guix-channel))
                (commit commit)))
      (arguments
       (substitute-keyword-arguments (package-arguments guix)
         ((#:configure-flags flags '())
          #~(cons*
             (string-append "--with-channel-commit=" #$commit)
             (filter (lambda (flag)
                       (not (string-prefix? "--with-channel-commit=" flag)))
                     #$flags))))))))

(define guix-for-images
  (mlambda (system)
    (cond
     ;; For testing purposes, use the guix package directly.
     (%use-snapshot-package? guix)
     ;; Normally, update the guix package to current commit.
     (else
      (guix-package/with-commit guix (guix-package-commit (current-guix)))))))

(define %binary-tarball-compression "xz")

;; Like guix pack -C xz -s --localstatedir --profile-name=current-guix guix
(define* (binary-tarball-for-system system #:key (extra-packages '()))
  (let* ((base-name (string-append "guix-binary-" (%guix-version) "." system))
         (manifest (packages->manifest (cons* guix extra-packages)))
         (profile (profile (content manifest)))
         (inputs `(("profile" ,profile)))
         (compression %binary-tarball-compression))
    (manifest-entry
      (name (string-append base-name ".tar." compression))
      (version (%guix-version))
      (item (monadic->declarative
             (self-contained-tarball
              base-name profile
              #:profile-name "current-guix"
              #:compressor (lookup-compressor compression)
              #:localstatedir? #t))))))

;; Like guix system image -t iso9660 \
;; --label="GUIX_$${system}_$(VERSION)" gnu/system/install.scm
(define* (iso-for-system system)
  (let* ((name (string-append
                "guix-system-install-" (%guix-version) "." system ".iso"))
         (base-os (make-installation-os
                   #:grub-displayed-version (%guix-version)
                   #:efi-only? (string=? system "aarch64-linux")))
         (base-image (os->image base-os #:type iso-image-type))
         (label (string-append "GUIX_" system "_"
                               (if (> (string-length (%guix-version)) 7)
                                   (string-take (%guix-version) 7)
                                   (%guix-version)))))
    (manifest-entry
     (name name)
     (version (%guix-version))
     (item (system-image
            (image-with-label
             (image
               (inherit base-image)
               (name (string->symbol name)))
             label))))))

;; Like guix system image -t qcow2 gnu/system/examples/vm-image.tmpl
(define* (qcow2-for-system system)
  (let* ((name (string-append
                "guix-system-vm-image-" (%guix-version) "." system ".qcow2"))
         (base-os-path
          (if (qcow2-gpt-for-system? system)
              (%vm-image-efi-path)
              (%vm-image-path)))
         (target-image-type
          (if (qcow2-gpt-for-system? system)
              qcow2-gpt-image-type
              qcow2-image-type))
         (base-os
          (operating-system-with-/etc/config.scm
           (operating-system-with-simple-provenance
            (load base-os-path) base-os-path)))
         (base-image (os->image base-os #:type target-image-type)))
    (manifest-entry
     (name name)
     (version (%guix-version))
     (item (system-image
             (image
               (inherit base-image)
               (volatile-root? #f)
               (name (string->symbol name))))))))

(define* (guix-source-tarball)
  (let ((guix (package
                (inherit guix)
                (native-inputs
                 (modify-inputs (package-native-inputs guix)
                   ;; graphviz-minimal -> graphviz
                   (replace "graphviz" graphviz)
                   (append imagemagick)
                   (append perl))))))
    (manifest-entry
      (name (string-append "guix-" (%guix-version) ".tar.gz"))
      (version (package-version guix))
      (item (dist-package
             guix
             ;; Guix is built from git source, not from tarball.
             ;; So it's fine to use its source directly.
             (package-source guix))))))

(define* (manifest-entry-with-parameters system entry
                                         #:key
                                         (guix-for-images-proc guix-for-images))
  (manifest-entry
    (inherit entry)
    (item
     (with-parameters
         ((%current-system system)
          (%current-target-system #f)
          (current-guix-package (guix-for-images-proc system)))
       (manifest-entry-item entry)))))

(define* (manifest-with-parameters system manifest
                                   #:key
                                   (guix-for-images-proc guix-for-images))
  "Returns entries in the manifest accompanied with %current-system,
%current-target-sytem and current-guix-package parameters."
  (make-manifest
   (map (cut manifest-entry-with-parameters system <>
             #:guix-for-images-proc guix-for-images-proc)
        (manifest-entries manifest))))

(define (artifacts-for-system/nonparameterized system)
  "Get all artifacts for given system.  This will always include the
guix-binary tarball and optionally iso and/or qcow2 images."
  (manifest
   (append
    (list
     (binary-tarball-for-system system))
    ;; TODO: After source tarball generation is ready, uncomment.
    ;; (if (source-tarball-for-system? system)
    ;;     (list (guix-source-tarball))
    ;;     '())
    (if (iso-for-system? system)
        (list (iso-for-system system))
        '())
    (if (qcow2-for-system? system)
        (list (qcow2-for-system system))
        '()))))

(define* (artifacts-for-system system
                               #:key
                               (guix-for-images-proc guix-for-images))
  "Collects all artifacts for a system.  Gives them the proper %current-system
and %current-target-system parameters, so the --system passed on CLI is
irrelevant."
  ;; NOTE: parameterizing current system, because the tarball seems to somehow
  ;; depend on it early on. I haven't investigated it, but seems like a bug. Could
  ;; it be the gexp->derivation + monadic->declarative, not passing down the
  ;; system?  Symptom: guix build --system=x86_64 -m artifacts-manifest.scm and
  ;; guix build --system=i686-linux -m artifacts-manifest.scm gives out different
  ;; results without the parameterization.
  (parameterize
      ((%current-system system)
       (%current-target-system #f)
       (current-guix-package (guix-for-images-proc system)))
    (manifest-with-parameters
     system
     (artifacts-for-system/nonparameterized system)
     #:guix-for-images-proc guix-for-images-proc)))

(define (manifest->union manifest)
  "Makes a union that will be a folder with all the entries symlinked.  This
is different from a profile as it expects the entries are just simple files
and symlinks them by their manifest-entry-name."
  (let ((entries (manifest-entries manifest)))
    (computed-file
     "artifacts-union"
     (with-imported-modules '((guix build union)
                              (guix build utils))
       #~(begin
           (use-modules (guix build utils))

           (mkdir-p #$output)

           (for-each
            (lambda* (entry)
              (symlink (cdr entry)
                       (string-append #$output "/" (car entry))))
            (list #$@(map (lambda (entry)
                            #~(cons
                               #$(manifest-entry-name entry)
                               #$(manifest-entry-item entry)))
                          entries))))))))

(define %supported-systems
  (or (and
       (getenv "SUPPORTED_SYSTEMS")
       (string-split (getenv "SUPPORTED_SYSTEMS") #\ ))
      '("x86_64-linux" "i686-linux"
        "armhf-linux" "aarch64-linux"
        "powerpc64le-linux" "riscv64-linux")))

(define supported-systems-union-manifest
  (concatenate-manifests
   (map artifacts-for-system
        %supported-systems)))

(when %use-snapshot-package?
  (warning (G_ "building images using the 'guix' package (snapshot)~%")))
(info (G_ "producing artifacts for the following systems: ~a~%")
          %supported-systems)
supported-systems-union-manifest