nix-flakes/modules/mizukios/features/system/grub-standalone.nix
2026-07-05 13:36:31 +09:00

74 lines
3.1 KiB
Nix

{ pkgs, config, ... }:
{
environment.systemPackages = with pkgs; [
grub2_efi
];
system.activationScripts = {
# This, has to be the LAST GRUB script to run.
# The reason is that a standalone GRUB image takes with it
# the configuration too, apparently.
# "90-generate-standalone-grub" = {
# text = ''
# if [ "$NIXOS_ACTION" = "switch" ] || [ "$NIXOS_ACTION" = "boot" ]; then
# echo "Generating standalone GRUB EFI entry..."
# ESP="${config.boot.loader.efi.efiSysMountPoint}"
# OUT_DIR="$ESP/EFI/NixOS-boot"
# FALLBACK_DIR="$ESP/EFI/BOOT"
# mkdir -p "$OUT_DIR" "$FALLBACK_DIR"
# OUT="$OUT_DIR/grubx64.efi"
# FALLBACK="$FALLBACK_DIR/BOOTX64.EFI"
# # GPT-5 told me to add all those modules, idk.
# ${pkgs.grub2_efi}/bin/grub-mkstandalone -O x86_64-efi -o "$OUT" \
# "boot/grub/grub.cfg=${pkgs.writeText "grub-standalone-template.cfg" ''
# search --file --no-floppy --set=esp /EFI/NixOS-boot/grubx64.efi
# set prefix=($esp)/grub
# if [ -f ($esp)/grub/grubenv ]; then
# load_env ($esp)/grub/grubenv
# fi
# if [ -f ($esp)/grub/grub.cfg ]; then
# configfile ($esp)/grub/grub.cfg
# else
# echo "Error: ($esp)/grub/grub.cfg not found."
# sleep 5
# fi
# ''}" \
# --modules="part_gpt part_msdos fat normal search search_fs_uuid search_label configfile linux gzio efi_gop efi_uga all_video gfxterm gfxmenu png jpeg tga font" \
# --disable-shim-lock
# # It's so stupid but otherwise it won't boot:
# # https://wejn.org/2021/09/fixing-grub-verification-requested-nobody-cares/
# ${pkgs.gnused}/bin/sed -i 's/SecureBoot/SecureB00t/' "$OUT"
# cp -f "$OUT" "$FALLBACK"
# fi
# '';
# };
"99-sign-all" = {
text = ''
if [ "$NIXOS_ACTION" = "switch" ] || [ "$NIXOS_ACTION" = "boot" ]; then
echo "Signing all EFI binaries with sbctl..."
# 1. ,
# 2. sign-all ,
# 3. || true .
# DB ( sbctl , )
${pkgs.sbctl}/bin/sbctl sign -s /boot/EFI/NixOS-boot/grubx64.efi || true
${pkgs.sbctl}/bin/sbctl sign -s /boot/EFI/BOOT/BOOTX64.EFI || true
# Windows
if [ -f /boot/EFI/Microsoft/Boot/bootmgfw.efi ]; then
${pkgs.sbctl}/bin/sbctl sign -s /boot/EFI/Microsoft/Boot/bootmgfw.efi || true
fi
# 4.
${pkgs.sbctl}/bin/sbctl sign-all || echo "Some files failed to sign, but continuing..."
fi
'';
};
};
}