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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.flame;
settingsFormat = pkgs.formats.json { };
# Accepts either a list of strings or a raw semicolon-separated string.
schemaToStr = x: if builtins.isList x then lib.concatStringsSep ";" x else x;
# Needed to prepopulate DB
sqlQuote = s: "'" + builtins.replaceStrings [ "'" ] [ "''" ] s + "'";
seedSql = pkgs.writeText "flame-seed.sql" ''
${lib.optionalString (cfg.apps != [ ] || cfg.categories != [ ]) ''
DELETE FROM bookmarks;
DELETE FROM categories;
DELETE FROM apps;
''}
${lib.concatMapStringsSep "\n" (app: ''
INSERT INTO apps (name, url, icon, description, isPinned, createdAt, updatedAt)
VALUES (${sqlQuote app.name}, ${sqlQuote app.url}, ${sqlQuote app.icon}, ${sqlQuote app.description}, ${
if app.isPinned then "1" else "0"
}, datetime('now'), datetime('now'));
'') cfg.apps}
${lib.concatMapStringsSep "\n" (cat: ''
INSERT INTO categories (name, isPinned, createdAt, updatedAt)
VALUES (${sqlQuote cat.name}, ${
if cat.isPinned then "1" else "0"
}, datetime('now'), datetime('now'));
${lib.concatMapStringsSep "\n" (bm: ''
INSERT INTO bookmarks (name, url, icon, categoryId, createdAt, updatedAt)
VALUES (${sqlQuote bm.name}, ${sqlQuote bm.url}, ${sqlQuote bm.icon}, (SELECT id FROM categories WHERE name = ${sqlQuote cat.name} ORDER BY id DESC LIMIT 1), datetime('now'), datetime('now'));
'') cat.bookmarks}
'') cfg.categories}
'';
cssFile = pkgs.writeText "flame-custom.css" cfg.customCSS;
# Build-time symlink farm of everything Flame ships except data/ and
# public/, which are left as empty placeholders here and populated at
# runtime (data/ is real state; public/ is refreshed from cfg.package
# on every start, since it holds built client assets).
appTree = pkgs.runCommand "flame-app-tree" { } ''
mkdir -p $out
for entry in ${cfg.package}/lib/flame/*; do
name=$(basename "$entry")
if [ "$name" != data ] && [ "$name" != public ]; then
ln -s "$entry" "$out/$name"
fi
done
mkdir -p $out/data $out/public
'';
# WEATHER_API_KEY is deliberately excluded here; it's injected at
# runtime from `weatherApiKeyFile` so it never touches the Nix store.
settingsFile = settingsFormat.generate "flame-settings.json" (
lib.filterAttrs (n: _: n != "weatherApiKeyFile") cfg.settings
// lib.optionalAttrs (cfg.settings ? greetingsSchema) {
greetingsSchema = schemaToStr cfg.settings.greetingsSchema;
}
// lib.optionalAttrs (cfg.settings ? daySchema) {
daySchema = schemaToStr cfg.settings.daySchema;
}
// lib.optionalAttrs (cfg.settings ? monthSchema) {
monthSchema = schemaToStr cfg.settings.monthSchema;
}
);
in
{
meta.maintainers = with lib.maintainers; [ DerGrumpf ];
options.services.flame = {
enable = lib.mkEnableOption "Flame, a self-hosted startpage for your server";
package = lib.mkPackageOption pkgs "flame" { };
port = lib.mkOption {
type = lib.types.port;
default = 5005;
description = "Port on which to serve the Flame web interface.";
};
passwordFile = lib.mkOption {
type = lib.types.path;
description = ''
Path to a file containing the password to log in to Flame's settings panel.
This is the recommended option as it avoids storing the password in the Nix store.
Compatible with sops-nix and agenix.
'';
example = "/run/secrets/flame-password";
};
openFirewall = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether to open the firewall for the port used by Flame.";
};
customCSS = lib.mkOption {
type = lib.types.lines;
default = "";
description = ''
Custom CSS injected into Flame's UI, written to
{file}`public/flame.css` on every service start. Can also be used
to define a fully custom theme via CSS custom properties — see
[Flame's Custom CSS wiki page](https://github.com/pawelmalak/flame/wiki/Custom-CSS).
'';
example = ''
.Home_SettingsButton__Qvn8C {
border-radius: 0 !important;
}
'';
};
categories = lib.mkOption {
type = lib.types.listOf (
lib.types.submodule {
options = {
name = lib.mkOption {
type = lib.types.str;
description = "Name of the bookmark category.";
};
isPinned = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether the category is pinned by default.";
};
bookmarks = lib.mkOption {
default = [ ];
description = "Bookmarks belonging to this category.";
type = lib.types.listOf (
lib.types.submodule {
options = {
name = lib.mkOption {
type = lib.types.str;
description = "Name of the bookmark.";
};
url = lib.mkOption {
type = lib.types.str;
description = "URL of the bookmark.";
};
icon = lib.mkOption {
type = lib.types.str;
default = "";
description = "Icon name or URL for the bookmark.";
};
};
}
);
};
};
}
);
default = [ ];
description = ''
Bookmark categories and their bookmarks. When non-empty, this
fully replaces the contents of Flame's `categories` and
`bookmarks` tables on every service start — any bookmarks added
through the web UI will not persist across restarts.
'';
example = [
{
name = "Dev";
bookmarks = [
{
name = "GitHub";
url = "https://github.com";
}
];
}
];
};
apps = lib.mkOption {
type = lib.types.listOf (
lib.types.submodule {
options = {
name = lib.mkOption {
type = lib.types.str;
description = "Name of the app.";
};
url = lib.mkOption {
type = lib.types.str;
description = "URL of the app.";
};
icon = lib.mkOption {
type = lib.types.str;
default = "cancel";
description = "Icon name or URL for the app.";
};
description = lib.mkOption {
type = lib.types.str;
default = "";
description = "Short description shown for the app.";
};
isPinned = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether the app is pinned by default.";
};
};
}
);
default = [ ];
description = ''
Applications shown on the dashboard. When non-empty, this fully
replaces the contents of Flame's `apps` table on every service
start — any apps added through the web UI will not persist
across restarts.
'';
example = [
{
name = "Router";
url = "http://192.168.1.1";
}
];
};
settings = lib.mkOption {
type = lib.types.submodule {
freeformType = settingsFormat.type;
options = {
weatherApiKeyFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = ''
Path to a file containing the API key obtained from https://www.weatherapi.com used for
Flame's weather widget.
Compatible with sops-nix and agenix.
'';
example = "/run/secrets/flame-weather-api-key";
};
};
};
default = { };
description = ''
Flame settings, written to Flame's settings JSON on every service
start. Accepts any key Flame's settings API supports; see
[Flame's source](https://github.com/pawelmalak/flame/blob/master/client/src/context/context.js)
for the current schema, since Flame does not publish separate
settings documentation.
`greetingsSchema`, `daySchema`, and `monthSchema` accept either a
list of strings or a single semicolon-separated string.
'';
example = {
lat = 52.52;
long = 13.405;
customTitle = "My Dashboard";
hideHeader = true;
};
};
};
config = lib.mkIf cfg.enable {
systemd.services = {
flame-seed = lib.mkIf (cfg.apps != [ ] || cfg.categories != [ ]) {
description = "Seed Flame apps and bookmarks";
after = [ "flame.service" ];
requires = [ "flame.service" ];
wantedBy = [ "flame.service" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
DynamicUser = true;
StateDirectory = "flame";
};
script = ''
for i in $(seq 1 30); do
if ${lib.getExe pkgs.sqlite} /var/lib/flame/app/data/db.sqlite \
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='apps';" | grep -q 1; then
break
fi
sleep 1
done
${lib.getExe pkgs.sqlite} /var/lib/flame/app/data/db.sqlite < ${seedSql}
'';
};
flame = {
description = "Flame, a self-hosted startpage for your server";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
preStart = ''
for entry in ${appTree}/*; do
name=$(basename "$entry")
if [ "$name" != data ] && [ "$name" != public ]; then
ln -sfn "$entry" /var/lib/flame/app/"$name"
fi
done
for entry in /var/lib/flame/app/data /var/lib/flame/app/public; do
if [ -L "$entry" ]; then
rm -f "$entry"
fi
done
mkdir -p /var/lib/flame/app/data/uploads /var/lib/flame/app/public
if [ ! -f /var/lib/flame/app/data/.secret ]; then
${lib.getExe pkgs.openssl} rand -hex 32 > /var/lib/flame/app/data/.secret
fi
chmod 644 /var/lib/flame/app/data/.secret
cp -r ${cfg.package}/lib/flame/public/. /var/lib/flame/app/public/
chmod -R u+w /var/lib/flame/app/public
install -m644 ${cssFile} /var/lib/flame/app/data/flame.css
${lib.getExe pkgs.jq} -n --slurpfile base ${cfg.package}/lib/flame/utils/init/initialConfig.json \
'$base[0]' > /var/lib/flame/app/data/config.json.tmp
${lib.optionalString (cfg.settings.weatherApiKeyFile != null) ''
weatherApiKey=$(cat ${cfg.settings.weatherApiKeyFile})
${lib.getExe pkgs.jq} --arg key "$weatherApiKey" '.WEATHER_API_KEY = $key' \
${settingsFile} > /var/lib/flame/app/data/settings-with-key.json
''}
${lib.getExe pkgs.jq} -s '.[0] * .[1]' \
/var/lib/flame/app/data/config.json.tmp \
${
if cfg.settings.weatherApiKeyFile != null then
"/var/lib/flame/app/data/settings-with-key.json"
else
settingsFile
} \
> /var/lib/flame/app/data/config.json
rm -f /var/lib/flame/app/data/config.json.tmp
chmod u+w /var/lib/flame/app/data/config.json
'';
serviceConfig = {
DynamicUser = true;
StateDirectory = [
"flame"
"flame/app"
];
WorkingDirectory = "/var/lib/flame/app";
Environment = [
"PORT=${toString cfg.port}"
"NODE_ENV=production"
"VERSION=${cfg.package.version}"
];
LoadCredential = [ "flame-password:${cfg.passwordFile}" ];
Restart = "always";
NoNewPrivileges = true;
PrivateTmp = true;
ProtectSystem = "strict";
ProtectHome = true;
CapabilityBoundingSet = "";
};
script = ''
export PASSWORD="$(cat "$CREDENTIALS_DIRECTORY/flame-password")"
exec ${lib.getExe pkgs.nodejs} --preserve-symlinks --preserve-symlinks-main server.js
'';
};
};
networking.firewall = lib.mkIf cfg.openFirewall {
allowedTCPPorts = [ cfg.port ];
};
};
}
|