diff options
Diffstat (limited to 'modules')
| -rw-r--r-- | modules/services/options.nix | 38 | ||||
| -rw-r--r-- | modules/wireguard/nixos/default.nix | 48 | ||||
| -rw-r--r-- | modules/wireguard/options.nix | 40 |
3 files changed, 88 insertions, 38 deletions
diff --git a/modules/services/options.nix b/modules/services/options.nix index cae9e72..e87ecb3 100644 --- a/modules/services/options.nix +++ b/modules/services/options.nix @@ -32,44 +32,6 @@ in { }; }; tailscale.enable = mkEnableOption "tailscale"; - wireguard = let - spokeOpts = { - ip = mkOption { - type = ip_addr; - }; - key = mkOption { - type = lib.types.str; - }; - }; - - hubOpts = - spokeOpts - // { - domain = mkOption { - type = lib.types.str; - }; - port = mkOption { - type = lib.types.port; - }; - }; - in { - # based on https://www.procustodibus.com/blog/2020/11/wireguard-hub-and-spoke-config - enable = mkEnableOption "wireguard"; - - peersConfig = { - hub = mkOption { - type = lib.types.attrsOf (lib.types.submodule {options = hubOpts;}); - }; - - spokes = mkOption { - type = lib.types.attrsOf (lib.types.submodule {options = spokeOpts;}); - }; - }; - - localPeer = mkOption { - type = lib.types.oneOf [hubOpts spokeOpts]; - }; - }; sshd.enable = mkEnableOption "OpenSSH server"; }; audio = { diff --git a/modules/wireguard/nixos/default.nix b/modules/wireguard/nixos/default.nix new file mode 100644 index 0000000..6f51ddf --- /dev/null +++ b/modules/wireguard/nixos/default.nix @@ -0,0 +1,48 @@ +{ + config, + lib, + ... +}: let + cfg = config.collinux.wireguard; +in + lib.mkIf cfg.enable { + boot.extraModulePackages = [config.boot.kernelPackages.wireguard]; + boot.kernel.sysctl."net.ipv4.ip_forward" = 1; + + networking.useNetworkd = true; + systemd.network = { + netdevs."10-wg" = { + netdevConfig = { + Kind = "wireguard"; + Name = "wg0"; + }; + + wireguardConfig = { + PrivateKeyFile = cfg.privateKeyFile; + ListenPort = 51820; + }; + + wireguardPeers = + builtins.map (m: { + PublicKey = m.publicKey; + AllowedIPs = [m.ip]; + Endpoint = m.endpoint; + }) + cfg.peers; + }; + + networks."12-wireguard" = { + name = "wg0"; + + networkConfig = { + Address = cfg.ip; + DHCP = "no"; + Gateway = cfg.gateway; + + IPMasquerade = "ipv4"; + IPv4Forwarding = true; + IPv6AcceptRA = false; + }; + }; + }; + } diff --git a/modules/wireguard/options.nix b/modules/wireguard/options.nix new file mode 100644 index 0000000..8e5398e --- /dev/null +++ b/modules/wireguard/options.nix @@ -0,0 +1,40 @@ +{lib, ...}: let + inherit (lib) mkOption mkEnableOption; +in { + options = { + collinux.wireguard = { + enable = mkEnableOption "wireguard"; + ip = mkOption { + type = lib.types.str; + description = "host's ip address on the wireguard network (with cidr)"; + }; + gateway = mkOption { + type = lib.types.str; + description = "host's gateway"; + }; + privateKeyFile = mkOption { + type = lib.types.str; + description = "path to local private key file"; + example = "/run/secretd.d/wireguard-key"; + }; + peers = mkOption { + type = lib.types.attrsOf (lib.types.submodule { + options = { + publicKey = mkOption { + type = lib.types.str; + description = "peer's public key"; + }; + ip = mkOption { + type = lib.types.str; + description = "peer's IP address"; + }; + endpoint = mkOption { + type = lib.types.str; + description = "peer's endpoint"; + }; + }; + }); + }; + }; + }; +} |
