From f64314685b3758c6c5dccc75531ead519e4ad034 Mon Sep 17 00:00:00 2001 From: Julius de Jeu Date: Fri, 19 Nov 2021 20:55:46 +0100 Subject: [PATCH] Deduplicate flake a lot with hosts file --- common/hosts.nix | 264 +++++++++++++++++++++++++++++++++++ flake.nix | 46 +++--- hosts/dhcp/configuration.nix | 34 +++++ hosts/dns/configuration.nix | 119 +--------------- 4 files changed, 327 insertions(+), 136 deletions(-) create mode 100644 common/hosts.nix create mode 100644 hosts/dhcp/configuration.nix diff --git a/common/hosts.nix b/common/hosts.nix new file mode 100644 index 0000000..c59b714 --- /dev/null +++ b/common/hosts.nix @@ -0,0 +1,264 @@ +[ + { + hostname = "pfsense"; + ip = "10.42.0.1"; + mac = "46:7F:DD:62:59:56"; + nix = false; + } + { + hostname = "pve"; + ip = "10.42.1.1"; + mac = "d0:67:e5:ef:0e:06"; + nix = false; + } + { + hostname = "idrac"; + ip = "10.42.1.2"; + mac = "d0:67:e5:ef:0e:0e"; + nix = false; + } + { + hostname = "pve-storage"; + ip = "10.42.1.4"; + mac = "00:25:90:55:43:86"; + nix = false; + } + { + hostname = "arch-base"; + ip = "10.42.2.1"; + mac = "00:0c:29:4b:41:56"; + nix = false; + } + { + hostname = "gitlab-host"; + ip = "10.42.2.2"; + mac = "fe:5a:ed:97:35:88"; + nix = false; + } + { + hostname = "media-host"; + ip = "10.42.2.3"; + mac = "00:0c:29:76:d6:cf"; + nix = false; + } + { + hostname = "storage-host"; + ip = "10.42.2.4"; + mac = "82:26:84:35:7e:fe"; + nix = false; + } + { + hostname = "ansible-host"; + ip = "10.42.2.5"; + mac = "ea:e6:b4:36:11:14"; + nix = false; + } + { + hostname = "cdn-host"; + ip = "10.42.2.6"; + mac = "ce:92:04:28:47:ce"; + nix = false; + } + { + hostname = "arch-torrent"; + ip = "10.42.2.7"; + mac = "9a:e8:40:6c:80:3d"; + nix = false; + } + { + hostname = "radarr"; + ip = "10.42.2.8"; + mac = "72:2b:4a:df:9c:67"; + nix = false; + } + { + hostname = "jackett"; + ip = "10.42.2.9"; + mac = "a6:85:1b:e0:03:83"; + nix = false; + } + { + hostname = "sonarr"; + ip = "10.42.2.10"; + mac = "5a:e2:69:8c:c9:31"; + nix = false; + } + { + hostname = "ombi"; + ip = "10.42.2.11"; + mac = "ce:c1:89:a9:62:f5"; + nix = false; + } + { + hostname = "media-host"; + ip = "10.42.2.12"; + mac = "36:60:21:e4:81:61"; + nix = false; + } + { + hostname = "jellyfin"; + ip = "10.42.2.13"; + mac = "16:1f:61:4d:a8:52"; + nix = false; + } + { + hostname = "media-homepage"; + ip = "10.42.2.14"; + mac = "fa:12:23:d8:ab:f1"; + nix = false; + } + { + hostname = "irssi"; + ip = "10.42.2.15"; + mac = "f6:f9:5d:0a:9d:3a"; + nix = false; + } + { + hostname = "factorio-host"; + ip = "10.42.2.16"; + mac = "6e:d5:13:d2:6f:49"; + nix = false; + } + { + hostname = "rke-host"; + ip = "10.42.2.17"; + mac = "da:1f:93:ab:0f:3a"; + nix = false; + } + { + hostname = "traefik-host"; + ip = "10.42.2.18"; + mac = "02:1b:3d:13:2f:8e"; + nix = false; + } + { + hostname = "postgres"; + ip = "10.42.2.19"; + mac = "46:46:a1:41:43:ef"; + nix = false; + } + { + hostname = "minecraft-host"; + ip = "10.42.2.20"; + mac = "e2:d9:07:63:29:db"; + nix = false; + } + { + hostname = "thelounge"; + ip = "10.42.2.21"; + mac = "9a:ba:49:b4:d2:24"; + nix = false; + } + { + hostname = "victoriametrics"; + ip = "10.42.2.22"; + mac = "46:d3:82:6b:f1:18"; + nix = false; + } + { + hostname = "coredns"; + ip = "10.42.2.24"; + mac = "fe:14:fd:2e:98:c7"; + nix = false; + } + { + hostname = "synapse"; + ip = "10.42.2.25"; + mac = "b6:b5:19:25:44:6e"; + nix = false; + } + { + hostname = "domlang"; + ip = "10.42.2.26"; + mac = "fa:ab:87:3d:54:49"; + nix = false; + } + { + hostname = "unifi"; + ip = "10.42.2.27"; + mac = "aa:f4:dd:b6:3c:7e"; + nix = false; + } + { + hostname = "ssh-host"; + ip = "10.42.2.28"; + mac = "22:87:05:0a:c9:a2"; + nix = false; + } + { + hostname = "k8s-1"; + ip = "10.42.3.1"; + mac = "5a:20:52:78:43:77"; + nix = false; + } + { + hostname = "k8s-2"; + ip = "10.42.3.2"; + mac = "d2:41:d3:50:f0:50"; + nix = false; + } + { + hostname = "k8s-3"; + ip = "10.42.3.3"; + mac = "f2:9b:dd:51:15:72"; + nix = false; + } + { + hostname = "base"; + ip = "10.42.20.1"; + mac = "4e:2e:0d:95:69:01"; + nix = false; + } + { + hostname = "nginx"; + ip = "10.42.20.2"; + mac = "52:0d:27:4a:1c:30"; + } + { + hostname = "gitea"; + ip = "10.42.20.3"; + mac = "b6:12:53:f6:f4:19"; + } + { + hostname = "vaultwarden"; + ip = "10.42.20.4"; + mac = "b2:f5:49:56:2b:d3"; + } + { + hostname = "k3s-1"; + nixname = "k3s"; + ip = "10.42.20.5"; + mac = "46:f8:fb:98:85:9c"; + lxc = false; + } + { + hostname = "minio"; + ip = "10.42.20.6"; + mac = "ee:13:9e:d7:3a:7d"; + } + { + hostname = "registry"; + ip = "10.42.20.7"; + mac = "ce:7a:27:f9:30:7a"; + } + { + hostname = "postgresql"; + ip = "10.42.20.8"; + mac = "02:3c:e2:f0:74:53"; + } + { + hostname = "grafana"; + ip = "10.42.20.9"; + mac = "d6:da:53:64:1a:c4"; + } + { + hostname = "dns"; + ip = "10.42.20.10"; + mac = "46:37:52:f3:a2:fb"; + } +/* { + hostname = "dhcp"; + ip = "10.42.42.42"; + mac = "46:37:52:f3:a2:fb"; + } */ +] diff --git a/flake.nix b/flake.nix index b0115b5..df0ce4b 100644 --- a/flake.nix +++ b/flake.nix @@ -18,8 +18,8 @@ [ ]) ++ [ "${./.}/hosts/${name}/configuration.nix" ]; specialArgs = { inputs = inputs; }; }; - mkDeploy = profile: hostname: { - hostname = hostname; + mkDeploy = profile: { + hostname = "${profile}.voidlocal"; fastConnection = true; profiles.system = { user = "root"; @@ -27,31 +27,31 @@ self.nixosConfigurations.${profile}; }; }; + hosts' = import ./common/hosts.nix; + nixHosts = (builtins.filter ({ nix ? true, ... }: nix) hosts'); + hostToConfig = z@{ hostname, nixname ? hostname, lxc ? true, ... }: + a: + a // { + ${nixname} = mkConfig { + name = nixname; + lxc = lxc; + }; + }; + + hostToDeploy = z@{ hostname, nixname ? hostname, lxc ? true, ... }: + a: + a // { + ${nixname} = mkDeploy nixname; + }; + + configs = nixpkgs.lib.fold hostToConfig { } nixHosts; + nodes = nixpkgs.lib.fold hostToDeploy { } nixHosts; in { - nixosConfigurations.nginx = mkConfig { name = "nginx"; }; - nixosConfigurations.gitea = mkConfig { name = "gitea"; }; - nixosConfigurations.vaultwarden = mkConfig { name = "vaultwarden"; }; - nixosConfigurations.k3s = mkConfig { - name = "k3s"; - lxc = false; - }; - nixosConfigurations.minio = mkConfig { name = "minio"; }; - nixosConfigurations.registry = mkConfig { name = "registry"; }; - nixosConfigurations.postgresql = mkConfig { name = "postgresql"; }; - nixosConfigurations.grafana = mkConfig { name = "grafana"; }; - nixosConfigurations.dns = mkConfig { name = "dns"; }; + nixosConfigurations = configs; - deploy.nodes.nginx = mkDeploy "nginx" "10.42.20.2"; - deploy.nodes.gitea = mkDeploy "gitea" "10.42.20.3"; - deploy.nodes.vaultwarden = mkDeploy "vaultwarden" "10.42.20.4"; - deploy.nodes.k3s = mkDeploy "k3s" "10.42.20.5"; - deploy.nodes.minio = mkDeploy "minio" "10.42.20.6"; - deploy.nodes.registry = mkDeploy "registry" "10.42.20.7"; - deploy.nodes.postgresql = mkDeploy "postgresql" "10.42.20.8"; - deploy.nodes.grafana = mkDeploy "grafana" "10.42.20.9"; - deploy.nodes.dns = mkDeploy "dns" "10.42.20.10"; + deploy.nodes = nodes; checks = builtins.mapAttrs (system: deployLib: deployLib.deployChecks self.deploy) deploy-rs.lib; diff --git a/hosts/dhcp/configuration.nix b/hosts/dhcp/configuration.nix new file mode 100644 index 0000000..f428301 --- /dev/null +++ b/hosts/dhcp/configuration.nix @@ -0,0 +1,34 @@ +{ config, pkgs, ... }: +let hosts = import ../../common/hosts.nix; +in { + imports = [ + # Import common config + ../../common/generic-lxc.nix + ../../common + ]; + + networking = { + hostName = "dns"; + interfaces.eth0 = { + # useDHCP = true; + # I used DHCP because sometimes I disconnect the LAN cable + ipv4.addresses = [{ + address = "10.42.42.42"; + prefixLength = 16; + }]; + }; + }; + + # This value determines the NixOS release from which the default + # settings for stateful data, like file locations and database versions + # on your system were taken. It‘s perfectly fine and recommended to leave + # this value at the release version of the first install of this system. + # Before changing this value read the documentation for this option + # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html). + system.stateVersion = "21.11"; # Did you read the comment? + + environment.systemPackages = with pkgs; [ dig ]; + + networking.firewall.enable = false; + +} diff --git a/hosts/dns/configuration.nix b/hosts/dns/configuration.nix index 1986d47..a040783 100644 --- a/hosts/dns/configuration.nix +++ b/hosts/dns/configuration.nix @@ -1,6 +1,5 @@ { config, pkgs, ... }: -let - +let hosts = import ../../common/hosts.nix; in { imports = [ # Import common config @@ -8,7 +7,9 @@ in { ../../common ]; - networking.hostName = "dns"; + networking = { + hostName = "dns"; + }; # This value determines the NixOS release from which the default # settings for stateful data, like file locations and database versions @@ -29,116 +30,9 @@ in { settings = { server = let localData = hostname: ip: ''"${hostname}.voidlocal. A ${ip}"''; - localData' = { hostname, ip }: localData hostname ip; + localData' = { hostname, ip, ... }: localData hostname ip; ptrData = hostname: ip: ''"${ip} ${hostname}.voidlocal"''; - ptrData' = { hostname, ip }: ptrData hostname ip; - - hosts = [ - { - hostname = "localhost"; - ip = "127.0.0.1"; - } - { - hostname = "pfsense"; - ip = "10.42.0.1"; - } - { - hostname = "pve"; - ip = "10.42.1.1"; - } - { - hostname = "idrac"; - ip = "10.42.1.2"; - } - { - hostname = "pve-storage"; - ip = "10.42.1.4"; - } - { - hostname = "arch-base"; - ip = "10.42.2.1"; - } - { - hostname = "gitlab-host"; - ip = "10.42.2.2"; - } - { - hostname = "storage-host"; - ip = "10.42.2.4"; - } - { - hostname = "cdn-host"; - ip = "10.42.2.6"; - } - { - hostname = "arch-torrent"; - ip = "10.42.2.7"; - } - { - hostname = "postgres"; - ip = "10.42.2.19"; - } - { - hostname = "thelounge"; - ip = "10.42.2.21"; - } - { - hostname = "unifi"; - ip = "10.42.2.27"; - } - { - hostname = "ssh-host"; - ip = "10.42.2.28"; - } - { - hostname = "k8s-1"; - ip = "10.42.3.1"; - } - { - hostname = "k8s-2"; - ip = "10.42.3.2"; - } - { - hostname = "k8s-3"; - ip = "10.42.3.3"; - } - { - hostname = "nginx"; - ip = "10.42.20.2"; - } - { - hostname = "gitea"; - ip = "10.42.20.3"; - } - { - hostname = "vaultwarden"; - ip = "10.42.20.4"; - } - { - hostname = "k3s-1"; - ip = "10.42.20.5"; - } - { - hostname = "minio"; - ip = "10.42.20.6"; - } - { - hostname = "registry"; - ip = "10.42.20.7"; - } - { - hostname = "postgresql"; - ip = "10.42.20.8"; - } - { - hostname = "grafana"; - ip = "10.42.20.9"; - } - { - hostname = "dns"; - ip = "10.42.20.10"; - } - ]; + ptrData' = { hostname, ip, ... }: ptrData hostname ip; in { use-syslog = "yes"; @@ -170,7 +64,6 @@ in { "fd00::/8" "fe80::/10" ]; - # addLocal "10.42.0.1" "pfsense"; }; forward-zone = { name = ''"."'';