1{
2 config,
3 lib,
4 options,
5 pkgs,
6 ...
7}:
8let
9 cfg = config.services.airsonic;
10 opt = options.services.airsonic;
11in
12{
13 options = {
14
15 services.airsonic = {
16 enable = lib.mkEnableOption "Airsonic, the Free and Open Source media streaming server (fork of Subsonic and Libresonic)";
17
18 user = lib.mkOption {
19 type = lib.types.str;
20 default = "airsonic";
21 description = "User account under which airsonic runs.";
22 };
23
24 home = lib.mkOption {
25 type = lib.types.path;
26 default = "/var/lib/airsonic";
27 description = ''
28 The directory where Airsonic will create files.
29 Make sure it is writable.
30 '';
31 };
32
33 virtualHost = lib.mkOption {
34 type = lib.types.nullOr lib.types.str;
35 default = null;
36 description = ''
37 Name of the nginx virtualhost to use and setup. If null, do not setup any virtualhost.
38 '';
39 };
40
41 listenAddress = lib.mkOption {
42 type = lib.types.str;
43 default = "127.0.0.1";
44 description = ''
45 The host name or IP address on which to bind Airsonic.
46 The default value is appropriate for first launch, when the
47 default credentials are easy to guess. It is also appropriate
48 if you intend to use the virtualhost option in the service
49 module. In other cases, you may want to change this to a
50 specific IP or 0.0.0.0 to listen on all interfaces.
51 '';
52 };
53
54 port = lib.mkOption {
55 type = lib.types.port;
56 default = 4040;
57 description = ''
58 The port on which Airsonic will listen for
59 incoming HTTP traffic. Set to 0 to disable.
60 '';
61 };
62
63 contextPath = lib.mkOption {
64 type = lib.types.path;
65 default = "/";
66 description = ''
67 The context path, i.e., the last part of the Airsonic
68 URL. Typically '/' or '/airsonic'. Default '/'
69 '';
70 };
71
72 maxMemory = lib.mkOption {
73 type = lib.types.int;
74 default = 100;
75 description = ''
76 The memory limit (max Java heap size) in megabytes.
77 Default: 100
78 '';
79 };
80
81 transcoders = lib.mkOption {
82 type = lib.types.listOf lib.types.path;
83 default = [ "${pkgs.ffmpeg.bin}/bin/ffmpeg" ];
84 defaultText = lib.literalExpression ''[ "''${pkgs.ffmpeg.bin}/bin/ffmpeg" ]'';
85 description = ''
86 List of paths to transcoder executables that should be accessible
87 from Airsonic. Symlinks will be created to each executable inside
88 ''${config.${opt.home}}/transcoders.
89 '';
90 };
91
92 jre = lib.mkPackageOption pkgs "jre8" {
93 extraDescription = ''
94 ::: {.note}
95 Airsonic only supports Java 8, airsonic-advanced requires at least
96 Java 11.
97 :::
98 '';
99 };
100
101 war = lib.mkOption {
102 type = lib.types.path;
103 default = "${pkgs.airsonic}/webapps/airsonic.war";
104 defaultText = lib.literalExpression ''"''${pkgs.airsonic}/webapps/airsonic.war"'';
105 description = "Airsonic war file to use.";
106 };
107
108 jvmOptions = lib.mkOption {
109 description = ''
110 Extra command line options for the JVM running AirSonic.
111 Useful for sending jukebox output to non-default alsa
112 devices.
113 '';
114 default = [
115 ];
116 type = lib.types.listOf lib.types.str;
117 example = [
118 "-Djavax.sound.sampled.Clip='#CODEC [plughw:1,0]'"
119 "-Djavax.sound.sampled.Port='#Port CODEC [hw:1]'"
120 "-Djavax.sound.sampled.SourceDataLine='#CODEC [plughw:1,0]'"
121 "-Djavax.sound.sampled.TargetDataLine='#CODEC [plughw:1,0]'"
122 ];
123 };
124
125 };
126 };
127
128 config = lib.mkIf cfg.enable {
129 systemd.services.airsonic = {
130 description = "Airsonic Media Server";
131 after = [ "network.target" ];
132 wantedBy = [ "multi-user.target" ];
133
134 preStart = ''
135 # Install transcoders.
136 rm -rf ${cfg.home}/transcode
137 mkdir -p ${cfg.home}/transcode
138 for exe in ${toString cfg.transcoders}; do
139 ln -sf "$exe" ${cfg.home}/transcode
140 done
141 '';
142 serviceConfig = {
143 ExecStart = ''
144 ${cfg.jre}/bin/java -Xmx${toString cfg.maxMemory}m \
145 -Dairsonic.home=${cfg.home} \
146 -Dserver.address=${cfg.listenAddress} \
147 -Dserver.port=${toString cfg.port} \
148 -Dserver.context-path=${cfg.contextPath} \
149 -Djava.awt.headless=true \
150 ${lib.optionalString (cfg.virtualHost != null) "-Dserver.use-forward-headers=true"} \
151 ${toString cfg.jvmOptions} \
152 -verbose:gc \
153 -jar ${cfg.war}
154 '';
155 Restart = "always";
156 User = "airsonic";
157 UMask = "0022";
158 };
159 };
160
161 services.nginx = lib.mkIf (cfg.virtualHost != null) {
162 enable = true;
163 recommendedProxySettings = true;
164 virtualHosts.${cfg.virtualHost} = {
165 locations.${cfg.contextPath}.proxyPass = "http://${cfg.listenAddress}:${toString cfg.port}";
166 };
167 };
168
169 users.users.airsonic = {
170 description = "Airsonic service user";
171 group = "airsonic";
172 name = cfg.user;
173 home = cfg.home;
174 createHome = true;
175 isSystemUser = true;
176 };
177 users.groups.airsonic = { };
178 };
179}