mmdvm-flake/MMDVMHost/module.nix

83 lines
2 KiB
Nix
Raw Normal View History

2024-12-19 13:58:57 +01:00
{ lib, config, pkgs, mmdvm_host, mmdvm_firmware, ... }:
2024-11-05 21:30:44 +01:00
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 = {
2024-11-05 21:37:03 +01:00
enable = mkEnableOption "MMDVMHost service";
2024-12-19 13:58:57 +01:00
host_package = mkOption {
2024-11-05 21:30:44 +01:00
default = mmdvm_host.x86_64-linux.default;
type = types.package;
};
2024-12-19 13:58:57 +01:00
firmware_package = mkOption {
default = mmdvm_firmware.x86_64-linux.default;
type = types.package;
};
2024-11-05 21:30:44 +01:00
user = mkOption {
type = types.str;
default = "afg";
};
2024-12-19 13:58:57 +01:00
group = mkOption {
type = types.str;
default = "afg";
};
config-file = mkOption {
type = types.str;
};
usb-tty = mkOption {
type = types.str;
default = "/dev/ttyUSB0";
};
2024-11-05 21:30:44 +01:00
};
config = mkIf cfg.enable {
# List packages installed in system profile. To search, run:
# $ nix search wget
environment.systemPackages = [
2024-12-19 13:58:57 +01:00
pkgs.stm32flash
cfg.host_package
2024-11-05 21:30:44 +01:00
];
systemd.services.mmdvm_host = {
wantedBy = [ "multi-user.target" ];
wants = [ "network.target" ];
after = [ "network.target" ];
2024-11-05 21:37:03 +01:00
description = "MMDVMHost service";
2024-11-05 21:30:44 +01:00
serviceConfig = {
Type = "exec";
User = cfg.user;
2024-12-19 13:58:57 +01:00
Group = cfg.group;
2024-11-05 21:30:44 +01:00
Restart = "always";
RestartSec = "3s";
2024-11-05 21:37:03 +01:00
RuntimeDirectory = "MMDVMHost";
2024-11-05 21:30:44 +01:00
};
script = ''
2024-12-19 13:58:57 +01:00
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
2024-12-19 14:05:36 +01:00
${pkgs.stm32flash}/bin/stm32flash -w ${cfg.firmware_package}/firmware/mmdvm_f4.bin -i ',-dtr,-rts,dtr,,:-dtr,rts,dtr' ${cfg.usb-tty}
2024-12-19 13:58:57 +01:00
echo "{cfg.firmware_package}" > /var/lib/mmdvm/firmware-version
fi
exec ${cfg.package}/bin/MMDVMHost ${cfg.config-file}
2024-11-05 21:30:44 +01:00
'';
};
};
}