nix

configuration files that power my machines
Log | Files | Refs | README | LICENSE

listenbrainz-mpd.nix (2959B)


      1 {
      2   config,
      3   lib,
      4   pkgs,
      5   ...
      6 }: let
      7   cfg = config.services.haravara.listenbrainz-mpd;
      8   configFile = pkgs.writeText "listenbrainz-mpd.toml" (''
      9       [submission]
     10       api_url = "${cfg.apiUrl}"
     11       genres_as_folksonomy = ${lib.boolToString cfg.genresAsFolksonomy}
     12 
     13       [mpd]
     14       address = "${cfg.mpdAddress}"
     15     ''
     16     + lib.optionalString (cfg.mpdPassword != "") ''
     17       password = "${cfg.mpdPassword}"
     18     '');
     19 in {
     20   options.services.haravara.listenbrainz-mpd = {
     21     enable = lib.mkEnableOption "ListenBrainz MPD client";
     22     tokenFile = lib.mkOption {
     23       type = lib.types.nullOr lib.types.path;
     24       default = null;
     25       description = "Path to the secret file containing the ListenBrainz token";
     26     };
     27     apiUrl = lib.mkOption {
     28       type = lib.types.str;
     29       default = "https://api.listenbrainz.org";
     30       description = "ListenBrainz API URL";
     31     };
     32     genresAsFolksonomy = lib.mkOption {
     33       type = lib.types.bool;
     34       default = false;
     35       description = "Submit genre tags as ListenBrainz folksonomy tags";
     36     };
     37     mpdAddress = lib.mkOption {
     38       type = lib.types.str;
     39       default = "localhost:6695";
     40       description = "MPD address to connect to";
     41     };
     42     mpdPassword = lib.mkOption {
     43       type = lib.types.str;
     44       default = "";
     45       description = "Password for MPD authentication";
     46     };
     47     mpdPort = lib.mkOption {
     48       type = lib.types.port;
     49       default = 37281;
     50       description = "MPD port";
     51     };
     52     mpdHostFile = lib.mkOption {
     53       type = lib.types.nullOr lib.types.path;
     54       default = null;
     55       description = "Path to the secret file containing MPD_HOST";
     56     };
     57     dataDir = lib.mkOption {
     58       type = lib.types.str;
     59       default = "/var/lib/listenbrainz-mpd";
     60       description = "Directory for ListenBrainz MPD state data";
     61     };
     62   };
     63 
     64   config = lib.mkIf cfg.enable {
     65     systemd.services.listenbrainz-mpd = {
     66       description = "ListenBrainz MPD client";
     67       after = ["network.target"];
     68       wantedBy = ["multi-user.target"];
     69 
     70       environment = {
     71         MPD_PORT = toString cfg.mpdPort;
     72         XDG_DATA_HOME = cfg.dataDir;
     73       };
     74 
     75       serviceConfig = {
     76         LoadCredential = lib.concatLists [
     77           (lib.optional (cfg.mpdHostFile != null) "mpd_host:${toString cfg.mpdHostFile}")
     78           (lib.optional (cfg.tokenFile != null) "token:${toString cfg.tokenFile}")
     79         ];
     80 
     81         ExecStart = pkgs.writeShellScript "listenbrainz-mpd-start" ''
     82           if [ -f "$CREDENTIALS_DIRECTORY/mpd_host" ]; then
     83             export MPD_HOST=$(cat "$CREDENTIALS_DIRECTORY/mpd_host")
     84           fi
     85           if [ -f "$CREDENTIALS_DIRECTORY/token" ]; then
     86             export LISTENBRAINZ_TOKEN=$(cat "$CREDENTIALS_DIRECTORY/token")
     87           fi
     88           exec ${pkgs.listenbrainz-mpd}/bin/listenbrainz-mpd --config ${configFile}
     89         '';
     90         Restart = "always";
     91         DynamicUser = true;
     92         StateDirectory = "listenbrainz-mpd";
     93       };
     94     };
     95   };
     96 }