unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob 6b08e61492a860de50af8904e0523f0b9c737a0d 34683 bytes (raw)
name: gnu/bootloader.scm 	 # note: path name is non-authoritative(*)

  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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2017 David Craven <david@craven.ch>
;;; Copyright © 2017, 2020, 2022 Mathieu Othacehe <othacehe@gnu.org>
;;; Copyright © 2017 Leo Famulari <leo@famulari.name>
;;; Copyright © 2019, 2021, 2023 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
;;; Copyright © 2022 Josselin Poiret <dev@jpoiret.xyz>
;;; Copyright © 2022 Reza Alizadeh Majd <r.majd@pantherx.org>
;;; Copyright © 2024 Tomas Volf <~@wolfsden.cz>
;;; Copyright © 2024 Lilah Tascheter <lilah@lunabee.space>
;;;
;;; 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 bootloader)
  #:autoload   (gnu build file-systems)
               (read-partition-label read-partition-uuid
                find-partition-by-label find-partition-by-uuid)
  #:use-module (gnu system file-systems)
  #:use-module (gnu system uuid)
  #:autoload   (guix build syscalls)
               (mounts mount-source mount-point mount-type)
  #:use-module (guix deprecation)
  #:use-module (guix diagnostics)
  #:use-module (guix gexp)
  #:use-module (guix i18n)
  #:use-module (guix modules)
  #:use-module (guix profiles)
  #:use-module (guix records)
  #:use-module (guix utils)
  #:use-module (ice-9 match)
  #:use-module (ice-9 receive)
  #:use-module (rnrs bytevectors)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-35)
  #:export (menu-entry
            menu-entry?
            menu-entry-label
            menu-entry-device
            menu-entry-device-mount-point
            menu-entry-device-subvol
            menu-entry-linux
            menu-entry-linux-arguments
            menu-entry-initrd
            menu-entry-multiboot-kernel
            menu-entry-multiboot-arguments
            menu-entry-multiboot-modules
            menu-entry-chain-loader

            normalize-file
            menu-entry->sexp
            sexp->menu-entry

            bootloader
            bootloader?
            bootloader-name
            bootloader-package
            bootloader-default-targets
            bootloader-installer
            bootloader-disk-image-installer
            bootloader-configuration-file
            bootloader-configuration-file-generator

            bootloader-target
            bootloader-target?
            bootloader-target-type
            bootloader-target-expected?
            bootloader-target-path
            bootloader-target-offset
            bootloader-target-device
            bootloader-target-file-system
            bootloader-target-label
            bootloader-target-uuid

            target-error?
            target-error-type
            target-error-targets

            gbegin
            :path :devpath :device :fs :label :uuid
            with-targets

            bootloader-configuration
            bootloader-configuration?
            bootloader-configuration-bootloader
            bootloader-configuration-targets
            bootloader-configuration-menu-entries
            bootloader-configuration-default-entry
            bootloader-configuration-efi-removable?
            bootloader-configuration-32bit?
            bootloader-configuration-timeout
            bootloader-configuration-keyboard-layout
            bootloader-configuration-theme
            bootloader-configuration-terminal-outputs
            bootloader-configuration-terminal-inputs
            bootloader-configuration-serial-unit
            bootloader-configuration-serial-speed
            bootloader-configuration-device-tree-support?
            bootloader-configuration-extra-initrd

            bootloader-configuration->gexp
            bootloader-configurations->gexp

            %efi-supported-systems
            efi-arch
            install-efi
            efi-bootloader-chain))

\f
;;;
;;; Menu-entry record.
;;;

