-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.nix
158 lines (144 loc) · 4.31 KB
/
default.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
{ config, lib, pkgs, modulesPath, ... }:
with lib;
let cfg = config.profiles.sparky-headscale;
in {
options.profiles.sparky-headscale = {
enable = mkEnableOption (mdDoc "Enable the SPARKY headscale profile. Requires a configured tailnet on the host.");
probeSubnet = mkOption {
type = types.str;
description = mdDoc ''
Subnet of the probes in the tailnet.
'';
};
metricsIP = mkOption {
type = types.str;
description = mdDoc ''
IP of the metrics server in the tailnet.
'';
};
fqdn = mkOption {
type = types.str;
description = mdDoc ''
The FQDN for the nginx vHost of the headscale server.
'';
};
derp = {
enable = mkOption {
type = types.bool;
default = false;
description = mdDoc ''
Enable a local DERP server.
'';
};
regionID = mkOption {
type = types.int;
description = mdDoc ''
Region ID for the local DERP server.
'';
};
regionCode = mkOption {
type = types.str;
description = mdDoc ''
Region code for the local DERP server.
'';
};
regionName = mkOption {
type = types.str;
description = mdDoc ''
Region name for the local DERP server.
'';
};
};
nginx = mkOption {
type = types.submodule (
recursiveUpdate
(import (modulesPath + "/services/web-servers/nginx/vhost-options.nix") { inherit config lib; }) {}
);
default = { };
example = literalExpression ''
{
# To enable encryption and let let's encrypt take care of certificate
forceSSL = true;
enableACME = true;
# To set the SPARKY headscale virtualHost as the default virtualHost;
default = true;
}
'';
description = mdDoc ''
With this option, you can customize the nginx virtualHost settings.
'';
};
};
config = mkIf cfg.enable {
services.headscale = {
enable = true;
address = "127.0.0.1";
port = 8080;
settings = {
server_url = "https://${cfg.fqdn}";
dns_config.nameservers = [ "1.1.1.1" "1.0.0.1" ];
db_port = 5432;
ip_prefixes = [ "100.64.0.0/10" ]; # use default headscale prefix
acl_policy_path = let
headscaleIP = config.profiles.sparky-tailnet.ip;
in pkgs.writeText "headscale-acl" (builtins.toJSON {
hosts = {
"host:headscale" = "${headscaleIP}/128";
"host:metrics" = "${cfg.metricsIP}/128";
"host:probes" = "${cfg.probeSubnet}";
};
acls = [
{
# Allow SSH Headscale --> Probes
action = "accept";
proto = "tcp";
src = [ "host:headscale" ];
dst = [ "host:probes:22" ];
}
{
# Allow ICMPv6
action = "accept";
proto = "58"; # ICMPv6
src = [ "*" ];
dst = [ "*:*" ];
}
{
# Allow VM remote_write Probes --> Metrics
action = "accept";
proto = "tcp";
src = [ "host:probes" ];
dst = [ "host:metrics:80" ];
}
];
});
derp.server = mkIf cfg.derp.enable {
enabled = cfg.derp.enable;
region_id = cfg.derp.regionID;
region_code = cfg.derp.regionCode;
region_name = cfg.derp.regionName;
stun_listen_addr = "0.0.0.0:3478";
};
};
};
environment.systemPackages = with pkgs; [ headscale ];
networking.firewall.allowedTCPPorts = [ 80 443 ];
networking.firewall.allowedUDPPorts = mkIf cfg.derp.enable [ 3478 ];
services.nginx = {
enable = true;
virtualHosts."${cfg.fqdn}" = mkMerge [
cfg.nginx
{
locations."/" = {
proxyPass = "http://${config.services.headscale.address}:${toString config.services.headscale.port}";
proxyWebsockets = true;
extraConfig = ''
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
'';
};
}
];
};
};
}