From a8e15efa130f94ff6a7ce5de14ca652c84c89572 Mon Sep 17 00:00:00 2001 From: Kalle Struik Date: Wed, 5 Feb 2025 16:19:19 +0100 Subject: [PATCH] Add non managed hosts --- config.nix | 3 +++ flake.nix | 14 ++++++++++---- hosts.nix | 3 ++- hosts/home-assistant.nix | 18 ++++++++++++++++++ roles/default.nix | 1 + roles/traefik.nix | 4 +++- utils.nix | 17 +++++++++++++++-- 7 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 hosts/home-assistant.nix diff --git a/config.nix b/config.nix index 2748b17..9949d67 100644 --- a/config.nix +++ b/config.nix @@ -1,4 +1,7 @@ { domain = "staging.kallestruik.nl"; shortDomain = "khs.li"; + # Networking + defaultDNS = [ "192.168.10.1" ]; + defaultGateway = "192.168.10.1"; } diff --git a/flake.nix b/flake.nix index 0ebe651..3139cfa 100644 --- a/flake.nix +++ b/flake.nix @@ -13,14 +13,20 @@ outputs = { self, nixpkgs, ... }@inputs: let + lib = nixpkgs.lib; outputs = self.outputs; - utils = import ./utils.nix { inherit inputs; }; homelabConfig = import ./config.nix; + utils = import ./utils.nix { inherit inputs homelabConfig; }; + roles = import ./roles { inherit utils; }; hosts = import ./hosts.nix { - inherit homelabConfig roles; - lib = nixpkgs.lib; + inherit + homelabConfig + roles + utils + lib + ; }; pkgs = nixpkgs.legacyPackages.x86_64-linux; in @@ -51,7 +57,7 @@ (utils.mkSystem { hostConfig = value; }) - ) hosts); + ) (lib.attrsets.filterAttrs (_: host: host.managed) hosts)); nixosConfigurations = { template = nixpkgs.lib.nixosSystem { specialArgs = { inherit inputs; }; diff --git a/hosts.nix b/hosts.nix index b196c5e..699fc17 100644 --- a/hosts.nix +++ b/hosts.nix @@ -2,6 +2,7 @@ roles, homelabConfig, lib, + utils, ... }: lib.attrsets.mapAttrs' ( @@ -15,6 +16,6 @@ lib.attrsets.mapAttrs' ( in { name = hostname; - value = cfg; + value = utils.mkHost cfg; } ) (builtins.readDir ./hosts) diff --git a/hosts/home-assistant.nix b/hosts/home-assistant.nix new file mode 100644 index 0000000..5632f1c --- /dev/null +++ b/hosts/home-assistant.nix @@ -0,0 +1,18 @@ +{ + hlConfig, + ... +}: +rec { + hostname = "homeassistant"; + managed = false; + ip = "192.168.10.98"; + + traefikRoutes = [ + { + name = "${hostname}"; + rule = "Host(`home.${hlConfig.domain}`)"; + target = "http://${ip}:8123"; + } + ]; + +} diff --git a/roles/default.nix b/roles/default.nix index 4a921ec..37eab5f 100644 --- a/roles/default.nix +++ b/roles/default.nix @@ -1,5 +1,6 @@ { utils, + ... }: { sonarr = utils.mkRole (import ./sonarr.nix); diff --git a/roles/traefik.nix b/roles/traefik.nix index 64f98d9..094be8e 100644 --- a/roles/traefik.nix +++ b/roles/traefik.nix @@ -24,7 +24,7 @@ config = let cfg = config.traefik; - routes = concatMap ( + roleRoutes = concatMap ( hostname: concatMap ( role: @@ -33,6 +33,8 @@ } ) hosts.${hostname}.roles ) (builtins.attrNames hosts); + hostRoutes = concatMap (hostname: hosts.${hostname}.traefikRoutes) (builtins.attrNames hosts); + routes = roleRoutes ++ hostRoutes; in { networking.firewall.allowedTCPPorts = [ diff --git a/utils.nix b/utils.nix index 20037c0..9de628f 100644 --- a/utils.nix +++ b/utils.nix @@ -1,5 +1,6 @@ { inputs, + homelabConfig, ... }: { @@ -32,9 +33,9 @@ prefixLength = 24; } ]; - nameservers = [ "192.168.10.1" ]; + nameservers = homelabConfig.defaultDNS; defaultGateway = { - address = "192.168.10.1"; + address = homelabConfig.defaultGateway; interface = "eth0"; }; }; @@ -49,4 +50,16 @@ inherit (cfg) name description nixosModule; traefikRoutes = cfg.traefikRoutes or ({ ... }: [ ]); }; + + mkHost = cfg: { + inherit (cfg) + hostname + managed + ip + stateVersion + ; + traefikRoutes = cfg.traefikRoutes or [ ]; + roles = cfg.roles or [ ]; + config = cfg.config or { }; + }; }