mmdvm-flake/MMDVMHost/module.nix
2024-12-19 14:05:36 +01:00

83 lines
2 KiB
Nix

{ lib, config, pkgs, mmdvm_host, mmdvm_firmware, ... }:
with lib;
let
# Shorter name to access final settings a
# user of hello.nix module HAS ACTUALLY SET.
# cfg is a typical convention.
cfg = config.services.mmdvm_host;
in
{
options.services.mmdvm_host = {
enable = mkEnableOption "MMDVMHost service";
host_package = mkOption {
default = mmdvm_host.x86_64-linux.default;
type = types.package;
};
firmware_package = mkOption {
default = mmdvm_firmware.x86_64-linux.default;
type = types.package;
};
user = mkOption {
type = types.str;
default = "afg";
};
group = mkOption {
type = types.str;
default = "afg";
};
config-file = mkOption {
type = types.str;
};
usb-tty = mkOption {
type = types.str;
default = "/dev/ttyUSB0";
};
};
config = mkIf cfg.enable {
# List packages installed in system profile. To search, run:
# $ nix search wget
environment.systemPackages = [
pkgs.stm32flash
cfg.host_package
];
systemd.services.mmdvm_host = {
wantedBy = [ "multi-user.target" ];
wants = [ "network.target" ];
after = [ "network.target" ];
description = "MMDVMHost service";
serviceConfig = {
Type = "exec";
User = cfg.user;
Group = cfg.group;
Restart = "always";
RestartSec = "3s";
RuntimeDirectory = "MMDVMHost";
};
script = ''
if [ ! -e /var/lib/mmdvm/firmware-version ]; then
mkdir -p /var/lib/mmdvm
touch /var/lib/mmdvm/firmware-version
fi
if [ "${cfg.firmware_package}" != "$(cat /var/lib/mmdvm/firmware-version)" ]; then
${pkgs.stm32flash}/bin/stm32flash -w ${cfg.firmware_package}/firmware/mmdvm_f4.bin -i ',-dtr,-rts,dtr,,:-dtr,rts,dtr' ${cfg.usb-tty}
echo "{cfg.firmware_package}" > /var/lib/mmdvm/firmware-version
fi
exec ${cfg.package}/bin/MMDVMHost ${cfg.config-file}
'';
};
};
}