From 37b2ba86639113d82f64424ca5e4d0b60c51237e Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Sun, 28 Jan 2024 21:32:46 -0300 Subject: [PATCH] feat: network homelab module --- hosts/homex/configuration.nix | 8 ++++- hosts/homex/network.nix | 14 -------- modules/nixos/homelab/default.nix | 8 +++-- modules/nixos/homelab/network.nix | 56 +++++++++++++++++++++++++++++ modules/nixos/homelab/tailscale.nix | 3 ++ 5 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 modules/nixos/homelab/network.nix diff --git a/hosts/homex/configuration.nix b/hosts/homex/configuration.nix index 6c38bef..e5ca509 100644 --- a/hosts/homex/configuration.nix +++ b/hosts/homex/configuration.nix @@ -10,7 +10,6 @@ ../../modules/nixos/config/host.nix ../../modules/nixos/homelab ./hardware-configuration.nix - ./network.nix ./secrets.nix ./users ]; @@ -18,11 +17,18 @@ homelab = { enable = true; flakeDir = "/home/guz/.nix#homex"; + name = "homex"; domain = "guz.local"; + ip = "100.66.139.89"; localIp = "192.168.1.10"; + network = { + enable = true; + interface = "eno1"; + }; + tailscale = { enable = true; mode = "both"; diff --git a/hosts/homex/network.nix b/hosts/homex/network.nix index 57b1e0e..61221d5 100644 --- a/hosts/homex/network.nix +++ b/hosts/homex/network.nix @@ -4,20 +4,6 @@ imports = [ ]; config = { host.networking.hostName = "homex"; - networking = { - dhcpcd.enable = true; - interfaces.eno1.ipv4.addresses = [{ - address = "192.168.1.10"; - prefixLength = 28; - }]; - defaultGateway = "192.168.1.1"; - nameservers = [ "100.100.100.100" "1.1.1.1" "8.8.8.8" ]; - }; - - boot.kernel.sysctl."net.ipv4.ip_forward" = 1; - boot.kernel.sysctl."net.ipv6.conf.all.forwarding" = 1; - - services.openssh.enable = true; }; } diff --git a/modules/nixos/homelab/default.nix b/modules/nixos/homelab/default.nix index e35326b..19adc6e 100644 --- a/modules/nixos/homelab/default.nix +++ b/modules/nixos/homelab/default.nix @@ -26,6 +26,7 @@ in ./adguard.nix ./caddy.nix ./forgejo.nix + ./network.nix ./tailscale.nix ]; options.homelab = with lib; with lib.types; { @@ -42,12 +43,13 @@ in type = either str path; default = "homelab.local"; }; - ip = mkOption { - type = str; - }; localIp = mkOption { type = str; }; + ip = mkOption { + type = str; + default = cfg.localIp; + }; handleDomains = mkOption { type = bool; default = true; diff --git a/modules/nixos/homelab/network.nix b/modules/nixos/homelab/network.nix new file mode 100644 index 0000000..0948a7c --- /dev/null +++ b/modules/nixos/homelab/network.nix @@ -0,0 +1,56 @@ +{ config, lib, pkgs, ... }: + +let + cfg = config.homelab.network; +in +{ + imports = [ ]; + options.homelab.network = with lib; with lib.types; { + enable = mkOption { + type = bool; + default = true; + }; + interface = mkOption { + type = str; + }; + localIp = mkOption { + type = str; + default = config.homelab.localIp; + }; + defaultGateway = mkOption { + type = str; + default = "192.168.1.1"; + }; + nameservers = mkOption { + type = listOf str; + default = [ "1.1.1.1" "8.8.8.8" ]; + }; + portForwarding = mkOption { + type = bool; + default = false; + }; + openssh = mkOption { + type = bool; + default = true; + }; + settings = { }; + }; + config = lib.mkIf cfg.enable { + networking = { + dhcpcd.enable = true; + interfaces."${cfg.interface}".ipv4.addresses = [{ + address = cfg.localIp; + prefixLength = 28; + }]; + defaultGateway = cfg.defaultGateway; + nameservers = [ + (if config.homelab.tailscale.enable then "100.100.100.100" else null) + ] ++ cfg.nameservers; + }; + + boot.kernel.sysctl."net.ipv4.ip_forward" = if cfg.portForwarding then 1 else 0; + boot.kernel.sysctl."net.ipv6.conf.all.forwarding" = if cfg.portForwarding then 1 else 0; + + services.openssh.enable = cfg.openssh; + }; +} diff --git a/modules/nixos/homelab/tailscale.nix b/modules/nixos/homelab/tailscale.nix index b03ff08..9bf1b53 100644 --- a/modules/nixos/homelab/tailscale.nix +++ b/modules/nixos/homelab/tailscale.nix @@ -28,6 +28,9 @@ in enable = true; useRoutingFeatures = cfg.mode; }; + + homelab.network = lib.mkIf cfg.exitNode { portForwarding = lib.mkDefault true; }; + }; }