feat(nixos): module: +support-bot

This commit is contained in:
2025-08-28 15:02:58 +00:00
parent 7dadd2290f
commit 00e02f328a
4 changed files with 175 additions and 2 deletions

View File

@@ -43,7 +43,7 @@
flake = ./.; flake = ./.;
nixpkgs = nixpkgs-25-05; nixpkgs = nixpkgs-25-05;
overlays = [ self.overlays.default ]; overlays = [ self.overlays.default ];
self-lib = import ./lib { inherit flake self inputs nixpkgs; }; self-lib = import ./lib { inherit flake self inputs; };
in self-lib.forAllSystemsWithPkgs ([(import rust-overlay)] ++ overlays) ({ in self-lib.forAllSystemsWithPkgs ([(import rust-overlay)] ++ overlays) ({
system, system,
pkgs, pkgs,

View File

@@ -1,4 +1,5 @@
{ flake, inputs, self, nixpkgs }: let { flake, inputs, self }: let
nixpkgs = inputs.nixpkgs-25-05;
lib = nixpkgs.lib; lib = nixpkgs.lib;
recursiveUpdate = nixpkgs.lib.recursiveUpdate; recursiveUpdate = nixpkgs.lib.recursiveUpdate;

View File

@@ -0,0 +1,71 @@
{
inputs,
flake,
self,
}:
{
pkgs,
lib,
config,
...
}: let
system = pkgs.system;
cfg = config.hectic.server-health;
# URLS="http://..." # default: none
# VOLUMES="/ /home" # default: all from df -P
in {
options = {
hectic.server-health = {
enable = lib.mkEnableOption "enable serverhelth services";
urls = lib.mkOption {
type = lib.types.port;
default = "5899";
description = ''
urls to check
'';
};
volumes = lib.mkOption {
type = lib.types.port;
default = "5899";
description = ''
volumes to check
'';
};
port = lib.mkOption {
type = lib.types.port;
default = "5899";
description = ''
service's port
'';
};
};
};
config = lib.mkIf cfg.enable {
systemd.services."hectic-server-health" = {
description = "Hectic server health check";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "simple";
ExecStart = "${self.packages.${system}.server-health}/bin/server-health";
Environment = (if cfg.urls != null then [
"URLS=${cfg.urls}"
] else []) ++ (if cfg.volumes != null then [
"VOLUMES=${cfg.volumes}"
] else []);
Restart = "always";
RestartSec = "5s";
# Shutdown configuration
TimeoutStopSec = "30s";
KillSignal = "SIGTERM";
KillMode = "mixed";
# Security and process management
RemainAfterExit = false;
StandardOutput = "journal";
StandardError = "journal";
};
};
};
}

View File

@@ -0,0 +1,101 @@
{ inputs, flake, self }:
{ config, pkgs, lib, ... }: let
cfg = config.hectic.support-bot;
system = pkgs.system;
packagesAttr = lib.mapAttrs (packageName: packageConfig:
packageConfig // {
name = packageName;
}) cfg;
packagesArr = builtins.attrValues packagesAttr;
in {
options = {
hectic.support-bot = lib.mkOption {
type = lib.types.attrsOf (
lib.types.submodule {
options = {
redisHost = lib.mkOption {
type = lib.types.host;
default = "localhost";
example = "localhost";
description = ''
redis db host
if localhost - module spawns redis
'';
};
redisPort = lib.mkOption {
type = lib.types.port;
example = "42069";
description = ''redis db port'';
};
redisDb = lib.mkOption {
type = lib.types.int;
apply = x: if x >= 0 && x <= 15 then x else throw "must be 0..15";
default = 0;
example = "0";
description = ''redis db number (0-15)'';
};
environmentPath = lib.mkOption {
type = lib.types.path;
example = ''
config.sops.secrets."name-of-service/environment".path
'';
description = ''
BOT_TOKEN=
BOT_DEV_ID=
BOT_GROUP_ID=
BOT_EMOJI_ID=
'';
};
};
}
);
default = { };
example = lib.literalExpression /* nix */ ''
{
"name-of-service" = {
environmentPath = config.sops.secrets."name-of-service/environment".path;
};
};
'';
description = "Declarative support bot config";
};
};
config = {
services.redis.servers = lib.mkMerge (map (supportConfig: {
"support-bot-${supportConfig.name}" = lib.mkIf (supportConfig.redisHost == "localhost") {
enable = true;
port = supportConfig.redisPort;
};
}) packagesArr);
systemd.services = lib.mkMerge (map (supportConfig: {
"support-bot-${supportConfig.name}" = {
description = "Support Bot Service";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "simple";
ExecStart = "${self.packages.${system}.support-bot}/bin/support-bot";
Restart = "always";
RestartSec = "5s";
EnvironmentFile = supportConfig.environmentPath;
Envitronmet = [
"REDIS_HOST=${supportConfig.redisHost}"
"REDIS_PORT=${supportConfig.redisPort}"
"REDIS_DB=${supportConfig.redisDb}"
];
# Shutdown configuration
TimeoutStopSec = "30s";
KillSignal = "SIGTERM";
KillMode = "mixed";
# Security and process management
RemainAfterExit = false;
StandardOutput = "journal";
StandardError = "journal";
};
};
}) packagesArr);
};
}