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