This repository has no description
9.3 kB
308 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
60in
61{
62 imports = [
63 ./news.nix
64 ./weather.nix
65 self.inputs.stockholm.nixosModules.acl
66 # self.inputs.stockholm.nixosModules.reaktor2
67 ];
68
69 users.users = {
70 "${name}" = rec {
71 inherit name;
72 createHome = true;
73 group = name;
74 uid = pkgs.stockholm.lib.genid_uint31 name;
75 description = "radio manager";
76 home = "/home/${name}";
77 useDefaultShell = true;
78 openssh.authorizedKeys.keys = [
79 self.keys.ssh.barnacle.public
80 self.keys.ssh.yubi_pgp.public
81 self.keys.ssh.yubi1.public
82 self.keys.ssh.yubi2.public
83 self.keys.ssh.solo2.public
84 ];
85 packages = [
86 good_track
87 skip_track
88 print_current
89 ];
90 };
91 };
92
93 users.groups = {
94 "radio" = { };
95 };
96
97 systemd.services.radio_watcher = {
98 wantedBy = [ "multi-user.target" ];
99 after = [ "radio.service" ];
100 serviceConfig = {
101 ExecStart = pkgs.writers.writeDash "radio_watcher" ''
102 set -efux
103 while :; do
104 ${pkgs.curl}/bin/curl -Ss http://localhost:8000/radio.ogg -o /dev/null
105 ${pkgs.systemd}/bin/systemctl restart radio
106 sleep 60
107 done
108 '';
109 Restart = "on-failure";
110 };
111 };
112
113 services.liquidsoap.streams.radio = ./radio.liq;
114 systemd.services.radio = {
115 environment = {
116 RADIO_PORT = "8002";
117 HOOK_TRACK_CHANGE = pkgs.writers.writeDash "on_change" ''
118 set -xefu
119 LIMIT=1000 #how many tracks to keep in the history
120 HISTORY_FILE=/var/lib/radio/recent
121
122 listeners=$(${pkgs.curl}/bin/curl -fSs http://localhost:8000/status-json.xsl |
123 ${pkgs.jq}/bin/jq '[.icestats.source[].listeners] | add' || echo 0)
124 echo "$(${pkgs.coreutils}/bin/date -Is)" "$filename" | ${pkgs.coreutils}/bin/tee -a "$HISTORY_FILE"
125 echo "$(${pkgs.coreutils}/bin/tail -$LIMIT "$HISTORY_FILE")" > "$HISTORY_FILE"
126 '';
127 MUSIC = "${music_dir}/the_playlist";
128 ICECAST_HOST = "localhost";
129 };
130 path = [
131 pkgs.yt-dlp
132 pkgs.bubblewrap
133 ];
134 serviceConfig.User = lib.mkForce "radio";
135 };
136
137 nixpkgs.config.packageOverrides = opkgs: {
138 liquidsoap = opkgs.liquidsoap.override {
139 runtimePackages = with opkgs; [
140 bubblewrap
141 curl
142 ffmpeg
143 yt-dlp
144 ];
145 };
146 icecast = opkgs.icecast.overrideAttrs (old: rec {
147 version = "2.5-beta3";
148
149 src = pkgs.fetchurl {
150 url = "http://downloads.xiph.org/releases/icecast/icecast-${version}.tar.gz";
151 sha256 = "sha256-4FDokoA9zBDYj8RAO/kuTHaZ6jZYBLSJZiX/IYFaCW8=";
152 };
153
154 NIX_CFLAGS_COMPILE = "-Wno-error=implicit-function-declaration";
155
156 buildInputs = old.buildInputs ++ [ pkgs.pkg-config ];
157 });
158 };
159 services.icecast = {
160 enable = true;
161 hostname = "radio.lassul.us";
162 admin.password = "hackme";
163 extraConf = ''
164 <authentication>
165 <source-password>hackme</source-password>
166 <admin-user>admin</admin-user>
167 <admin-password>hackme</admin-password>
168 </authentication>
169 <logging>
170 <accesslog>-</accesslog>
171 <errorlog>-</errorlog>
172 <loglevel>3</loglevel>
173 </logging>
174 <mount type="normal">
175 <mount-name>/radio.badge</mount-name>
176 <queue-size>2048000</queue-size>
177 <burst-size>128000</burst-size>
178 </mount>
179 '';
180 };
181
182 krebs.iptables = {
183 tables = {
184 filter.INPUT.rules = [
185 {
186 predicate = "-p tcp --dport 8000";
187 target = "ACCEPT";
188 }
189 {
190 predicate = "-i retiolum -p tcp --dport 8001";
191 target = "ACCEPT";
192 }
193 {
194 predicate = "-i retiolum -p tcp --dport 8002";
195 target = "ACCEPT";
196 }
197 ];
198 };
199 };
200
201 krebs.htgen.radio = {
202 port = 8001;
203 user = {
204 name = "radio";
205 };
206 scriptFile = pkgs.writeDash "radio" ''
207 case "$Method $Request_URI" in
208 "POST /skip")
209 printf 'HTTP/1.1 200 OK\r\n'
210 printf 'Connection: close\r\n'
211 printf '\r\n'
212 msg=$(${skip_track}/bin/skip_track)
213 echo "$msg"
214 exit
215 ;;
216 "POST /good")
217 printf 'HTTP/1.1 200 OK\r\n'
218 printf 'Connection: close\r\n'
219 printf '\r\n'
220 msg=$(${good_track}/bin/good_track)
221 echo "$msg"
222 exit
223 ;;
224 esac
225 '';
226 };
227
228 security.acme.certs."radio.r".server = config.krebs.ssl.acmeURL;
229
230 networking.firewall.allowedTCPPorts = [ 80 ];
231 services.nginx = {
232 enable = true;
233 virtualHosts."radio.r" = {
234 enableACME = true;
235 addSSL = true;
236 locations."/".extraConfig = ''
237 # https://github.com/aswild/icecast-notes#core-nginx-config
238 proxy_pass http://localhost:8000;
239 # Disable request size limit, very important for uploading large files
240 client_max_body_size 0;
241
242 # Enable support `Transfer-Encoding: chunked`
243 chunked_transfer_encoding on;
244
245 # Disable request and response buffering, minimize latency to/from Icecast
246 proxy_buffering off;
247 proxy_request_buffering off;
248
249 # Icecast needs HTTP/1.1, not 1.0 or 2
250 proxy_http_version 1.1;
251
252 # Forward all original request headers
253 proxy_pass_request_headers on;
254
255 # Set some standard reverse proxy headers. Icecast server currently ignores these,
256 # but may support them in a future version so that access logs are more useful.
257 proxy_set_header Host $host;
258 proxy_set_header X-Real-IP $remote_addr;
259 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
260 proxy_set_header X-Forwarded-Proto $scheme;
261
262 # get source ip for weather reports
263 proxy_set_header user-agent "$http_user_agent; client-ip=$remote_addr";
264 '';
265 locations."= /recent".extraConfig = ''
266 default_type "text/plain";
267 alias /var/lib/radio/recent;
268 '';
269 locations."= /current".extraConfig = ''
270 proxy_pass http://localhost:8002;
271 '';
272 locations."= /skip".extraConfig = ''
273 proxy_pass http://localhost:8001;
274 '';
275 locations."= /good".extraConfig = ''
276 proxy_pass http://localhost:8001;
277 '';
278 locations."= /radio.sh".alias = pkgs.writeScript "radio.sh" ''
279 #!/bin/sh
280 trap 'exit 0' EXIT
281 while sleep 1; do
282 mpv \
283 --cache-secs=0 --demuxer-readahead-secs=0 --untimed --cache-pause=no \
284 'http://radio.lassul.us/radio.ogg'
285 done
286 '';
287 locations."= /controls".extraConfig = ''
288 default_type "text/html";
289 alias ${./controls.html};
290 '';
291 extraConfig = ''
292 add_header 'Access-Control-Allow-Origin' '*';
293 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
294 '';
295 };
296 };
297 services.syncthing.settings.folders."/home/lass/tmp/the_playlist" = {
298 path = lib.mkForce "/var/music/the_playlist";
299 devices = [
300 "mors"
301 "prism"
302 "radio"
303 ];
304 };
305 krebs.acl."/var/music/the_playlist"."u:lass:X".parents = true;
306 krebs.acl."/var/music/the_playlist"."u:lass:rwX" = { };
307 krebs.acl."/var/music/the_playlist"."u:radio:rwX" = { };
308}