Summary Files Commits Branches @commit @diff @stats
path: root / modules / server
diff options
context:
space:
mode:
Commit126092fb6624b9ed54f28b887d7658a749a2e3ac (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-01-25 10:26:10 +0200
Committer
 Syndamia <kamen@syndamia.com>
2026-01-25 10:26:10 +0200
Filesa5fbe87f801be3a845b5f61a056d828828c1738b /modules/server
nix-126092fb6624b9ed54f28b887d7658a749a2e3ac.tar
nix-126092fb6624b9ed54f28b887d7658a749a2e3ac.tar.gz
nix-126092fb6624b9ed54f28b887d7658a749a2e3ac.zip
Parent4a93734a74aaefa2f85c9a5483d1668483209267 (diff)
feat(modules/server): srv module automatically calculates a default gateway
1 files changed, 37 insertions, 2 deletions
-rw-r--r--modules/server/srv.nix39
diff --git a/modules/server/srv.nix b/modules/server/srv.nix
index 7701ee5..0ec63bd 100644
--- a/modules/server/srv.nix
+++ b/modules/server/srv.nix
@@ -41,9 +41,44 @@
};
in {
- networking.interfaces = mkIf
+ networking = mkIf
(config.srv.static != null)
- (netinterfaces config);
+ {
+ interfaces = netinterfaces config;
+
+ # Calculate the gateway using the given static ip and prefix
+ # We're assuming the gateway is the first (min) host on the network
+ defaultGateway.address =
+ let
+ # 192.168.0.100 -> [ "192" "168" "0" "100" ]
+ ipBytes = builtins.map
+ (x: lib.toInt x)
+ (lib.splitString "." config.srv.static.ip);
+
+ # 2 ^^ x
+ pow2 = x: if x == 0 then 1 else 2 * (pow2 (x - 1));
+ # 24 -> 0b11111111_11111111_11111111_00000000
+ mask = 4294967295 - ((pow2 (32 - config.srv.static.prefixLength)) - 1);
+
+ # 0b11111111_11111111_11111111_00000000 -> [ 255 255 255 0 ]
+ maskBytes = [
+ (builtins.bitAnd (mask / 16777216) 255)
+ (builtins.bitAnd (mask / 65536) 255)
+ (builtins.bitAnd (mask / 256) 255)
+ (builtins.bitAnd mask 255)
+ ];
+
+ # ipBytes & maskBytes
+ networkIP = lib.zipListsWith lib.bitAnd ipBytes maskBytes;
+ # min possible host
+ gatewayIP = (lib.take 3 networkIP) ++ [ ((lib.last networkIP) + 1) ];
+ in
+ lib.mkDefault
+ (lib.concatMapStringsSep
+ "."
+ builtins.toString
+ gatewayIP);
+ };
virtualisation.vmVariant.networking.interfaces = mkIf
(config.srv.static != null)