From 5427f3d1f0ea4357cd4af0bffee7248d640c6ffc Mon Sep 17 00:00:00 2001 From: Robert Helgesson Date: Thu, 15 Sep 2022 22:19:00 +0200 Subject: [PATCH] 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 --- docs/release-notes/rl-2211.adoc | 5 +++- modules/services/mpd.nix | 23 ++++++++++++++++-- .../services/mpd/basic-configuration.conf | 2 +- .../services/mpd/basic-configuration.nix | 7 +++++- .../mpd/before-state-version-22_11.nix | 24 +++++++++++++++++++ tests/modules/services/mpd/default.nix | 6 ++++- tests/modules/services/mpd/xdg-music-dir.conf | 11 +++++++++ tests/modules/services/mpd/xdg-music-dir.nix | 22 +++++++++++++++++ .../services/mpdris2/basic-configuration.nix | 2 ++ .../services/mpdris2/with-password.nix | 2 ++ 10 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 tests/modules/services/mpd/before-state-version-22_11.nix create mode 100644 tests/modules/services/mpd/xdg-music-dir.conf create mode 100644 tests/modules/services/mpd/xdg-music-dir.nix diff --git a/docs/release-notes/rl-2211.adoc b/docs/release-notes/rl-2211.adoc index ed2fa55ce..dccace68c 100644 --- a/docs/release-notes/rl-2211.adoc +++ b/docs/release-notes/rl-2211.adoc @@ -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 <> option now defaults to the +value of <> if <> is +enabled. Otherwise it is undefined and must be specified in the user +configuration. diff --git a/modules/services/mpd.nix b/modules/services/mpd.nix index f71d96a44..ea0d1a978 100644 --- a/modules/services/mpd.nix +++ b/modules/services/mpd.nix @@ -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. + + If is + true 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 diff --git a/tests/modules/services/mpd/basic-configuration.conf b/tests/modules/services/mpd/basic-configuration.conf index 54d6059fa..59b3568b3 100644 --- a/tests/modules/services/mpd/basic-configuration.conf +++ b/tests/modules/services/mpd/basic-configuration.conf @@ -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" diff --git a/tests/modules/services/mpd/basic-configuration.nix b/tests/modules/services/mpd/basic-configuration.nix index c1f288980..6d6250d90 100644 --- a/tests/modules/services/mpd/basic-configuration.nix +++ b/tests/modules/services/mpd/basic-configuration.nix @@ -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 = { }; diff --git a/tests/modules/services/mpd/before-state-version-22_11.nix b/tests/modules/services/mpd/before-state-version-22_11.nix new file mode 100644 index 000000000..6e044b366 --- /dev/null +++ b/tests/modules/services/mpd/before-state-version-22_11.nix @@ -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"' + ''; +} diff --git a/tests/modules/services/mpd/default.nix b/tests/modules/services/mpd/default.nix index 22d1638e5..d2fd2a292 100644 --- a/tests/modules/services/mpd/default.nix +++ b/tests/modules/services/mpd/default.nix @@ -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; +} diff --git a/tests/modules/services/mpd/xdg-music-dir.conf b/tests/modules/services/mpd/xdg-music-dir.conf new file mode 100644 index 000000000..56fe71b49 --- /dev/null +++ b/tests/modules/services/mpd/xdg-music-dir.conf @@ -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" + + + diff --git a/tests/modules/services/mpd/xdg-music-dir.nix b/tests/modules/services/mpd/xdg-music-dir.nix new file mode 100644 index 000000000..775043c15 --- /dev/null +++ b/tests/modules/services/mpd/xdg-music-dir.nix @@ -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} + ''; +} diff --git a/tests/modules/services/mpdris2/basic-configuration.nix b/tests/modules/services/mpdris2/basic-configuration.nix index 5e809df39..b8c595be2 100644 --- a/tests/modules/services/mpdris2/basic-configuration.nix +++ b/tests/modules/services/mpdris2/basic-configuration.nix @@ -7,6 +7,8 @@ multimediaKeys = true; }; + services.mpd.musicDirectory = "/home/hm-user/music"; + test.stubs.mpdris2 = { }; nmt.script = '' diff --git a/tests/modules/services/mpdris2/with-password.nix b/tests/modules/services/mpdris2/with-password.nix index fcfe15315..af4ef33cc 100644 --- a/tests/modules/services/mpdris2/with-password.nix +++ b/tests/modules/services/mpdris2/with-password.nix @@ -10,6 +10,8 @@ }; }; + services.mpd.musicDirectory = "/home/hm-user/music"; + test.stubs.mpdris2 = { }; nmt.script = ''