Skip to content

Commit

Permalink
Add Nix module
Browse files Browse the repository at this point in the history
  • Loading branch information
Brawl345 committed Sep 7, 2024
1 parent 392472b commit 0216f19
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 3 deletions.
6 changes: 3 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
in
{

# nixosModules = {
# default = ./module.nix;
# };
nixosModules = {
default = ./module.nix;
};

overlays.default = final: prev: {
rssbot = self.packages.${prev.system}.default;
Expand Down
116 changes: 116 additions & 0 deletions module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{ config, lib, pkgs, ... }:

let
cfg = config.services.rssbot;
defaultUser = "rssbot";
inherit (lib) mkEnableOption mkPackageOption mkOption mkIf types optionalAttrs;
in
{
options.services.rssbot = {
enable = mkEnableOption "RSS bot for Telegram";

package = mkPackageOption pkgs "rssbot" { };

user = mkOption {
type = types.str;
default = defaultUser;
description = "User under which RSS Bot runs.";
};

adminId = mkOption {
type = types.int;
description = "Admin ID";
};

botTokenFile = mkOption {
type = types.path;
description = "File containing Telegram Bot Token";
};

# TODO: Find a way to load a custom post.gohtml
# template = mkOption {
# type = types.nullOr types.lines;
# default = null;
# description = "Custom post.gohtml template content";
# };

database = {
host = lib.mkOption {
type = types.str;
description = "Database host.";
default = "localhost";
};

port = mkOption {
type = types.port;
default = 3306;
description = "Database port";
};

name = lib.mkOption {
type = types.str;
description = "Database name.";
default = defaultUser;
};

user = lib.mkOption {
type = types.str;
description = "Database username.";
default = defaultUser;
};

passwordFile = lib.mkOption {
type = types.path;
description = "Database user password file.";
};
};

};

config = mkIf cfg.enable {
systemd.services.rssbot = {
description = "RSS Bot for Telegram";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];

script = ''
export BOT_TOKEN="$(< $CREDENTIALS_DIRECTORY/BOT_TOKEN )"
export MYSQL_PASSWORD="$(< $CREDENTIALS_DIRECTORY/MYSQL_PASSWORD )"
exec ${cfg.package}/bin/rssbot
'';

serviceConfig = {
LoadCredential = [
"BOT_TOKEN:${cfg.botTokenFile}"
"MYSQL_PASSWORD:${cfg.database.passwordFile}"
];

Restart = "always";
User = cfg.user;
Group = defaultUser;
WorkingDirectory = "/var/lib/rssbot";
};

environment = {
ADMIN_ID = toString cfg.adminId;
MYSQL_HOST = cfg.database.host;
MYSQL_PORT = toString cfg.database.port;
MYSQL_USER = cfg.database.user;
MYSQL_DB = cfg.database.name;
};
};

users = optionalAttrs (cfg.user == defaultUser) {
users.${defaultUser} = {
isSystemUser = true;
group = defaultUser;
description = "RSS Bot user";
};

groups.${defaultUser} = { };
};

};

}

0 comments on commit 0216f19

Please sign in to comment.