How to enable numlock in configuration.nix

I would like to have numlock enabled as soon as possible during the boot process by Nixos. Can i achieve this by some setting in configuration.nix?

I tried boot.initrd.preLVMCommands = "setleds +num"; and services.xserver.displayManager.sddm.autoNumlock = true; but they don’t work.

I’ve been using this for a while.

{ config
, lib
, pkgs
, ...
}:
let
  cfg = config.ncfg.services.numlock-on-tty;
in
{
  options.ncfg.services.numlock-on-tty = {
    enable = lib.mkEnableOption "Enable numlock";
  };

  config = lib.mkIf cfg.enable {
    systemd.services.numLockOnTty = {
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        # /run/current-system/sw/bin/setleds -D +num < "$tty";
        ExecStart = lib.mkForce (pkgs.writeShellScript "numLockOnTty" ''
          for tty in /dev/tty{1..6}; do
              ${pkgs.kbd}/bin/setleds -D +num < "$tty";
          done
        '');
      };
    };
  };
}

1 Like

systemd.user.timers.“numlockx_boot” = {
wantedBy = [ “timers.target” ];
timerConfig = {
OnStartupSec = “1us”;
AccuracySec = “1us”;
Unit = “numlockx.service”;
};
};

systemd.user.timers.“numlockx_sleep” = {
wantedBy = [
“suspend.target”
“hibernate.target”
“hybrid-sleep.target”
“suspend-then-hibernate.target”
];
after = [
“suspend.target”
“hibernate.target”
“hybrid-sleep.target”
“suspend-then-hibernate.target”
];
timerConfig = {
AccuracySec = “1us”;
Unit = “numlockx.service”;
};
};

systemd.user.services.“numlockx” = {
script = ‘’
${pkgs.numlockx}/bin/numlockx on
‘’;
serviceConfig = {
Type = “oneshot”; # “simple” für Prozesse, die weiterlaufen sollen
};
};

1 Like

So what is the difference between the two solution? Like which one is better. It seems that the latter one is more complete, but seems a bit more convoluted.

Check out: https://wiki.archlinux.org/title/Activating_numlock_on_bootup

Looks like numlockx only works inside a X11 session.

Yes, but i guess, the solution offered by @mackenzie ought to work through systemd in the TTY.

So that should be the 1.2 header on the Arch wiki: https://wiki.archlinux.org/title/Activating_numlock_on_bootup#With_systemd_service

It just seems that it doesn’t work when I add this to my nix config.

How did you add this to your nix config? If you’re not familiar with nix, try only copying this part to your configuration.nix:

systemd.services.numLockOnTty = {
  wantedBy = [ "multi-user.target" ];
  serviceConfig = {
    # /run/current-system/sw/bin/setleds -D +num < "$tty";
    ExecStart = lib.mkForce (pkgs.writeShellScript "numLockOnTty" ''
      for tty in /dev/tty{1..6}; do
          ${pkgs.kbd}/bin/setleds -D +num < "$tty";
      done
    '');
  };
};
2 Likes

There is an option for numlock in home-manager.
https://nix-community.github.io/home-manager/options.html#opt-xsession.numlock.enable

Cheers, that seems to do the trick, when i build and switched, and moved to another TTY, the num lock was enabled. Great stuff. I guess I was thrown off by the ncfg part and didn’t really understand what it did, so I decided to leave it in.

Big thanks