Enabling Dell XPS's Mediabuttons on NixOS+XMonad
Bare with me, this posts covers a solution that may partially work for some but will probably cause some trouble down the road. I’ll get to it in a minute. I’m a XMonad user, so I ended up relying on a solution that
Trying actkbd
Inside your configuration.nix, or whichever file you import from your configuration.nix with the purpose of containing audio-related settings, add the following snippet:
services.actkbd = with pkgs; {
enable = true;
bindings = [
# "Mute" media key
{
keys = [ 113 ];
events = [ "key" ];
command = "${alsaUtils}/bin/amixer -q set Master toggle";
}
# "Lower Volume" media key
{
keys = [ 114 ];
events = [ "key" "rep" ];
command = "${alsaUtils}/bin/amixer -q set Master 1- unmute";
}
# "Raise Volume" media key
{
keys = [ 115 ];
events = [ "key" "rep" ];
command = "${alsaUtils}/bin/amixer -q set Master 1+ unmute";
}
];
};
The keycodes for the snippet have been acquired by switching to a virtual
terminal and running
showkey
. The showkey application displays the keycodes for the pressed keys
and exits after 10 seconds without input. In my case the keycodes for mute,
volume down and volume up represented the keycodes 113, 114 and 115
respectively.
Since my function keys have been configured in my BIOS to
behave as function keys instead of multimedia keys, I can switch between
virtual terminals by simultaneously pressing Ctrl
, Alt
and any of my
function keys ranging from F1
through F12
.
Triggering the multimedia behavior for mute, which is set
up on the F1
key, will require me to simultaneously press Fn
and F1
. Note
that Fn
indicates to the system that I intent to trigger the alternate
behavior for the given key.
The problem with this approach is that the actkbd daemon
executes commands as root
. Since the PulseAudio server imposes restrictions
on who may connect to it, we need to connect as the same user as the one
running the server.
XDG_RUNTIME_DIR (/run/user/1988) is not owned by us (uid 0), but by uid 1988! (This could e g happen if you try to connect to a non-root PulseAudio as a root user, over the native protocol. Don't do that.)
ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused
amixer: Mixer attach default error: Connection refused
XMonad
Retrieve the keysym for the required buttons in xev
(0x1008ff12
,
0x1008ff11
and 0x1008ff13
in my case) and add these to the xmonad.hs.
I’ve chosen to define my audio keys as a seperate list
audioKeys = [
((0, 0x1008ff12), spawn "amixer -q set Master toggle")
, ((0, 0x1008ff11), spawn "amixer -q set Master 10%-")
, ((0, 0x1008ff13), spawn "amixer -q set Master 10%+")
]
and concatenate them to the other bindings in order to produce myKeys
:wink:
myKeys = [
-- some other keybindings
] ++ audioKeys
but you should do whatever works best for your setup. Check out the XMonad config tips for some interesting ideas regarding the structuring of your configuration.