PlayStation 3/4 Controller Over Bluetooth on NixOS

2018-12-20

This blog post explains how to setup a PlayStation 3 or PlayStation 4 controller so it can be used over Bluetooth on NixOS. This uses a NixOS package called bluezFull.

Intro

I wanted to use my PlayStation 3 controllers over Bluetooth to play games on NixOS.

NixOS's bluez package originally didn't have support for the PlayStation 3 and PlayStation 4 Bluetooth controllers, so it took a little work to be able to use them. I ended up creating a new bluez derivation that enables support for PlayStation controllers called bluezFull.

Using the bluezFull derivation

In order to use your PlayStation 3 or PlayStation 4 controllers over Bluetooth, you must enable Bluetooth and make sure the bluezFull package is used. This can be done by specifying the following in your /etc/nixos/configuration.nix:

{ config, pkgs, lib, ... }:

{
  imports = [
    ...
  ];

  hardware.bluetooth.enable = true;
  hardware.bluetooth.package = pkgs.bluezFull;
}

bluezFull is different from bluez because it enables all the Bluez plugins, including support for the PlayStation 3 and PlayStation 4 controllers (called "sixaxis"), the Wiimote, and midi.

After adding this to your /etc/nixos/configuration.nix file, you will need to run sudo nixos-rebuild switch to actually enable the Bluetooth systemd service.

How to actually use the PlayStation 3 controller

In order to actually use a Bluetooth PlayStation controller, you need to pair it with your computer.

You can do so with the following steps1:

  1. Start bluetoothctl (which is provided by the bluez or bluezFull package):

    $ bluetoothctl
  2. Enable the agent and set it as default:

    [bluetooth]# agent on
    [bluetooth]# default-agent
  3. Power on your laptop's Bluetooth receiver, and set it as discoverable and pairable:

    [bluetooth]# power on
    [bluetooth]# discoverable on
    [bluetooth]# pairable on
  4. Connect the PlayStation 3 controller to the system using a USB cable and press the PlayStation button.

  5. bluetoothctl should show the new device and ask you if you want to allow the service authorization request. Type yes:

    [NEW] Device 00:07:F4:1E:B5:AA Sony PLAYSTATION(R)3 Controller
    [agent] Authorize service 00018824-1234-1234-8000-00b04bbb34ff (yes/no): yes

    If your PlayStation controller is not listed automatically, there is a big chance that either your controller is broken or you're accidentally using the bluez package instead of bluezFull.

    You might also try the devices command.

  6. Disconnect the USB cable from the PlayStation 3 controller.

  7. Trust the PlayStation 3 controller.

    [bluetooth]# trust 00:07:F4:1E:B5:AA

    This makes it so that you computer will automatically connect to the PlayStation 3 controller over bluetooth the next time the controller is turned on.

  8. The PlayStation 3 controller is now paired. Quit bluetoothctl:

    [bluetooth]# quit

Now you should be able to use your PlayStation 3 controller as a normal joystick.

Unlike the actual PlayStation, your PlayStation 3 controller will not turn off automatically after a period of inactivity. You must turn the controller off manually by pressing and holding the PlayStation button for 15 seconds.

The steps for setting up a PlayStation 4 controller are similar. They can be found on the Gentoo Wiki.

Earlier Versions of NixOS

The bluezFull package is available in the master branch of nixpkgs as of December 13th, 2018. This means you will be able to use it with NixOS-19.03, but not with earlier NixOS releases like NixOS-18.09.

If you're using NixOS-18.09, you should be able to add support for the PlayStation 3 and PlayStation 4 controllers to bluez like the following. Add this to your /etc/nixos/configuration.nix file:

{ config, pkgs, lib, ... }:

{
  imports = [
    ...
  ];

  hardware.bluetooth.enable = true;
  hardware.bluetooth.package = pkgs.bluez.overrideAttrs (oldAttrs: {
    configureFlags = oldAttrs.configureFlags ++ [ "--enable-sixaxis" ];
  });
}

This enables Bluetooth, as well as setting the Bluetooth package to use. This uses the normal bluez package, but it adds the --enable-sixaxis flag to the flags passed to ./configure when building bluez.

This works well, but the bluez package will have to be rebuilt with this new flag. The bluez package is relatively small, so rebuilding should only take a few minutes.

Non-standard Controllers

Knock-off or imitation PlayStation controllers may not work with bluez. I don't know a good way around this.

I've had trouble with an imitation PlayStation 3 controller being detected as a keyboard instead of as a controller.

Conclusion

Using PlayStation 3 or PlayStation 4 controllers on NixOS is relatively straight-forward, as long as you use the bluezFull package.

Footnotes


  1. These steps are taken from the Gentoo wiki. There is also some good info on the Arch Linux wiki.↩︎

tags: nixos