This repository has no description
12 kB
391 lines
1{
2 self,
3 config,
4 pkgs,
5 lib,
6 ...
7}:
8
9let
10 name = "radio";
11
12 music_dir = "/var/music";
13
14 skip_track = pkgs.writers.writeBashBin "skip_track" ''
15 set -eu
16
17 # TODO come up with new rating, without moving files
18 # current_track=$(${pkgs.curl}/bin/curl -fSs http://localhost:8002/current | ${pkgs.jq}/bin/jq -r .filename)
19 # track_infos=$(${print_current}/bin/print_current)
20 # skip_count=$(${pkgs.attr}/bin/getfattr -n user.skip_count --only-values "$current_track" || echo 0)
21 # if [[ "$current_track" =~ .*/the_playlist/music/.* ]] && [ "$skip_count" -le 2 ]; then
22 # skip_count=$((skip_count+1))
23 # ${pkgs.attr}/bin/setfattr -n user.skip_count -v "$skip_count" "$current_track"
24 # echo skipping: "$track_infos" skip_count: "$skip_count"
25 # else
26 # mkdir -p "$music_dir"/the_playlist/.graveyard/
27 # mv "$current_track" "$music_dir"/the_playlist/.graveyard/
28 # echo killing: "$track_infos"
29 # fi
30 ${pkgs.curl}/bin/curl -fSs -X POST http://localhost:8002/skip |
31 ${pkgs.jq}/bin/jq -r '.filename'
32 '';
33
34 good_track = pkgs.writeBashBin "good_track" ''
35 set -eu
36
37 current_track=$(${pkgs.curl}/bin/curl -fSs http://localhost:8002/current | ${pkgs.jq}/bin/jq -r .filename)
38 track_infos=$(${print_current}/bin/print_current)
39 # TODO come up with new rating, without moving files
40 # if [[ "$current_track" =~ .*/the_playlist/music/.* ]]; then
41 # ${pkgs.attr}/bin/setfattr -n user.skip_count -v 0 "$current_track"
42 # else
43 # mv "$current_track" "$music_dir"/the_playlist/music/ || :
44 # fi
45 echo good: "$track_infos"
46 '';
47
48 print_current = pkgs.writeDashBin "print_current" ''
49 file=$(${pkgs.curl}/bin/curl -fSs http://localhost:8002/current |
50 ${pkgs.jq}/bin/jq -r '.filename' |
51 ${pkgs.gnused}/bin/sed 's,^${music_dir},,'
52 )
53 link=$(${pkgs.curl}/bin/curl http://localhost:8002/current |
54 ${pkgs.jq}/bin/jq -r '.filename' |
55 ${pkgs.gnused}/bin/sed 's@.*\(.\{11\}\)\.ogg@https://youtu.be/\1@'
56 )
57 echo "$file": "$link"
58 '';
59
60 set_irc_topic = pkgs.writeDash "set_irc_topic" ''
61 ${pkgs.curl}/bin/curl -fsS --unix-socket /home/radio/reaktor.sock http://z/ \
62 -H content-type:application/json \
63 -d "$(${pkgs.jq}/bin/jq -n \
64 --arg text "$1" '{
65 command:"TOPIC",
66 params:["#the_playlist",$text]
67 }'
68 )"
69 '';
70
71 write_to_irc = pkgs.writeDash "write_to_irc" ''
72 ${pkgs.curl}/bin/curl -fsSv --unix-socket /home/radio/reaktor.sock http://z/ \
73 -H content-type:application/json \
74 -d "$(${pkgs.jq}/bin/jq -n \
75 --arg text "$1" '{
76 command:"PRIVMSG",
77 params:["#the_playlist",$text]
78 }'
79 )"
80 '';
81
82in
83{
84 imports = [
85 ./news.nix
86 ./weather.nix
87 self.inputs.stockholm.nixosModules.acl
88 self.inputs.stockholm.nixosModules.reaktor2
89 ];
90
91 users.users = {
92 "${name}" = rec {
93 inherit name;
94 createHome = true;
95 group = name;
96 uid = pkgs.stockholm.lib.genid_uint31 name;
97 description = "radio manager";
98 home = "/home/${name}";
99 useDefaultShell = true;
100 openssh.authorizedKeys.keys = [
101 self.keys.ssh.barnacle.public
102 self.keys.ssh.yubi_pgp.public
103 self.keys.ssh.yubi1.public
104 self.keys.ssh.yubi2.public
105 self.keys.ssh.solo2.public
106 ];
107 packages = [
108 good_track
109 skip_track
110 print_current
111 ];
112 };
113 };
114
115 users.groups = {
116 "radio" = { };
117 };
118
119 systemd.services.radio_watcher = {
120 wantedBy = [ "multi-user.target" ];
121 after = [ "radio.service" ];
122 serviceConfig = {
123 ExecStart = pkgs.writers.writeDash "radio_watcher" ''
124 set -efux
125 while :; do
126 ${pkgs.curl}/bin/curl -Ss http://localhost:8000/radio.ogg -o /dev/null
127 ${pkgs.systemd}/bin/systemctl restart radio
128 sleep 60
129 done
130 '';
131 Restart = "on-failure";
132 };
133 };
134
135 services.liquidsoap.streams.radio = ./radio.liq;
136 systemd.services.radio = {
137 environment = {
138 RADIO_PORT = "8002";
139 HOOK_TRACK_CHANGE = pkgs.writers.writeDash "on_change" ''
140 set -xefu
141 LIMIT=1000 #how many tracks to keep in the history
142 HISTORY_FILE=/var/lib/radio/recent
143
144 listeners=$(${pkgs.curl}/bin/curl -fSs http://localhost:8000/status-json.xsl |
145 ${pkgs.jq}/bin/jq '[.icestats.source[].listeners] | add' || echo 0)
146 echo "$(${pkgs.coreutils}/bin/date -Is)" "$filename" | ${pkgs.coreutils}/bin/tee -a "$HISTORY_FILE"
147 echo "$(${pkgs.coreutils}/bin/tail -$LIMIT "$HISTORY_FILE")" > "$HISTORY_FILE"
148 ${set_irc_topic} "playing: $filename listeners: $listeners stream: https://radio.lassul.us/radio.ogg"
149 '';
150 MUSIC = "${music_dir}/the_playlist";
151 ICECAST_HOST = "localhost";
152 };
153 path = [
154 pkgs.yt-dlp
155 pkgs.bubblewrap
156 ];
157 serviceConfig.User = lib.mkForce "radio";
158 };
159
160 nixpkgs.config.packageOverrides = opkgs: {
161 liquidsoap = opkgs.liquidsoap.override {
162 runtimePackages = with opkgs; [
163 bubblewrap
164 curl
165 ffmpeg
166 yt-dlp
167 ];
168 };
169 icecast = opkgs.icecast.overrideAttrs (old: rec {
170 version = "2.5-beta3";
171
172 src = pkgs.fetchurl {
173 url = "http://downloads.xiph.org/releases/icecast/icecast-${version}.tar.gz";
174 sha256 = "sha256-4FDokoA9zBDYj8RAO/kuTHaZ6jZYBLSJZiX/IYFaCW8=";
175 };
176
177 NIX_CFLAGS_COMPILE = "-Wno-error=implicit-function-declaration";
178
179 buildInputs = old.buildInputs ++ [ pkgs.pkg-config ];
180 });
181 };
182 services.icecast = {
183 enable = true;
184 hostname = "radio.lassul.us";
185 admin.password = "hackme";
186 extraConf = ''
187 <authentication>
188 <source-password>hackme</source-password>
189 <admin-user>admin</admin-user>
190 <admin-password>hackme</admin-password>
191 </authentication>
192 <logging>
193 <accesslog>-</accesslog>
194 <errorlog>-</errorlog>
195 <loglevel>3</loglevel>
196 </logging>
197 <mount type="normal">
198 <mount-name>/radio.badge</mount-name>
199 <queue-size>2048000</queue-size>
200 <burst-size>128000</burst-size>
201 </mount>
202 '';
203 };
204
205 krebs.iptables = {
206 tables = {
207 filter.INPUT.rules = [
208 {
209 predicate = "-p tcp --dport 8000";
210 target = "ACCEPT";
211 }
212 {
213 predicate = "-i retiolum -p tcp --dport 8001";
214 target = "ACCEPT";
215 }
216 {
217 predicate = "-i retiolum -p tcp --dport 8002";
218 target = "ACCEPT";
219 }
220 ];
221 };
222 };
223
224 # allow reaktor2 to modify files
225 systemd.services."reaktor2-the_playlist".serviceConfig.DynamicUser = lib.mkForce false;
226 systemd.services."reaktor2-the_playlist".serviceConfig.Group = lib.mkForce "radio";
227
228 krebs.reaktor2.the_playlist = {
229 hostname = "irc.hackint.org";
230 port = "6697";
231 useTLS = true;
232 nick = "the_playlist";
233 username = "radio";
234 API.listen = "unix:/home/radio/reaktor.sock";
235 plugins = [
236 {
237 plugin = "register";
238 config = {
239 channels = [
240 "#the_playlist"
241 "#krebs"
242 ];
243 };
244 }
245 {
246 plugin = "system";
247 config = {
248 workdir = config.krebs.reaktor2.the_playlist.stateDir;
249 hooks.PRIVMSG = [
250 {
251 activate = "match";
252 pattern = "^(?:.*\\s)?\\s*the_playlist:\\s*([0-9A-Za-z._][0-9A-Za-z._-]*)(?:\\s+(.*\\S))?\\s*$";
253 command = 1;
254 arguments = [ 2 ];
255 commands = {
256 skip.filename = "${skip_track}/bin/skip_track";
257 next.filename = "${skip_track}/bin/skip_track";
258 bad.filename = "${skip_track}/bin/skip_track";
259
260 good.filename = "${good_track}/bin/good_track";
261 nice.filename = "${good_track}/bin/good_track";
262 like.filename = "${good_track}/bin/good_track";
263
264 current.filename = "${print_current}/bin/print_current";
265 wish.filename = pkgs.writeDash "wish" ''
266 echo "youtube-dl:$1" | ${pkgs.curl}/bin/curl -fSs http://localhost:8002/wish -d @- > /dev/null
267 '';
268 wishlist.filename = pkgs.writeDash "wishlist" ''
269 ${pkgs.curl}/bin/curl -fSs http://localhost:8002/wish | ${pkgs.jq}/bin/jq -r '.[]'
270 '';
271 suggest.filename = pkgs.writeDash "suggest" ''
272 echo "$@" >> playlist_suggest
273 '';
274 };
275 }
276 ];
277 };
278 }
279 ];
280 };
281
282 krebs.htgen.radio = {
283 port = 8001;
284 user = {
285 name = "radio";
286 };
287 scriptFile = pkgs.writeDash "radio" ''
288 case "$Method $Request_URI" in
289 "POST /skip")
290 printf 'HTTP/1.1 200 OK\r\n'
291 printf 'Connection: close\r\n'
292 printf '\r\n'
293 msg=$(${skip_track}/bin/skip_track)
294 ${write_to_irc} "$msg"
295 echo "$msg"
296 exit
297 ;;
298 "POST /good")
299 printf 'HTTP/1.1 200 OK\r\n'
300 printf 'Connection: close\r\n'
301 printf '\r\n'
302 msg=$(${good_track}/bin/good_track)
303 ${write_to_irc} "$msg"
304 echo "$msg"
305 exit
306 ;;
307 esac
308 '';
309 };
310
311 security.acme.certs."radio.r".server = config.krebs.ssl.acmeURL;
312
313 networking.firewall.allowedTCPPorts = [ 80 ];
314 services.nginx = {
315 enable = true;
316 virtualHosts."radio.r" = {
317 enableACME = true;
318 addSSL = true;
319 locations."/".extraConfig = ''
320 # https://github.com/aswild/icecast-notes#core-nginx-config
321 proxy_pass http://localhost:8000;
322 # Disable request size limit, very important for uploading large files
323 client_max_body_size 0;
324
325 # Enable support `Transfer-Encoding: chunked`
326 chunked_transfer_encoding on;
327
328 # Disable request and response buffering, minimize latency to/from Icecast
329 proxy_buffering off;
330 proxy_request_buffering off;
331
332 # Icecast needs HTTP/1.1, not 1.0 or 2
333 proxy_http_version 1.1;
334
335 # Forward all original request headers
336 proxy_pass_request_headers on;
337
338 # Set some standard reverse proxy headers. Icecast server currently ignores these,
339 # but may support them in a future version so that access logs are more useful.
340 proxy_set_header Host $host;
341 proxy_set_header X-Real-IP $remote_addr;
342 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
343 proxy_set_header X-Forwarded-Proto $scheme;
344
345 # get source ip for weather reports
346 proxy_set_header user-agent "$http_user_agent; client-ip=$remote_addr";
347 '';
348 locations."= /recent".extraConfig = ''
349 default_type "text/plain";
350 alias /var/lib/radio/recent;
351 '';
352 locations."= /current".extraConfig = ''
353 proxy_pass http://localhost:8002;
354 '';
355 locations."= /skip".extraConfig = ''
356 proxy_pass http://localhost:8001;
357 '';
358 locations."= /good".extraConfig = ''
359 proxy_pass http://localhost:8001;
360 '';
361 locations."= /radio.sh".alias = pkgs.writeScript "radio.sh" ''
362 #!/bin/sh
363 trap 'exit 0' EXIT
364 while sleep 1; do
365 mpv \
366 --cache-secs=0 --demuxer-readahead-secs=0 --untimed --cache-pause=no \
367 'http://radio.lassul.us/radio.ogg'
368 done
369 '';
370 locations."= /controls".extraConfig = ''
371 default_type "text/html";
372 alias ${./controls.html};
373 '';
374 extraConfig = ''
375 add_header 'Access-Control-Allow-Origin' '*';
376 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
377 '';
378 };
379 };
380 services.syncthing.settings.folders."/home/lass/tmp/the_playlist" = {
381 path = lib.mkForce "/var/music/the_playlist";
382 devices = [
383 "mors"
384 "prism"
385 "radio"
386 ];
387 };
388 krebs.acl."/var/music/the_playlist"."u:lass:X".parents = true;
389 krebs.acl."/var/music/the_playlist"."u:lass:rwX" = { };
390 krebs.acl."/var/music/the_playlist"."u:radio:rwX" = { };
391}