From 34a4849d7023ff7756c048246bc9d7c303170748 Mon Sep 17 00:00:00 2001
From: "S.Karrmann" <S.Karrmann@web.de>
Date: Sun, 20 Nov 2022 21:12:52 +0100
Subject: [PATCH] grub-configfile

---
 doc/guix.texi             | 42 +++++++++++++++++++++++++++++----------
 gnu/bootloader.scm        | 29 +++++++++++++++++++++++----
 gnu/bootloader/grub.scm   | 10 ++++++++++
 tests/boot-parameters.scm | 11 ++++++++++
 4 files changed, 78 insertions(+), 14 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index eaecfd0daa..c30880b8bc 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -109,6 +109,7 @@ Copyright @copyright{} 2022 Reily Siegel@*
 Copyright @copyright{} 2022 Simon Streit@*
 Copyright @copyright{} 2022 (@*
 Copyright @copyright{} 2022 John Kehayias@*
+Copyright @copyright{} 2022 Dr. Stefan Karrmann@*

 Permission is granted to copy, distribute and/or modify this document
 under the terms of the GNU Free Documentation License, Version 1.3 or
@@ -6127,7 +6128,7 @@ Transformation Options}) so it should be lossless.

 @item --profile=@var{profile}
 @itemx -p @var{profile}
-Create an environment containing the packages installed in @var{profile}.
+Create an environment containing the packages installed in @var{profile}.
 Use @command{guix package} (@pxref{Invoking guix package}) to create
 and manage profiles.

@@ -6573,7 +6574,7 @@ interpreted as packages that will be added to the environment directly.

 @item --profile=@var{profile}
 @itemx -p @var{profile}
-Create an environment containing the packages installed in @var{profile}.
+Create an environment containing the packages installed in @var{profile}.
 Use @command{guix package} (@pxref{Invoking guix package}) to create
 and manage profiles.

@@ -12512,7 +12513,7 @@ candidates, and even to test their impact on packages that depend on
 them:

 @example
-guix build elogind --with-source=@dots{}/shepherd-0.9.0rc1.tar.gz
+guix build elogind --with-source=@dots{}/shepherd-0.9.0rc1.tar.gz
 @end example

 @dots{} or to build from a checkout in a pristine environment:
@@ -23294,7 +23295,7 @@ created for.
 Restricts all controllers to the specified transport. @code{'dual} means both
 BR/EDR and LE are enabled (if supported by the hardware).

-Possible values are:
+Possible values are:

 @itemize @bullet
 @item
@@ -38229,6 +38230,27 @@ The list of commands for loading Multiboot modules.  For example:
              @dots{}))
 @end lisp