(define-record-type* <menu-entry>
  menu-entry make-menu-entry
  menu-entry?
  (label           menu-entry-label)
  (device          menu-entry-device       ; file system uuid, label, or #f
                   (default #f))
  (device-mount-point menu-entry-device-mount-point
                   (default #f))
  (device-subvol menu-entry-device-subvol
                   (default #f))
  (linux           menu-entry-linux
                   (default #f))
  (linux-arguments menu-entry-linux-arguments
                   (default '()))          ; list of string-valued gexps
  (initrd          menu-entry-initrd       ; file name of the initrd as a gexp
                   (default #f))
  (multiboot-kernel menu-entry-multiboot-kernel
                    (default #f))
  (multiboot-arguments menu-entry-multiboot-arguments
                       (default '()))      ; list of string-valued gexps
  (multiboot-modules menu-entry-multiboot-modules
                     (default '()))        ; list of multiboot commands, where
                                           ; a command is a list of <string>
  (chain-loader     menu-entry-chain-loader
                    (default #f)))         ; string, path of efi file

(define (normalize-file entry file)
  "Normalize a file FILE stored in a menu entry into one suitable for a
bootloader.  Realizes device-mount-point and device-subvol."
  (match-menu-entry entry (device-mount-point device-subvol)
    ;; Avoid using cut procedure from SRFI-26 inside G-exp.
    (let ((mount (and=> device-mount-point (cut string-trim <> #\/))))
      #~(let* ((file (string-trim #$file #\/))
               (file (if (and #$mount (string-prefix? #$mount file))
                         (substring file (string-length #$mount))
                         file)))
          (string-append (or #$device-subvol "") "/" file)))))

(define (report-menu-entry-error menu-entry)
  (raise
   (condition
    (&message
     (message
      (format #f (G_ "invalid menu-entry: ~a") menu-entry)))
    (&fix-hint
     (hint
      (G_ "Please chose only one of:
@enumerate
@item direct boot by specifying fields @code{linux},
@code{linux-arguments} and @code{linux-modules},
@item multiboot by specifying fields @code{multiboot-kernel},
@code{multiboot-arguments} and @code{multiboot-modules},
@item chain-loader by specifying field @code{chain-loader}.
@end enumerate"))))))

(define (menu-entry->sexp entry)
  "Return ENTRY serialized as an sexp."
  (define (device->sexp device)
    (match device
      ((? uuid? uuid)
       `(uuid ,(uuid-type uuid) ,(uuid->string uuid)))
      ((? file-system-label? label)
       `(label ,(file-system-label->string label)))
      (_ device)))
  (match entry
    (($ <menu-entry> label device mount-point subvol
                     (? identity linux) linux-arguments (? identity initrd)
                     #f () () #f)
     `(menu-entry (version 0)
                  (label ,label)
                  (device ,(device->sexp device))
                  (device-mount-point ,mount-point)
                  (linux ,linux)
                  (linux-arguments ,linux-arguments)
                  (initrd ,initrd)
                  (device-subvol ,subvol)))
    (($ <menu-entry> label device mount-point subvol #f () #f
                     (? identity multiboot-kernel) multiboot-arguments
                     multiboot-modules #f)
     `(menu-entry (version 0)
                  (label ,label)
                  (device ,(device->sexp device))
                  (device-mount-point ,mount-point)
                  (multiboot-kernel ,multiboot-kernel)
                  (multiboot-arguments ,multiboot-arguments)
                  (multiboot-modules ,multiboot-modules)
                  (device-subvol ,subvol)))
    (($ <menu-entry> label device mount-point subvol #f () #f #f () ()
                     (? identity chain-loader))
     `(menu-entry (version 0)
                  (label ,label)
                  (device ,(device->sexp device))
                  (device-mount-point ,mount-point)
                  (chain-loader ,chain-loader)
                  (device-subvol ,subvol)))
    (_ (report-menu-entry-error entry))))

(define (sexp->menu-entry sexp)
  "Turn SEXP, an sexp as returned by 'menu-entry->sexp', into a <menu-entry>
record."
  ;; XXX: The match ORs shadow subvol.
  (define subvol #f)
  (define (sexp->device device-sexp)
    (match device-sexp
      (('uuid type uuid-string)
       (uuid uuid-string type))
      (('label label)
       (file-system-label label))
      (_ device-sexp)))
  (match sexp
    (('menu-entry ('version 0)
                  ('label label) ('device device)
                  ('device-mount-point mount-point)
                  ('linux linux) ('linux-arguments linux-arguments)
                  ('initrd initrd)
                  (or ('device-subvol subvol _ ...) (_ ...)))
     (menu-entry
      (label label)
      (device (sexp->device device))
      (device-mount-point mount-point)
      (device-subvol subvol)
      (linux linux)
      (linux-arguments linux-arguments)
      (initrd initrd)))
    (('menu-entry ('version 0)
                  ('label label) ('device device)
                  ('device-mount-point mount-point) ('device-subvol subvol)
                  ('multiboot-kernel multiboot-kernel)
                  ('multiboot-arguments multiboot-arguments)
                  ('multiboot-modules multiboot-modules)
                  (or ('device-subvol subvol _ ...) (_ ...)))
     (menu-entry
      (label label)
      (device (sexp->device device))
      (device-mount-point mount-point)
      (device-subvol subvol)
      (multiboot-kernel multiboot-kernel)
      (multiboot-arguments multiboot-arguments)
      (multiboot-modules multiboot-modules)))
    (('menu-entry ('version 0)
                  ('label label) ('device device)
                  ('device-mount-point mount-point) ('device-subvol subvol)
                  ('chain-loader chain-loader)
                  (or ('device-subvol subvol _ ...) (_ ...)))
     (menu-entry
      (label label)
      (device (sexp->device device))
      (device-mount-point mount-point)
      (device-subvol subvol)
      (chain-loader chain-loader)))))

\f
;;;
;;; Bootloader record.
;;;

;; The <bootloader> record contains fields expressing how the bootloader
;; should be installed. Every bootloader in gnu/bootloader/ directory
;; has to be described by this record.

(define-record-type* <bootloader>
  bootloader make-bootloader
  bootloader?
  (name                            bootloader-name)
  (package                         bootloader-package)
  (default-targets bootloader-default-targets (default '()))
  (installer                       bootloader-installer)
  (disk-image-installer            bootloader-disk-image-installer
                                   (default #f))
  (configuration-file              bootloader-configuration-file)
  (configuration-file-generator    bootloader-configuration-file-generator))

\f
;;;
;;; Bootloader target record.
;;;

;; <bootloader-target> represents different kinds of targets in a
;; normalized form.

(define-record-type* <bootloader-target>
  bootloader-target make-bootloader-target bootloader-target?
  (type bootloader-target-type)                            ; symbol
  (expected? bootloader-target-expected? (default #f))     ; bool

  (path bootloader-target-path (default #f))               ; string|#f
  (offset bootloader-target-offset (thunked)               ; symbol|#f
          (default (and (bootloader-target-path this-record)
                        (not (eq? (bootloader-target-type this-record) 'root))
                        'root)))
  (device bootloader-target-device (default #f))           ; string|#f
  (file-system bootloader-target-file-system (default #f)) ; string|#f
  (label bootloader-target-label (default #f))             ; string|#f
  (uuid bootloader-target-uuid (default #f)))              ; uuid|#f

(define-condition-type &target-error &error target-error?
  (type target-error-type)
  (targets target-error-targets))

(define (pathcat p1 p2)
  (string-append (string-trim-right p1 #\/) "/" (string-trim p2 #\/)))

(define* (get-target-of-type type targets #:optional require?)
  "Finds a target in TARGETS of type TYPE, returns REQUIRE? if #false,
or provides an error otherwise."
  (define (type? target)
    (eq? type (bootloader-target-type target)))
  (match (filter type? targets)
    ((target _ ...) target)
    (_ (and require?
            (raise
              (condition
                (&message (message (G_ "required, but not provided")))
                (&target-error (type type) (targets targets))))))))

(define (parent-of target targets)
  "Resolve the parent of TARGET in TARGETS, return #f if orphan."
  (and=> (bootloader-target-offset target)
         (cut get-target-of-type <> targets #t)))

(define (unfold-pathcat target targets)
  "Find the full VFS path of TARGET."
  (let ((quit (lambda (t) (not (and=> t bootloader-target-path))))
        (parent-of (cut parent-of <> targets)))
    (reduce pathcat #f
      (unfold quit bootloader-target-path parent-of target))))

(define (target-base? t)
  (or (not t) (match-record t <bootloader-target>
                (expected? offset device label uuid)
                (or device label uuid (not offset) expected?))))

(define (type-major? target) (memq target '(root esp disk)))

(define (ensure types targets end)
  (let* ((used-in (cute unfold end identity (cut parent-of <> targets) <>))
         (cons-in (lambda (t) (cons t (used-in t))))
         (ensure (map (cut get-target-of-type <> targets #t) types)))
    (filter identity (apply append (map cons-in ensure)))))

(define* (ensure-target-types types targets #:optional (base? #f))
  "Ensures all TYPES are provided in TARGETS.  Returns #t iff every ensured
target and its requirements are fully provided.  Errors out when a required TYPE
isn't provided.  When BASE?, only ensure path requirements up to a device."
  (not (any bootloader-target-expected?
         (ensure types targets (if base? target-base? not)))))

(define (ensure-majors types targets)
  "Errors out when a required TYPE isn't provided, or when use of multiple major
targets is detected."
  (let* ((all (map bootloader-target-type (ensure types targets target-base?)))
         (majors (delete-duplicates (filter type-major? all) eq?)))
    (if (< (length majors) 2) #t
      (raise (condition (&message (message (G_ "multiple major targets used")))
                        (&target-error (type majors) (targets targets)))))))



(define (gbegin . gex)
  "Sequence provided g-expressions."
  (case (length gex) ((0) #f) ((1) (car gex)) (else #~(begin #$@gex))))

;; syntax matching on free literals breaks easily, so bind them
(define-syntax-rule (define-literal id) (define-syntax id (syntax-rules ())))
(define-literal :path)
(define-literal :devpath)
(define-literal :device)
(define-literal :fs)
(define-literal :label)
(define-literal :uuid)

(define-syntax with-targets
  (cut syntax-case <> ()
    ((_ targets-expr block ...)
     (let* ((genvars (compose generate-temporaries make-list))
            (targets (car (genvars 1))))
       (define (resolve in target base)
         (with-syntax ((target target) (base base) (targets targets))
           (syntax-case in
             (:path :devpath :device :fs :label :uuid)
             ((name _) (not (identifier? #'name))
              #`(_ (syntax-error "binds must be to identifiers" #,in)))
             ((name :device) #'(name (bootloader-target-device base)))
             ((name :label) #'(name (bootloader-target-label base)))
             ((name :uuid) #'(name (bootloader-target-uuid base)))
             ((name :fs) #'(name (bootloader-target-file-system base)))
             ((name :path) #'(name (unfold-pathcat target targets)))
             ((name :devpath)
              #'(name (if (target-base? target)
                          "/"
                          (pathcat "/" (bootloader-target-path target)))))
             (_ #`(_ (syntax-error "invalid binding spec" #,in))))))

       (define (binds spec)
         (syntax-case spec (=>)
           ((type => binds ...)
            (with-syntax (((target base) (genvars 2)) (targets targets))
              (append
                #`((get (lambda (t) (get-target-of-type t targets #t)))
                   (target (get type))
                   (base (if (target-base? target)
                             target
                             (get (bootloader-target-offset target)))))
                (map (cut resolve <> #'target #'base) #'(binds ...)))))
           (_ #f)))

       (define blocks
         (cut syntax-case <> ()
           ((spec ... expr)
            (let* ((path? (cut syntax-case <> (:path) ((_ :path) #t) (_ #f)))
                   (qualified? (cut syntax-case <> (=>)
                                 ((_ => spec ...) (any path? #'(spec ...)))
                                 (_ #f)))
                   (specs #'(spec ...))
                   (lets (apply append (filter-map binds specs)))
                   (type (cut syntax-case <> (=>)
                           ((t => _ ...) #'t) (t #'t))))
              (receive (full part) (partition qualified? specs)
                #`(and (ensure-majors (list #,@(map type specs)) #,targets)
                       (ensure-target-types (list #,@(map type part))
                                            #,targets #t)
                       (ensure-target-types (list #,@(map type full))
                                            #,targets #f)
                       (let* #,lets expr)))))
           (bad #'(syntax-error "malformed block" bad))))
       "Using the list TARGETS, evaluate and sequence each BLOCK to produce a
gexp.  BLOCK is a set of SPECs followed by an EXPR (evaluating to a gexp).
Each SPEC denotes a type of target to guard EXPR on their existance and
full-qualification.  This procedure is linear in regard to BLOCKs.

SPEC may be of the following forms:
@itemize
@item 'TYPE Requires TYPE to be fully present or promised. Errors otherwise.
@item ('TYPE => (VAR COMPONENT) ...): As type, but also binds variables. TYPE's
      COMPONENT is bound to the variable VAR as described below.
@end itemize

Available COMPONENTs are:
@itemize
@item :path (fully-qualified)
@item :devpath (relative from device)
@item :device (auto-detected from uuid and label if not user-provided)
@item :fs
@item :label
@item :uuid
@end itemize

Note that installers may be called multiple times with different targets being
fully-qualified.  To ensure that targets aren't installed multiple times, make sure
that each BLOCK ensures at least one major target, either directly or indirectly.
Likewise, at most one major target should be ensured per BLOCK, under the same
conditions.  Major targets originate from disk image handling, and are currently:
@itemize
@item disk
@item root
@item esp
@end itemize"
       #`(let ((#,targets targets-expr))
           (apply gbegin (filter identity
                                 (list #,@(map blocks #'(block ...))))))))
    (bad #'(syntax-error "must provide targets" bad))))

\f
;;;
;;; Bootloader configuration record.
;;;

;; The <bootloader-configuration> record contains bootloader independant
;; configuration used to fill bootloader configuration file.


(define-record-type* <bootloader-configuration>
  bootloader-configuration make-bootloader-configuration
  bootloader-configuration?
  (bootloader
   bootloader-configuration-bootloader)   ;<bootloader>
  (targets               %bootloader-configuration-targets
                         (default #f))     ;list of strings
  (menu-entries          bootloader-configuration-menu-entries
                         (default '()))   ;list of <menu-entry>
  (default-entry         bootloader-configuration-default-entry
                         (default 0))     ;integer
  (efi-removable?        bootloader-configuration-efi-removable?
                         (default #f))    ;bool
  (32bit?                bootloader-configuration-32bit?
                         (default #f))    ;bool
  (timeout               bootloader-configuration-timeout
                         (default 5))     ;seconds as integer
  (keyboard-layout       bootloader-configuration-keyboard-layout
                         (default #f))    ;<keyboard-layout> | #f
  (theme                 bootloader-configuration-theme
                         (default #f))    ;bootloader-specific theme
  (terminal-outputs      bootloader-configuration-terminal-outputs
                         (default '(gfxterm)))   ;list of symbols
  (terminal-inputs       bootloader-configuration-terminal-inputs
                         (default '()))   ;list of symbols
  (serial-unit           bootloader-configuration-serial-unit
                         (default #f))    ;integer | #f
  (serial-speed          bootloader-configuration-serial-speed
                         (default #f))    ;integer | #f
  (device-tree-support?  bootloader-configuration-device-tree-support?
                         (default #t))    ;boolean
  (extra-initrd          bootloader-configuration-extra-initrd
                         (default #f)))   ;string | #f


(define (bootloader-configuration-targets config)
  (or (%bootloader-configuration-targets config)
      ;; XXX: At least the GRUB installer (see (gnu bootloader grub)) has this
      ;; peculiar behavior of installing fonts and GRUB modules when DEVICE is #f,
      ;; hence the default value of '(#f) rather than '().
      (list #f)))

\f
;;;
;;; Bootloader installation paths.
;;;

(define (target-overrides . layers)
  (let* ((types (flat-map (cute map bootloader-target-type <>) layers))
         ;; TODO: use loop instead of fold for early termination.
         (pred (lambda (type layer found)
                 (or found (get-target-of-type type layer))))
         (find (lambda (type) (fold (cute pred type <> <>) #f layers))))
    (filter identity (map find (delete-duplicates types)))))

(define (normalize targets)
  "Augments TARGETS with filesystem information at runtime, allowing
users to specify a lot less information.  Puts TARGETS into a normal
form, where each path is fully specified up to a device offset."
  (define (mass m)
    `((,(mount-source m) . ,m)
      (,(mount-point m) . ,m)))

  (define (accessible=> d f)
    (and d (access? d R_OK) (f d)))

  (define (fixuuid target)
    (match-record target <bootloader-target> (uuid file-system)
      (let ((type (cond ((not file-system) 'dce)
                        ((member file-system '("vfat" "fat32")) 'fat)
                        ((string=? file-system "ntfs") 'ntfs)
                        ((string=? file-system "iso9660") 'iso9660)
                        (else 'dce))))
        (bootloader-target (inherit target)
          (uuid (cond ((uuid? uuid) uuid)
                  ((bytevector? uuid) (bytevector->uuid uuid type))
                  ((string? uuid) (string->uuid uuid type))
                  (else #f)))))))

  (define (arborify target targets)
    (let* ((up (lambda (t) (and t (parent-of t targets))))
           (proto (unfold target-base? identity up (up target) list))
           (chain (reverse (cons target proto))))
      (bootloader-target
        (inherit target)
        (offset (and=> (car chain) bootloader-target-type))
        (path (reduce pathcat #f (map bootloader-target-path (cdr chain)))))))

  (let ((amounts (delay (apply append (map mass (mounts))))))
    (define (assoc-mnt f)
      (lambda (v) (and=> (assoc-ref (force amounts) v) f)))

    (define (scrape target)
      (match-record target <bootloader-target>
        (expected? path offset device label uuid file-system)
        (if expected? target
          (bootloader-target
            (inherit target)
            (device (or device
                        (false-if-exception
                          (or (and=> uuid find-partition-by-uuid)
                              (and=> label find-partition-by-label)))
                        (and path ((assoc-mnt mount-source)
                                   (unfold-pathcat target targets)))))
            (label (or label (accessible=> device read-partition-label)))
            (uuid (or uuid (accessible=> device read-partition-uuid)))
            (file-system (or file-system (and=> device (assoc-mnt mount-type))))
            (offset (and path offset))
            (path (or path (and=> device (assoc-mnt mount-point))))))))

    (let ((mid (map (compose fixuuid scrape) targets)))
      (map (cut arborify <> mid) mid))))

(define* (bootloader-configuration->gexp bootloader-config args #:key
                                          (root-offset "/") (overrides '()))
  "Returns a gexp to install BOOTLOADER-CONFIG to its targets, passing ARGS
to each installer alongside the additional #:bootloader-config keyword
arguments.  Target OVERRIDES are applied and all path targets have ROOT-OFFSET
applied.  The following keyword arguments are expected in ARGS:
@enumerate
@item current-boot-alternative
@item old-boot-alternatives
@item locale (from bootmeta)
@item store-directory-prefix (from bootmeta)
@item store-crypto-devices (from bootmeta)
@end enumerate"
  (let* ((bootloader (bootloader-configuration-bootloader bootloader-config))
         (installer (bootloader-installer bootloader))
         (auto-targets (list (bootloader-target
                               (type 'root)
                               (path root-offset)
                               (offset #f))))
         (targets (target-overrides
                    overrides
                    (bootloader-configuration-targets bootloader-config)
                    auto-targets
                    (bootloader-default-targets bootloader)))
         (conf (bootloader-configuration
                 (inherit bootloader-config)
                 (targets (normalize targets)))))
    (apply installer #:bootloader-config conf args)))

(define (bootloader-configurations->gexp bootloader-configs . rest)
  (apply gbegin (filter-map (cut apply bootloader-configuration->gexp <> rest)
                            bootloader-configs)))

\f
;;;
;;; Bootloader installation to ESP.
;;;

;; systems currently supported by efi-arch. should be used for packages relying
;; on it.
(define %efi-supported-systems
  '("i686-linux" "x86_64-linux" "armhf-linux" "aarch64-linux" "riscv64-linux"))

(define* (efi-arch #:key (target (or (%current-target-system) (%current-system)))
                         (32? #f))
  "Returns the UEFI architecture name for the current target, in lowercase."
  (cond ((target-x86-32? target)  "ia32")
        ((target-x86-64? target)  (if 32? "ia32" "x64"))
        ((target-arm32? target)   "arm")
        ((target-aarch64? target) (if 32? "arm" "aa64"))
        ((target-riscv64? target) (if 32? "riscv32" "riscv64"))
        (else (raise (formatted-message (G_ "no UEFI standard arch for ~a!")
                                        target)))))

(define (lazy-efibootmgr)
  "Lazy-loaded efibootmgr package, in order to prevent circular refs."
  (module-ref (resolve-interface '(gnu packages linux)) 'efibootmgr))

(define (install-efi bootloader-config plan)
  "Returns a gexp installing PLAN to the ESP, as denoted by the 'vendir target.
PLAN is a gexp of a list of '(BUILDER DEST-BASENAME . LABEL) triples, that
should be in boot order.  If the user selects a removable bootloader, only the
first entry in PLAN is used."
  (match-record bootloader-config <bootloader-configuration>
    (targets efi-removable? 32bit?)
    (if efi-removable?
      ;; Hard code the output location to a well-known path recognized by
      ;; compliant firmware.  See "3.5.1.1 Removable Media Boot Behaviour":
      ;; http://www.uefi.org/sites/default/files/resources/UEFI%20Spec%202_6.pdf
      (with-targets targets
        (('esp => (path :path))
         #~(let ((boot #$(string-append path "/EFI/BOOT"))
                 (arch #$(string-upcase (efi-arch #:32? 32bit?)))
                 (builder (car (car #$plan))))
             (mkdir-p boot)
             ;; Only realize the first planspec.
             (builder (string-append boot "/BOOT" arch ".EFI")))))
      ;; Install normally if not configured as removable.
      (with-targets targets
        (('vendir => (vendir :path) (loader :devpath) (disk :device))
         #~(install-efi #+(file-append (lazy-efibootmgr) "/sbin/efibootmgr")
                        #$vendir #$loader #$disk #$plan))))))

(define (efi-bootloader-profile packages files hooks)
  "Creates a profile from the lists of PACKAGES and FILES from the store.
This profile is meant to be used by the bootloader-installer.

FILES is a list of file or directory names from the store, which will be
symlinked into the profile.  If a directory name ends with '/', then the
directory content instead of the directory itself will be symlinked into the
profile.

FILES may contain file like objects produced by procedures like plain-file,
local-file, etc., or package contents produced with file-append.

HOOKS lists additional hook functions to modify the profile."
  (define* (efi-bootloader-profile-hook manifest #:optional system)
    (define build
        (with-imported-modules '((guix build utils))
          #~(begin
            (use-modules ((guix build utils)
                          #:select (mkdir-p strip-store-file-name))
                         ((ice-9 ftw)
                          #:select (scandir))
                         ((srfi srfi-1)
                          #:select (append-map every remove))
                         ((srfi srfi-26)
                          #:select (cut)))
            (define (symlink-to file directory transform)
              "Creates a symlink to FILE named (TRANSFORM FILE) in DIRECTORY."
              (symlink file (string-append directory "/" (transform file))))
            (define (directory-content directory)
              "Creates a list of absolute path names inside DIRECTORY."
              (map (lambda (name)
                     (string-append directory name))
                   (or (scandir directory (lambda (name)
                                            (not (member name '("." "..")))))
                       '())))
            (define name-ends-with-/? (cut string-suffix? "/" <>))
            (define (name-is-store-entry? name)
              "Return #t if NAME is a direct store entry and nothing inside."
              (not (string-index (strip-store-file-name name) #\/)))
            (let* ((files '#$files)
                   (directories (filter name-ends-with-/? files))
                   (names-from-directories
                    (append-map (lambda (directory)
                                  (directory-content directory))
                                directories))
                   (names (append names-from-directories
                                  (remove name-ends-with-/? files))))
              (mkdir-p #$output)
              (if (every file-exists? names)
                  (begin
                    (for-each (lambda (name)
                               (symlink-to name #$output
                                            (if (name-is-store-entry? name)
                                                strip-store-file-name
                                                basename)))
                              names)
                    #t)
                  #f)))))

    (gexp->derivation "efi-bootloader-profile"
                      build
                      #:system system
                      #:local-build? #t
                      #:substitutable? #f
                      #:properties
                      `((type . profile-hook)
                        (hook . efi-bootloader-profile-hook))))

  (profile (content (packages->manifest packages))
           (name "efi-bootloader-profile")
           (hooks (cons efi-bootloader-profile-hook hooks))
           (locales? #f)
           (allow-collisions? #f)
           (relative-symlinks? #f)))

(define* (efi-bootloader-chain final-bootloader
                               #:key
                               (packages '())
                               (files '())
                               (hooks '())
                               installer
                               disk-image-installer)
  "Define a chain of bootloaders with the FINAL-BOOTLOADER, optional PACKAGES,
and optional directories and files from the store given in the list of FILES.

The package of the FINAL-BOOTLOADER and all PACKAGES and FILES will be placed
in an efi-bootloader-profile, which will be passed to the INSTALLER.

FILES may contain file-like objects produced by procedures like plain-file,
local-file, etc., or package contents produced with file-append.

If a directory name in FILES ends with '/', then the directory content instead
of the directory itself will be symlinked into the efi-bootloader-profile.

The procedures in the HOOKS list can be used to further modify the bootloader
profile.  It is possible to pass a single function instead of a list.

If the INSTALLER argument is used, then this gexp procedure will be called to
install the efi-bootloader-profile.  Otherwise the installer of the
FINAL-BOOTLOADER will be called.

If the DISK-IMAGE-INSTALLER is used, then this gexp procedure will be called
to install the efi-bootloader-profile into a disk image.  Otherwise the
disk-image-installer of the FINAL-BOOTLOADER will be called."
  (bootloader
    (inherit final-bootloader)
    (name "efi-bootloader-chain")
    (package
     (efi-bootloader-profile (cons (bootloader-package final-bootloader)
                                   packages)
                             files
                             (if (list? hooks)
                                 hooks
                                 (list hooks))))
    (installer
     (or installer
         (bootloader-installer final-bootloader)))
    (disk-image-installer
     (or disk-image-installer
         (bootloader-disk-image-installer final-bootloader)))))

debug log:

solving 6b08e61492 ...
found 6b08e61492 in https://yhetil.org/guix-patches/52e31df0e5a3a0d0c4b015d135d5eb0ce3e4829e.1726827025.git.herman@rimm.ee/ ||
	https://yhetil.org/guix-patches/e5a67b99cf8792af7870903e8517bc42b60c9528.1727345067.git.herman@rimm.ee/
found f1352122a9 in https://yhetil.org/guix-patches/33b51456dc709d17c8be3776471e0599f83eaec1.1727345067.git.herman@rimm.ee/ ||
	https://yhetil.org/guix-patches/9ebe41c442f375788d3783fb780d11f8bdf3ed75.1726827025.git.herman@rimm.ee/
found c77de6f55e in https://yhetil.org/guix-patches/36b06c055689a23a29e1ad8cc0e2617a1f57f900.1726827025.git.herman@rimm.ee/ ||
	https://yhetil.org/guix-patches/2ce2a5d1b077a35dcfc95c707703f8c0a11bf3b2.1727345067.git.herman@rimm.ee/
found 0c24996205 in https://yhetil.org/guix-patches/221de7ca9881aa279048d2e71bbed6263b26b165.1726827025.git.herman@rimm.ee/ ||
	https://yhetil.org/guix-patches/d7820e89fbf046495d02e860f015c83fce0a7d18.1727345067.git.herman@rimm.ee/
found 3ea50a4004 in https://yhetil.org/guix-patches/5b95c2bdceeeefb36857f7dfb869d19140fbae9d.1726827025.git.herman@rimm.ee/ ||
	https://yhetil.org/guix-patches/de896f29150ecc2d0bc731ddb571296f94ca5b15.1727345067.git.herman@rimm.ee/
found 865521e6e5 in https://yhetil.org/guix-patches/19cb7c8d9d8d8cf1693007365a6867674325ccab.1726827025.git.herman@rimm.ee/ ||
	https://yhetil.org/guix-patches/74c789e74594d538308d33633ed8540283dcde49.1727345067.git.herman@rimm.ee/
found f32e90e79d in https://git.savannah.gnu.org/cgit/guix.git
preparing index
index prepared:
100644 f32e90e79ddc264bb6c6867d095990e5c5a60104	gnu/bootloader.scm

applying [1/6] https://yhetil.org/guix-patches/19cb7c8d9d8d8cf1693007365a6867674325ccab.1726827025.git.herman@rimm.ee/
diff --git a/gnu/bootloader.scm b/gnu/bootloader.scm
index f32e90e79d..865521e6e5 100644

Checking patch gnu/bootloader.scm...
Applied patch gnu/bootloader.scm cleanly.

skipping https://yhetil.org/guix-patches/74c789e74594d538308d33633ed8540283dcde49.1727345067.git.herman@rimm.ee/ for 865521e6e5
index at:
100644 865521e6e553bbfea242300f697dc5720bc4f566	gnu/bootloader.scm

applying [2/6] https://yhetil.org/guix-patches/5b95c2bdceeeefb36857f7dfb869d19140fbae9d.1726827025.git.herman@rimm.ee/
diff --git a/gnu/bootloader.scm b/gnu/bootloader.scm
index 865521e6e5..3ea50a4004 100644

Checking patch gnu/bootloader.scm...
Applied patch gnu/bootloader.scm cleanly.

skipping https://yhetil.org/guix-patches/de896f29150ecc2d0bc731ddb571296f94ca5b15.1727345067.git.herman@rimm.ee/ for 3ea50a4004
index at:
100644 3ea50a4004ba2b0981e047fb2d63dbf68e401391	gnu/bootloader.scm

applying [3/6] https://yhetil.org/guix-patches/221de7ca9881aa279048d2e71bbed6263b26b165.1726827025.git.herman@rimm.ee/
diff --git a/gnu/bootloader.scm b/gnu/bootloader.scm
index 3ea50a4004..0c24996205 100644

Checking patch gnu/bootloader.scm...
Applied patch gnu/bootloader.scm cleanly.

skipping https://yhetil.org/guix-patches/d7820e89fbf046495d02e860f015c83fce0a7d18.1727345067.git.herman@rimm.ee/ for 0c24996205
index at:
100644 0c249962052fceb7471ae501fedd5b152cfdcbc2	gnu/bootloader.scm

applying [4/6] https://yhetil.org/guix-patches/36b06c055689a23a29e1ad8cc0e2617a1f57f900.1726827025.git.herman@rimm.ee/
diff --git a/gnu/bootloader.scm b/gnu/bootloader.scm
index 0c24996205..c77de6f55e 100644

Checking patch gnu/bootloader.scm...
Applied patch gnu/bootloader.scm cleanly.

skipping https://yhetil.org/guix-patches/2ce2a5d1b077a35dcfc95c707703f8c0a11bf3b2.1727345067.git.herman@rimm.ee/ for c77de6f55e
index at:
100644 c77de6f55e3742ce370c8e077564083ae9845197	gnu/bootloader.scm

applying [5/6] https://yhetil.org/guix-patches/33b51456dc709d17c8be3776471e0599f83eaec1.1727345067.git.herman@rimm.ee/
diff --git a/gnu/bootloader.scm b/gnu/bootloader.scm
index c77de6f55e..f1352122a9 100644

Checking patch gnu/bootloader.scm...
Applied patch gnu/bootloader.scm cleanly.

skipping https://yhetil.org/guix-patches/9ebe41c442f375788d3783fb780d11f8bdf3ed75.1726827025.git.herman@rimm.ee/ for f1352122a9
index at:
100644 f1352122a9066e71df17a5294d85a9b602de1b89	gnu/bootloader.scm

applying [6/6] https://yhetil.org/guix-patches/52e31df0e5a3a0d0c4b015d135d5eb0ce3e4829e.1726827025.git.herman@rimm.ee/
diff --git a/gnu/bootloader.scm b/gnu/bootloader.scm
index f1352122a9..6b08e61492 100644

Checking patch gnu/bootloader.scm...
Applied patch gnu/bootloader.scm cleanly.

skipping https://yhetil.org/guix-patches/e5a67b99cf8792af7870903e8517bc42b60c9528.1727345067.git.herman@rimm.ee/ for 6b08e61492
index at:
100644 6b08e61492a860de50af8904e0523f0b9c737a0d	gnu/bootloader.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).