Skip to content

Commit

Permalink
mpd: use XDG music dir if XDG user dirs are enabled
Browse files Browse the repository at this point in the history
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 nix-community#3225
  • Loading branch information
rycee committed Sep 16, 2022
1 parent b0247ce commit 5427f3d
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 6 deletions.
5 changes: 4 additions & 1 deletion docs/release-notes/rl-2211.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
23 changes: 21 additions & 2 deletions modules/services/mpd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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.
'';
};

Expand Down Expand Up @@ -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" ];
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/modules/services/mpd/basic-configuration.conf
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
7 changes: 6 additions & 1 deletion tests/modules/services/mpd/basic-configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = { };

Expand Down
24 changes: 24 additions & 0 deletions tests/modules/services/mpd/before-state-version-22_11.nix
Original file line number Diff line number Diff line change
@@ -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"'
'';
}
6 changes: 5 additions & 1 deletion tests/modules/services/mpd/default.nix
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 11 additions & 0 deletions tests/modules/services/mpd/xdg-music-dir.conf
Original file line number Diff line number Diff line change
@@ -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"



22 changes: 22 additions & 0 deletions tests/modules/services/mpd/xdg-music-dir.nix
Original file line number Diff line number Diff line change
@@ -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}
'';
}
2 changes: 2 additions & 0 deletions tests/modules/services/mpdris2/basic-configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
multimediaKeys = true;
};

services.mpd.musicDirectory = "/home/hm-user/music";

test.stubs.mpdris2 = { };

nmt.script = ''
Expand Down
2 changes: 2 additions & 0 deletions tests/modules/services/mpdris2/with-password.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
};
};

services.mpd.musicDirectory = "/home/hm-user/music";

test.stubs.mpdris2 = { };

nmt.script = ''
Expand Down

0 comments on commit 5427f3d

Please sign in to comment.