Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mpdscribble: add module #6259

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions modules/lib/maintainers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@
github = "mifom";
githubId = 23462908;
};
msyds = {
name = "Madeleine Sydney Ślaga";
email = "[email protected]";
github = "msyds";
githubId = 65362461;
};
nikp123 = {
name = "nikp123";
email = "[email protected]";
Expand Down
11 changes: 11 additions & 0 deletions modules/misc/news.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1946,6 +1946,17 @@ in {
speed, features, or native UIs. Ghostty provides all three.
'';
}

{
time = "2025-01-02T11:21:19+00:00";
message = ''
A new module is available: 'services.mpdscribble'.

A MPD client which submits information about tracks being played to a
scrobbler (e.g. last.fm)
'';
}

{
time = "2025-01-04T15:00:00+00:00";
condition = hostPlatform.isLinux;
Expand Down
1 change: 1 addition & 0 deletions modules/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ let
./services/mopidy.nix
./services/mpd.nix
./services/mpdris2.nix
./services/mpdscribble.nix
./services/mpd-discord-rpc.nix
./services/mpd-mpris.nix
./services/mpris-proxy.nix
Expand Down
219 changes: 219 additions & 0 deletions modules/services/mpdscribble.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
{ config, lib, options, pkgs, ... }:

with lib;

let
cfg = config.services.mpdscribble;
mpdCfg = config.services.mpd;
mpdOpt = options.services.mpd;

endpointUrls = {
"last.fm" = "http://post.audioscrobbler.com";
"libre.fm" = "http://turtle.libre.fm";
"jamendo" = "http://postaudioscrobbler.jamendo.com";
"listenbrainz" = "http://proxy.listenbrainz.org";
};

mkSection = secname: secCfg: ''
[${secname}]
url = ${secCfg.url}
username = ${secCfg.username}
password = {{${secname}_PASSWORD}}
journal = /var/lib/mpdscribble/${secname}.journal
'';

endpoints = concatStringsSep "\n" (mapAttrsToList mkSection cfg.endpoints);
cfgTemplate = pkgs.writeText "mpdscribble.conf" ''
## This file was automatically genenrated by home-manager and will be
## overwritten. Do not edit. Edit your home-manager configuration instead.

## mpdscribble - an audioscrobbler for the Music Player Daemon.
## http://mpd.wikia.com/wiki/Client:mpdscribble

# HTTP proxy URL.
${optionalString (cfg.proxy != null) "proxy = ${cfg.proxy}"}

# The location of the mpdscribble log file. The special value
# "syslog" makes mpdscribble use the local syslog daemon. On most
# systems, log messages will appear in /var/log/daemon.log then.
# "-" means log to stderr (the current terminal).
log = -

# How verbose mpdscribble's logging should be. Default is 1.
verbose = ${toString cfg.verbose}

# How often should mpdscribble save the journal file? [seconds]
journal_interval = ${toString cfg.journalInterval}

# The host running MPD, possibly protected by a password
# ([PASSWORD@]HOSTNAME).
host = ${
(optionalString (cfg.passwordFile != null) "{{MPD_PASSWORD}}@") + cfg.host
}

# The port that the MPD listens on and mpdscribble should try to
# connect to.
port = ${toString cfg.port}

${endpoints}
'';

configFile =
"\${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/mpdscribble/mpdscribble.conf";

replaceSecret = secretFile: placeholder: targetFile:
optionalString (secretFile != null) ''
${pkgs.replace-secret}/bin/replace-secret '${placeholder}' '${secretFile}' "${targetFile}" '';

preStart = pkgs.writeShellApplication {
name = "mpdscribble-pre-start";
runtimeInputs = [ pkgs.replace-secret pkgs.coreutils ];
text = ''
configFile="${configFile}"
mkdir -p "$(dirname "$configFile")"
cp --no-preserve=mode,ownership -f "${cfgTemplate}" "$configFile"
${replaceSecret cfg.passwordFile "{{MPD_PASSWORD}}" "$configFile"}
${concatStringsSep "\n" (mapAttrsToList (secname: cfg:
replaceSecret cfg.passwordFile "{{${secname}_PASSWORD}}" "$configFile")
cfg.endpoints)}
'';
};

start = pkgs.writeShellScript "mpdscribble-start" ''
configFile="${configFile}"
exec ${pkgs.mpdscribble}/bin/mpdscribble --no-daemon --conf "$configFile"
'';

localMpd = (cfg.host == "localhost" || cfg.host == "127.0.0.1");

in {
###### interface

options.services.mpdscribble = {

enable = mkEnableOption ''
mpdscribble, an MPD client which submits info about tracks being played to
Last.fm (formerly AudioScrobbler)
'';

proxy = mkOption {
default = null;
type = types.nullOr types.str;
description = ''
HTTP proxy URL.
'';
};

verbose = mkOption {
default = 1;
type = types.int;
description = ''
Log level for the mpdscribble daemon.
'';
};

journalInterval = mkOption {
default = 600;
example = 60;
type = types.int;
description = ''
How often should mpdscribble save the journal file? [seconds]
'';
};

host = mkOption {
default = (if mpdCfg.network.listenAddress != "any" then
mpdCfg.network.listenAddress
else
"localhost");
defaultText = literalExpression ''
if config.${mpdOpt.network.listenAddress} != "any"
then config.${mpdOpt.network.listenAddress}
else "localhost"
'';
type = types.str;
description = ''
Host for the mpdscribble daemon to search for a mpd daemon on.
'';
};

passwordFile = mkOption {
default = null;
type = types.nullOr types.str;
description = ''
File containing the password for the mpd daemon.
'';
};

port = mkOption {
default = mpdCfg.network.port;
defaultText = literalExpression "config.${mpdOpt.network.port}";
type = types.port;
description = ''
Port for the mpdscribble daemon to search for a mpd daemon on.
'';
};

endpoints = mkOption {
type = (let
endpoint = { name, ... }: {
options = {
url = mkOption {
type = types.str;
default = endpointUrls.${name} or "";
description =
"The url endpoint where the scrobble API is listening.";
};
username = mkOption {
type = types.str;
description = ''
Username for the scrobble service.
'';
};
passwordFile = mkOption {
type = types.nullOr types.str;
description =
"File containing the password, either as MD5SUM or cleartext.";
};
};
};
in types.attrsOf (types.submodule endpoint));
default = { };
example = {
"last.fm" = {
username = "foo";
passwordFile = "/run/secrets/lastfm_password";
};
};
description = ''
Endpoints to scrobble to.
If the endpoint is one of "${
concatStringsSep ''", "'' (attrNames endpointUrls)
}" the url is set automatically.
'';
};

};

###### implementation

config = mkIf cfg.enable {
systemd.user.services.mpdscribble = {
Unit = {
Description = "mpdscribble mpd scrobble client";
After = [ "network.target" ] ++ optional localMpd "mpd.service";
};
Install.WantedBy = [ "multi-user.target" ];
Service = {
StateDirectory = "mpdscribble";
RuntimeDirectory = "mpdscribble";
RuntimeDirectoryMode = "700";
# TODO use LoadCredential= instead of running preStart with full privileges?
ExecStartPre = "+${preStart}/bin/mpdscribble-pre-start";
ExecStart = "${start}";
};
};
};

meta.maintainers = [ lib.hm.maintainers.msyds ];
}
1 change: 1 addition & 0 deletions tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ in import nmtSrc {
./modules/services/mpd
./modules/services/mpd-mpris
./modules/services/mpdris2
./modules/services/mpdscribble
./modules/services/nix-gc
./modules/services/osmscout-server
./modules/services/pantalaimon
Expand Down
28 changes: 28 additions & 0 deletions tests/modules/services/mpdscribble/basic-configuration.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{ config, lib, pkgs, ... }:

with lib;

{
services.mpdscribble = {
enable = true;
endpoints = {
"libre.fm" = {
username = "musicfan1992";
passwordFile = toString ./password-file;
};
"https://music.com/" = {
username = "musicHATER1000";
passwordFile = toString ./password-file;
};
};
};

home.stateVersion = "22.11";

test.stubs.mpd = { };

nmt.script = ''
serviceFile=$(normalizeStorePaths home-files/.config/systemd/user/mpdscribble.service)
assertFileContent "$serviceFile" ${./basic-configuration.service}
'';
}
14 changes: 14 additions & 0 deletions tests/modules/services/mpdscribble/basic-configuration.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Install]
WantedBy=multi-user.target

[Service]
ExecStart=/nix/store/00000000000000000000000000000000-mpdscribble-start
ExecStartPre=+/nix/store/00000000000000000000000000000000-mpdscribble-pre-start/bin/mpdscribble-pre-start
RuntimeDirectory=mpdscribble
RuntimeDirectoryMode=700
StateDirectory=mpdscribble

[Unit]
After=network.target
After=mpd.service
Description=mpdscribble mpd scrobble client
1 change: 1 addition & 0 deletions tests/modules/services/mpdscribble/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ mpdscribble-basic-configuration = ./basic-configuration.nix; }
Loading