aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilan Pässler <milan@petabyte.dev>2020-10-31 18:15:24 +0100
committerMilan Pässler <milan@petabyte.dev>2020-10-31 18:15:24 +0100
commita21a48e18beda18f776c7d97991561c592db1e7c (patch)
tree6578698329c1b33fcdc47bc9d325ffa0e219fddc
parentb68a2b47de510ec87b12513d9c305571ceae9cc9 (diff)
grub2: generate grub.cfg from module config
-rw-r--r--modules/grub2/default.nix69
-rw-r--r--modules/grub2/files/grub-scan.cfg30
-rw-r--r--modules/grub2/files/grub.cfg184
3 files changed, 120 insertions, 163 deletions
diff --git a/modules/grub2/default.nix b/modules/grub2/default.nix
index 2f918f476eef..d2da4dd6cf0f 100644
--- a/modules/grub2/default.nix
+++ b/modules/grub2/default.nix
@@ -9,6 +9,39 @@ let
else
"fallback/payload";
+ configText = (readFile ./files/grub.cfg) +
+ (optionalString (cfg.scanDevices) (readFile ./files/grub-scan.cfg)) +
+ (optionalString (cfg.users != {})
+ (concatStringsSep "\n" (mapAttrsToList (n: u: ''
+ ${if u.passwordIsHashed then "password_pbkdf2" else "password"} ${n} ${u.password}
+ '') cfg.users)) + ''
+ set superusers="${concatStringsSep " " (attrNames (filterAttrs (n: u: u.superuser) cfg.users))}"
+ export superusers
+ ''
+ ) +
+ (optionalString cfg.generateSecondaryPayloadEntries (
+ concatMapStrings (n: ''
+ menuentry '${removePrefix "img/" n}' {
+ chainloader (cbfsdisk)/${n}
+ }
+ '') (filter (hasPrefix "img/") (attrNames config.corenix.extraFiles))
+ ));
+
+ userOpts = { ... }: {
+ options = {
+ superuser = mkOption {
+ type = types.bool;
+ default = true;
+ };
+ password = mkOption {
+ type = types.str;
+ };
+ passwordIsHashed = mkOption {
+ type = types.bool;
+ default = true;
+ };
+ };
+ };
in {
options.grub2 = {
enable = mkEnableOption "grub2 coreboot primary payload";
@@ -18,14 +51,44 @@ in {
default = false;
};
+ generateSecondaryPayloadEntries = mkOption {
+ type = types.bool;
+ default = true;
+ };
+
+ scanDevices = mkOption {
+ type = types.bool;
+ default = true;
+ description = ''
+ Scan internal and external storage devices for GRUB2/syslinux/isolinux/NetBSD
+ configs and at runtime and create boot entries for each of them.
+ '';
+ };
+
+ extraConfig = mkOption {
+ type = types.lines;
+ default = "";
+ };
+
+ configFile = mkOption {
+ type = types.path;
+ };
+
+ users = mkOption {
+ type = types.attrsOf (types.submodule userOpts);
+ default = {};
+ };
+
font = mkOption {
type = types.path;
- #default = "${pkgs.dejavu_fonts}/share/fonts/truetype/DejaVuSansMono.ttf";
default = "${pkgs.unifont}/share/fonts/truetype/unifont.ttf";
+ example = "${pkgs.dejavu_fonts}/share/fonts/truetype/DejaVuSansMono.ttf";
};
};
- config = lib.mkIf cfg.enable {
+ config = mkIf cfg.enable {
+ grub2.configFile = pkgs.writeText "grub.cfg" configText;
+
corenix.extraFiles = {
${payloadName} = {
type = "payload";
@@ -36,7 +99,7 @@ in {
buildInputs = with pkgs; [ grub2 ];
} "grub-mkfont --range=0x20-0x7E,0x2501-0x251F,0x2191-0x2193 --size=14 -o $out ${cfg.font}"
);
- "etc/grub.cfg".src = ./files/grub.cfg;
+ "etc/grub.cfg".src = cfg.configFile;
"background.png".src = ./files/background.png;
};
};
diff --git a/modules/grub2/files/grub-scan.cfg b/modules/grub2/files/grub-scan.cfg
new file mode 100644
index 000000000000..f6761b7bcbcc
--- /dev/null
+++ b/modules/grub2/files/grub-scan.cfg
@@ -0,0 +1,30 @@
+for x in (ahci*,*) (usb*.*) ; do
+ if [ -f "${x}/netbsd" ] ; then
+ menuentry "Load NetBSD from $x" $x {
+ root=$2
+ knetbsd /netbsd
+ }
+ fi
+ for path in '' /grub /grub2 /boot /boot/grub /boot/grub2; do
+ if [ -f "${x}${path}/grub.cfg" ] ; then
+ menuentry "Load config from ${x}" $x $path {
+ root=$2
+ configfile "/${3}/grub.cfg"
+ }
+ fi
+ done
+ for path in '' /boot; do
+ if [ -f "${x}${path}/syslinux/syslinux.cfg" ] ; then
+ menuentry "Load syslinux config from ${x}" $x $path {
+ root=$2
+ syslinux_configfile -s "${3}/syslinux/syslinux.cfg"
+ }
+ fi
+ if [ -f "${x}${path}/isolinux/isolinux.cfg" ] ; then
+ menuentry "Load isolinux config from ${x}" $x $path {
+ root=$2
+ syslinux_configfile -i "${3}/isolinux/isolinux.cfg"
+ }
+ fi
+ done
+done
diff --git a/modules/grub2/files/grub.cfg b/modules/grub2/files/grub.cfg
index 5ed7fff569b8..9b4548fbf048 100644
--- a/modules/grub2/files/grub.cfg
+++ b/modules/grub2/files/grub.cfg
@@ -1,70 +1,30 @@
-function try_user_config {
- set root="${1}"
- for dir in boot grub grub2 boot/grub boot/grub2; do
- for name in libreboot_ autoboot_ librecore_ coreboot_ ''; do
- if [ -f /"${dir}"/"${name}"grub.cfg ]; then
- unset superusers
- configfile /"${dir}"/"${name}"grub.cfg
- fi
- done
- done
-}
-
-function search_grub {
- for i in 0 1; do
- # raw devices
- try_user_config "(${1}${i})"
- for part in 1 2 3 4 5; do
- # MBR/GPT partitions
- try_user_config "(${1}${i},${part})"
- done
- done
-}
-
-function try_isolinux_config {
- set root="${1}"
- for dir in '' /boot; do
- if [ -f "${dir}"/isolinux/isolinux.cfg ]; then
- syslinux_configfile -i "${dir}"/isolinux/isolinux.cfg
- elif [ -f "${dir}"/syslinux/syslinux.cfg ]; then
- syslinux_configfile -s "${dir}"/syslinux/syslinux.cfg
- fi
- done
-}
-
-function search_isolinux {
- for i in 0 1; do
- # raw devices
- try_isolinux_config "(${1}${i})"
- for part in 1 2 3 4 5; do
- # MBR/GPT partitions
- try_isolinux_config "(${1}${i},${part})"
- done
- done
-}
+insmod regexp
+insmod ahci
+insmod part_msdos
+insmod part_gpt
function load_video {
- if [ x$feature_all_video_module = xy ]; then
- insmod all_video
- else
- insmod efi_gop
- insmod efi_uga
- insmod ieee1275_fb
- insmod vbe
- insmod vga
- insmod video_bochs
- insmod video_cirrus
- fi
+ if [ x$feature_all_video_module = xy ]; then
+ insmod all_video
+ else
+ insmod efi_gop
+ insmod efi_uga
+ insmod ieee1275_fb
+ insmod vbe
+ insmod vga
+ insmod video_bochs
+ insmod video_cirrus
+ fi
}
if loadfont (cbfsdisk)/font.pf2 ; then
- set gfxmode=auto
- load_video
- insmod gfxterm
- set locale_dir=$prefix/locale
- set lang=en_US
- insmod gettext
+ set gfxmode=auto
+ load_video
+ insmod gfxterm
+ set locale_dir=$prefix/locale
+ set lang=en_US
+ insmod gettext
fi
terminal_input console
@@ -72,111 +32,15 @@ terminal_output gfxterm
gfxpayload=keep
if [ x$feature_timeout_style = xy ] ; then
- set timeout_style=menu
- set timeout=1
+ set timeout_style=menu
+ set timeout=1
# Fallback normal timeout code in case the timeout_style feature is
# unavailable.
else
- set timeout=5
+ set timeout=5
fi
insmod png
if background_image (cbfsdisk)/background.png; then
true
fi
-
-#Scan for OS on internal HDD
-insmod regexp
-insmod ahci
-insmod part_msdos
-for x in (ahci*,*) ; do
- if [ -f "$x/netbsd" ] ; then
- menuentry "Load NetBSD from $x" $x {
- root=$2
- knetbsd /netbsd
- }
- fi
-done
-
-for x in (usb*,*) ; do
- if [ -f "$x/netbsd" ] ; then
- menuentry "Load NetBSD from $x" $x {
- root=$2
- knetbsd /netbsd
- }
- fi
-done
-
-for x in (ahci*,*) ; do
- if [ -f "$x/grub/grub.cfg" ] ; then
- menuentry "Load Config from $x" $x {
- root=$2
- configfile /grub/grub.cfg
- }
- fi
- if [ -f "$x/boot/grub/grub.cfg" ] ; then
- menuentry "Load Config from $x" $x {
- root=$2
- configfile /boot/grub/grub.cfg
- }
- fi
-done
-
-for x in (usb*,*) ; do
- if [ -f "$x/grub/grub.cfg" ] ; then
- menuentry "Load Config from $x" $x {
- root=$2
- configfile /grub/grub.cfg
- }
- fi
- if [ -f "$x/boot/grub/grub.cfg" ] ; then
- menuentry "Load Config from $x" $x {
- root=$2
- configfile /boot/grub/grub.cfg
- }
- fi
-done
-
-
-menuentry 'Search ISOLINUX menu (AHCI) [a]' --hotkey='a' {
- search_isolinux ahci
-}
-
-menuentry 'Search ISOLINUX menu (USB) [u]' --hotkey='u' {
- search_isolinux usb
-}
-
-menuentry 'Search ISOLINUX menu (CD/DVD) [d]' --hotkey='d' {
- insmod ata
- for dev in ata0 ata1 ata2 ata3 ahci1; do
- try_isolinux_config "(${dev})"
- done
-}
-
-menuentry 'Search for GRUB2 configuration on external media [s]' --hotkey='s' {
- search_grub usb
-}
-
-menuentry 'nvramcui' {
- chainloader (cbfsdisk)/img/nvramcui
-}
-
-menuentry 'coreinfo' {
- chainloader (cbfsdisk)/img/coreinfo
-}
-
-menuentry 'Memtest86+' {
- chainloader (cbfsdisk)/img/memtest
-}
-
-menuentry 'tint' {
- chainloader (cbfsdisk)/img/tint
-}
-
-menuentry 'Poweroff [p]' --hotkey='p' {
- halt
-}
-
-menuentry 'Reboot [r]' --hotkey='r' {
- reboot
-}