This repository has no description
12 kB
385 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 = with config.krebs.users; [
101 lass.pubkey
102 ];
103 packages = [
104 good_track
105 skip_track
106 print_current
107 ];
108 };
109 };
110
111 users.groups = {
112 "radio" = { };
113 };
114
115 systemd.services.radio_watcher = {
116 wantedBy = [ "multi-user.target" ];
117 after = [ "radio.service" ];
118 serviceConfig = {
119 ExecStart = pkgs.writers.writeDash "radio_watcher" ''
120 set -efux
121 while :; do
122 ${pkgs.curl}/bin/curl -Ss http://localhost:8000/radio.ogg -o /dev/null
123 ${pkgs.systemd}/bin/systemctl restart radio
124 sleep 60
125 done
126 '';
127 Restart = "on-failure";
128 };
129 };
130
131 services.liquidsoap.streams.radio = ./radio.liq;
132 systemd.services.radio = {
133 environment = {
134 RADIO_PORT = "8002";
135 HOOK_TRACK_CHANGE = pkgs.writers.writeDash "on_change" ''
136 set -xefu
137 LIMIT=1000 #how many tracks to keep in the history
138 HISTORY_FILE=/var/lib/radio/recent
139
140 listeners=$(${pkgs.curl}/bin/curl -fSs http://localhost:8000/status-json.xsl |
141 ${pkgs.jq}/bin/jq '[.icestats.source[].listeners] | add' || echo 0)
142 echo "$(${pkgs.coreutils}/bin/date -Is)" "$filename" | ${pkgs.coreutils}/bin/tee -a "$HISTORY_FILE"
143 echo "$(${pkgs.coreutils}/bin/tail -$LIMIT "$HISTORY_FILE")" > "$HISTORY_FILE"
144 ${set_irc_topic} "playing: $filename listeners: $listeners stream: https://radio.lassul.us/radio.ogg"
145 '';
146 MUSIC = "${music_dir}/the_playlist";
147 ICECAST_HOST = "localhost";
148 };
149 path = [
150 pkgs.yt-dlp
151 pkgs.bubblewrap
152 ];
153 serviceConfig.User = lib.mkForce "radio";
154 };
155
156 nixpkgs.config.packageOverrides = opkgs: {
157 liquidsoap = opkgs.liquidsoap.override {
158 runtimePackages = with opkgs; [
159 bubblewrap
160 curl
161 ffmpeg
162 yt-dlp
163 ];
164 };
165 icecast = opkgs.icecast.overrideAttrs (old: rec {
166 version = "2.5-beta3";
167
168 src = pkgs.fetchurl {
169 url = "http://downloads.xiph.org/releases/icecast/icecast-${version}.tar.gz";
170 sha256 = "sha256-4FDokoA9zBDYj8RAO/kuTHaZ6jZYBLSJZiX/IYFaCW8=";
171 };
172
173 NIX_CFLAGS_COMPILE = "-Wno-error=implicit-function-declaration";
174
175
176
177 buildInputs = old.buildInputs ++ [ pkgs.pkg-config ];
178 });
179 };
180 services.icecast = {
181 enable = true;
182 hostname = "radio.lassul.us";
183 admin.password = "hackme";
184 extraConf = ''
185 <authentication>
186 <source-password>hackme</source-password>
187 <admin-user>admin</admin-user>
188 <admin-password>hackme</admin-password>
189 </authentication>
190 <logging>
191 <accesslog>-</accesslog>
192 <errorlog>-</errorlog>
193 <loglevel>3</loglevel>
194 </logging>
195 <mount type="normal">
196 <mount-name>/radio.badge</mount-name>
197 <queue-size>2048000</queue-size>
198 <burst-size>128000</burst-size>
199 </mount>
200 '';
201 };
202
203 krebs.iptables = {
204 tables = {
205 filter.INPUT.rules = [
206 {
207 predicate = "-p tcp --dport 8000";
208 target = "ACCEPT";
209 }
210 {
211 predicate = "-i retiolum -p tcp --dport 8001";
212 target = "ACCEPT";
213 }
214 {
215 predicate = "-i retiolum -p tcp --dport 8002";
216 target = "ACCEPT";
217 }
218 ];
219 };
220 };
221
222 # allow reaktor2 to modify files
223 systemd.services."reaktor2-the_playlist".serviceConfig.DynamicUser = lib.mkForce false;
224 systemd.services."reaktor2-the_playlist".serviceConfig.Group = lib.mkForce "radio";
225
226 krebs.reaktor2.the_playlist = {
227 hostname = "irc.hackint.org";
228 port = "6697";
229 useTLS = true;
230 nick = "the_playlist";
231 username = "radio";
232 API.listen = "unix:/home/radio/reaktor.sock";
233 plugins = [
234 {
235 plugin = "register";
236 config = {
237 channels = [
238 "#the_playlist"
239 "#krebs"
240 ];
241 };
242 }
243 {
244 plugin = "system";
245 config = {
246 workdir = config.krebs.reaktor2.the_playlist.stateDir;
247 hooks.PRIVMSG = [
248 {
249 activate = "match";
250 pattern = "^(?:.*\\s)?\\s*the_playlist:\\s*([0-9A-Za-z._][0-9A-Za-z._-]*)(?:\\s+(.*\\S))?\\s*$";
251 command = 1;
252 arguments = [ 2 ];
253 commands = {
254 skip.filename = "${skip_track}/bin/skip_track";
255 next.filename = "${skip_track}/bin/skip_track";
256 bad.filename = "${skip_track}/bin/skip_track";
257
258 good.filename = "${good_track}/bin/good_track";
259 nice.filename = "${good_track}/bin/good_track";
260 like.filename = "${good_track}/bin/good_track";
261
262 current.filename = "${print_current}/bin/print_current";
263 wish.filename = pkgs.writeDash "wish" ''
264 echo "youtube-dl:$1" | ${pkgs.curl}/bin/curl -fSs http://localhost:8002/wish -d @- > /dev/null
265 '';
266 wishlist.filename = pkgs.writeDash "wishlist" ''
267 ${pkgs.curl}/bin/curl -fSs http://localhost:8002/wish | ${pkgs.jq}/bin/jq -r '.[]'
268 '';
269 suggest.filename = pkgs.writeDash "suggest" ''
270 echo "$@" >> playlist_suggest
271 '';
272 };
273 }
274 ];
275 };
276 }
277 ];
278 };
279
280 krebs.htgen.radio = {
281 port = 8001;
282 user = {
283 name = "radio";
284 };
285 scriptFile = pkgs.writeDash "radio" ''
286 case "$Method $Request_URI" in
287 "POST /skip")
288 printf 'HTTP/1.1 200 OK\r\n'
289 printf 'Connection: close\r\n'
290 printf '\r\n'
291 msg=$(${skip_track}/bin/skip_track)
292 ${write_to_irc} "$msg"
293 echo "$msg"
294 exit
295 ;;
296 "POST /good")
297 printf 'HTTP/1.1 200 OK\r\n'
298 printf 'Connection: close\r\n'
299 printf '\r\n'
300 msg=$(${good_track}/bin/good_track)
301 ${write_to_irc} "$msg"
302 echo "$msg"
303 exit
304 ;;
305 esac
306 '';
307 };
308
309 networking.firewall.allowedTCPPorts = [ 80 ];
310 services.nginx = {
311 enable = true;
312 virtualHosts."radio.r" = {
313 locations."/".extraConfig = ''
314 # https://github.com/aswild/icecast-notes#core-nginx-config
315 proxy_pass http://localhost:8000;
316 # Disable request size limit, very important for uploading large files
317 client_max_body_size 0;
318
319 # Enable support `Transfer-Encoding: chunked`
320 chunked_transfer_encoding on;
321
322 # Disable request and response buffering, minimize latency to/from Icecast
323 proxy_buffering off;
324 proxy_request_buffering off;
325
326 # Icecast needs HTTP/1.1, not 1.0 or 2
327 proxy_http_version 1.1;
328
329 # Forward all original request headers
330 proxy_pass_request_headers on;
331
332 # Set some standard reverse proxy headers. Icecast server currently ignores these,
333 # but may support them in a future version so that access logs are more useful.
334 proxy_set_header Host $host;
335 proxy_set_header X-Real-IP $remote_addr;
336 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
337 proxy_set_header X-Forwarded-Proto $scheme;
338
339 # get source ip for weather reports
340 proxy_set_header user-agent "$http_user_agent; client-ip=$remote_addr";
341 '';
342 locations."= /recent".extraConfig = ''
343 default_type "text/plain";
344 alias /var/lib/radio/recent;
345 '';
346 locations."= /current".extraConfig = ''
347 proxy_pass http://localhost:8002;
348 '';
349 locations."= /skip".extraConfig = ''
350 proxy_pass http://localhost:8001;
351 '';
352 locations."= /good".extraConfig = ''
353 proxy_pass http://localhost:8001;
354 '';
355 locations."= /radio.sh".alias = pkgs.writeScript "radio.sh" ''
356 #!/bin/sh
357 trap 'exit 0' EXIT
358 while sleep 1; do
359 mpv \
360 --cache-secs=0 --demuxer-readahead-secs=0 --untimed --cache-pause=no \
361 'http://radio.lassul.us/radio.ogg'
362 done
363 '';
364 locations."= /controls".extraConfig = ''
365 default_type "text/html";
366 alias ${./controls.html};
367 '';
368 extraConfig = ''
369 add_header 'Access-Control-Allow-Origin' '*';
370 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
371 '';
372 };
373 };
374 services.syncthing.settings.folders."/home/lass/tmp/the_playlist" = {
375 path = lib.mkForce "/var/music/the_playlist";
376 devices = [
377 "mors"
378 "prism"
379 "radio"
380 ];
381 };
382 krebs.acl."/var/music/the_playlist"."u:lass:X".parents = true;
383 krebs.acl."/var/music/the_playlist"."u:lass:rwX" = { };
384 krebs.acl."/var/music/the_playlist"."u:radio:rwX" = { };
385}