nix

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

mpdscribble.nix (2573B)


      1 {
      2   config,
      3   lib,
      4   pkgs,
      5   ...
      6 }: let
      7   cfg = config.services.haravara.mpdscribble;
      8 in {
      9   options.services.haravara.mpdscribble = {
     10     enable = lib.mkEnableOption "mpdscribble";
     11     username = lib.mkOption {
     12       type = lib.types.str;
     13       default = "";
     14       description = "Username for the scrobbling service";
     15     };
     16     passwordFile = lib.mkOption {
     17       type = lib.types.nullOr lib.types.path;
     18       default = null;
     19       description = "Path to the secret file containing the password or MD5 hash";
     20     };
     21     serviceUrl = lib.mkOption {
     22       type = lib.types.str;
     23       default = "https://post.audioscrobbler.com/";
     24       description = "URL of the scrobbling service";
     25     };
     26     mpdHostFile = lib.mkOption {
     27       type = lib.types.nullOr lib.types.path;
     28       default = null;
     29       description = "Path to the secret file";
     30     };
     31     mpdPort = lib.mkOption {
     32       type = lib.types.port;
     33       default = 6600;
     34     };
     35   };
     36 
     37   config = lib.mkIf cfg.enable {
     38     systemd.services.mpdscribble = {
     39       description = "mpdscribble scrobbler";
     40       after = ["network.target"];
     41       wantedBy = ["multi-user.target"];
     42 
     43       environment = {
     44         XDG_DATA_HOME = "/var/lib/mpdscribble";
     45       };
     46 
     47       serviceConfig = {
     48         LoadCredential = lib.concatLists [
     49           (lib.optional (cfg.mpdHostFile != null) "mpd_host:${toString cfg.mpdHostFile}")
     50           (lib.optional (cfg.passwordFile != null) "password:${toString cfg.passwordFile}")
     51         ];
     52 
     53         ExecStart = pkgs.writeShellScript "mpdscribble-start" ''
     54                     CONF_FILE="$STATE_DIRECTORY/mpdscribble.conf"
     55 
     56                     MPD_HOST="localhost"
     57                     if [ -f "$CREDENTIALS_DIRECTORY/mpd_host" ]; then
     58                       MPD_HOST=$(cat "$CREDENTIALS_DIRECTORY/mpd_host")
     59                     fi
     60 
     61                     PASSWORD=""
     62                     if [ -f "$CREDENTIALS_DIRECTORY/password" ]; then
     63                       PASSWORD=$(cat "$CREDENTIALS_DIRECTORY/password")
     64                     fi
     65 
     66                     cat > "$CONF_FILE" <<EOF
     67           verbose = 2
     68           log = -
     69           host = $MPD_HOST
     70           port = ${toString cfg.mpdPort}
     71 
     72           [last.fm]
     73           url = ${cfg.serviceUrl}
     74           username = ${cfg.username}
     75           password = $PASSWORD
     76           journal = $STATE_DIRECTORY/lastfm.journal
     77           EOF
     78 
     79                     exec ${pkgs.mpdscribble}/bin/mpdscribble --no-daemon --conf "$CONF_FILE"
     80         '';
     81         Restart = "always";
     82         DynamicUser = true;
     83         StateDirectory = "mpdscribble";
     84       };
     85     };
     86   };
     87 }