mpd: use XDG music dir if XDG user dirs are enabled

If the user has enabled the XDG user directories module then we can
use the XDG music directory in the MPD module. Otherwise we'll leave
the option undefined so that the user is forced to define the
directory to use.

This applies to state version 22.11 and above.

Fixes #3225
This commit is contained in:
Robert Helgesson 2022-09-15 22:19:00 +02:00
parent b0247ceedc
commit 5427f3d1f0
No known key found for this signature in database
GPG Key ID: 36BDAA14C2797E89
10 changed files with 98 additions and 6 deletions

View File

@ -93,4 +93,7 @@ release 20.03. Use `services.picom` instead.
The state version in this release includes the changes below.
These changes are only active if the `home.stateVersion` option is set to "22.11" or later.
* No changes.
* The <<opt-services.mpd.musicDirectory>> option now defaults to the
value of <<opt-xdg.userDirs.music>> if <<opt-xdg.userDirs.enable>> is
enabled. Otherwise it is undefined and must be specified in the user
configuration.

View File

@ -52,11 +52,18 @@ in {
musicDirectory = mkOption {
type = with types; either path str;
default = "${config.home.homeDirectory}/music";
defaultText = "$HOME/music";
defaultText = literalExpression ''
''${home.homeDirectory}/music if state version < 22.11
''${xdg.userDirs.music} if xdg.userDirs.enable == true
undefined otherwise
'';
apply = toString; # Prevent copies to Nix store.
description = ''
The directory where mpd reads music from.
</para><para>
If <xref linkend="opt-xdg.userDirs.enable"/> is
<literal>true</literal> then the defined XDG music directory is used.
Otherwise, you must explicitly specify a value.
'';
};
@ -146,6 +153,17 @@ in {
(lib.hm.assertions.assertPlatform "services.mpd" pkgs lib.platforms.linux)
];
services.mpd = mkMerge [
(mkIf (versionAtLeast config.home.stateVersion "22.11"
&& config.xdg.userDirs.enable) {
musicDirectory = mkOptionDefault config.xdg.userDirs.music;
})
(mkIf (versionOlder config.home.stateVersion "22.11") {
musicDirectory = mkOptionDefault "${config.home.homeDirectory}/music";
})
];
systemd.user.services.mpd = {
Unit = {
After = [ "network.target" "sound.target" ];
@ -164,6 +182,7 @@ in {
${pkgs.bash}/bin/bash -c "${pkgs.coreutils}/bin/mkdir -p '${cfg.dataDir}' '${cfg.playlistDirectory}'"'';
};
};
systemd.user.sockets.mpd = mkIf cfg.network.startWhenNeeded {
Socket = {
ListenStream = let

View File

@ -1,4 +1,4 @@
music_directory "/home/hm-user/music"
music_directory "/my/music/dir"
playlist_directory "/home/hm-user/.local/share/mpd/playlists"
db_file "/home/hm-user/.local/share/mpd/tag_cache"

View File

@ -3,7 +3,12 @@
with lib;
{
services.mpd.enable = true;
services.mpd = {
enable = true;
musicDirectory = "/my/music/dir";
};
home.stateVersion = "22.11";
test.stubs.mpd = { };

View File

@ -0,0 +1,24 @@
{ config, lib, pkgs, ... }:
with lib;
{
services.mpd.enable = true;
home.stateVersion = "18.09";
test.stubs.mpd = { };
nmt.script = ''
serviceFile=$(normalizeStorePaths home-files/.config/systemd/user/mpd.service)
assertFileContent "$serviceFile" ${./basic-configuration.service}
confFile=$(grep -o \
'/nix/store/.*-mpd.conf' \
$TESTED/home-files/.config/systemd/user/mpd.service)
assertFileContains \
"$confFile" \
'music_directory "/home/hm-user/music"'
'';
}

View File

@ -1 +1,5 @@
{ mpd-basic-configuration = ./basic-configuration.nix; }
{
mpd-basic-configuration = ./basic-configuration.nix;
mpd-before-state-version-22_11 = ./before-state-version-22_11.nix;
mpd-xdg-music-dir = ./xdg-music-dir.nix;
}

View File

@ -0,0 +1,11 @@
music_directory "/home/hm-user/Music"
playlist_directory "/home/hm-user/.local/share/mpd/playlists"
db_file "/home/hm-user/.local/share/mpd/tag_cache"
state_file "/home/hm-user/.local/share/mpd/state"
sticker_file "/home/hm-user/.local/share/mpd/sticker.sql"
bind_to_address "127.0.0.1"

View File

@ -0,0 +1,22 @@
{ config, lib, pkgs, ... }:
with lib;
{
services.mpd.enable = true;
xdg.userDirs.enable = true;
home.stateVersion = "22.11";
test.stubs.mpd = { };
nmt.script = ''
serviceFile=$(normalizeStorePaths home-files/.config/systemd/user/mpd.service)
assertFileContent "$serviceFile" ${./basic-configuration.service}
confFile=$(grep -o \
'/nix/store/.*-mpd.conf' \
$TESTED/home-files/.config/systemd/user/mpd.service)
assertFileContent "$confFile" ${./xdg-music-dir.conf}
'';
}

View File

@ -7,6 +7,8 @@
multimediaKeys = true;
};
services.mpd.musicDirectory = "/home/hm-user/music";
test.stubs.mpdris2 = { };
nmt.script = ''

View File

@ -10,6 +10,8 @@
};
};
services.mpd.musicDirectory = "/home/hm-user/music";
test.stubs.mpdris2 = { };
nmt.script = ''