MPD

From NixOS Wiki
Revision as of 06:05, 20 September 2021 by imported>Nix (add Software/Applications subcategory and Audio category)

Installation

First you have to enable sound support in the NixOS configuration.nix :

# Enable sound.
sound.enable = true;
hardware.pulseaudio.enable = true;

Now, this article will show you how to setup MPD by using Pulseaudio.

Enable the mpd service :

services.mpd.enable = true;

To specify MPD to output using Pulseaudio, we have to specify audio_output in MPD's configuration :

services.mpd.extraConfig = ''
  audio_output {
    type "pulse"
    name "Pulseaudio"
  }
'';

type is to define whether we are using Pulseaudio or ALSA. name can be what you want.

Now, according to https://wiki.archlinux.org/index.php/Music_Player_Daemon/Tips_and_tricks#Local_(with_separate_mpd_user), this will not work, because MPD and Pulseaudio are ran by different users. Still according to the Arch Wiki, we can "configure mpd to use pulseaudio's tcp module to send sound to localhost".

So, to enable the required Pulseaudio modules, add

hardware.pulseaudio.extraConfig = "load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1";

to configuration.nix

And add server "127.0.0.1" to MPD's config to tell it to connect to Pulseaudio's local sound server.

In

services.mpd.extraConfig = ''
  audio_output {
    type "pulse"
    name "Pulseaudio"
  }
'';

Add the server "127.0.0.1" line :

services.mpd.extraConfig = ''
  audio_output {
    type "pulse"
    name "Pulseaudio"
    server "127.0.0.1"
  }
'';

Done ! You can now test if everything is working by using a MPD client, such as MPC.

Note : before testing, restart the MPD service :

# systemctl restart mpd

Summary

# Enable sound.
sound.enable = true;
hardware.pulseaudio = {
  enable = true;
  extraConfig = "load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1"; # Needed by mpd to be able to use Pulseaudio
};

# Music daemon, can be accessed through mpc or an other client
services.mpd = {
  enable = true;
  extraConfig = ''
    audio_output {
      type "pulse" # MPD must use Pulseaudio
      name "Pulseaudio" # Whatever you want
      server "127.0.0.1" # MPD must connect to the local sound server
    }
  '';
};

Further reading

The legendary Arch Wiki : https://wiki.archlinux.org/index.php/Mpd

Another solution - pulseaudio with systemWide

You can also set pulseaudio running system wide.

sound.enable = true;
  hardware.pulseaudio = {
    systemWide = true;
    enable = true;
  };

services.mpd = {
  enable = true ;
  extraConfig = ''
    audio_output {
      type "pulse"
      name "Pulseaudio"
      mixer_type      "hardware"      # optional
      mixer_device    "default"       # optional
      mixer_control   "PCM"           # optional
      mixer_index     "0"             # optional
    }  '';
  ## optional 
  network.listenAddress = "any"; # allow to control mop from any host
  musicDirectory = "/path/to/some/music"; # ptah to your music
};

You can also use alsa, just add audio output to services.mpd.extraConfig

services.mpd = {
  enabled = true;
  extraConfig = ''
    audio_output {
      type "alsa"
      name "alsa"
      device			"hw:0,0"	# optional 
      format			"44100:16:2"	# optional
      mixer_type		"hardware"
      mixer_device		"default"
      mixer_control		"PCM"
    }
  '';
}