MPD: Difference between revisions

fix links
Novida (talk | contribs)
m fix duplicate code block error
 
(5 intermediate revisions by 5 users not shown)
Line 1: Line 1:
== Installation ==
[https://www.musicpd.org/ Music Player Daemon] (MPD) is a flexible, powerful, server-side application for playing music. Through plugins and libraries, it can play a variety of sound files while being controlled by its network protocol.
A typical config, running MPD system-wide, will look like this:
 
== Home-manager Installation ==
Home-manager has a module for MPD which runs it as a systemd user service, giving it easier access to your user's sound server. In most cases no specific audio output configuration is necessary:
<syntaxhighlight lang="nix">services.mpd = {
  enable = true;
  musicDirectory = "/path/to/music";
  # Optional:
  network.listenAddress = "any"; # if you want to allow non-localhost connections
  network.startWhenNeeded = true; # systemd feature: only start MPD service upon connection to its socket
};</syntaxhighlight>
 
 
== NixOS Installation ==
A typical NixOS config, running MPD system-wide, will look like this:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
services.mpd = {
services.mpd = {
   enable = true;
   enable = true;
   musicDirectory = "/path/to/music";
   musicDirectory = "/path/to/music";
   extraConfig = ''
   settings = {
     # must specify one or more outputs in order to play audio!
     # must specify one or more audio_output blocks in order to play audio!
     # (e.g. ALSA, PulseAudio, PipeWire), see next sections
     # (e.g. ALSA, PulseAudio, PipeWire), see next sections
   '';
   };


   # Optional:
   # Optional:
Line 18: Line 31:


== PulseAudio ==
== PulseAudio ==
In order to use MPD with PulseAudio, enable sound support in the NixOS <code>configuration.nix</code> :
In order to use system-wide MPD with PulseAudio, enable sound support in the NixOS <code>configuration.nix</code> :
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
# Enable sound.
# Enable sound.
Line 27: Line 40:
Then, add a PulseAudio output to MPD:
Then, add a PulseAudio output to MPD:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
services.mpd.extraConfig = ''
services.mpd.settings = {
   audio_output {
   audio_output = [
    type "pulse"
    {
    name "My PulseAudio" # this can be whatever you want
      type = "pulse";
   }
      name = "My PulseAudio"; # this can be whatever you want
'';
    }
   ];
};
</syntaxhighlight>
</syntaxhighlight>


Line 47: Line 62:
And add <code>server "127.0.0.1"</code> to MPD's config to tell it to connect to PulseAudio's local sound server.
And add <code>server "127.0.0.1"</code> to MPD's config to tell it to connect to PulseAudio's local sound server.
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
services.mpd.extraConfig = ''
services.mpd.settings = {
   audio_output {
   audio_output = [
    type "pulse"
    {
    name "Pulseaudio"
      type = "pulse";
    server "127.0.0.1" # add this line - MPD must connect to the local sound server
      name = "Pulseaudio";
   }
      server = "127.0.0.1"; # add this line - MPD must connect to the local sound server
'';
    }
   ];
};
</syntaxhighlight>
</syntaxhighlight>
After editing the configuration and running <code># nixos-rebuild switch</code>, you can test if everything is working by using a MPD client, such as <code>MPC</code>.
After editing the configuration and running <code># nixos-rebuild switch</code>, you can test if everything is working by using a MPD client, such as <code>MPC</code>.
Line 61: Line 78:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
hardware.pulseaudio.systemWide = true;  
hardware.pulseaudio.systemWide = true;


services.mpd.extraConfig = ''
services.mpd.settings = {
     audio_output {
  audio_output = [
       type "pulse"
     {
       name "Pulseaudio"
       type = "pulse";
       mixer_type     "hardware"     # optional
       name = "Pulseaudio";
       mixer_device   "default"       # optional
       mixer_type   = "hardware"; # optional
       mixer_control   "PCM"           # optional
       mixer_device = "default"# optional
       mixer_index     "0"             # optional
       mixer_control = "PCM";    # optional
     } '';
       mixer_index = "0";        # optional
     }
  ];
};
};
</syntaxhighlight>
</syntaxhighlight>
Line 78: Line 97:
You can also use alsa, just add audio output to services.mpd.extraConfig:
You can also use alsa, just add audio output to services.mpd.extraConfig:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
services.mpd.extraConfig = ''
services.mpd.settings = {
   audio_output {
   audio_output = [
    type "alsa"
    {
    name "My ALSA"
      type = "alsa";
    device "hw:0,0" # optional  
      name = "My ALSA";
    format "44100:16:2" # optional
      device       = "hw:0,0"; # optional
    mixer_type "hardware"
      format       = "44100:16:2"; # optional
    mixer_device "default"
      mixer_type   = "hardware";
    mixer_control "PCM"
      mixer_device = "default";
   }
      mixer_control = "PCM";
'';
    }
   ];
};
</syntaxhighlight>
</syntaxhighlight>


Line 94: Line 115:
Make sure PipeWire is enabled. See [[PipeWire]]
Make sure PipeWire is enabled. See [[PipeWire]]


To use PipeWire, create an <code>audio_output</code> for it:
To use PipeWire with a system-wide MPD instance, create an <code>audio_output</code> for it:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
services.mpd.extraConfig = ''
services.mpd.settings = {
   audio_output {
   audio_output = [
    type "pipewire"
    {
    name "My PipeWire Output"
      type = "pipewire";
   }
      name = "My PipeWire Output";
'';
    }
   ];
};
</syntaxhighlight>
</syntaxhighlight>
See https://mpd.readthedocs.io/en/stable/plugins.html#pipewire for more options.
See https://mpd.readthedocs.io/en/stable/plugins.html#pipewire for more options.
Line 107: Line 130:


=== PipeWire workaround ===
=== PipeWire workaround ===
PipeWire typically runs as a normal user, while MPD will run under a system user. A workaround is to configure MPD to run under the same user as PipeWire:
PipeWire typically runs as a normal user, while a system-wide MPD instance will run under a system user. A workaround is to configure MPD to run under the same user as PipeWire:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">