How to enable num lock for the disk decryption passphrase?

I’ve installed NixOS using the graphical installer where I enabled disk encryption. Then I switched to GRUB from the default systemd bootloader.

When the computer starts, after the GRUB menu, on stage 1 of NixOS boot process, I’m asked to enter the passphrase to decrypt the disk. At this moment, num lock is always disabled. I want it to always be enabled for this moment, so that I can quickly enter a numerical passphrase with one hand. How can this be achieved?

My BIOS doesn’t have a configuration option for num lock on boot. Also what’s I’ve noticed is, even if I turn the num lock on manually some time after GRUB, it turns off again before the passphrase prompt. As I assume, setting up a systemd service wouldn’t work, since it’s too early in the boot process.

Arch Linux has an extensive wiki entry on just this topic: Activating numlock on bootup - ArchWiki

Turns out you need to modify the initramfs via a mkinitcpio hook. I am not entirely sure how to do the same on NixOS however. Maybe someone else does.

1 Like

As I understand, mkinitcpio is specific to Arch Linux. The recommended AUR package contains a simple hook that runs the following bash code:

INITTY=/dev/tty[1-6]
for tty in $INITTY; do
  /usr/bin/setleds -D +num < $tty
done

It also adds the /usr/bin/setleds binary to initramfs.

While I was able to get that shell code to execute on NixOS at seemingly the right time by adding it to boot.initrd.preDeviceCommands in the configuration.nix, it doesn’t work since setleds binary isn’t available. I can’t figure out how to tell Nix to add it to iniramfs.

That’s why I like responding to questions even if I don’t have the same problem. I already learnt something! :slight_smile:

There is an boot.initrd.extraFiles option, maybe that can be of help?

1 Like

You need to explicitly copy all files into the initrd, because of course /nix/store doesn’t exist yet.
I think something like this should work:

  boot.initrd.extraUtilsCommands = ''
    copy_bin_and_libs ${pkgs.kbd}/bin/setleds
  '';
1 Like

Thanks everyone! Here’s the working solution I put in /etc/nixos/configuration.nix:

  # Enable num lock early on boot
  boot.initrd.extraUtilsCommands = ''
    copy_bin_and_libs ${pkgs.kbd}/bin/setleds
  '';
  boot.initrd.preDeviceCommands = ''
    INITTY=/dev/tty[1-6]
    for tty in $INITTY; do
      /bin/setleds -D +num < $tty
    done
  '';

I didn’t test extraFiles because this way seems more streamlined. However, for some reason, boot.initrd.extraUtilsCommands is not documented. There’s already an issue for that.

3 Likes