-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
service.nix
94 lines (81 loc) · 2.26 KB
/
service.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
{ config, lib, pkgs, ... }:
let
glot =
(import ./default.nix {}).glot.components.exes.glot;
cfg =
config.services.glot;
commonEnvironment = {
LC_ALL = "en_US.UTF-8";
LOCALE_ARCHIVE = "${pkgs.glibcLocales}/lib/locale/locale-archive";
};
in
{
options = {
services.glot = {
enable = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether to enable glot";
};
enablePostgres = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether to enable postgres";
};
environment = lib.mkOption {
type = lib.types.attrs;
default = {};
description = "Environment variables for the service";
};
};
};
config = lib.mkMerge [
(
lib.mkIf cfg.enable {
# Service user
users.extraUsers.glot = {
isNormalUser = true;
description = "glot service user";
};
# Systemd service
systemd.services.glot = {
description = "glot";
wantedBy = [ "multi-user.target" ];
requires =
if cfg.enablePostgres then
[ "postgresql.service" ]
else
[];
serviceConfig =
{
WorkingDirectory = "${cfg.environment.WORK_DIR}";
ExecStart = "${glot}/bin/glot";
Restart = "always";
User = "glot";
};
environment = commonEnvironment // cfg.environment;
};
}
)
(
lib.mkIf cfg.enablePostgres {
# Database
services.postgresql = {
enable = true;
package = pkgs.postgresql_12;
enableTCPIP = true;
authentication = lib.mkOverride 10 ''
local all all trust
host all all ::1/128 trust
host all all 127.0.0.1/32 md5
'';
initialScript = pkgs.writeText "backend-initScript" ''
CREATE ROLE ${cfg.environment.DB_USER} WITH LOGIN PASSWORD '${cfg.environment.DB_PASS}' CREATEDB;
CREATE DATABASE ${cfg.environment.DB_NAME};
GRANT ALL PRIVILEGES ON DATABASE ${cfg.environment.DB_NAME} TO ${cfg.environment.DB_USER};
'';
};
}
)
];
}