Marshall Bowers

Conjurer of code. Devourer of art. Pursuer of æsthetics.

Mo Media Keys Mo Problems

Thursday, April 23, 2020
496 words
3 minute read

It's hard to believe that there is only a week left in April. This month has managed to somehow fly by while still feeling like an absolute eternity.

With all the time stuck indoors, I decided to sit down and try to get my keyboard's1 media keys configured to work with my NixOS setup. While this may sound like a trivial exercise, it ended up being a decent amount of effort to get everything setup.

For context, I run spotifyd to play my music from Spotify. I can't explain it, but there's something that absolutely tickles me about having your music player daemonized. spotifyd supports integration with media controls through the MPRIS D-BUS Interface Specification. This functionality is behind a feature flag, which means that in order to get access to it spotifyd needs to be built with the flag.

Thanks to a coincidentally-timed pull request I was able to avoid doing any work to update the Nix package for spotifyd. However, it did require a day or two of waiting in order for the PR to get merged and for the package registry to get rebuilt.

While sitting around waiting for the upstream changes I started learning about Nix overlays, which appeared to be the optimal route for building spotifyd with the right features enabled.

After trawling through a number of docs and tinkering around a bit, I arrived at the following snippet to add to my home.nix file:

{
  nixpkgs.overlays = [
    (self: super:
    {
      spotifyd = super.spotifyd.override {
        withPulseAudio = true;
        withMpris = true;
      };
    })
  ];

  # ...
}

This overlay replaces the spotifyd Nix package with a version that has the withPulseAudio and withMpris options enabled. These options, in turn, pass the corresponding feature flags when building spotifyd with cargo. In this case, the withMpris option is the key to turning on support for MPRIS within spotifyd.

Once spotifyd was setup with MPRIS support it could be controlled using an MPRIS controller, like playerctl.

playerctl -l reveals that playerctl can see spotifyd as a player:

$ playerctl -l
spotifyd

The last thing I had to do was wire up the media keys to use playerctl to issue commands to spotifyd. I use bspwm as my window manager, so achieving this was just a matter of adding some lines to my sxhkd configuration:

XF86AudioRaiseVolume
    playerctl --player=spotifyd volume .03+

XF86AudioLowerVolume
    playerctl --player=spotifyd volume .03-

XF86AudioPlay
    playerctl --player=spotifyd play-pause

XF86AudioNext
    playerctl --player=spotifyd next

XF86AudioPrev
    playerctl --player=spotifyd previous

Success! My media keys were now working as expected... almost.

Unfortunately, there still seems to be an issue where I can't control spotifyd's volume via playerctl, but that's an issue for another day.

Now if you'll excuse me, I'm off to listen to some music.

Marshall

1

A WASD V2 87-key mechanical keyboard