Backlight

From NixOS Wiki
Revision as of 17:08, 6 May 2018 by imported>Samueldr (WIP, cleaning and re-working sections)

Introduction

This page documents methods for controlling backlight (aka screen brightness) and tips to control it via hotkeys.

xbacklight

xbacklight uses X to change the light settings. This can be inconvenient in some situations, e.g. for use with the services.actkbd service, which doesn't know about the X session. It, though, has an history of being more compatible with different hardware, especially newer hardware[citation needed]

To install xbacklight globally, add this to your configuration.nix.

  environment.systemPackages = with pkgs; [ xorg.xbacklight ];

Alternatively, use nix-env -iA nixos.xorg.xbacklight to install it to your user profile.

light

light does not use X to change the light settings. This can be used in situations where the X service isn't available. While it does not use X, it will need some privileges to work. This means that it needs to either be installed in a specific way (with a SUID wrapper) or used using sudo, or ran with superuser privileges.

To enable the use of light with SUID wrappers, add this to your configuration.nix.

  programs.light.enable = true;

The following commands will allow you to test light:

  • light -U 30 — the screen should become darker.
  • light -A 30 — the screen should become brighter.

Be careful using light -U, as you might turn your backlight completely off! You will not be able to see what you're typing anymore.

Tips

Key mapping

Once the backend functionality of controlling the backlight is verified, you can assign that to an actual key. Different laptops and keyboards may map the hotkeys to different key codes.

Key mapping - obtain key codes via xev

While logged into an X session:
nix-shell -p xorg.xev
xev
Xev reports key codes, but they did not seem to be the right codes for use with actkbd.

Key mapping - obtain key codes via actkbd

First enable the actkbd service (see below).
To read key codes via actkbd, you need to select the right input event provider from /dev/input/. Each input device generating input events has it's own entry there, enumerated with an number. To find out which one generates the events from the hotkeys, it is viable (if tedious) to run the below command with each available entry, and see which one generates event notifications when pressing the brightness hotkeys:
actkbd -v9 -n -s -d /dev/input/event<number>
Once you discover the right one, it will report key presses like so:
Event: <number>:key
Event: <number>:rel
Those represent the key-press and key-release actions. The number is the key code to use for mapping.

Key mapping - map via actkbd service

To map keys using actkbd service and the light program, add the following to your configuration.nix file:

services.actkbd = {
  enable = true;
  bindings = [
    {keys = [ <brightnessDown> ]; events = [ "key" ]; command = "run/wrappers/bin/light -U 10";}
    {keys = [ <brightnessUp> ]; events = [ "key" ]; command = "run/wrappers/bin/light -A 10";}
  ];
};

Where <brightnessUp> and <brightnessDown> are the key codes obtained before.

Working configuration on Lenovo T440

To enable backlight control on a Lenovo T440, add to configuration.nix:

programs.light.enable = true;
services.actkbd = {
  enable = true;
  bindings = [
   { keys = [ 224 ]; events = [ "key" ]; command = "/run/wrappers/bin/light -A 10"; }
   { keys = [ 225 ]; events = [ "key" ]; command = "/run/wrappers/bin/light -U 10"; }
  ];
};

Tested with NixOS 18.03, and in combination with lightdm & i3.

See also