forked from
lass.pds.lassul.us/superconfig
This repository has no description
9.7 kB
310 lines
1{
2 self,
3 pkgs,
4 lib,
5 ...
6}:
7
8let
9 name = "radio";
10
11 stockholmPkgs = self.inputs.stockholm.packages.${pkgs.stdenv.hostPlatform.system};
12
13 music_dir = "/var/music";
14
15 # dash 0.5.13 has a regression where `read` builtin discards input when stdin is a socket
16 # This breaks htgen which uses tcpserver. Pin htgen's dash to 0.5.12 until upstream fixes it.
17 # See: https://git.kernel.org/pub/scm/utils/dash/dash.git/commit/?id=1d072e9c3292281a7eee54c41fec117ff22723e5
18 dash-0_5_12 = pkgs.dash.overrideAttrs (_old: rec {
19 version = "0.5.12";
20 src = pkgs.fetchurl {
21 url = "http://gondor.apana.org.au/~herbert/dash/files/dash-${version}.tar.gz";
22 hash = "sha256-akdKxG6LCzKRbExg32lMggWNMpfYs4W3RQgDDKSo8oo=";
23 };
24 });
25
26 htgen-fixed = stockholmPkgs.htgen.override {
27 pkgs = pkgs // {
28 dash = dash-0_5_12;
29 };
30 };
31
32 skip_track = pkgs.writers.writeBashBin "skip_track" ''
33 set -eu
34
35 # TODO come up with new rating, without moving files
36 # current_track=$(${pkgs.curl}/bin/curl -fSs http://localhost:8002/current | ${pkgs.jq}/bin/jq -r .filename)
37 # track_infos=$(${print_current}/bin/print_current)
38 # skip_count=$(${pkgs.attr}/bin/getfattr -n user.skip_count --only-values "$current_track" || echo 0)
39 # if [[ "$current_track" =~ .*/the_playlist/music/.* ]] && [ "$skip_count" -le 2 ]; then
40 # skip_count=$((skip_count+1))
41 # ${pkgs.attr}/bin/setfattr -n user.skip_count -v "$skip_count" "$current_track"
42 # echo skipping: "$track_infos" skip_count: "$skip_count"
43 # else
44 # mkdir -p "$music_dir"/the_playlist/.graveyard/
45 # mv "$current_track" "$music_dir"/the_playlist/.graveyard/
46 # echo killing: "$track_infos"
47 # fi
48 ${pkgs.curl}/bin/curl -fSs -X POST http://localhost:8002/skip |
49 ${pkgs.jq}/bin/jq -r '.filename'
50 '';
51
52 good_track = pkgs.writers.writeBashBin "good_track" ''
53 set -eu
54
55 current_track=$(${pkgs.curl}/bin/curl -fSs http://localhost:8002/current | ${pkgs.jq}/bin/jq -r .filename)
56 track_infos=$(${print_current}/bin/print_current)
57 # TODO come up with new rating, without moving files
58 # if [[ "$current_track" =~ .*/the_playlist/music/.* ]]; then
59 # ${pkgs.attr}/bin/setfattr -n user.skip_count -v 0 "$current_track"
60 # else
61 # mv "$current_track" "$music_dir"/the_playlist/music/ || :
62 # fi
63 echo good: "$track_infos"
64 '';
65
66 print_current = pkgs.writers.writeDashBin "print_current" ''
67 file=$(${pkgs.curl}/bin/curl -fSs http://localhost:8002/current |
68 ${pkgs.jq}/bin/jq -r '.filename' |
69 ${pkgs.gnused}/bin/sed 's,^${music_dir},,'
70 )
71 link=$(${pkgs.curl}/bin/curl http://localhost:8002/current |
72 ${pkgs.jq}/bin/jq -r '.filename' |
73 ${pkgs.gnused}/bin/sed 's@.*\(.\{11\}\)\.ogg@https://youtu.be/\1@'
74 )
75 echo "$file": "$link"
76 '';
77
78in
79{
80 imports = [
81 ./news.nix
82 ./weather.nix
83 self.inputs.stockholm.nixosModules.acl
84 ];
85
86 users.users = {
87 "${name}" = rec {
88 inherit name;
89 createHome = true;
90 group = name;
91 uid = self.inputs.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.writers.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 networking.firewall.allowedTCPPorts = [
233 80
234 8000
235 ];
236 services.nginx = {
237 enable = true;
238 virtualHosts."radio.r" = {
239 locations."/".extraConfig = ''
240 # https://github.com/aswild/icecast-notes#core-nginx-config
241 proxy_pass http://localhost:8000;
242 # Disable request size limit, very important for uploading large files
243 client_max_body_size 0;
244
245 # Enable support `Transfer-Encoding: chunked`
246 chunked_transfer_encoding on;
247
248 # Disable request and response buffering, minimize latency to/from Icecast
249 proxy_buffering off;
250 proxy_request_buffering off;
251
252 # Icecast needs HTTP/1.1, not 1.0 or 2
253 proxy_http_version 1.1;
254
255 # Forward all original request headers
256 proxy_pass_request_headers on;
257
258 # Set some standard reverse proxy headers. Icecast server currently ignores these,
259 # but may support them in a future version so that access logs are more useful.
260 proxy_set_header Host $host;
261 proxy_set_header X-Real-IP $remote_addr;
262 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
263 proxy_set_header X-Forwarded-Proto $scheme;
264
265 # get source ip for weather reports
266 proxy_set_header user-agent "$http_user_agent; client-ip=$remote_addr";
267 '';
268 locations."= /recent".extraConfig = ''
269 default_type "text/plain";
270 alias /var/lib/radio/recent;
271 '';
272 locations."= /current".extraConfig = ''
273 proxy_pass http://localhost:8002;
274 '';
275 locations."= /skip".extraConfig = ''
276 proxy_pass http://localhost:8001;
277 '';
278 locations."= /good".extraConfig = ''
279 proxy_pass http://localhost:8001;
280 '';
281 locations."= /radio.sh".alias = pkgs.writeScript "radio.sh" ''
282 #!/bin/sh
283 trap 'exit 0' EXIT
284 while sleep 1; do
285 mpv \
286 --cache-secs=0 --demuxer-readahead-secs=0 --untimed --cache-pause=no \
287 'http://radio.lassul.us/radio.ogg'
288 done
289 '';
290 locations."= /controls".extraConfig = ''
291 default_type "text/html";
292 alias ${./controls.html};
293 '';
294 extraConfig = ''
295 add_header 'Access-Control-Allow-Origin' '*';
296 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
297 '';
298 };
299 };
300 services.syncthing.settings.folders."/home/lass/tmp/the_playlist" = {
301 path = lib.mkForce "/var/music/the_playlist";
302 devices = [
303 "mors"
304 "radio"
305 ];
306 };
307 krebs.acl."/var/music/the_playlist"."u:lass:X".parents = true;
308 krebs.acl."/var/music/the_playlist"."u:lass:rwX" = { };
309 krebs.acl."/var/music/the_playlist"."u:radio:rwX" = { };
310}