1
0
mirror of https://github.com/kmein/niveum synced 2026-03-17 18:41:09 +01:00
Files
niveum/systems/zaatar/mpd.nix

167 lines
4.3 KiB
Nix
Raw Normal View History

2022-03-10 21:52:12 +01:00
{
config,
pkgs,
lib,
...
}: let
firewall = (import ../../lib).firewall lib;
inherit (import ../../lib) tmpfilesConfig;
2023-07-13 22:13:22 +02:00
mukkeMountPoint = "/mnt/mukke";
streams = import ../../lib/streams.nix {
di-fm-key = "%DI_FM_KEY%"; # TODO lib.strings.fileContents <secrets/di.fm/key>;
2021-01-27 00:03:32 +01:00
};
multi-room-audio-port = 8000;
2022-03-10 21:52:12 +01:00
in {
services.syncthing = let
mpd-directory = config.services.mpd.dataDir;
in {
2021-08-08 08:16:50 +02:00
enable = true;
2021-09-05 17:05:00 +02:00
user = config.services.mpd.user; # config.users.extraUsers.moodle.name;
openDefaultPorts = true;
configDir = "${mpd-directory}/.config/syncthing";
dataDir = "${mpd-directory}/.config/syncthing";
cert = config.age.secrets.syncthing-cert.path;
key = config.age.secrets.syncthing-key.path;
2021-12-01 09:47:14 +01:00
devices = {
inherit ((import ../../lib).syncthing.devices) kabsa manakish heym;
2021-12-01 09:47:14 +01:00
};
2023-07-13 22:13:22 +02:00
folders."${config.services.mpd.musicDirectory}/sync" = {
2022-03-10 21:52:12 +01:00
devices = ["heym" "kabsa" "manakish"];
2021-12-01 09:47:14 +01:00
id = "music";
type = "receiveonly";
2021-08-08 08:16:50 +02:00
};
};
services.mpd = {
enable = true;
network.listenAddress = "0.0.0.0";
extraConfig = ''
log_level "default"
auto_update "yes"
audio_output {
2021-04-03 15:01:54 +02:00
type "pulse"
name "zaatar single room audio system"
}
'';
};
2023-07-13 22:13:22 +02:00
fileSystems.${mukkeMountPoint} = {
device = "//mukke.r/public";
fsType = "cifs";
options = [
"guest"
"nofail"
"noauto"
"ro"
"rsize=16777216"
"cache=loose"
];
};
systemd.tmpfiles.rules = [
(tmpfilesConfig {
type = "L+";
mode = "0644";
user = "mpd";
group = "mpd";
path = "${config.services.mpd.musicDirectory}/mukke";
argument = mukkeMountPoint;
})
];
2022-03-10 21:52:12 +01:00
environment.systemPackages = [pkgs.mpc_cli];
2020-11-06 10:38:33 +01:00
2022-03-10 21:52:12 +01:00
networking.firewall = let
dport = config.services.mpd.network.port;
protocol = "tcp";
rules = [
2022-03-10 21:52:12 +01:00
(firewall.accept {
inherit dport protocol;
source = "192.168.0.0/16";
})
(firewall.accept {
inherit dport protocol;
source = "127.0.0.0/8";
})
];
in {
2022-03-10 21:52:12 +01:00
allowedTCPPorts = [80];
extraCommands = firewall.addRules rules;
extraStopCommands = firewall.removeRules rules;
};
systemd.services.mpd-playlists = {
before = ["mpd.service"];
wantedBy = ["mpd.service"];
script = let
tags = lib.lists.unique (lib.concatMap ({tags ? [], ...}: tags) streams);
tagStreams = tag: lib.filter ({tags ? [], ...}: lib.elem tag tags) streams;
makePlaylist = name: streams: pkgs.writeText "${name}.m3u" (lib.concatMapStringsSep "\n" (lib.getAttr "stream") streams);
playlistDirectory = pkgs.linkFarm "playlists" (
[
{
name = "all.m3u";
path = makePlaylist "all" streams;
}
]
++ map (tag: {
name = "${tag}.m3u";
path = makePlaylist tag (tagStreams tag);
})
tags
);
playlistDirectoryPath = "/var/lib/mpd/playlists";
in ''
export DI_FM_KEY="$(cat "$CREDENTIALS_DIRECTORY/di-fm-key")"
rm -rf ${playlistDirectoryPath}
mkdir ${playlistDirectoryPath}
for m3u in $(ls ${playlistDirectory})
do
${pkgs.gnused}/bin/sed s/%DI_FM_KEY%/"$DI_FM_KEY"/g ${playlistDirectory}/"$m3u" > ${playlistDirectoryPath}/"$(basename "$m3u")"
done
'';
serviceConfig = {
LoadCredential = [
"di-fm-key:${config.age.secrets.di-fm-key.path}"
];
};
};
2022-03-29 20:11:13 +02:00
services.ympd = {
enable = true;
mpd.port = config.services.mpd.network.port;
2021-01-26 23:05:16 +01:00
};
age.secrets = {
ympd-basicAuth = {
file = ../../secrets/zaatar-ympd-basicAuth.age;
owner = "nginx";
group = "nginx";
mode = "400";
};
syncthing-cert.file = ../../secrets/zaatar-syncthing-cert.age;
syncthing-key.file = ../../secrets/zaatar-syncthing-key.age;
di-fm-key.file = ../../secrets/di-fm-key.age;
};
2021-01-26 23:05:16 +01:00
services.nginx = {
enable = true;
recommendedGzipSettings = true;
recommendedOptimisation = true;
recommendedProxySettings = true;
recommendedTlsSettings = true;
2021-09-19 11:41:36 +02:00
virtualHosts."radio.kmein.r" = {
basicAuthFile = config.age.secrets.ympd-basicAuth.path;
2021-01-26 23:05:16 +01:00
locations."/" = {
2022-03-29 20:11:13 +02:00
proxyPass = "http://127.0.0.1:${config.services.ympd.webPort}";
proxyWebsockets = true;
2021-01-26 23:05:16 +01:00
};
};
};
}