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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
|
{
config,
lib,
pkgs,
...
}:
let
inherit (lib)
mkEnableOption
mkIf
mkOption
mkPackageOption
types
;
inherit (lib.attrsets) filterAttrsRecursive;
cfg = config.services.headplane;
settingsFormat = pkgs.formats.yaml { };
filterSettings = lib.converge (
filterAttrsRecursive (
_: v:
!lib.elem v [
{ }
null
]
)
);
agentSettings = cfg.settings.integration.agent;
settings = cfg.settings // {
integration = cfg.settings.integration // {
agent = if agentSettings == null || !agentSettings.enabled then null else agentSettings;
};
};
settingsFile = settingsFormat.generate "headplane-config.yaml" (filterSettings settings);
in
{
options.services.headplane = {
enable = mkEnableOption "Headplane";
package = mkPackageOption pkgs "headplane" { };
agent.package = mkPackageOption pkgs "headplane-agent" { };
debug = mkEnableOption "debug logging";
settings = mkOption {
description = ''
Headplane configuration options. Generates a YAML config file.
See <https://github.com/tale/headplane/blob/main/config.example.yaml>.
'';
type = types.submodule {
options = {
server = mkOption {
type = types.submodule {
options = {
host = mkOption {
type = types.str;
default = "127.0.0.1";
description = "The host address to bind to.";
example = "0.0.0.0";
};
port = mkOption {
type = types.port;
default = 3000;
description = "The port to listen on.";
};
base_url = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
The base URL for Headplane. Used for OIDC redirect callback URL
detection. Should not include the dashboard prefix (/admin).
'';
example = "https://headplane.example.com";
};
cookie_secret_path = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
Path to a file containing the cookie secret.
The secret must be exactly 32 characters long.
'';
example = lib.literalExpression "config.sops.secrets.headplane_cookie.path";
};
cookie_secure = mkOption {
type = types.bool;
default = true;
description = ''
Should the cookies only work over HTTPS?
Set to false if running via HTTP without a proxy.
Recommended to be true in production.
'';
};
cookie_max_age = mkOption {
type = types.ints.positive;
default = 86400;
description = "The maximum age of the session cookie in seconds.";
};
cookie_domain = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Restrict the cookie to a specific domain.
This may not work as expected if not using a reverse proxy.
'';
example = "example.com";
};
data_path = mkOption {
type = types.path;
default = "/var/lib/headplane";
description = ''
The path to persist Headplane specific data.
All data going forward is stored in this directory, including the internal database and any cache related files.
'';
example = "/var/lib/headplane";
};
proxy_auth = mkOption {
type = types.nullOr (
types.submodule {
options = {
enabled = mkEnableOption ''
delegating Headplane authentication to a trusted reverse proxy
instead of logging in through Headplane directly. Identity
headers are only trusted on requests whose client IP matches
`allowed_cidrs`; all Headscale API calls then use
`headscale.api_key_path`
'';
allowed_cidrs = mkOption {
type = types.listOf types.str;
default = [
"127.0.0.1/32"
"::1/128"
];
description = ''
Client CIDRs allowed to authenticate via the configured proxy
headers.
'';
example = [ "10.0.0.0/8" ];
};
trusted_proxy_cidrs = mkOption {
type = types.listOf types.str;
default = [
"127.0.0.1/32"
"::1/128"
];
description = ''
Direct proxy CIDRs trusted to supply `ip_header`.
'';
example = [ "127.0.0.1/32" ];
};
ip_header = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Header containing the original client IP, such as
`X-Forwarded-For` or `X-Real-IP`. Only read when the direct
socket peer matches `trusted_proxy_cidrs`.
'';
example = "X-Forwarded-For";
};
user_header = mkOption {
type = types.str;
default = "Remote-User";
description = ''
Header containing the stable authenticated user identity.
'';
example = "Remote-User";
};
email_header = mkOption {
type = types.nullOr types.str;
default = null;
description = "Optional header containing the authenticated user's email address.";
example = "Remote-Email";
};
name_header = mkOption {
type = types.nullOr types.str;
default = null;
description = "Optional header containing the authenticated user's display name.";
example = "Remote-Name";
};
picture_header = mkOption {
type = types.nullOr types.str;
default = null;
description = "Optional header containing the authenticated user's profile picture URL.";
example = "Remote-Picture";
};
};
}
);
default = null;
description = ''
Delegate Headplane authentication to a trusted reverse proxy. See the
upstream [Proxy Authentication docs](https://github.com/tale/headplane/blob/main/docs/features/proxy-auth.md).
'';
};
};
};
default = { };
description = "Server configuration for Headplane web application.";
};
headscale = mkOption {
type = types.submodule {
options = {
api_key_path = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
Path to a file containing a Headscale API key.
This is required for OIDC authentication aswell for the Headplane agent.
'';
example = lib.literalExpression "config.sops.secrets.headplane_pre_authkey.path";
};
url = mkOption {
type = types.str;
default = "http://127.0.0.1:${toString config.services.headscale.port}";
defaultText = lib.literalExpression "http://127.0.0.1:\${toString config.services.headscale.port}";
description = ''
The URL to your Headscale instance.
All API requests are routed through this URL.
THIS IS NOT the gRPC endpoint, but the HTTP endpoint.
IMPORTANT: If you are using TLS this MUST be set to `https://`.
'';
example = "https://headscale.example.com";
};
tls_cert_path = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
Path to a file containing the TLS certificate.
'';
example = lib.literalExpression "config.sops.secrets.tls_cert.path";
};
public_url = mkOption {
type = types.nullOr types.str;
default = config.services.headscale.settings.server_url;
defaultText = lib.literalExpression "config.services.headscale.settings.server_url";
description = "Public URL if different. This affects certain parts of the web UI.";
example = "https://headscale.example.com";
};
config_path = mkOption {
type = types.nullOr types.path;
default = config.services.headscale.configFile;
defaultText = lib.literalExpression "config.services.headscale.configFile";
description = ''
Path to the Headscale configuration file.
This is optional, but HIGHLY recommended for the best experience.
If this is read only, Headplane will show your configuration settings
in the Web UI, but they cannot be changed.
'';
example = "/etc/headscale/config.yaml";
};
config_strict = mkEnableOption ''
Headplane internally validates the Headscale configuration
to ensure that it changes the configuration in a safe way.
Disabled by default because it clashes with how the Headplane works in NixOS.
'';
dns_records_path = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
If you are using `dns.extra_records_path` in your Headscale configuration, you need to set this to the path for Headplane to be able to read the DNS records.
Ensure that the file is both readable and writable by the Headplane process.
When using this, Headplane will no longer need to automatically restart Headscale for DNS record changes.
'';
example = "/var/lib/headplane/extra_records.json";
};
};
};
default = { };
description = "Headscale specific settings for Headplane integration.";
};
integration = mkOption {
type = types.submodule {
options = {
agent = mkOption {
type = types.nullOr (
types.submodule {
options = {
enabled = mkOption {
type = types.bool;
default = false;
description = ''
The Headplane agent allows retrieving information about nodes.
This allows the UI to display version, OS, and connectivity data.
You will see the Headplane agent in your Tailnet as a node when it connects.
'';
};
executable_path = mkOption {
type = types.path;
readOnly = true;
default = "${cfg.agent.package}/bin/hp_agent";
defaultText = lib.literalExpression ''"''${config.services.headplane.agent.package}/bin/hp_agent"'';
description = ''
Path to the headplane agent binary.
'';
};
host_name = mkOption {
type = types.str;
default = "headplane-agent";
description = "Optionally change the name of the agent in the Tailnet.";
};
cache_ttl = mkOption {
type = types.ints.positive;
default = 180000;
description = ''
How long to cache agent information (in milliseconds).
If you want data to update faster, reduce the TTL, but this will increase the frequency of requests to Headscale.
'';
};
work_dir = mkOption {
type = types.path;
default = "/var/lib/headplane/agent";
description = ''
Do not change this unless you are running a custom deployment.
The work_dir represents where the agent will store its data to be able to automatically reauthenticate with your Tailnet.
It needs to be writable by the user running the Headplane process.
'';
};
};
}
);
default = null;
description = "Agent configuration for the Headplane agent.";
};
proc = mkOption {
type = types.submodule {
options = {
enabled = mkOption {
type = types.bool;
default = true;
description = ''
Enable "Native" integration that works when Headscale and
Headplane are running outside of a container. There is no additional
configuration, but you need to ensure that the Headplane process
can terminate the Headscale process.
'';
};
};
};
default = { };
description = "Native process integration settings.";
};
};
};
default = { };
description = "Integration configurations for Headplane to interact with Headscale.";
};
oidc = mkOption {
type = types.nullOr (
types.submodule {
options = {
enabled = mkOption {
type = types.bool;
default = true;
description = ''
Explicitly control OIDC availability.
Set to false to define OIDC config without enabling it.
'';
};
issuer = mkOption {
type = types.str;
description = "URL to OpenID issuer.";
example = "https://provider.example.com/issuer-url";
};
client_id = mkOption {
type = types.str;
description = "The client ID for the OIDC client.";
example = "your-client-id";
};
client_secret_path = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
Path to a file containing the OIDC client secret.
'';
example = lib.literalExpression "config.sops.secrets.oidc_client_secret.path";
};
disable_api_key_login = mkOption {
type = types.bool;
default = false;
description = "Whether to disable API key login.";
};
token_endpoint_auth_method = mkOption {
type = types.nullOr (
types.enum [
"client_secret_post"
"client_secret_basic"
"client_secret_jwt"
]
);
default = null;
description = ''
The token endpoint authentication method.
If not set, Headplane will auto-detect the best method
and fall back to client_secret_basic.
'';
};
use_pkce = mkOption {
type = types.bool;
default = false;
description = ''
Whether to use PKCE when authenticating users.
Your OIDC provider must support PKCE and it must be enabled on the client.
'';
};
profile_picture_source = mkOption {
type = types.enum [
"oidc"
"gravatar"
];
default = "oidc";
description = "Source for user profile pictures.";
};
scope = mkOption {
type = types.str;
default = "openid email profile";
description = "OIDC scope to request.";
};
extra_params = mkOption {
type = types.nullOr (types.attrsOf types.str);
default = null;
description = "Extra parameters to send to the OIDC provider.";
example = {
prompt = "consent";
};
};
authorization_endpoint = mkOption {
type = types.nullOr types.str;
default = null;
description = "Custom authorization endpoint URL.";
example = "https://provider.example.com/authorize";
};
token_endpoint = mkOption {
type = types.nullOr types.str;
default = null;
description = "Custom token endpoint URL.";
example = "https://provider.example.com/token";
};
userinfo_endpoint = mkOption {
type = types.nullOr types.str;
default = null;
description = "Custom userinfo endpoint URL.";
example = "https://provider.example.com/userinfo";
};
};
}
);
default = null;
description = "OIDC Configuration for authentication.";
};
};
};
default = { };
};
};
config = mkIf cfg.enable {
assertions = [
{
assertion = config.services.headscale.enable;
message = ''
services.headplane requires services.headscale.enable = true.
The headplane module references the headscale systemd unit
(in `after`/`requires`) and reads its configFile, port, user,
and group. Enable headscale or disable headplane.
'';
}
{
assertion = cfg.settings.server.cookie_secret_path != null;
message = ''
services.headplane.settings.server.cookie_secret_path must be set.
Headplane refuses to start without either `cookie_secret` or
`cookie_secret_path` (validated at startup, see upstream
app/server/config/schema.ts). The NixOS module only exposes the
*_path form to keep secrets out of the world-readable /nix/store.
Provide a path to a file containing a 32-character secret, e.g.
via systemd `LoadCredential` or sops-nix.
'';
}
{
assertion = cfg.settings.oidc == null || cfg.settings.headscale.api_key_path != null;
message = ''
services.headplane.settings.headscale.api_key_path must be set
when services.headplane.settings.oidc is non-null.
Headplane's OIDC flow requires a Headscale API key to mint sessions.
'';
}
{
assertion =
agentSettings == null || !agentSettings.enabled || cfg.settings.headscale.api_key_path != null;
message = ''
services.headplane.settings.headscale.api_key_path must be set when the agent is enabled.
'';
}
{
assertion =
cfg.settings.server.proxy_auth == null
|| !cfg.settings.server.proxy_auth.enabled
|| cfg.settings.headscale.api_key_path != null;
message = ''
services.headplane.settings.headscale.api_key_path must be set
when services.headplane.settings.server.proxy_auth.enabled is true.
Proxy authentication requires a Headscale API key to make Headscale
API calls on behalf of proxy-authenticated users.
'';
}
];
environment = {
systemPackages = [ cfg.package ];
etc."headplane/config.yaml".source = settingsFile;
};
systemd.services.headplane = {
description = "Headscale Web UI";
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [
"network-online.target"
config.systemd.services.headscale.name
];
requires = [ config.systemd.services.headscale.name ];
restartTriggers = [ settingsFile ];
environment = {
HEADPLANE_DEBUG_LOG = toString cfg.debug;
};
serviceConfig = {
User = config.services.headscale.user;
Group = config.services.headscale.group;
StateDirectory = "headplane";
ExecStart = lib.getExe cfg.package;
Restart = "always";
RestartSec = 5;
# TODO: Harden `systemd` security according to the "The Principle of Least Power".
# See: `$ systemd-analyze security headplane`.
};
};
};
}
|