-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnixos-module.nix
190 lines (187 loc) · 6.28 KB
/
nixos-module.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
self: {
config,
pkgs,
lib,
...
}:
with builtins; let
std = pkgs.lib;
com = config.services.comentario;
in {
options = with lib; {
services.comentario = {
enable = mkEnableOption "comentario";
package = {
backend = mkPackageOption pkgs "comentario" {};
frontend = mkPackageOption pkgs "comentario-fe" {};
};
user = mkOption {
type = types.str;
description = "The user as which the Comentario backend service will run.";
default = "comentario";
};
group = mkOption {
type = types.str;
description = "The group as which the Comentario backend service will run.";
default = "comentario";
};
dir = {
state = mkOption {
type = types.str;
readOnly = true;
default = "/var/lib/${com.user}";
};
runtime = mkOption {
type = types.str;
readOnly = true;
default = "/run/${com.user}";
};
configuration = mkOption {
type = types.str;
readOnly = true;
default = "/etc/${com.user}";
};
};
socketPath = mkOption {
type = types.str;
readOnly = true;
default = "${com.dir.runtime}/comentario.sock";
description = "Path to the Unix socket on which the service will listen.";
};
settings = mkOption {
description = "Environment variables set for the Comentario service, as documented [here](https://docs.comentario.app/en/configuration/backend/static/).";
type = types.submoduleWith {
modules = [
({
config,
lib,
...
}: {
freeformType = with lib.types; attrsOf str;
options = with lib; {
HOST = mkOption {
type = types.str;
default = "localhost";
description = "Address on which to listen.";
};
PORT = mkOption {
type = types.port;
default = 8080;
description = "Port on which to listen.";
};
BASE_URL = mkOption {
type = types.str;
# TODO :: Generate default value from virtualHost.domain if present
default = "http://localhost:${toString config.port}";
};
SECRETS_FILE = mkOption {
type = types.str;
default = "secrets.yaml";
description = "[Secret configuration data](https://docs.comentario.app/en/configuration/backend/secrets/).";
};
STATIC_PATH = mkOption {
type = types.str;
};
DB_MIGRATION_PATH = mkOption {
type = types.str;
};
TEMPLATE_PATH = mkOption {
type = types.str;
};
};
})
];
};
default = {};
};
settingsFile = mkOption {
type = types.path;
readOnly = true;
default = pkgs.writeText "comentario.cfg" (std.concatStringsSep "\n" (map (key: "${key}=${lib.escapeShellArg (toString com.settings.${key})}") (attrNames com.settings)));
description = "The environment file generated from `config.services.comentario.settings`.";
};
virtualHost = {
domain = mkOption {
type = types.str;
description = "The domain of the virtualhost through which the Comentario service is proxied.";
example = "comments.website.example";
};
nginx = {
enable = mkEnableOption "Nginx VHost for Comentario";
};
};
};
};
config = lib.mkIf com.enable (lib.mkMerge [
{
users.users.${com.user} = {
isSystemUser = true;
group = com.group;
};
users.groups.${com.group} = {};
services.comentario.settings = {
STATIC_PATH = toString com.package.frontend;
DB_MIGRATION_PATH = "${com.package.backend}/lib/comentario/db";
TEMPLATE_PATH = "${com.package.backend}/lib/comentario/templates";
};
# adapted from https://gitlab.com/comentario/comentario/-/blob/dev/resources/systemd/system/comentario.service?ref_type=heads
systemd.services."comentario" = {
path = [com.package.backend];
description = "comentario";
after = ["network.target"];
wantedBy = ["multi-user.target"];
serviceConfig = {
Type = "simple";
EnvironmentFile = com.settingsFile;
ExecStart = "${com.package.backend}/bin/comentario -v --socket-path=\"${com.socketPath}\"";
User = com.user;
Group = com.group;
StateDirectory = com.user;
StateDirectoryMode = "0750";
ConfigurationDirectory = com.user;
ConfigurationDirectoryMode = "0750";
RuntimeDirectory = com.user;
RuntimeDirectoryMode = "0750";
WorkingDirectory = com.dir.state;
# Hardening
UMask = "0077";
PrivateTmp = true;
PrivateUsers = true;
ProtectHome = true;
ProtectProc = true;
ProtectSystem = true;
PrivateMounts = true;
PrivateDevices = true;
ProtectClock = true;
ProtectHostname = true;
ProtectKernelModules = true;
ProtectControlGroups = true;
ProtectKernelLogs = true;
RestrictRealtime = true;
RestrictAddressFamilies = ["AF_INET" "AF_INET6" "AF_UNIX"];
MemoryDenyWriteExecute = true;
NoNewPrivileges = true;
# TODO :: RootDirectory
# TODO :: CapbalitiyBoundingSet
};
};
}
(lib.mkIf com.virtualHost.nginx.enable {
services.nginx.virtualHosts.${com.virtualHost.domain} = let
# TODO :: use unix socket instead
proxyPass = "http://127.0.0.1:${toString com.settings.PORT}";
in {
locations."/" = {
inherit proxyPass;
recommendedProxySettings = true;
};
# this is necessary for live comment section updates
locations."/ws" = {
inherit proxyPass;
proxyWebsockets = true;
recommendedProxySettings = true;
};
};
})
]);
}