mifasol.nix (2850B)
1 { 2 config, 3 lib, 4 pkgs, 5 haravara, 6 ... 7 }: let 8 cfg = config.services.haravara.mifasol; 9 mifasol = pkgs.callPackage ../../lib/mifasol.nix {}; 10 in { 11 options.services.haravara.mifasol = { 12 enable = lib.mkEnableOption "mifasol music streaming server"; 13 14 port = lib.mkOption { 15 type = lib.types.port; 16 default = 6620; 17 }; 18 19 domain = lib.mkOption { 20 type = lib.types.str; 21 description = "Public domain for the mifasol instance"; 22 }; 23 24 dataDir = lib.mkOption { 25 type = lib.types.str; 26 default = "/var/lib/haravara/mifasol"; 27 }; 28 29 songsDir = lib.mkOption { 30 type = lib.types.nullOr lib.types.str; 31 default = null; 32 example = "/mnt/meepo/mifasol"; 33 }; 34 35 uid = lib.mkOption { 36 type = lib.types.int; 37 default = 991; 38 description = "uid for the mifasol user"; 39 }; 40 41 gid = lib.mkOption { 42 type = lib.types.int; 43 default = 991; 44 description = "gid for the mifasol group"; 45 }; 46 47 authLabel = haravara.authLabelOption; 48 }; 49 50 config = lib.mkIf cfg.enable { 51 systemd.services.mifasol = { 52 description = "mifasol music server"; 53 wantedBy = ["multi-user.target"]; 54 after = ["network.target"]; 55 56 unitConfig = lib.mkIf (cfg.songsDir != null) { 57 RequiresMountsFor = cfg.songsDir; 58 }; 59 60 serviceConfig = { 61 User = "mifasol"; 62 Group = "mifasol"; 63 ExecStartPre = '' 64 ${lib.getExe mifasol} -c ${cfg.dataDir} config \ 65 -disable-ssl -n ${toString cfg.port} -hostnames ${cfg.domain} 66 ''; 67 ExecStart = "${lib.getExe mifasol} -c ${cfg.dataDir} run"; 68 Restart = "on-failure"; 69 70 NoNewPrivileges = true; 71 ProtectSystem = "strict"; 72 ProtectHome = true; 73 PrivateTmp = true; 74 ReadWritePaths = [cfg.dataDir] ++ lib.optional (cfg.songsDir != null) cfg.songsDir; 75 76 BindPaths = lib.mkIf (cfg.songsDir != null) ["${cfg.songsDir}:${cfg.dataDir}/data"]; 77 }; 78 }; 79 80 systemd.tmpfiles.rules = 81 [ 82 "d ${cfg.dataDir} 0750 mifasol mifasol -" 83 ] 84 ++ lib.optional (cfg.songsDir != null) "d ${cfg.songsDir} 0750 mifasol mifasol -"; 85 86 users = { 87 users.mifasol = { 88 isSystemUser = true; 89 group = "mifasol"; 90 home = cfg.dataDir; 91 inherit (cfg) uid; 92 }; 93 groups.mifasol = {inherit (cfg) gid;}; 94 }; 95 96 services.caddy.virtualHosts."${cfg.domain}".extraConfig = 97 if cfg.authLabel == null 98 then '' 99 reverse_proxy 127.0.0.1:${toString cfg.port} 100 '' 101 else '' 102 @app not path /api/v1* /isalive 103 handle @app { 104 ${haravara.mkAuthImport cfg.authLabel} 105 reverse_proxy 127.0.0.1:${toString cfg.port} 106 } 107 handle { 108 reverse_proxy 127.0.0.1:${toString cfg.port} 109 } 110 ''; 111 }; 112 }