{ config, pkgs, lib, ... }:
let
name = "radio";
music_dir = "/home/radio/music";
skip_track = pkgs.writers.writeBashBin "skip_track" ''
set -eu
# TODO come up with new rating, without moving files
# music_dir=${lib.escapeShellArg music_dir}
# current_track=$(${pkgs.curl}/bin/curl -fSs http://localhost:8002/current | ${pkgs.jq}/bin/jq -r .filename)
# track_infos=$(${print_current}/bin/print_current)
# skip_count=$(${pkgs.attr}/bin/getfattr -n user.skip_count --only-values "$current_track" || echo 0)
# if [[ "$current_track" =~ .*/the_playlist/music/.* ]] && [ "$skip_count" -le 2 ]; then
# skip_count=$((skip_count+1))
# ${pkgs.attr}/bin/setfattr -n user.skip_count -v "$skip_count" "$current_track"
# echo skipping: "$track_infos" skip_count: "$skip_count"
# else
# mkdir -p "$music_dir"/the_playlist/.graveyard/
# mv "$current_track" "$music_dir"/the_playlist/.graveyard/
# echo killing: "$track_infos"
# fi
${pkgs.curl}/bin/curl -X POST http://localhost:8002/skip
'';
good_track = pkgs.writeBashBin "good_track" ''
set -eu
music_dir=${lib.escapeShellArg music_dir}
current_track=$(${pkgs.curl}/bin/curl -fSs http://localhost:8002/current | ${pkgs.jq}/bin/jq -r .filename)
track_infos=$(${print_current}/bin/print_current)
# TODO come up with new rating, without moving files
# if [[ "$current_track" =~ .*/the_playlist/music/.* ]]; then
# ${pkgs.attr}/bin/setfattr -n user.skip_count -v 0 "$current_track"
# else
# mv "$current_track" "$music_dir"/the_playlist/music/ || :
# fi
echo good: "$track_infos"
'';
print_current = pkgs.writeDashBin "print_current" ''
${pkgs.curl}/bin/curl -fSs http://localhost:8002/current | ${pkgs.jq}/bin/jq -r '"\(.filename): \(.purl)"'
'';
set_irc_topic = pkgs.writeDash "set_irc_topic" ''
${pkgs.curl}/bin/curl -fsS --unix-socket /home/radio/reaktor.sock http://z/ \
-H content-type:application/json \
-d "$(${pkgs.jq}/bin/jq -n \
--arg text "$1" '{
command:"TOPIC",
params:["#the_playlist",$text]
}'
)"
'';
write_to_irc = pkgs.writeDash "write_to_irc" ''
${pkgs.curl}/bin/curl -fsSv --unix-socket /home/radio/reaktor.sock http://z/ \
-H content-type:application/json \
-d "$(${pkgs.jq}/bin/jq -n \
--arg text "$1" '{
command:"PRIVMSG",
params:["#the_playlist",$text]
}'
)"
'';
in {
imports = [
./news.nix
./weather.nix
];
users.users = {
"${name}" = rec {
inherit name;
createHome = lib.mkForce false;
group = name;
uid = pkgs.stockholm.lib.genid_uint31 name;
description = "radio manager";
home = "/home/${name}";
useDefaultShell = true;
openssh.authorizedKeys.keys = with config.krebs.users; [
lass.pubkey
];
};
};
users.groups = {
"radio" = {};
};
krebs.per-user.${name}.packages = with pkgs; [
good_track
skip_track
print_current
];
services.liquidsoap.streams.radio = ./radio.liq;
systemd.services.radio = {
environment = {
RADIO_PORT = "8002";
HOOK_TRACK_CHANGE = pkgs.writers.writeDash "on_change" ''
set -xefu
LIMIT=1000 #how many tracks to keep in the history
HISTORY_FILE=/var/lib/radio/recent
listeners=$(${pkgs.curl}/bin/curl -fSs lassul.us:8000/status-json.xsl |
${pkgs.jq}/bin/jq '[.icestats.source[].listeners] | add' || echo 0)
echo "$(${pkgs.coreutils}/bin/date -Is)" "$filename" | ${pkgs.coreutils}/bin/tee -a "$HISTORY_FILE"
echo "$(${pkgs.coreutils}/bin/tail -$LIMIT "$HISTORY_FILE")" > "$HISTORY_FILE"
${set_irc_topic} "playing: $filename listeners: $listeners"
'';
MUSIC = "${music_dir}/the_playlist";
ICECAST_HOST = "localhost";
};
path = [
pkgs.yt-dlp
];
serviceConfig.User = lib.mkForce "radio";
};
services.icecast = {
enable = true;
hostname = "radio.lassul.us";
admin.password = "hackme";
extraConf = ''