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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.librechat;
meiliCfg = config.services.meilisearch;
format = pkgs.formats.yaml { };
configFile = format.generate "librechat.yaml" cfg.settings;
exportCredentials =
n: _: ''export ${n}="$(${config.systemd.package}/bin/systemd-creds cat ${n}_FILE)"'';
exportAllCredentials = vars: lib.concatStringsSep "\n" (lib.mapAttrsToList exportCredentials vars);
getLoadCredentialList = lib.mapAttrsToList (n: v: "${n}_FILE:${v}") cfg.credentials;
in
{
options.services.librechat = {
enable = lib.mkEnableOption "the LibreChat server";
package = lib.mkPackageOption pkgs "librechat" { };
openFirewall = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Whether to open the port in the firewall.
'';
};
dataDir = lib.mkOption {
type = lib.types.path;
default = "/var/lib/librechat";
example = "/persist/librechat";
description = "Absolute path for where the LibreChat server will use as its data directory to store logs, user uploads, and generated images.";
};
user = lib.mkOption {
type = lib.types.str;
default = "librechat";
example = "alice";
description = "The user to run the service as.";
};
group = lib.mkOption {
type = lib.types.str;
default = "librechat";
example = "users";
description = "The group to run the service as.";
};
credentials = lib.mkOption {
type = lib.types.attrsOf lib.types.path;
default = { };
example = {
CREDS_KEY = "/run/secrets/creds_key";
};
description = ''
Environment variables which are loaded from the contents of files at a file paths, mainly used for secrets.
See [LibreChat environment variables](https://www.librechat.ai/docs/configuration/dotenv).
Alternatively you can use `services.librechat.credentialsFile` to define all the variables in a single file.
'';
};
env = lib.mkOption {
type = lib.types.submodule {
freeformType =
with lib.types;
attrsOf (oneOf [
str
path
(coercedTo int toString str)
(coercedTo float toString str)
(coercedTo port toString str)
(coercedTo bool (x: if x then "true" else "false") str)
]);
options = {
CONFIG_PATH = lib.mkOption {
default = configFile;
internal = true;
readOnly = true;
};
PORT = lib.mkOption {
type = with lib.types; coercedTo port toString str;
default = 3080;
example = 2309;
description = "The value that will be passed to the PORT environment variable, telling LibreChat what to listen on.";
};
LIBRECHAT_LOG_DIR = lib.mkOption {
type = lib.types.str;
default = "${cfg.dataDir}/logs";
defaultText = lib.literalExpression "/var/lib/librechat/logs";
description = ''
Logs will be saved into this directory.
By default it is relative to `services.librechat.dataDir`.
'';
};
};
};
example = {
ALLOW_REGISTRATION = true;
HOST = "0.0.0.0";
PORT = 2309;
CONSOLE_JSON_STRING_LENGTH = 255;
};
description = ''
Environment variables that will be set for the service.
See [LibreChat environment variables](https://www.librechat.ai/docs/configuration/dotenv).
'';
};
credentialsFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
description = ''
Path to a file that contains environment variables.
See [LibreChat environment variables](https://www.librechat.ai/docs/configuration/dotenv).
Example content of the file:
```
CREDS_KEY=6d6deb03cdfb27ea454f6b9ddd42494bdce4af25d50d8aee454ddce583690cc5
```
Alternatively you can use `services.librechat.credentials` to define the value of each variable in a separate file.
'';
default = "/dev/null";
example = "/run/secrets/librechat";
};
settings = lib.mkOption {
type = lib.types.submodule {
freeformType = format.type;
};
default = {
version = "1.2.1";
};
example = {
version = "1.2.1";
cache = true;
interface = {
privacyPolicy = {
externalUrl = "https://librechat.ai/privacy-policy";
openNewTab = true;
};
};
endpoints = {
custom = [
{
name = "OpenRouter";
apiKey = "\${OPENROUTER_KEY}";
baseURL = "https://openrouter.ai/api/v1";
models = {
default = [ "meta-llama/llama-3-70b-instruct" ];
fetch = true;
};
titleConvo = true;
titleModule = "meta-llama/llama-3-70b-instruct";
dropParams = [ "stop" ];
modelDisplayLabel = "OpenRouter";
}
];
};
};
description = ''
A free-form attribute set that will be written to librechat.yaml.
See the [LibreChat configuration options](https://www.librechat.ai/docs/configuration/librechat_yaml).
You can use environment variables by wrapping them in $\{}. Take care to escape the \$ character.
'';
};
enableLocalDB = lib.mkEnableOption "a local mongodb instance";
meilisearch = lib.mkOption {
type = lib.types.submodule {
options = {
enable = lib.mkOption {
type = lib.types.bool;
default = false;
example = true;
description = ''
Whether to enable and configure Meilisearch locally for Librechat.
You will manually need to set `services.meilisearch.masterKeyFile`.
'';
};
};
};
default = { };
description = ''
See [LibreChat search feature](https://www.librechat.ai/docs/features/search).
'';
};
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = cfg.env ? MONGO_URI || cfg.credentials ? MONGO_URI;
message = "MongoDB is not configured, either set `services.librechat.enableLocalDB = true` or provide your own MongoDB instance by setting `services.librechat.env.MONGO_URI` or `services.credentials.MONGO_URI`.";
}
{
assertion =
cfg.credentialsFile != "/dev/null"
|| (
(cfg.env ? CREDS_KEY || cfg.credentials ? CREDS_KEY)
&& (cfg.env ? CREDS_IV || cfg.credentials ? CREDS_IV)
&& (cfg.env ? JWT_SECRET || cfg.credentials ? JWT_SECRET)
&& (cfg.env ? JWT_REFRESH_SECRET || cfg.credentials ? JWT_REFRESH_SECRET)
);
message = ''
CREDS_KEY, CREDS_IV, JWT_SECRET, and JWT_REFRESH_SECRET must be defined in `services.librechat.credentials` and point to locations of files on the host or in a file that `services.credentialsFile` is pointing to.
Alternatively it can be defined in `services.librechat.env` with literal values but they will be saved within the world-readable nix store.;
You can use https://www.librechat.ai/toolkit/creds_generator to generate these.
'';
}
{
assertion = cfg.meilisearch.enable -> meiliCfg.masterKeyFile != null;
message = ''
LibreChat's Meilisearch integration requires `services.meilisearch.masterKeyFile` to be set.
'';
}
];
networking.firewall.allowedTCPPorts = lib.optional cfg.openFirewall (lib.toInt cfg.env.PORT);
systemd.tmpfiles.settings."10-librechat"."${cfg.dataDir}".d = {
mode = "0755";
inherit (cfg) user group;
};
systemd.services.librechat = {
wantedBy = [ "multi-user.target" ];
after = [
"tmpfiles.target"
]
++ lib.optional cfg.meilisearch.enable "meilisearch.service";
wants = lib.optional cfg.meilisearch.enable "meilisearch.service";
description = "Open-source app for all your AI conversations, fully customizable and compatible with any AI provider";
environment = cfg.env;
script = # sh
''
${exportAllCredentials cfg.credentials}
cd ${cfg.dataDir}
${lib.getExe cfg.package}
'';
serviceConfig = {
Type = "simple";
User = cfg.user;
Group = cfg.group;
StateDirectory = baseNameOf cfg.dataDir;
WorkingDirectory = cfg.dataDir;
LoadCredential = getLoadCredentialList;
EnvironmentFile = cfg.credentialsFile;
Restart = "on-failure";
RestartSec = 10;
# Hardening
CapabilityBoundingSet = "";
NoNewPrivileges = true;
PrivateUsers = true;
PrivateTmp = true;
PrivateDevices = true;
PrivateMounts = true;
ProtectClock = true;
ProtectControlGroups = true;
ProtectHome = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
RestrictAddressFamilies = [
"AF_INET"
"AF_INET6"
"AF_UNIX"
];
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
UMask = "0077";
};
};
users.users.librechat = lib.mkIf (cfg.user == "librechat") {
name = "librechat";
isSystemUser = true;
group = "librechat";
description = "LibreChat server user";
};
users.groups.librechat = lib.mkIf (cfg.user == "librechat") { };
services.librechat.env.MONGO_URI = lib.mkIf cfg.enableLocalDB "mongodb://localhost:27017";
services.mongodb.enable = lib.mkIf cfg.enableLocalDB true;
services.meilisearch.enable = lib.mkIf cfg.meilisearch.enable true;
services.librechat.env.SEARCH = lib.mkIf cfg.meilisearch.enable true;
services.librechat.env.MEILI_HOST = lib.mkIf cfg.meilisearch.enable "http://${meiliCfg.settings.http_addr}";
services.librechat.credentials.MEILI_MASTER_KEY = lib.mkIf cfg.meilisearch.enable meiliCfg.masterKeyFile;
};
meta.maintainers = with lib.maintainers; [
gepbird
niklaskorz
rrvsh
];
}
|