Run mpd server (user service? system service?)

I’d like to set up a Music Player Daemon service that I can access from machines on my home network. Specifically, I’m planning on running ncmpcpp clients on a NixOS box and on a mac. I’ve gotten somewhat familiar with configuring simple systemd services, but I’m new to mpd.

I imagine ultimately I’d run the server on some dedicated hardware, maybe a raspberry pi. But as a first stab, I thought I’d try to set it up as a systemwide service on the NixOS machine itself. I added the following to my configuration.nix

services.mpd = { enable = true; startWhenNeeded = true; };

With this in place, the nixos mpd module creates an mpd user whose HOME is /var/lib/mpd, and configures the service to run as that user with music_directory set to /var/lib/mpd/music.

If I start a client and check the server logs (journalctl -u mpd), I see it complaining that /var/lib/mpd/music doesn’t exist. I suppose I could address this (and similar) errors by creating the missing directories and moving my music into that location, but it has me wondering if I’m going about it wrong.

I understand mpd can be run as a user service rather than a system service. I haven’t used systemd user services before, so I’m looking at systemd/User - ArchWiki to get familiar with the concept.

Does anyone have examples I could reference to better understand how to run a systemd user service under NixOS? Or is there a totally different tack you’d suggest?

I don’t have experience with running mpd on NixOS, but have you checked all options that the NixOS mpd module supports? See NixOS Search - Loading...

You’ll see the music directory option in there as it well. Just make sure th mpd user has permission the music directory.

Systemd user services are for “run this while I’m logged in”. Daemons that should be available all the time are system services.

I’ve got mpd running just fine, but i’m not using the startWhenNeeded.
You can find the exact implementation of the services.mpd module here:

You can see in the module that the music directory isn’t created automatically, you have to do that yourself.
And of course you also have to add your music in there.
If you decide to point services.mpd.musicDirectory to somewhere else, you have to take care of the permissions, so make sure that the mpd user and group (default: mpd:mpd) are able to access your directory. Best is to run “su -s /run/current-system/sw/bin/bash - mpd”, cd into your music directory and “cat *.mp3 > /dev/null” to check the permissions.
Once that’s clean, you should have no issues.