xin/modules/rtlamr2mqtt.nix

82 lines
2.0 KiB
Nix
Raw Permalink Normal View History

2023-09-12 08:44:05 -06:00
{ config
, lib
, pkgs
, ...
}:
with pkgs; let
cfg = config.services.rtlamr2mqtt;
2023-09-12 08:44:05 -06:00
rtlamr2mqtt = pkgs.python3Packages.callPackage ../pkgs/rtlamr2mqtt.nix { };
settingsFormat = pkgs.formats.json { };
settingsType = settingsFormat.type;
prettyJSON = conf:
2023-09-12 08:44:05 -06:00
pkgs.runCommandLocal "rtlamr2mqtt-config.json" { } ''
echo '${
builtins.toJSON conf
}' | ${pkgs.buildPackages.jq}/bin/jq 'del(._module)' > $out
'';
2023-09-12 08:44:05 -06:00
in
{
options = with lib; {
services.rtlamr2mqtt = {
enable = mkEnableOption "Enable rtlamr2mqtt";
user = mkOption {
2023-09-12 08:44:05 -06:00
type = with types; oneOf [ str int ];
default = "rtlamr2mqtt";
description = ''
The user the service will use.
'';
};
group = mkOption {
2023-09-12 08:44:05 -06:00
type = with types; oneOf [ str int ];
default = "rtlamr2mqtt";
description = ''
The user the service will use.
'';
};
configuration = mkOption {
type = settingsType;
description = ''
Specify the configuration for rtlamr2mqtt in Nix.
'';
};
package = mkOption {
type = types.package;
default = rtlamr2mqtt;
defaultText = literalExpression "pkgs.rtlamr2mqtt";
description = "The package to use for rtlamr2mqtt";
};
};
};
config = lib.mkIf cfg.enable {
2023-09-12 08:44:05 -06:00
users.groups.rtlamr2mqtt = { };
users.users.rtlamr2mqtt = {
description = "rtlamr2mqtt service user";
isSystemUser = true;
home = "/var/lib/rtlamr2mqtt";
createHome = true;
group = "rtlamr2mqtt";
2023-09-12 08:44:05 -06:00
extraGroups = [ "plugdev" ];
};
systemd.services.rtlamr2mqtt = {
enable = true;
description = "rtlamr2mqtt server";
2023-09-12 08:44:05 -06:00
wantedBy = [ "multi-user.target" ];
serviceConfig = {
User = cfg.user;
Group = cfg.group;
RuntimeDirectory = "rtlamr2mqtt";
ExecStart = "${cfg.package}/bin/rtlamr2mqtt ${prettyJSON cfg.configuration}";
};
};
};
}