commit 11bb48b5d89b88f9a6e3be04e036ca94df219c8f
parent 582a67c1b83a07747c8a7f5b2094437d14d31e79
Author: mtmn <miro@haravara.org>
Date: Sat, 11 Apr 2026 03:52:20 +0200
feat: remote listenbrainz-mpd
Diffstat:
3 files changed, 32 insertions(+), 49 deletions(-)
diff --git a/hosts/nixaran/configuration.nix b/hosts/nixaran/configuration.nix
Binary files differ.
diff --git a/modules/services/listenbrainz-mpd.nix b/modules/services/listenbrainz-mpd.nix
@@ -5,18 +5,22 @@
...
}: let
cfg = config.services.haravara.listenbrainz-mpd;
+ configFile = pkgs.writeText "listenbrainz-mpd.toml" ''
+ [submission]
+ api_url = "${cfg.apiUrl}"
+ genres_as_folksonomy = ${lib.boolToString cfg.genresAsFolksonomy}
+
+ [mpd]
+ address = "${cfg.mpdAddress}"
+ password = "${cfg.mpdPassword}"
+ '';
in {
options.services.haravara.listenbrainz-mpd = {
enable = lib.mkEnableOption "ListenBrainz MPD client";
tokenFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
- description = "Path to the file containing the ListenBrainz token";
- };
- token = lib.mkOption {
- type = lib.types.nullOr lib.types.str;
- default = null;
- description = "The ListenBrainz token (unsecure, prefer tokenFile)";
+ description = "Path to the secret file containing the ListenBrainz token";
};
apiUrl = lib.mkOption {
type = lib.types.str;
@@ -34,18 +38,14 @@ in {
type = lib.types.str;
default = "";
};
+ mpdPort = lib.mkOption {
+ type = lib.types.port;
+ default = 37281;
+ };
mpdHostFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
- description = "Path to the file containing the MPD host";
- };
- mpdHostEnv = lib.mkOption {
- type = lib.types.nullOr lib.types.str;
- default = null;
- };
- mpdPortEnv = lib.mkOption {
- type = lib.types.nullOr lib.types.int;
- default = null;
+ description = "Path to the secret file containing MPD_HOST";
};
};
@@ -55,46 +55,29 @@ in {
after = ["network.target"];
wantedBy = ["multi-user.target"];
+ environment = {
+ MPD_PORT = toString cfg.mpdPort;
+ XDG_DATA_HOME = "/var/lib/listenbrainz-mpd";
+ };
+
serviceConfig = {
- ExecStartPre = pkgs.writeShellScript "listenbrainz-mpd-setup" ''
- mkdir -p "$RUNTIME_DIRECTORY/config"
- TOKEN=$([ -n "${toString (cfg.tokenFile != null)}" ] && cat "${toString cfg.tokenFile}" || echo "${toString cfg.token}")
- cat > "$RUNTIME_DIRECTORY/config/config.toml" <<EOF
- [submission]
- token = "$TOKEN"
- api_url = "${cfg.apiUrl}"
- genres_as_folksonomy = ${lib.boolToString cfg.genresAsFolksonomy}
+ LoadCredential =
+ (lib.optional (cfg.mpdHostFile != null) "mpd_host:${toString cfg.mpdHostFile}")
+ ++ (lib.optional (cfg.tokenFile != null) "token:${toString cfg.tokenFile}");
- [mpd]
- address = "${cfg.mpdAddress}"
- password = "${cfg.mpdPassword}"
- EOF
+ ExecStart = pkgs.writeShellScript "listenbrainz-mpd-start" ''
+ if [ -f "$CREDENTIALS_DIRECTORY/mpd_host" ]; then
+ export MPD_HOST=$(cat "$CREDENTIALS_DIRECTORY/mpd_host")
+ fi
+ if [ -f "$CREDENTIALS_DIRECTORY/token" ]; then
+ export LISTENBRAINZ_TOKEN=$(cat "$CREDENTIALS_DIRECTORY/token")
+ fi
+ exec ${pkgs.listenbrainz-mpd}/bin/listenbrainz-mpd --config ${configFile}
'';
Restart = "always";
DynamicUser = true;
- RuntimeDirectory = "listenbrainz-mpd";
+ StateDirectory = "listenbrainz-mpd";
};
-
- environment =
- lib.optionalAttrs (cfg.mpdHostEnv != null || cfg.mpdHostFile != null) {
- # We'll set this in ExecStart wrapper if it's a file, but let's see if we can use environment variables
- }
- // lib.optionalAttrs (cfg.mpdPortEnv != null) {
- MPD_PORT = toString cfg.mpdPortEnv;
- }
- // {
- XDG_CONFIG_HOME = "/run/listenbrainz-mpd/config";
- };
};
-
- # Update ExecStart to handle MPD_HOST from file if provided
- systemd.services.listenbrainz-mpd.serviceConfig.ExecStart = lib.mkForce (pkgs.writeShellScript "listenbrainz-mpd-start" ''
- if [ -n "${toString (cfg.mpdHostFile != null)}" ]; then
- export MPD_HOST=$(cat "${toString cfg.mpdHostFile}")
- elif [ -n "${toString (cfg.mpdHostEnv != null)}" ]; then
- export MPD_HOST="${toString cfg.mpdHostEnv}"
- fi
- exec ${pkgs.listenbrainz-mpd}/bin/listenbrainz-mpd
- '');
};
}
diff --git a/secrets/mpd-port.age b/secrets/mpd-port.age
Binary files differ.