-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.nix
More file actions
173 lines (146 loc) · 4.27 KB
/
module.nix
File metadata and controls
173 lines (146 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.rssbot;
defaultUser = "rssbot";
inherit (lib)
mkEnableOption
mkPackageOption
mkOption
mkIf
types
optional
optionalAttrs
optionalString
;
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.nullOr types.path;
default = null;
description = "Database user password file.";
};
socket = mkOption {
type = types.nullOr types.path;
default =
if config.services.rssbot.database.passwordFile == null then "/run/mysqld/mysqld.sock" else null;
example = "/run/mysqld/mysqld.sock";
description = "Path to the unix socket file to use for authentication.";
};
createLocally = mkOption {
type = types.bool;
default = true;
description = "Create the database locally";
};
};
};
config = mkIf cfg.enable {
assertions = [
{
assertion = !(cfg.database.socket != null && cfg.database.passwordFile != null);
message = "Only one of services.rssbot.database.socket or services.rssbot.database.passwordFile can be set.";
}
{
assertion = cfg.database.socket != null || cfg.database.passwordFile != null;
message = "Either services.rssbot.database.socket or services.rssbot.database.passwordFile must be set.";
}
];
services.mysql = lib.mkIf cfg.database.createLocally {
enable = lib.mkDefault true;
package = lib.mkDefault pkgs.mariadb;
ensureDatabases = [ cfg.database.name ];
ensureUsers = [
{
name = cfg.database.user;
ensurePermissions = {
"${cfg.database.name}.*" = "ALL PRIVILEGES";
};
}
];
};
systemd.services.rssbot = {
description = "RSS Bot for Telegram";
after = [ "network-online.target" "mysql.service" ];
requires = [ "network-online.target" "mysql.service" ];
wantedBy = [ "multi-user.target" ];
script = ''
export BOT_TOKEN="$(< $CREDENTIALS_DIRECTORY/BOT_TOKEN )"
${optionalString (cfg.database.passwordFile != null) ''
export MYSQL_PASSWORD="$(< $CREDENTIALS_DIRECTORY/MYSQL_PASSWORD )"
''}
exec ${cfg.package}/bin/rssbot
'';
serviceConfig = {
LoadCredential = [
"BOT_TOKEN:${cfg.botTokenFile}"
] ++ optional (cfg.database.passwordFile != null) "MYSQL_PASSWORD:${cfg.database.passwordFile}";
Restart = "always";
User = cfg.user;
Group = defaultUser;
};
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;
MYSQL_SOCKET = cfg.database.socket;
};
};
users = optionalAttrs (cfg.user == defaultUser) {
users.${defaultUser} = {
isSystemUser = true;
group = defaultUser;
description = "RSS Bot user";
};
groups.${defaultUser} = { };
};
};
}