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