From e39d8cb9092c9699e68a5ffc9059b09259c4c4aa Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sun, 19 May 2024 12:41:14 +0200 Subject: [PATCH] init: static-hosts module --- clanModules/flake-module.nix | 1 + clanModules/static-hosts/README.md | 2 ++ clanModules/static-hosts/default.nix | 24 ++++++++++++++++++++++++ docs/mkdocs.yml | 1 + 4 files changed, 28 insertions(+) create mode 100644 clanModules/static-hosts/README.md create mode 100644 clanModules/static-hosts/default.nix diff --git a/clanModules/flake-module.nix b/clanModules/flake-module.nix index a5ef1b9c..fcf6defb 100644 --- a/clanModules/flake-module.nix +++ b/clanModules/flake-module.nix @@ -14,6 +14,7 @@ root-password = ./root-password; sshd = ./sshd; sunshine = ./sunshine; + static-hosts = ./static-hosts; syncthing = ./syncthing; thelounge = ./thelounge; user-password = ./root-password; diff --git a/clanModules/static-hosts/README.md b/clanModules/static-hosts/README.md new file mode 100644 index 00000000..30192f64 --- /dev/null +++ b/clanModules/static-hosts/README.md @@ -0,0 +1,2 @@ +Statically configure the host names of machines based on their respective zerotier-ip. +--- diff --git a/clanModules/static-hosts/default.nix b/clanModules/static-hosts/default.nix new file mode 100644 index 00000000..6cc5f106 --- /dev/null +++ b/clanModules/static-hosts/default.nix @@ -0,0 +1,24 @@ +{ lib, config, ... }: +{ + options.clan.static-hosts = { + excludeHosts = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = [ config.clanCore.machineName ]; + description = "Hosts that should be excluded"; + }; + }; + + config.networking.hosts = + let + clanDir = config.clanCore.clanDir; + machineDir = clanDir + "/machines/"; + zerotierIpMachinePath = machines: machineDir + machines + "/facts/zerotier-ip"; + machines = builtins.readDir machineDir; + filteredMachines = lib.filterAttrs ( + name: _: !(lib.elem name config.clan.static-hosts.excludeHosts) + ) machines; + in + (lib.mapAttrs' ( + machine: _: lib.nameValuePair (builtins.readFile (zerotierIpMachinePath machine)) [ machine ] + ) filteredMachines); +} diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 393fd574..8241675c 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -62,6 +62,7 @@ nav: - reference/clanModules/sshd.md - reference/clanModules/sunshine.md - reference/clanModules/syncthing.md + - reference/clanModules/static-hosts.md - reference/clanModules/thelounge.md - reference/clanModules/user-password.md - reference/clanModules/xfce.md -- 2.45.2