at 23.11-beta 2.8 kB view raw
1{ config, lib, pkgs, ... }: 2 3with pkgs; 4with lib; 5 6let 7 uid = config.ids.uids.mopidy; 8 gid = config.ids.gids.mopidy; 9 cfg = config.services.mopidy; 10 11 mopidyConf = writeText "mopidy.conf" cfg.configuration; 12 13 mopidyEnv = buildEnv { 14 name = "mopidy-with-extensions-${mopidy.version}"; 15 paths = closePropagation cfg.extensionPackages; 16 pathsToLink = [ "/${mopidyPackages.python.sitePackages}" ]; 17 nativeBuildInputs = [ makeWrapper ]; 18 postBuild = '' 19 makeWrapper ${mopidy}/bin/mopidy $out/bin/mopidy \ 20 --prefix PYTHONPATH : $out/${mopidyPackages.python.sitePackages} 21 ''; 22 }; 23in { 24 25 options = { 26 27 services.mopidy = { 28 29 enable = mkEnableOption (lib.mdDoc "Mopidy, a music player daemon"); 30 31 dataDir = mkOption { 32 default = "/var/lib/mopidy"; 33 type = types.str; 34 description = lib.mdDoc '' 35 The directory where Mopidy stores its state. 36 ''; 37 }; 38 39 extensionPackages = mkOption { 40 default = []; 41 type = types.listOf types.package; 42 example = literalExpression "[ pkgs.mopidy-spotify ]"; 43 description = lib.mdDoc '' 44 Mopidy extensions that should be loaded by the service. 45 ''; 46 }; 47 48 configuration = mkOption { 49 default = ""; 50 type = types.lines; 51 description = lib.mdDoc '' 52 The configuration that Mopidy should use. 53 ''; 54 }; 55 56 extraConfigFiles = mkOption { 57 default = []; 58 type = types.listOf types.str; 59 description = lib.mdDoc '' 60 Extra config file read by Mopidy when the service starts. 61 Later files in the list overrides earlier configuration. 62 ''; 63 }; 64 65 }; 66 67 }; 68 69 ###### implementation 70 71 config = mkIf cfg.enable { 72 73 systemd.tmpfiles.rules = [ 74 "d '${cfg.dataDir}' - mopidy mopidy - -" 75 ]; 76 77 systemd.services.mopidy = { 78 wantedBy = [ "multi-user.target" ]; 79 after = [ "network.target" "sound.target" ]; 80 description = "mopidy music player daemon"; 81 serviceConfig = { 82 ExecStart = "${mopidyEnv}/bin/mopidy --config ${concatStringsSep ":" ([mopidyConf] ++ cfg.extraConfigFiles)}"; 83 User = "mopidy"; 84 }; 85 }; 86 87 systemd.services.mopidy-scan = { 88 description = "mopidy local files scanner"; 89 serviceConfig = { 90 ExecStart = "${mopidyEnv}/bin/mopidy --config ${concatStringsSep ":" ([mopidyConf] ++ cfg.extraConfigFiles)} local scan"; 91 User = "mopidy"; 92 Type = "oneshot"; 93 }; 94 }; 95 96 users.users.mopidy = { 97 inherit uid; 98 group = "mopidy"; 99 extraGroups = [ "audio" ]; 100 description = "Mopidy daemon user"; 101 home = cfg.dataDir; 102 }; 103 104 users.groups.mopidy.gid = gid; 105 106 }; 107 108}