nixos/modules/services/wireguard.nix

92 lines
3.3 KiB
Nix
Raw Normal View History

2023-12-07 17:18:55 +08:00
inputs:
{
options.nixos.services.wireguard = let inherit (inputs.lib) mkOption types; in
2023-12-07 17:18:55 +08:00
{
enable = mkOption { type = types.bool; default = false; };
# wg genkey | wg pubkey
publicKey = mkOption { type = types.nonEmptyStr; };
lighthouse = mkOption { type = types.bool; default = false; };
behindNat = mkOption
2024-01-19 19:45:56 +08:00
{
type = types.bool;
default = inputs.config.nixos.services.xray.client.enable;
};
listenIp = mkOption { type = types.nullOr types.nonEmptyStr; default = null; };
# if the host is behind xray, it should listen on another port, to make xray succeffully listen on 51820
listenPort = mkOption
{
type = types.ints.unsigned;
default = if inputs.config.nixos.services.wireguard.behindNat then 51821 else 51820;
};
wireguardIp = mkOption { type = types.nonEmptyStr; };
peers = mkOption { type = types.nonEmptyListOf types.nonEmptyStr; default = []; };
2023-12-07 17:18:55 +08:00
};
config =
let
inherit (inputs.lib) mkIf mkMerge;
inherit (inputs.config.nixos.services) wireguard;
inherit (builtins) map toString listToAttrs filter;
in mkIf wireguard.enable (mkMerge
2024-01-19 19:45:56 +08:00
[
{
assertions =
[{
assertion = !wireguard.behindNat -> wireguard.listenIp != null;
message = "wireguard.listenIp should be not null when behindNat is false.";
}];
}
2024-03-25 17:03:13 +08:00
{
networking =
2023-12-07 17:18:55 +08:00
{
firewall =
{
allowedUDPPorts = inputs.lib.mkIf (!wireguard.behindNat) [ wireguard.listenPort ];
2024-09-29 17:30:09 +08:00
trustedInterfaces = [ "wireguard" ];
};
2024-03-25 17:03:13 +08:00
wireguard.interfaces.wireguard =
2023-12-14 23:34:03 +08:00
{
2024-03-25 17:03:13 +08:00
ips = [ "${wireguard.wireguardIp}/24" ];
inherit (wireguard) listenPort;
privateKeyFile = inputs.config.sops.secrets."wireguard/privateKey".path;
peers = map
2024-03-25 17:03:13 +08:00
(peer:
{
publicKey = peer.publicKey;
allowedIPs = [ (if peer.lighthouse then "192.168.83.0/24" else "${peer.wireguardIp}/32") ];
endpoint = mkIf (!peer.behindNat) "${peer.listenIp}:${builtins.toString peer.listenPort}";
persistentKeepalive = mkIf peer.lighthouse 5;
2024-03-25 17:03:13 +08:00
})
(map
(peer: inputs.topInputs.self.nixosConfigurations.${peer}.config.nixos.services.wireguard)
2024-03-25 17:03:13 +08:00
wireguard.peers);
2024-01-19 19:45:56 +08:00
};
2024-03-25 17:03:13 +08:00
};
sops.secrets."wireguard/privateKey" = {};
# somehow fix wireguard connection
systemd.services = mkIf wireguard.behindNat (listToAttrs (map
2024-03-25 17:03:13 +08:00
(peer:
{
name = "wireguard-ping-${peer.name}";
value =
2024-01-19 19:45:56 +08:00
{
2024-03-25 17:03:13 +08:00
description = "ping ${peer.name}";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig =
2024-01-19 19:45:56 +08:00
{
2024-03-25 17:03:13 +08:00
ExecStart = "${inputs.pkgs.iputils}/bin/ping -i 5 ${peer.value.wireguardIp}";
Restart = "always";
2024-01-19 19:45:56 +08:00
};
2024-03-25 17:03:13 +08:00
};
})
(filter (peer: !peer.value.behindNat) (map
2024-03-25 17:03:13 +08:00
(peer:
{
name = peer;
value = inputs.topInputs.self.nixosConfigurations.${peer}.config.nixos.services.wireguard;
2024-01-19 19:45:56 +08:00
})
2024-03-25 17:03:13 +08:00
wireguard.peers))));
}
]);
2023-12-07 17:18:55 +08:00
}