This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

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