-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.nix
161 lines (147 loc) · 4.33 KB
/
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
{ config, lib, pkgs, ... }:
let
cfg = config.services.exam-poll;
mkService = service: lib.mkMerge [
{
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
environment = {
HOME = "/tmp";
};
serviceConfig = {
Type = "exec";
Restart = "on-failure";
DynamicUser = true;
UMask = "0077";
WorkingDirectory = /tmp;
# Hardening
CapabilityBoundingSet = [ "" ];
DeviceAllow = [ "" ];
LockPersonality = true;
NoNewPrivileges = true;
PrivateDevices = true;
PrivateMounts = true;
PrivateTmp = true;
ProtectClock = true;
ProtectControlGroups = true;
ProtectHome = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
ProtectSystem = "strict";
RemoveIPC = true;
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
SystemCallArchitectures = "native";
};
}
service
];
in
{
options.services.exam-poll = with lib; {
enable = mkEnableOption "Exam Poll";
configureNginx = mkOption {
type = types.bool;
default = true;
description = "Whether to configure nginx as reverse proxy.";
};
frontend = {
package = mkOption {
type = types.package;
default = pkgs.exam-poll-frontend;
description = "The package to use.";
};
port = mkOption {
type = types.port;
default = 3000;
description = "The port to listen on.";
};
hostName = mkOption {
type = types.str;
example = "exam-poll.example.com";
description = "The hostname the application will be served on.";
};
};
backend = {
package = mkOption {
type = types.package;
default = pkgs.exam-poll-backend;
description = "The package to use.";
};
port = mkOption {
type = types.port;
default = 8000;
description = "The port to listen on.";
};
hostName = mkOption {
type = types.str;
example = "exam-poll-api.example.com";
description = "The hostname the api will be served on.";
};
};
mongodb = {
uri = mkOption {
type = types.str;
example = "mongodb://localhost:27017";
description = "MongoDB connection string.";
};
database = mkOption {
type = types.str;
description = "The database name.";
};
collection = mkOption {
type = types.str;
description = "The collection name.";
};
};
};
config = lib.mkIf cfg.enable {
systemd.services = {
exam-poll-frontend = mkService {
description = "Exam Poll frontend";
environment = {
PORT = toString cfg.frontend.port;
};
serviceConfig = {
ExecStart = lib.getExe (cfg.frontend.package.override {
localApiBaseUrl = "http://localhost:${toString cfg.backend.port}";
publicApiBaseUrl = "https://${cfg.backend.hostName}";
});
};
};
exam-poll-backend = mkService {
description = "Exam Poll backend";
environment = {
EXAM_POLL_HTTP_LISTEN = "localhost:${toString cfg.backend.port}";
EXAM_POLL_CORS_LIST = "https://${cfg.frontend.hostName},localhost";
EXAM_POLL_MONGODB = cfg.mongodb.uri;
EXAM_POLL_DATABASE = cfg.mongodb.database;
EXAM_POLL_COLLECTION = cfg.mongodb.collection;
};
serviceConfig = {
ExecStart = lib.getExe pkgs.exam-poll-backend;
};
};
};
services.nginx = lib.mkIf cfg.configureNginx {
enable = true;
recommendedProxySettings = true;
virtualHosts = {
${cfg.frontend.hostName} = {
forceSSL = lib.mkDefault true;
enableACME = lib.mkDefault true;
locations."/".proxyPass = "http://localhost:${toString cfg.frontend.port}";
};
${cfg.backend.hostName} = {
forceSSL = lib.mkDefault true;
enableACME = lib.mkDefault true;
locations."/".proxyPass = "http://localhost:${toString cfg.backend.port}";
};
};
};
};
}