diff --git a/flake.nix b/flake.nix index e2f6866..856d997 100644 --- a/flake.nix +++ b/flake.nix @@ -43,7 +43,7 @@ flake = ./.; nixpkgs = nixpkgs-25-05; 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) ({ system, pkgs, diff --git a/lib/default.nix b/lib/default.nix index 8748958..dd7f914 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,4 +1,5 @@ -{ flake, inputs, self, nixpkgs }: let +{ flake, inputs, self }: let + nixpkgs = inputs.nixpkgs-25-05; lib = nixpkgs.lib; recursiveUpdate = nixpkgs.lib.recursiveUpdate; diff --git a/nixos/module/hectic/service/server-health.nix b/nixos/module/hectic/service/server-health.nix new file mode 100644 index 0000000..eff8707 --- /dev/null +++ b/nixos/module/hectic/service/server-health.nix @@ -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"; + }; + }; + }; +} diff --git a/nixos/module/hectic/service/support-bot.nix b/nixos/module/hectic/service/support-bot.nix new file mode 100644 index 0000000..0a78fc2 --- /dev/null +++ b/nixos/module/hectic/service/support-bot.nix @@ -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); + }; +}