-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
243 lines (234 loc) · 8.02 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
process-compose.url = "github:diamondburned/process-compose?ref=better-fps";
process-compose.inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
outputs =
{
self,
nixpkgs,
flake-utils,
process-compose,
...
}@inputs:
let
lib = nixpkgs.lib;
twipiPublicHost = "twipi.libdb.so";
serverPhoneNumbers = [ "+19876543210" ];
clientPhoneNumbers = [ "+11234567890" ];
in
flake-utils.lib.eachDefaultSystem (
system:
let
lib = nixpkgs.lib;
pkgs = nixpkgs.legacyPackages.${system}.extend (
self: super: { inherit (process-compose.packages.${system}) process-compose; }
);
mkJSONConfig = name: config: pkgs.writeText name (builtins.toJSON config);
mkProcessesConfig = (
{
basePort,
production ? false,
stateDirectory,
}:
with lib;
with builtins;
rec {
twicp = {
port = basePort;
command = "task dev";
healthPath = "/";
environment = {
HOST = if production then "0.0.0.0" else "localhost";
TWIPI_URL =
if production then "https://${twipiPublicHost}" else "http://localhost:${toString twipi.port}";
};
dependsOn = [ "twipi" ];
};
fakesms = {
port = basePort + 1;
command = "task dev";
healthPath = "/";
environment = {
HOST = if production then "0.0.0.0" else "localhost";
WSBRIDGE_URL =
if production then
"wss://${twipiPublicHost}/api/fakesms/ws"
else
"ws://localhost:${toString twipi.port}/api/fakesms/ws";
WSBRIDGE_NUMBER_SELF = head clientPhoneNumbers;
WSBRIDGE_NUMBER_SERVER = head serverPhoneNumbers;
};
dependsOn = [ "twipi" ];
};
twipi = {
port = basePort + 100;
command = "task dev";
healthPath = "/health";
environment = {
TWID_CONFIG = mkJSONConfig "twid.json" {
listen_addr = "localhost:${toString twipi.port}";
twisms = {
services = [
{
module = "wsbridge_server";
http_path = "/api/fakesms/ws";
phone_numbers = serverPhoneNumbers;
acknowledgement_timeout = "1s";
# TODO: figure out why SQLite is shitting the bed.
# message_queue = {
# sqlite = {
# path = "${stateDirectory}/twipi/wsbridge-queue.sqlite3";
# max_age = "1400h";
# };
# };
}
];
};
twicmd = {
parsers = [ { module = "slash"; } ];
services = [
{
module = "http";
name = "discord";
base_url = "http://localhost:${toString twidiscord.port}";
}
{
module = "http";
name = "ttt";
base_url = "http://localhost:${toString twittt.port}";
}
];
};
};
};
dependsOn = [
"twidiscord"
"twittt"
];
};
twidiscord = {
port = basePort + 101;
command = ''
go run . \
-l :${toString twidiscord.port} \
-p "${stateDirectory}/twidiscord/state.db"
'';
healthPath = "/health";
after = [ "twipi-generate" ];
};
twittt = {
port = basePort + 102;
command = "go run . -l :${toString twittt.port}";
healthPath = "/health";
after = [ "twipi-generate" ];
};
twipi-generate = {
workingDirectory = "twipi";
command = ''
task generate
'';
};
}
);
processComposeFile =
args:
mkJSONConfig "process-compose.json" (
let
stateDirectory = "/tmp/twipi";
processesConfig = mkProcessesConfig (
{
basePort = 5000;
inherit stateDirectory;
}
// args
);
mapDependsOn =
processes: condition:
builtins.listToAttrs (map (process: lib.nameValuePair process { inherit condition; }) processes);
in
{
version = "0.5";
processes = lib.mapAttrs' (name: process: {
inherit name;
value = (
{
working_dir = "./" + (process.workingDirectory or name);
command =
let
script = pkgs.writeShellScript "${name}.sh" ''
set -ex
mkdir -p "$STATE_DIRECTORY"
${process.command}
'';
in
"nix develop -c ${script}";
environment = lib.mapAttrsToList (k: v: "${k}=${v}") (
(process.environment or { })
// (lib.optionalAttrs (process ? "port") { PORT = toString process.port; })
// ({ STATE_DIRECTORY = stateDirectory + "/" + name; })
);
shutdown = {
signal = 2; # SIGINT
timeout_seconds = 5;
};
depends_on =
{ }
// (mapDependsOn (process.after or [ ]) "process_completed_successfully")
// (mapDependsOn (process.dependsOn or [ ]) "process_healthy");
}
// (lib.optionalAttrs (process ? "healthPath") {
readiness_probe = {
http_get = {
host = "localhost";
port = process.port;
path = process.healthPath;
};
period_seconds = 1;
failure_threshold = 300;
};
availability = {
restart = "exit_on_failure";
backoff_seconds = 10;
};
})
);
}) processesConfig;
}
);
processCompose =
args:
pkgs.writeShellApplication rec {
name = "twipi-dev";
meta.mainProgram = "twipi-dev";
runtimeInputs = [ pkgs.process-compose ];
text = ''
exec process-compose up \
--config ${processComposeFile args} \
--ref-rate 100ms \
--no-server \
--keep-tui
'';
};
in
{
packages = rec {
dev = processCompose { };
prod = processCompose { production = true; };
};
devShells.default = pkgs.mkShell {
packages = [
self.formatter.${system}
pkgs.go_1_22
(pkgs.writeShellScriptBin "run" "nix run .?submodules=1#dev")
];
};
formatter = pkgs.nixfmt-rfc-style;
}
);
}