This repository has no description
9.7 kB
314 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 # dash 0.5.13 has a regression where `read` builtin discards input when stdin is a socket
15 # This breaks htgen which uses tcpserver. Pin htgen's dash to 0.5.12 until upstream fixes it.
16 # See: https://git.kernel.org/pub/scm/utils/dash/dash.git/commit/?id=1d072e9c3292281a7eee54c41fec117ff22723e5
17 dash-0_5_12 = pkgs.dash.overrideAttrs (_old: rec {
18 version = "0.5.12";
19 src = pkgs.fetchurl {
20 url = "http://gondor.apana.org.au/~herbert/dash/files/dash-${version}.tar.gz";
21 hash = "sha256-akdKxG6LCzKRbExg32lMggWNMpfYs4W3RQgDDKSo8oo=";
22 };
23 });
24
25 htgen-fixed = pkgs.htgen.override {
26 pkgs = pkgs // {
27 dash = dash-0_5_12;
28 };
29 };
30
31 skip_track = pkgs.writers.writeBashBin "skip_track" ''
32 set -eu
33
34 # TODO come up with new rating, without moving files
35 # current_track=$(${pkgs.curl}/bin/curl -fSs http://localhost:8002/current | ${pkgs.jq}/bin/jq -r .filename)
36 # track_infos=$(${print_current}/bin/print_current)
37 # skip_count=$(${pkgs.attr}/bin/getfattr -n user.skip_count --only-values "$current_track" || echo 0)
38 # if [[ "$current_track" =~ .*/the_playlist/music/.* ]] && [ "$skip_count" -le 2 ]; then
39 # skip_count=$((skip_count+1))
40 # ${pkgs.attr}/bin/setfattr -n user.skip_count -v "$skip_count" "$current_track"
41 # echo skipping: "$track_infos" skip_count: "$skip_count"
42 # else
43 # mkdir -p "$music_dir"/the_playlist/.graveyard/
44 # mv "$current_track" "$music_dir"/the_playlist/.graveyard/
45 # echo killing: "$track_infos"
46 # fi
47 ${pkgs.curl}/bin/curl -fSs -X POST http://localhost:8002/skip |
48 ${pkgs.jq}/bin/jq -r '.filename'
49 '';
50
51 good_track = pkgs.writeBashBin "good_track" ''
52 set -eu
53
54 current_track=$(${pkgs.curl}/bin/curl -fSs http://localhost:8002/current | ${pkgs.jq}/bin/jq -r .filename)
55 track_infos=$(${print_current}/bin/print_current)
56 # TODO come up with new rating, without moving files
57 # if [[ "$current_track" =~ .*/the_playlist/music/.* ]]; then
58 # ${pkgs.attr}/bin/setfattr -n user.skip_count -v 0 "$current_track"
59 # else
60 # mv "$current_track" "$music_dir"/the_playlist/music/ || :
61 # fi
62 echo good: "$track_infos"
63 '';
64
65 print_current = pkgs.writeDashBin "print_current" ''
66 file=$(${pkgs.curl}/bin/curl -fSs http://localhost:8002/current |
67 ${pkgs.jq}/bin/jq -r '.filename' |
68 ${pkgs.gnused}/bin/sed 's,^${music_dir},,'
69 )
70 link=$(${pkgs.curl}/bin/curl http://localhost:8002/current |
71 ${pkgs.jq}/bin/jq -r '.filename' |
72 ${pkgs.gnused}/bin/sed 's@.*\(.\{11\}\)\.ogg@https://youtu.be/\1@'
73 )
74 echo "$file": "$link"
75 '';
76
77in
78{
79 imports = [
80 ./news.nix
81 ./weather.nix
82 self.inputs.stockholm.nixosModules.acl
83 # self.inputs.stockholm.nixosModules.reaktor2
84 ];
85
86 users.users = {
87 "${name}" = rec {
88 inherit name;
89 createHome = true;
90 group = name;
91 uid = pkgs.stockholm.lib.genid_uint31 name;
92 description = "radio manager";
93 home = "/home/${name}";
94 useDefaultShell = true;
95 openssh.authorizedKeys.keys = [
96 self.keys.ssh.barnacle.public
97 self.keys.ssh.yubi_pgp.public
98 self.keys.ssh.termux_massulus.public
99 self.keys.ssh.yubi1.public
100 self.keys.ssh.yubi2.public
101 self.keys.ssh.solo2.public
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=100000 #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 '';
145 MUSIC = "${music_dir}/the_playlist";
146 ICECAST_HOST = "localhost";
147 };
148 path = [
149 pkgs.bubblewrap
150 ];
151 serviceConfig.User = lib.mkForce "radio";
152 };
153
154 nixpkgs.config.packageOverrides = opkgs: {
155 liquidsoap = opkgs.liquidsoap.override {
156 runtimePackages = with opkgs; [
157 bubblewrap
158 curl
159 ffmpeg
160 ];
161 };
162 icecast = opkgs.icecast.overrideAttrs (old: rec {
163 version = "2.5-beta3";
164
165 src = pkgs.fetchurl {
166 url = "http://downloads.xiph.org/releases/icecast/icecast-${version}.tar.gz";
167 sha256 = "sha256-4FDokoA9zBDYj8RAO/kuTHaZ6jZYBLSJZiX/IYFaCW8=";
168 };
169
170 NIX_CFLAGS_COMPILE = "-Wno-error=implicit-function-declaration";
171
172 buildInputs = old.buildInputs ++ [ pkgs.pkg-config ];
173 });
174 };
175 services.icecast = {
176 enable = true;
177 hostname = "radio.lassul.us";
178 admin.password = "hackme";
179 extraConf = ''
180 <authentication>
181 <source-password>hackme</source-password>
182 <admin-user>admin</admin-user>
183 <admin-password>hackme</admin-password>
184 </authentication>
185 <logging>
186 <accesslog>-</accesslog>
187 <errorlog>-</errorlog>
188 <loglevel>3</loglevel>
189 </logging>
190 <mount type="normal">
191 <mount-name>/radio.badge</mount-name>
192 <queue-size>2048000</queue-size>
193 <burst-size>128000</burst-size>
194 </mount>
195 '';
196 };
197
198 networking.firewall.interfaces.retiolum.allowedTCPPorts = [
199 8001
200 8002
201 ];
202
203 krebs.htgen.radio = {
204 package = htgen-fixed;
205 port = 8001;
206 user = {
207 name = "radio";
208 };
209 scriptFile = pkgs.writeDash "radio" ''
210 set -x
211 case "''${Method:-} ''${Request_URI:-}" in
212 "POST /skip")
213 printf 'HTTP/1.1 200 OK\r\n'
214 printf 'Connection: close\r\n'
215 printf '\r\n'
216 msg=$(${skip_track}/bin/skip_track)
217 echo "$msg"
218 exit
219 ;;
220 "POST /good")
221 printf 'HTTP/1.1 200 OK\r\n'
222 printf 'Connection: close\r\n'
223 printf '\r\n'
224 msg=$(${good_track}/bin/good_track)
225 echo "$msg"
226 exit
227 ;;
228 esac
229 '';
230 };
231
232 security.acme.certs."radio.r".server = config.krebs.ssl.acmeURL;
233
234 networking.firewall.allowedTCPPorts = [
235 80
236 8000
237 ];
238 services.nginx = {
239 enable = true;
240 virtualHosts."radio.r" = {
241 enableACME = true;
242 addSSL = true;
243 locations."/".extraConfig = ''
244 # https://github.com/aswild/icecast-notes#core-nginx-config
245 proxy_pass http://localhost:8000;
246 # Disable request size limit, very important for uploading large files
247 client_max_body_size 0;
248
249 # Enable support `Transfer-Encoding: chunked`
250 chunked_transfer_encoding on;
251
252 # Disable request and response buffering, minimize latency to/from Icecast
253 proxy_buffering off;
254 proxy_request_buffering off;
255
256 # Icecast needs HTTP/1.1, not 1.0 or 2
257 proxy_http_version 1.1;
258
259 # Forward all original request headers
260 proxy_pass_request_headers on;
261
262 # Set some standard reverse proxy headers. Icecast server currently ignores these,
263 # but may support them in a future version so that access logs are more useful.
264 proxy_set_header Host $host;
265 proxy_set_header X-Real-IP $remote_addr;
266 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
267 proxy_set_header X-Forwarded-Proto $scheme;
268
269 # get source ip for weather reports
270 proxy_set_header user-agent "$http_user_agent; client-ip=$remote_addr";
271 '';
272 locations."= /recent".extraConfig = ''
273 default_type "text/plain";
274 alias /var/lib/radio/recent;
275 '';
276 locations."= /current".extraConfig = ''
277 proxy_pass http://localhost:8002;
278 '';
279 locations."= /skip".extraConfig = ''
280 proxy_pass http://localhost:8001;
281 '';
282 locations."= /good".extraConfig = ''
283 proxy_pass http://localhost:8001;
284 '';
285 locations."= /radio.sh".alias = pkgs.writeScript "radio.sh" ''
286 #!/bin/sh
287 trap 'exit 0' EXIT
288 while sleep 1; do
289 mpv \
290 --cache-secs=0 --demuxer-readahead-secs=0 --untimed --cache-pause=no \
291 'http://radio.lassul.us/radio.ogg'
292 done
293 '';
294 locations."= /controls".extraConfig = ''
295 default_type "text/html";
296 alias ${./controls.html};
297 '';
298 extraConfig = ''
299 add_header 'Access-Control-Allow-Origin' '*';
300 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
301 '';
302 };
303 };
304 services.syncthing.settings.folders."/home/lass/tmp/the_playlist" = {
305 path = lib.mkForce "/var/music/the_playlist";
306 devices = [
307 "mors"
308 "radio"
309 ];
310 };
311 krebs.acl."/var/music/the_playlist"."u:lass:X".parents = true;
312 krebs.acl."/var/music/the_playlist"."u:lass:rwX" = { };
313 krebs.acl."/var/music/the_playlist"."u:radio:rwX" = { };
314}