From 7584ff9a39bb24b00a175f3c003f952df149634b Mon Sep 17 00:00:00 2001 From: Henri Dohmen Date: Sat, 31 May 2025 14:22:37 +0200 Subject: [PATCH] dns --- flake.nix | 4 +-- host/default.nix | 4 ++- lib.nix | 68 ++++++++++++++++-------------------------------- var/lan-dns.nix | 18 +++++++++++++ var/wg.nix | 3 +++ 5 files changed, 49 insertions(+), 48 deletions(-) create mode 100644 var/lan-dns.nix diff --git a/flake.nix b/flake.nix index 733c04b..dfceea2 100644 --- a/flake.nix +++ b/flake.nix @@ -26,9 +26,9 @@ lib = nixpkgs.lib; lib' = import ./lib.nix { inherit lib; }; - specialArgs = { + specialArgs = rec { inherit inputs lib'; - var = (lib'.walk-dir ./var).map_import_with_lib; + var = (lib'.walk-dir ./var)._map (f: import f { inherit lib var; }); }; overlays = _: { nixpkgs.overlays = [ colmena.overlay ]; diff --git a/host/default.nix b/host/default.nix index e735632..54044fe 100644 --- a/host/default.nix +++ b/host/default.nix @@ -1,4 +1,4 @@ -{ ... }: +{ var, ... }: { imports = [ ./boot.nix @@ -7,4 +7,6 @@ ./shell.nix ./users.nix ]; + + networking.extraHosts = var.lan-dns.hostsFile; } diff --git a/lib.nix b/lib.nix index 205c30c..213614f 100644 --- a/lib.nix +++ b/lib.nix @@ -1,51 +1,29 @@ { lib, ... }: with builtins; let - lib' = rec { - walk-dir = - let - walk-dir-inner = - path: - let - dir = readDir path; + walk-dir-inner = + path: + let + dir = readDir path; + in + lib.mapAttrs' (filename: value: { + name = lib.removeSuffix ".nix" filename; + value = + if value == "regular" then + path + "/${filename}" + else if value == "directory" then + walk-dir-inner (path + "/${filename}") + else + throw "Items of type ${value} are unsupported."; + }) dir; - in - lib.mapAttrs' (filename: value: { - name = lib.removeSuffix ".nix" filename; - value = - if value == "regular" then - path + "/${filename}" - else if value == "directory" then - walk-dir-inner (path + "/${filename}") - else - throw "Items of type ${value} are unsupported."; - }) dir; - - helper-attrs = - subpaths: - let - _files = lib.collect (x: isPath x || isString x) subpaths; - _nix_files = filter (lib.hasSuffix ".nix") _files; - in - rec { - to_mod = _: { - imports = _nix_files; - }; - to_mod_without_default = without_default.to_mod; - collect_nix_files = _nix_files; - map_import = lib.mapAttrsRecursive (_: import) subpaths; - map_import_with_lib = lib.mapAttrsRecursive (_: x: (import x) { inherit lib lib'; }) subpaths; - without_default = - let - subpaths' = removeAttrs subpaths [ "default" ]; - in - with-helper-attrs subpaths'; - }; - - with-helper-attrs = - x: if isAttrs x then lib.mapAttrs (_: with-helper-attrs) x // helper-attrs x else x; - in - p: with-helper-attrs (walk-dir-inner p); + helper-attrs = subpaths: { + _map = f: lib.mapAttrsRecursive (_: f) subpaths; }; + + with-helper-attrs = + x: if isAttrs x then lib.mapAttrs (_: with-helper-attrs) x // helper-attrs x else x; in -lib' +{ + walk-dir = p: with-helper-attrs (walk-dir-inner p); +} diff --git a/var/lan-dns.nix b/var/lan-dns.nix new file mode 100644 index 0000000..39851dd --- /dev/null +++ b/var/lan-dns.nix @@ -0,0 +1,18 @@ +{ lib, var, ... }: +let + lan-tld = ".lan"; + lan-base-domain = ".hdohmen.de"; + lan-hosts = lib.mapAttrs' (name: value: { + name = "${name}${lan-tld}"; + inherit value; + }) var.wg.ips; +in +rec { + hostsFile = lib.concatStringsSep "\n" (lib.mapAttrsFlatten (n: v: "${n}\t${v}") hosts); + hosts = + lan-hosts + // lib.mapAttrs' (name: value: { + name = "${name}${lan-base-domain}"; + inherit value; + }) lan-hosts; +} diff --git a/var/wg.nix b/var/wg.nix index 483f139..d3ac12d 100644 --- a/var/wg.nix +++ b/var/wg.nix @@ -32,4 +32,7 @@ rec { lib.attrValues (lib.filterAttrs (n: _: n != host) wireguard-network) ); + ips = + with builtins; + mapAttrs (name: value: head (lib.splitString "/" (head value.ips))) wireguard-network; }