-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
211 lines (184 loc) · 6.45 KB
/
flake.nix
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
{
description = "A very marrano bot.";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
flakelight.url = "github:nix-community/flakelight";
};
outputs = { self, flakelight, ... }@inputs:
flakelight ./. {
inherit inputs;
devShell.packages = pkgs: [
pkgs.go
pkgs.gopls
pkgs.sqlite
pkgs.litecli
pkgs.gnumake
pkgs.xh
pkgs.entr
pkgs.nixpkgs-fmt
pkgs.nixd
];
package = { pkgs, lib, buildGoModule, ... }: buildGoModule {
name = "marrano-bot";
src = ./.;
nativeBuildInputs = [ pkgs.go ];
vendorHash = "sha256-x4KV95HWtYouO9Ah603wkbCAiA94H8Tl8fwYiUsAweo=";
meta = {
platforms = lib.platforms.all;
};
};
formatters = {
"*.go" = "go fmt";
};
nixosModule =
{ config
, pkgs
, lib
, system
, ...
}:
with lib; let
cfg = config.services.marrano-bot;
opt = options.services.marrano-bot;
pkg = self.packages.${pkgs.system}.default;
hardeningOptions = { }; # TODO systemd hardened settings `systemd analyze security marrano-bot`
in
{
options.services.marrano-bot = {
enable =
mkEnableOption (lib.mdDoc "Enable MarranoBot Service")
// {
description = lib.mdDoc ''
Whether to enable the marrano-bot webserver daemon.
'';
};
port = mkOption {
type = types.port;
default = 64041;
description = lib.mdDoc "marrano-bot http service port.";
};
hostName = mkOption {
type = types.str;
default = "bot.marrani.lol";
description = lib.mdDoc "marrano-bot public hostname. Used to receive webhook updates.";
};
openPort = mkOption {
type = types.bool;
default = false;
description = lib.mdDoc "Open firewall port.";
};
token = mkOption {
type = types.string;
default = "";
description = lib.mdDoc "Telegram bot token";
};
dataDir = mkOption {
type = types.path;
default = "/var/lib/marrano-bot";
description = lib.mdDoc "The directory that will host the database file and config.edn";
};
databaseFile = mkOption {
type = types.path;
default = "${cfg.dataDir}/marrano-bot.sqlite";
};
ageSecret = mkOption {
type = types.string;
default = "marrano-bot.edn";
};
user = mkOption {
type = types.str;
default = "marrano-bot";
description = lib.mdDoc ''
User account under which marrano-bot runs.
'';
};
group = mkOption {
type = types.str;
default = "marrano-bot";
description = lib.mdDoc ''
Group under which marrano-bot runs.
'';
};
logLevel = mkOption {
type = types.str;
default = "info";
description = lib.mdDoc ''
Possible values:
- debug
- info
- warn
- error
'';
};
};
config = mkIf cfg.enable {
assertions = [
{
assertion = config.age != null;
message = "Age and the age secret 'marrano-bot' is required!";
}
{
assertion = config.age.secrets.marrano-bot != null;
message = "Age secret 'marrano-bot' is required!";
}
];
users.groups = mkIf (cfg.group == "marrano-bot") {
marrano-bot = { };
};
users.users = mkIf (cfg.user == "marrano-bot") {
marrano-bot = {
group = cfg.group;
shell = pkgs.bashInteractive;
home = cfg.dataDir;
isSystemUser = true;
description = "marrano-bot Daemon user";
};
};
system.activationScripts.marrano-bot = ''
mkdir -m 1700 -p ${cfg.dataDir}
chown -R ${cfg.user}:${cfg.group} ${cfg.dataDir}
'';
systemd.services.marrano-bot = {
description = "A very marrano bot";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
path = [ pkg ];
environment = {
PORT = toString cfg.port;
DATABASE_FILE = cfg.databaseFile;
LOG_LEVEL = cfg.logLevel;
};
serviceConfig = {
# NOTE: needed (r)agenix secret!
LoadCredential = "marrano-bot.toml:${config.age.secrets.marrano-bot.path}";
User = cfg.user;
Group = cfg.group;
Type = "simple";
Restart = "on-failure";
WorkingDirectory = cfg.dataDir;
ExecStart = "${pkgs.marrano-bot}/bin/marrano-bot -c \${CREDENTIALS_DIRECTORY}/marrano-bot.toml";
};
};
networking.firewall = mkIf cfg.openPort {
allowedTCPPorts = [ cfg.port ];
};
#
# Reverse proxies
#
services.caddy.virtualHosts."${cfg.hostName}" = {
serverAliases = mkDefault [ "www.${cfg.hostName}" ];
extraConfig = ''
encode gzip
reverse_proxy :${toString cfg.port}
'';
};
services.nginx.virtualHosts."${cfg.hostName}" = {
serverName = mkDefault cfg.hostName;
locations."/".proxyPass = "https://127.0.0.1:${toString cfg.port}";
enableACME = mkDefault true;
forceSSL = mkDefault true;
};
};
};
};
}