-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathvoyager.nix
135 lines (133 loc) · 3.94 KB
/
voyager.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
{ self, ... }: {
perSystem = { self', pkgs, system, config, crane, stdenv, dbg, ... }:
let
voyager = crane.buildWorkspaceMember {
crateDirFromRoot = "voyager";
extraEnv = {
SQLX_OFFLINE = "1";
};
};
in
{
packages = voyager.packages // {
voy-send-msg = pkgs.writeShellApplication {
name = "voy-send-msg";
runtimeInputs = [ pkgs.curl ];
text = ''
set -e
curl localhost:65534/msg -H "content-type: application/json" -d "$@"
'';
};
};
checks = voyager.checks;
};
flake.nixosModules.voyager = { lib, pkgs, config, ... }:
with lib;
let
cfg = config.services.voyager;
in
{
options.services.voyager = {
enable = mkEnableOption "Voyager service";
package = mkOption {
type = types.package;
default = self.packages.${pkgs.system}.voyager;
};
chains = mkOption {
# The configuration design is breaking quite often, would be a waste
# of effort to fix the type for now.
type = types.attrs;
};
workers = mkOption {
type = types.int;
default = 20;
};
runtime-max-secs = mkOption {
type = types.int;
default = 1800;
};
db-url = mkOption {
type = types.str;
default = "postgres://voyager:voyager@localhost/voyager";
};
db-min-conn = mkOption {
type = types.int;
default = 20;
};
db-max-conn = mkOption {
type = types.int;
default = 20;
};
log-level = mkOption {
type = types.str;
default = "info";
description = "RUST_LOG passed to voyager";
example = "voyager=debug";
};
log-format = mkOption {
type = types.enum [ "json" "text" ];
default = "json";
example = "text";
};
};
config =
let
configJson = pkgs.writeText "config.json" (builtins.toJSON {
chain = cfg.chains;
voyager = {
num_workers = cfg.workers;
queue = {
type = "pg-queue";
database_url = cfg.db-url;
min_connections = cfg.db-min-conn;
max_connections = cfg.db-max-conn;
idle_timeout = null;
max_lifetime = null;
};
};
});
in
mkIf cfg.enable {
systemd.services.voyager-migration = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
description = "Voyager Migration";
serviceConfig = {
Type = "oneshot";
ExecStart = ''
${pkgs.lib.meta.getExe cfg.package} \
--config-file-path ${configJson} \
-l ${cfg.log-format} \
run-migrations
'';
};
environment = {
RUST_LOG = "debug";
RUST_BACKTRACE = "full";
};
};
systemd.services.voyager = {
wantedBy = [ "multi-user.target" ];
after = [ "voyager-migration.service" ];
partOf = [ "voyager-migration.service" ];
requires = [ "voyager-migration.service" ];
description = "Voyager";
serviceConfig = {
Type = "simple";
ExecStart = ''
${pkgs.lib.getExe cfg.package} \
--config-file-path ${configJson} \
-l ${cfg.log-format} \
relay
'';
Restart = mkForce "always";
RestartSec = 10;
RuntimeMaxSec = cfg.runtime-max-secs;
};
environment = {
RUST_LOG = "${cfg.log-level}";
};
};
};
};
}