This repository has no description
12 kB
383 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 buildInputs = old.buildInputs ++ [ pkgs.pkg-config ];
176 });
177 };
178 services.icecast = {
179 enable = true;
180 hostname = "radio.lassul.us";
181 admin.password = "hackme";
182 extraConf = ''
183 <authentication>
184 <source-password>hackme</source-password>
185 <admin-user>admin</admin-user>
186 <admin-password>hackme</admin-password>
187 </authentication>
188 <logging>
189 <accesslog>-</accesslog>
190 <errorlog>-</errorlog>
191 <loglevel>3</loglevel>
192 </logging>
193 <mount type="normal">
194 <mount-name>/radio.badge</mount-name>
195 <queue-size>2048000</queue-size>
196 <burst-size>128000</burst-size>
197 </mount>
198 '';
199 };
200
201 krebs.iptables = {
202 tables = {
203 filter.INPUT.rules = [
204 {
205 predicate = "-p tcp --dport 8000";
206 target = "ACCEPT";
207 }
208 {
209 predicate = "-i retiolum -p tcp --dport 8001";
210 target = "ACCEPT";
211 }
212 {
213 predicate = "-i retiolum -p tcp --dport 8002";
214 target = "ACCEPT";
215 }
216 ];
217 };
218 };
219
220 # allow reaktor2 to modify files
221 systemd.services."reaktor2-the_playlist".serviceConfig.DynamicUser = lib.mkForce false;
222 systemd.services."reaktor2-the_playlist".serviceConfig.Group = lib.mkForce "radio";
223
224 krebs.reaktor2.the_playlist = {
225 hostname = "irc.hackint.org";
226 port = "6697";
227 useTLS = true;
228 nick = "the_playlist";
229 username = "radio";
230 API.listen = "unix:/home/radio/reaktor.sock";
231 plugins = [
232 {
233 plugin = "register";
234 config = {
235 channels = [
236 "#the_playlist"
237 "#krebs"
238 ];
239 };
240 }
241 {
242 plugin = "system";
243 config = {
244 workdir = config.krebs.reaktor2.the_playlist.stateDir;
245 hooks.PRIVMSG = [
246 {
247 activate = "match";
248 pattern = "^(?:.*\\s)?\\s*the_playlist:\\s*([0-9A-Za-z._][0-9A-Za-z._-]*)(?:\\s+(.*\\S))?\\s*$";
249 command = 1;
250 arguments = [ 2 ];
251 commands = {
252 skip.filename = "${skip_track}/bin/skip_track";
253 next.filename = "${skip_track}/bin/skip_track";
254 bad.filename = "${skip_track}/bin/skip_track";
255
256 good.filename = "${good_track}/bin/good_track";
257 nice.filename = "${good_track}/bin/good_track";
258 like.filename = "${good_track}/bin/good_track";
259
260 current.filename = "${print_current}/bin/print_current";
261 wish.filename = pkgs.writeDash "wish" ''
262 echo "youtube-dl:$1" | ${pkgs.curl}/bin/curl -fSs http://localhost:8002/wish -d @- > /dev/null
263 '';
264 wishlist.filename = pkgs.writeDash "wishlist" ''
265 ${pkgs.curl}/bin/curl -fSs http://localhost:8002/wish | ${pkgs.jq}/bin/jq -r '.[]'
266 '';
267 suggest.filename = pkgs.writeDash "suggest" ''
268 echo "$@" >> playlist_suggest
269 '';
270 };
271 }
272 ];
273 };
274 }
275 ];
276 };
277
278 krebs.htgen.radio = {
279 port = 8001;
280 user = {
281 name = "radio";
282 };
283 scriptFile = pkgs.writeDash "radio" ''
284 case "$Method $Request_URI" in
285 "POST /skip")
286 printf 'HTTP/1.1 200 OK\r\n'
287 printf 'Connection: close\r\n'
288 printf '\r\n'
289 msg=$(${skip_track}/bin/skip_track)
290 ${write_to_irc} "$msg"
291 echo "$msg"
292 exit
293 ;;
294 "POST /good")
295 printf 'HTTP/1.1 200 OK\r\n'
296 printf 'Connection: close\r\n'
297 printf '\r\n'
298 msg=$(${good_track}/bin/good_track)
299 ${write_to_irc} "$msg"
300 echo "$msg"
301 exit
302 ;;
303 esac
304 '';
305 };
306
307 networking.firewall.allowedTCPPorts = [ 80 ];
308 services.nginx = {
309 enable = true;
310 virtualHosts."radio.r" = {
311 locations."/".extraConfig = ''
312 # https://github.com/aswild/icecast-notes#core-nginx-config
313 proxy_pass http://localhost:8000;
314 # Disable request size limit, very important for uploading large files
315 client_max_body_size 0;
316
317 # Enable support `Transfer-Encoding: chunked`
318 chunked_transfer_encoding on;
319
320 # Disable request and response buffering, minimize latency to/from Icecast
321 proxy_buffering off;
322 proxy_request_buffering off;
323
324 # Icecast needs HTTP/1.1, not 1.0 or 2
325 proxy_http_version 1.1;
326
327 # Forward all original request headers
328 proxy_pass_request_headers on;
329
330 # Set some standard reverse proxy headers. Icecast server currently ignores these,
331 # but may support them in a future version so that access logs are more useful.
332 proxy_set_header Host $host;
333 proxy_set_header X-Real-IP $remote_addr;
334 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
335 proxy_set_header X-Forwarded-Proto $scheme;
336
337 # get source ip for weather reports
338 proxy_set_header user-agent "$http_user_agent; client-ip=$remote_addr";
339 '';
340 locations."= /recent".extraConfig = ''
341 default_type "text/plain";
342 alias /var/lib/radio/recent;
343 '';
344 locations."= /current".extraConfig = ''
345 proxy_pass http://localhost:8002;
346 '';
347 locations."= /skip".extraConfig = ''
348 proxy_pass http://localhost:8001;
349 '';
350 locations."= /good".extraConfig = ''
351 proxy_pass http://localhost:8001;
352 '';
353 locations."= /radio.sh".alias = pkgs.writeScript "radio.sh" ''
354 #!/bin/sh
355 trap 'exit 0' EXIT
356 while sleep 1; do
357 mpv \
358 --cache-secs=0 --demuxer-readahead-secs=0 --untimed --cache-pause=no \
359 'http://radio.lassul.us/radio.ogg'
360 done
361 '';
362 locations."= /controls".extraConfig = ''
363 default_type "text/html";
364 alias ${./controls.html};
365 '';
366 extraConfig = ''
367 add_header 'Access-Control-Allow-Origin' '*';
368 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
369 '';
370 };
371 };
372 services.syncthing.settings.folders."/home/lass/tmp/the_playlist" = {
373 path = lib.mkForce "/var/music/the_playlist";
374 devices = [
375 "mors"
376 "prism"
377 "radio"
378 ];
379 };
380 krebs.acl."/var/music/the_playlist"."u:lass:X".parents = true;
381 krebs.acl."/var/music/the_playlist"."u:lass:rwX" = { };
382 krebs.acl."/var/music/the_playlist"."u:radio:rwX" = { };
383}