puter/hosts/vessel/audiocomp.nix

88 lines
2.2 KiB
Nix
Raw Normal View History

2024-09-15 12:16:32 +00:00
{
inputs,
lib,
pkgs,
...
}: let
audiocomp = pkgs.writeShellApplication {
name = "audiocomp";
runtimeInputs = [
pkgs.parallel
pkgs.rsync
2024-09-15 14:37:09 +00:00
pkgs.openssh
2024-09-15 12:16:32 +00:00
];
text = let
remoteDir = inputs.self.nixosConfigurations.abacus.config.services.navidrome.settings.MusicFolder;
enc = pkgs.writeShellApplication {
name = "enc";
runtimeInputs = [
pkgs.opusTools
];
text = ''
2024-09-15 19:09:52 +00:00
src=$(realpath -- "$1")
dst=$src
dst=''${dst%.flac}.opus
dst=/srv/compmusic/''${dst#/srv/music/}
2024-09-15 12:16:32 +00:00
if [[ -f "$dst" ]]; then
exit
fi
mkdir --parents -- "$(dirname -- "$dst")"
2024-09-15 19:09:52 +00:00
echo "encoding ''${src@Q} -> ''${dst@Q}" >&2
2024-09-15 17:09:31 +00:00
exec opusenc --quiet --bitrate 96.000 -- "$src" "$dst"
2024-09-15 12:16:32 +00:00
'';
};
2024-09-15 19:09:52 +00:00
clean = pkgs.writeShellApplication {
name = "clean";
text = ''
del=$(realpath -- "$1")
chk=$del
chk=''${chk%.opus}.flac
chk=/srv/music/''${chk#/srv/compmusic/}
if [[ ! -f "$chk" ]]; then
echo "deleting ''${del@Q}" >&2
rm --force -- "$del"
fi
'';
};
2024-09-15 12:16:32 +00:00
in ''
2024-09-15 19:09:52 +00:00
shopt -s globstar nullglob
pushd /srv/music
2024-09-15 17:09:41 +00:00
find . -name '*.flac' -print0 | parallel --null -- ${lib.getExe enc} {}
2024-09-15 19:09:52 +00:00
popd
pushd /srv/compmusic
find . -name '*.flac' -exec ${clean} {} \;
popd
2024-09-15 12:16:32 +00:00
rsync --verbose --verbose --archive --update --delete --mkpath --exclude lost+found \
--rsh 'ssh -i /etc/ssh/ssh_host_ed25519_key -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' \
-- /srv/compmusic/ root@wrz.one:${remoteDir}
'';
};
in {
2024-09-15 14:15:47 +00:00
systemd.services.audiocomp = {
description = "Compress and sync music";
serviceConfig = {
Type = "oneshot";
User = "root";
Group = "root";
ExecStart = lib.getExe audiocomp;
};
};
systemd.timers.audiocomp = {
description = "Compress and sync music daily";
wantedBy = ["timers.target"];
timerConfig = {
OnCalendar = "*-*-* 03:00:00";
Persistent = true;
Unit = "audiocomp.service";
};
};
2024-09-15 12:16:32 +00:00
}