Can't Get Mpd to work with Ncmpcpp and Pipewire

Here’s my current mpd config:

    environment.systemPackages = with pkgs; [
      mpc-cli
    ];
    services.mpd = {
      enable = true;
      musicDirectory = "/home/${username}/Music";
      user = username; #string containing my username"
      extraConfig = ''
        audio_output {
          type "pipewire"
          name "pipewire boi"
        }
      '';

      # Optional:
      network.listenAddress = "any"; # if you want to allow non-localhost connections
    };

ncmpcpp --version returns:

ncmpcpp 0.9.2

optional screens compiled-in:
 - tag editor
 - tiny tag editor
 - outputs
 - visualizer
 - clock

encoding detection: enabled
built with support for: fftw ncurses taglib

systemctl status --user pipewire returns:

● pipewire.service - PipeWire Multimedia Service
     Loaded: loaded (/etc/systemd/user/pipewire.service; linked-runtime; preset: enabled)
    Drop-In: /nix/store/qlsj8h3w4b0b1lh3rfbkl8j8rqr738jp-user-units/pipewire.service.d
             └─overrides.conf
     Active: active (running) since Tue 2022-11-29 20:36:46 EST; 54s ago
TriggeredBy: ● pipewire.socket
   Main PID: 30073 (pipewire)
      Tasks: 5 (limit: 19127)
     Memory: 7.3M
        CPU: 64ms
     CGroup: /user.slice/user-1000.slice/user@1000.service/session.slice/pipewire.service
             └─30073 /nix/store/vw9d67i7d2s1l7r77w3f2pdiack62ww2-pipewire-0.3.59/bin/pipewire

Nov 29 20:36:46 Entertainer systemd[1904]: Started PipeWire Multimedia Service.
Nov 29 20:36:46 Entertainer pipewire[30073]: mod.rt: Can't find xdg-portal: (null)
Nov 29 20:36:46 Entertainer pipewire[30073]: mod.rt: found session bus but no portal
Nov 29 20:36:46 Entertainer pipewire[30073]: [0:29:44.403635571] [30073]  INFO IPAManager ipa_manager.cpp:143 libcamera is not installed. Adding '/nix/store/src/ipa' to the IPA search path
Nov 29 20:36:46 Entertainer pipewire[30073]: [0:29:44.403802139] [30073]  INFO Camera camera_manager.cpp:293 libcamera v0.0.1

ncmpcpp sees all my music, but when I add music to the playlist and try to unpause nothing happens.

running mpc after that returns this:

[paused]  #1/1   0:00/2:50 (0%)
volume:100%   repeat: off   random: off   single: off   consume: off
ERROR: Failed to open "pipewire boi" (pipewire); Failed to connect stream: Host is down

But clearly it isn’t down, because the service is running.

mpd is running as a system service, not a user service, so it doesn’t have any access to user session environment variables, and thus can’t find the pipewire instance. That’s my guess at least.

I run mpd from home-manager as a user service instead, so it has access to my pulseaudio instance. In the past I also got around this by opening up the pulseaudio server to tcp connections from localhost, then having mpd connect to it that way.

1 Like

Doesn’t this make it a user service?

No, that runs it as the user but not as a user service.

How would I run it as a user service? Would I Have to write my own service with the mpd package?

edit: Nevermind, home manager exists

Yes see systemd.user.services options

@Sandro @BattleCh1cken Could you please elaborate on how you solved it?

I set up mpd according to the article in the Nixos wiki like this:

  services.mpd = {
    enable = true;
    user = "sperber";
    musicDirectory = "/home/sperber/Speicher/Audio/Musik";
    extraConfig = ''
      audio_output {
        type "pipewire"
        name "My PipeWire Output"
      }
    '';
    startWhenNeeded =
      true; # systemd feature: only start MPD service upon connection to its socket
  };
  systemd.services.mpd.environment = {
    # https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/609
    XDG_RUNTIME_DIR =
      "/run/user/1000"; # User-id 1000 must match above user. MPD will look inside this directory for the PipeWire socket.
  };

but when i run ncmpcpp it shows that it connected successfully and shows no errors, but it doesn’t show me audio files, although i set the correct musicDirectory.

Or can’t mpd look into subfolders? My music is organized in folders in the directory that i’ve defined as musicDirectory.

I also set the musicDirectory in ncmpcpp config. Is that even necessary or do i have to define it only once, either in mpd oder ncmpcpp?