{ config , options , lib , pkgs , mpdlisten , ... }: with lib; { options.services.mpdlisten = { enable = mkEnableOption "mpdlisten"; package = mkPackageOption pkgs "mpdlisten" { }; mpd = { port = mkOption { default = "6600"; type = types.str; description = lib.mdDoc '' Host for the mpd daemon to connect to. ''; }; host = mkOption { default = "localhost"; type = types.str; description = lib.mdDoc '' Host for the mpd daemon to connect to. ''; }; }; listenbrainz = { tokenFile = mkOption { default = null; type = types.nullOr types.path; description = '' File containing token to use for connecting to listenbrainz service. ''; }; username = mkOption { default = null; type = types.nullOr types.str; description = '' Username for listenbrainz. Used for playlist retrieval. ''; }; fetchExploration = mkOption { default = false; type = types.bool; description = '' If set to true will add listenbrainz weekly exploration as playlist to mpd. ''; }; fetchJams = mkOption { default = false; type = types.bool; description = '' If set to true will add listenbrainz weekly jam as playlist to mpd. ''; }; syncLoved = mkOption { default = false; type = types.bool; description = '' If set to true will create playlist Loved based on loved song in mpd. ''; }; }; }; config = let cfg = config.services.mpdlisten; localMpd = cfg.mpd.host == "localhost" || cfg.mpd.host == "127.0.0.1"; mpdlistenConfig = lib.generators.toINI { } { mpd = { address = cfg.mpd.host; inherit (cfg.mpd) port; }; listenbrainz = with cfg.listenbrainz; { inherit tokenFile; inherit username; fetch_jams = fetchJams; fetch_exploration = fetchExploration; sync_loved = syncLoved; }; }; mpdlistenConfigFile = pkgs.writeText "mpdlistenconfig.ini" mpdlistenConfig; in mkIf cfg.enable { assertions = [ { assertion = cfg.listenbrainz.tokenFile != null; message = "You need to specify listenbrainz token file"; } { assertion = !cfg.listenbrainz.fetchJams || cfg.listenbrainz.username != null; message = "You need to specify username if you want to fetch weekly jams!"; } { assertion = !cfg.listenbrainz.fetchExploration || cfg.listenbrainz.username != null; message = "You need to specify username if you want to fetch weekly explorations!"; } { assertion = !cfg.listenbrainz.syncLoved || cfg.listenbrainz.username != null; message = "You need to specify username if you want to sync loved songs!"; } ]; systemd.services.mpdlisten = { after = [ "network.target" ] ++ (optional localMpd "mpd.service"); description = "mpdlisten mpd scrobble client"; wantedBy = [ "multi-user.target" ]; serviceConfig = { DynamicUser = true; StateDirectory = "mpdlisten"; RuntimeDirectory = "mpdlisten"; RuntimeDirectoryMode = "700"; LoadCredential = "token:${cfg.listenbrainz.tokenFile}"; ExecStart = "${mpdlisten.packages.x86_64-linux.mpdlisten}/bin/mpdlisten -c ${mpdlistenConfigFile}"; }; }; }; }