Skip to content

Commit

Permalink
nixos: Add support for scalable fonts in Grub menus (#26227)
Browse files Browse the repository at this point in the history
The default font is unreadably small on some hidpi displays. This
makes it possible to specify a TrueType or OpenType font at any point
size, and it will automatically be converted to the format the Grub
uses.
  • Loading branch information
benley committed Jun 10, 2017
1 parent 1fe9518 commit 700e22f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
30 changes: 29 additions & 1 deletion nixos/modules/system/boot/loader/grub/grub.nix
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,21 @@ let
)) + ":" + (makeSearchPathOutput "bin" "sbin" [
pkgs.mdadm pkgs.utillinux
]);
font = if lib.last (lib.splitString "." cfg.font) == "pf2"
then cfg.font
else "${convertedFont}";
});

bootDeviceCounters = fold (device: attr: attr // { "${device}" = (attr."${device}" or 0) + 1; }) {}
(concatMap (args: args.devices) cfg.mirroredBoots);

convertedFont = (pkgs.runCommand "grub-font-converted.pf2" {}
(builtins.concatStringsSep " "
([ "${realGrub}/bin/grub-mkfont"
cfg.font
"--output" "$out"
] ++ (optional (cfg.fontSize!=null) "--size ${toString cfg.fontSize}")))
);
in

{
Expand Down Expand Up @@ -276,7 +286,7 @@ in
extraInitrd = mkOption {
type = types.nullOr types.path;
default = null;
example = "/boot/extra_initrafms.gz";
example = "/boot/extra_initramfs.gz";
description = ''
The path to a second initramfs to be supplied to the kernel.
This ramfs will not be copied to the store, so that it can
Expand Down Expand Up @@ -305,6 +315,24 @@ in
'';
};

font = mkOption {
type = types.nullOr types.path;
default = "${realGrub}/share/grub/unicode.pf2";
description = ''
Path to a TrueType, OpenType, or pf2 font to be used by Grub.
'';
};

fontSize = mkOption {
type = types.nullOr types.int;
example = literalExample 16;
default = null;
description = ''
Font size for the grub menu. Ignored unless <literal>font</literal>
is set to a ttf or otf font.
'';
};

gfxmodeEfi = mkOption {
default = "auto";
example = "1024x768";
Expand Down
6 changes: 5 additions & 1 deletion nixos/modules/system/boot/loader/grub/install-grub.pl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ sub runCommand {
my $gfxmodeBios = get("gfxmodeBios");
my $bootloaderId = get("bootloaderId");
my $forceInstall = get("forceInstall");
my $font = get("font");
$ENV{'PATH'} = get("path");

die "unsupported GRUB version\n" if $grubVersion != 1 && $grubVersion != 2;
Expand Down Expand Up @@ -281,7 +282,7 @@ sub GrubFs {
insmod vbe
fi
insmod font
if loadfont " . $grubBoot->path . "/grub/fonts/unicode.pf2; then
if loadfont " . $grubBoot->path . "/converted-font.pf2; then
insmod gfxterm
if [ \"\${grub_platform}\" = \"efi\" ]; then
set gfxmode=$gfxmodeEfi
Expand All @@ -294,6 +295,9 @@ sub GrubFs {
fi
";

if ($font) {
copy $font, "$bootPath/converted-font.pf2" or die "cannot copy $font to $bootPath\n";
}
if ($splashImage) {
# FIXME: GRUB 1.97 doesn't resize the background image if it
# doesn't match the video resolution.
Expand Down

0 comments on commit 700e22f

Please sign in to comment.