git.iko.soy/typeable/nixpkgs
on master help sign in
grub: add boot.loader.grub.efiInstallAsRemovable

Closes #16374

commit1c9ac8aabc3105b14c762459bd14a5d53f691449 authorobadz <obadz-git@obadz.com> dateTue 13 Sep 2016, 18:46 (10 years ago) parenta0b643e
3 files changed+58−6
nixos/modules/installer/tools/nixos-generate-config.pl+4−1 nixos/modules/system/boot/loader/grub/grub.nix+47−1 nixos/modules/system/boot/loader/grub/install-grub.pl+7−4
nixos/modules/installer/tools/nixos-generate-config.pl+4−1
@@ -527,8 +527,11 @@ EOF
527527 # Use the GRUB 2 boot loader.
528528 boot.loader.grub.enable = true;
529529 boot.loader.grub.version = 2;
530+ # boot.loader.grub.efiSupport = true;
531+ # boot.loader.grub.efiInstallAsRemovable = true;
532+ # boot.loader.efi.efiSysMountPoint = "/boot/efi";
530533 # Define on which hard drive you want to install Grub.
531− # boot.loader.grub.device = "/dev/sda";
534+ # boot.loader.grub.device = "/dev/sda"; # or "nodev" for efi only
532535EOF
533536 }
534537
nixos/modules/system/boot/loader/grub/grub.nix+47−1
@@ -55,7 +55,7 @@ let
5555 inherit (cfg)
5656 version extraConfig extraPerEntryConfig extraEntries
5757 extraEntriesBeforeNixOS extraPrepareConfig configurationLimit copyKernels
58− default fsIdentifier efiSupport gfxmodeEfi gfxmodeBios;
58+ default fsIdentifier efiSupport efiInstallAsRemovable gfxmodeEfi gfxmodeBios;
5959 path = (makeBinPath ([
6060 pkgs.coreutils pkgs.gnused pkgs.gnugrep pkgs.findutils pkgs.diffutils pkgs.btrfs-progs
6161 pkgs.utillinux ] ++ (if cfg.efiSupport && (cfg.version == 2) then [pkgs.efibootmgr ] else [])
@@ -357,6 +357,44 @@ in
357357 '';
358358 };
359359
360+ efiInstallAsRemovable = mkOption {
361+ default = false;
362+ example = true;
363+ type = types.bool;
364+ description = ''
365+ Whether to invoke <literal>grub-install</literal> with
366+ <literal>--removable</literal>.
367+
368+ Unless turn this on, GRUB will install itself somewhere (exactly
369+ where depends on other config variables) in
370+ <literal>boot.loader.efi.efiSysMountPoint</literal>. If you've set
371+ <literal>boot.loader.efi.canTouchEfiVariables</literal> *AND* you
372+ are currently booted in UEFI mode, then GRUB will use
373+ <literal>efibootmgr</literal> to modify the boot order in the NVRAM's
374+ EFI variables of your computer to include this location. If you are
375+ *not* booted in UEFI mode at the time grub is being installed, the
376+ NVRAM will not be modified, and your system will not find GRUB at
377+ boot time. GRUB will still succeed (althgouh you'll see a warning
378+ printed "<literal>efibootmgr: EFI variables are not supported on
379+ this system.</literal>").
380+
381+ If you do turn this feature on, then GRUB will install itself
382+ in a specific special location within <literal>efiSysMountPoint</literal>
383+ (namely <literal>EFI/boot/boot$arch.efi</literal>) which is the firmwares
384+ are hardcoded to try first, regardless of NVRAM EFI variables.
385+
386+ To summarize, turn this on if:
387+ <itemizedlist>
388+ <listitem>You are installing NixOS and want it to boot in UEFI mode,
389+ but you are currently booted in legacy mode</listitem>
390+ <listitem>You want to make a drive that will boot regardless of
391+ the NVRAM state of the computer (like a USB "removable" drive)</listitem>
392+ <listitem>You simply dislike the idea of depending on some NVRAM
393+ state to make your drive bootable</listitem>
394+ </itemizedlist>
395+ '';
396+ };
397+
360398 enableCryptodisk = mkOption {
361399 default = false;
362400 type = types.bool;
@@ -484,6 +522,14 @@ in
484522 assertion = !cfg.trustedBoot.enable || cfg.trustedBoot.systemHasTPM == "YES_TPM_is_activated";
485523 message = "Trusted GRUB can break the system! Confirm that the system has an activated TPM by setting 'systemHasTPM'.";
486524 }
525+ {
526+ assertion = cfg.efiInstallAsRemovable -> cfg.efiSupport;
527+ message = "If you wish to to use boot.loader.grub.efiInstallAsRemovable, then turn on boot.loader.grub.efiSupport";
528+ }
529+ {
530+ assertion = cfg.efiInstallAsRemovable -> !config.boot.loader.efi.canTouchEfiVariables;
531+ message = "If you wish to to use boot.loader.grub.efiInstallAsRemovable, then turn off boot.loader.efi.canTouchEfiVariables";
532+ }
487533 ] ++ flip concatMap cfg.mirroredBoots (args: [
488534 {
489535 assertion = args.devices != [ ];
nixos/modules/system/boot/loader/grub/install-grub.pl+7−4
@@ -60,6 +60,7 @@ my $grubTargetEfi = get("grubTargetEfi");
6060my $bootPath = get("bootPath");
6161my $storePath = get("storePath");
6262my $canTouchEfiVariables = get("canTouchEfiVariables");
63+my $efiInstallAsRemovable = get("efiInstallAsRemovable");
6364my $efiSysMountPoint = get("efiSysMountPoint");
6465my $gfxmodeEfi = get("gfxmodeEfi");
6566my $gfxmodeBios = get("gfxmodeBios");
@@ -544,13 +545,15 @@ if (($requireNewInstall != 0) && ($efiTarget eq "no" || $efiTarget eq "both")) {
544545# install EFI GRUB
545546if (($requireNewInstall != 0) && ($efiTarget eq "only" || $efiTarget eq "both")) {
546547 print STDERR "installing the GRUB $grubVersion EFI boot loader into $efiSysMountPoint...\n";
548+ my @command = ("$grubEfi/sbin/grub-install", "--recheck", "--target=$grubTargetEfi", "--boot-directory=$bootPath", "--efi-directory=$efiSysMountPoint");
547549 if ($canTouchEfiVariables eq "true") {
548− system("$grubEfi/sbin/grub-install", "--recheck", "--target=$grubTargetEfi", "--boot-directory=$bootPath", "--efi-directory=$efiSysMountPoint", "--bootloader-id=$bootloaderId") == 0
549− or die "$0: installation of GRUB EFI into $efiSysMountPoint failed\n";
550+ push @command, "--bootloader-id=$bootloaderId";
550551 } else {
551− system("$grubEfi/sbin/grub-install", "--recheck", "--target=$grubTargetEfi", "--boot-directory=$bootPath", "--efi-directory=$efiSysMountPoint", "--no-nvram") == 0
552− or die "$0: installation of GRUB EFI into $efiSysMountPoint failed\n";
552+ push @command, "--no-nvram";
553+ push @command, "--removable" if $efiInstallAsRemovable eq "true";
553554 }
555+
556+ (system @command) == 0 or die "$0: installation of GRUB EFI into $efiSysMountPoint failed\n";
554557}
555558
556559