-
Notifications
You must be signed in to change notification settings - Fork 0
/
innernet.nix
91 lines (76 loc) · 2.41 KB
/
innernet.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
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.innernet;
in {
meta.maintainers = with maintainers; [ PhilTaken ];
options.services.innernet = with types; {
enable = mkEnableOption "innernet client daemon";
port = mkOption {
type = port;
default = 51820;
description = "The port to listen on for tunnel traffic";
};
configFile = mkOption {
default = null;
type = nullOr path;
description = "Path to the config file for the innernet server interface.";
};
config = mkOption {
default = null;
type = nullOr str;
description = "Configuration as string";
};
interfaceName = mkOption {
default = null;
type = nullOr str;
description = "Interface name for the server";
example = "innernet0";
};
package = mkOption {
type = package;
default = pkgs.innernet;
defaultText = "pkgs.innernet";
description = "The package to use for innernet";
};
openFirewall = mkOption {
type = bool;
default = false;
};
};
config = let
interfaceName = if cfg.configFile != null && cfg.interfaceName == null
then builtins.head (builtins.match "/nix/store/[a-zA-Z0-9]+-([a-zA-Z_-]+).conf" "${cfg.configFile}")
else cfg.interfaceName;
in mkIf cfg.enable {
assertions = [
{
assertion = (cfg.configFile == null) != (cfg.config == null);
message = "Either but not both `configFile` and `config` should be specified for innernet.";
}
];
networking.wireguard.enable = true;
networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];
networking.firewall.allowedUDPPorts = mkIf cfg.openFirewall [ cfg.port ];
environment.systemPackages = [ cfg.package ]; # for the CLI
environment.etc = {
"innernet-server/${interfaceName}.conf" = {
mode = "0644";
text = if cfg.configFile != null
then fileContents "${cfg.configFile}"
else "${cfg.config}";
};
};
systemd.packages = [ cfg.package ];
systemd.services.innernetd = {
after = [ "network-online.target" "nss-lookup.target" ];
wantedBy = [ "multi-user.target" ];
path = [ pkgs.iproute ];
environment = { RUST_LOG = "info"; };
serviceConfig = {
Restart = "always";
ExecStart = "${cfg.package}/bin/innernet-server serve ${interfaceName}";
};
};
};
}