+                    (device (uuid "5cbb9a70-a3c8-4384-a085-9e6896058343"))
+                    (configfile "/boot/grub/grub.cfg"))))))
+
+@item @code{configfile} (default: @code{#f})
+A string that can be accepted by @code{grub}'s @code{chainloader}
+directive. This has no effect if either @code{linux} or
+@code{multiboot-kernel} fields are specified. The following is an
+example of switching to a different GNU/GRUB menu.
+
+@lisp
+(bootloader
+ (bootloader-configuration
+  ;; @dots{}
+  (menu-entries
+   (list
+    (menu-entry
+     (label "GNU/Linux")
+     (device (uuid "5cbb9a70-a3c8-4384-a085-9e6896058343"))
+     (configfile "/boot/grub/grub.cfg"))))))
+@end lisp
+
 @item @code{chain-loader} (default: @code{#f})
 A string that can be accepted by @code{grub}'s @code{chainloader}
 directive. This has no effect if either @code{linux} or
@@ -40116,7 +40138,7 @@ A clause can have one of the following forms:
 (@var{field-name}
  (@var{type} @var{default-value})
  @var{documentation})
-
+
 (@var{field-name}
  (@var{type} @var{default-value})
  @var{documentation}
@@ -40165,7 +40187,7 @@ A simple serializer procedure could look like this:
 (define (serialize-boolean field-name value)
   (let ((value (if value "true" "false")))
     #~(string-append #$field-name #$value)))
-@end lisp
+@end lisp

 In some cases multiple different configuration records might be defined
 in the same file, but their serializers for the same type might have to
@@ -40183,7 +40205,7 @@ manually specify a custom @var{serializer} for every field.

 (define (bar-serialize-string field-name value)
   @dots{})
-
+
 (define-configuration foo-configuration
   (label
    (string)
@@ -40215,7 +40237,7 @@ macro which is a shorthand of this.
   (field
    (string "test")
    "Some documentation."))
-@end lisp
+@end lisp
 @end deffn

 @deffn {Scheme Syntax} define-maybe @var{type}
@@ -42708,7 +42730,7 @@ down in its dependency graph.  As it turns out, GLib does not have a
    from /gnu/store/@dots{}-glib-2.62.6/lib/libglib-2.0.so.0
 #1  0x00007ffff608a7d6 in gobject_init_ctor ()
    from /gnu/store/@dots{}-glib-2.62.6/lib/libgobject-2.0.so.0
-#2  0x00007ffff7fe275a in call_init (l=<optimized out>, argc=argc@@entry=1, argv=argv@@entry=0x7fffffffcfd8,
+#2  0x00007ffff7fe275a in call_init (l=<optimized out>, argc=argc@@entry=1, argv=argv@@entry=0x7fffffffcfd8,
     env=env@@entry=0x7fffffffcfe8) at dl-init.c:72
 #3  0x00007ffff7fe2866 in call_init (env=0x7fffffffcfe8, argv=0x7fffffffcfd8, argc=1, l=<optimized out>)
     at dl-init.c:118
@@ -42737,7 +42759,7 @@ Starting program: /gnu/store/@dots{}-profile/bin/sh -c exec\ inkscape
 #0  g_getenv (variable=variable@@entry=0x7ffff60c7a2e "GOBJECT_DEBUG") at ../glib-2.62.6/glib/genviron.c:252
 #1  0x00007ffff608a7d6 in gobject_init () at ../glib-2.62.6/gobject/gtype.c:4380
 #2  gobject_init_ctor () at ../glib-2.62.6/gobject/gtype.c:4493
-#3  0x00007ffff7fe275a in call_init (l=<optimized out>, argc=argc@@entry=3, argv=argv@@entry=0x7fffffffd088,
+#3  0x00007ffff7fe275a in call_init (l=<optimized out>, argc=argc@@entry=3, argv=argv@@entry=0x7fffffffd088,
     env=env@@entry=0x7fffffffd0a8) at dl-init.c:72
 @dots{}
 @end example
diff --git a/gnu/bootloader.scm b/gnu/bootloader.scm
index da65b9d5d5..f34d222f69 100644
--- a/gnu/bootloader.scm
+++ b/gnu/bootloader.scm
@@ -6,6 +6,7 @@
 ;;; 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 © 2022 Dr. Stefan Karrmann <S.Karrmann@web.de>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -49,6 +50,7 @@
             menu-entry-multiboot-arguments
             menu-entry-multiboot-modules
             menu-entry-chain-loader
+            menu-entry-configfile

             menu-entry->sexp
             sexp->menu-entry
@@ -109,8 +111,10 @@
   (multiboot-modules menu-entry-multiboot-modules
                      (default '()))        ; list of multiboot commands, where
                                            ; a command is a list of <string>
+  (configfile       menu-entry-configfile
+                    (default #f))          ; string, path of grub.cfg file
   (chain-loader     menu-entry-chain-loader
-                    (default #f)))         ; string, path of efi file
+                    (default #f)))          ; string, path of efi file

 (define (report-menu-entry-error menu-entry)
   (raise
@@ -126,6 +130,7 @@
 @code{linux-arguments} and @code{linux-modules},
 @item multiboot by specifying fields @code{multiboot-kernel},
 @code{multiboot-arguments} and @code{multiboot-modules},
+@item configfile by specifying field @code{configfile}.
 @item chain-loader by specifying field @code{chain-loader}.
 @end enumerate"))))))

@@ -141,7 +146,7 @@
   (match entry
     (($ <menu-entry> label device mount-point
                      (? identity linux) linux-arguments (? identity initrd)
-                     #f () () #f)
+                     #f () () #f #f)
      `(menu-entry (version 0)
                   (label ,label)
                   (device ,(device->sexp device))
@@ -151,7 +156,7 @@
                   (initrd ,initrd)))
     (($ <menu-entry> label device mount-point #f () #f
                      (? identity multiboot-kernel) multiboot-arguments
-                     multiboot-modules #f)
+                     multiboot-modules #f #f)
      `(menu-entry (version 0)
                   (label ,label)
                   (device ,(device->sexp device))
@@ -159,13 +164,20 @@
                   (multiboot-kernel ,multiboot-kernel)
                   (multiboot-arguments ,multiboot-arguments)
                   (multiboot-modules ,multiboot-modules)))
-    (($ <menu-entry> label device mount-point #f () #f #f () ()
+    (($ <menu-entry> label device mount-point #f () #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)))
+    (($ <menu-entry> label device mount-point #f () #f #f () ()
+                     (? identity configfile) #f)
+     `(menu-entry (version 0)
+                  (label ,label)
+                  (device ,(device->sexp device))
+                  (device-mount-point ,mount-point)
+                  (configfile ,configfile)))
     (_ (report-menu-entry-error entry))))

 (define (sexp->menu-entry sexp)
@@ -204,6 +216,15 @@ record."
       (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)
+                  ('configfile configfile) _ ...)
+     (menu-entry
+      (label label)
+      (device (sexp->device device))
+      (device-mount-point mount-point)
+      (configfile configfile)))
     (('menu-entry ('version 0)
                   ('label label) ('device device)
                   ('device-mount-point mount-point)
diff --git a/gnu/bootloader/grub.scm b/gnu/bootloader/grub.scm
index 7283257354..2afc1e0b09 100644
--- a/gnu/bootloader/grub.scm
+++ b/gnu/bootloader/grub.scm
@@ -9,6 +9,7 @@
 ;;; Copyright © 2020 Stefan <stefan-guix@vodafonemail.de>
 ;;; Copyright © 2022 Karl Hallsby <karl@hallsby.com>
 ;;; Copyright © 2022 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
+;;; Copyright © 2022 Dr. Stefan Karrmann <S.Karrmann@web.de>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -416,6 +417,15 @@ menuentry ~s {
                     #$root-index (string-join (list #$@arguments) " " 'prefix)
                     (string-join (map string-join '#$modules)
                                  "\n  module " 'prefix))))
+       (configfile
+        #~(format port "
+menuentry ~s {
+  ~a
+  configfile ~a
+}~%
+                  #$label
+                  #$(grub-root-search device configfile)
+                  #$configfile"))
        (chain-loader
         #~(format port "
 menuentry ~s {
diff --git a/tests/boot-parameters.scm b/tests/boot-parameters.scm
index 03a1d01aff..028cffc73c 100644
--- a/tests/boot-parameters.scm
+++ b/tests/boot-parameters.scm
@@ -1,6 +1,7 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2019, 2020 Miguel Ángel Arruga Vivas <rosen644835@gmail.com>
 ;;; Copyright © 2022 Josselin Poiret <dev@jpoiret.xyz>
+;;; Copyright © 2022 Dr. Stefan Karrmann <S.Karrmann@web.de>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -318,6 +319,12 @@
    (linux "/boot/bzImage")
    (initrd "/boot/initrd.cpio.gz")))

+(define %configfile-menu-entry
+  (menu-entry
+   (label "test-configfile")
+   (device (uuid "6d5b13d4-6092-46d0-8be4-073dc07413cc"))
+   (configfile "/boot/grub/grub.cfg")))
+
 (test-equal "menu-entry roundtrip, uuid"
   %uuid-menu-entry
   (sexp->menu-entry (menu-entry->sexp %uuid-menu-entry)))
@@ -326,4 +333,8 @@
   %file-system-label-menu-entry
   (sexp->menu-entry (menu-entry->sexp %file-system-label-menu-entry)))

+(test-equal "menu-entry roundtrip, configfile"
+  %configfile-menu-entry
+  (sexp->menu-entry (menu-entry->sexp %configfile-menu-entry)))
+
 (test-end "boot-parameters")
--
2.30.2

