summaryrefslogtreecommitdiff
path: root/sys/net/options.nix
diff options
context:
space:
mode:
authorAlejandro Soto <alejandro@34project.org>2025-01-01 16:46:08 -0600
committerAlejandro Soto <alejandro@34project.org>2025-01-01 21:49:38 -0600
commit083c1d226a03fb84fb1e717adc1581049df689cf (patch)
tree4df12bdf7d4560023160359035f10c2217df6e79 /sys/net/options.nix
parentb1b3a635db095a54c1863b01b7ecb290e539c152 (diff)
sys: refactor address and network number management
Diffstat (limited to 'sys/net/options.nix')
-rw-r--r--sys/net/options.nix166
1 files changed, 166 insertions, 0 deletions
diff --git a/sys/net/options.nix b/sys/net/options.nix
new file mode 100644
index 0000000..292989a
--- /dev/null
+++ b/sys/net/options.nix
@@ -0,0 +1,166 @@
+{ config, lib, ... }:
+with lib; {
+ options.local.nets = with lib.types; mkOption {
+ readOnly = true;
+
+ type = attrsOf (submodule ({ config, ... }: {
+ options =
+ let
+ v4config = config.v4;
+ v6config = config.v6;
+ in
+ {
+ hosts = mkOption {
+ default = { };
+
+ type = attrsOf (submodule {
+ options = {
+ v4 = mkOption {
+ default = null;
+
+ type = nullOr (submodule ({ config, ... }: {
+ options = {
+ suffix = mkOption {
+ type = str;
+ };
+
+ address = mkOption {
+ type = str;
+ readOnly = true;
+ };
+
+ cidr = mkOption {
+ type = str;
+ readOnly = true;
+ };
+
+ single = mkOption {
+ type = str;
+ readOnly = true;
+ };
+ };
+
+ config = {
+ address =
+ if v4config.bits == 0
+ then config.suffix
+ else if v4config.bits == 32
+ then v4config.subnet
+ else "${v4config.prefix}.${config.suffix}";
+
+ cidr = "${config.address}/${toString v4config.bits}";
+ single = "${config.address}/32";
+ };
+ }));
+ };
+
+ v6 = mkOption {
+ default = null;
+
+ type = nullOr (submodule ({ config, ... }: {
+ options = {
+ suffix = mkOption {
+ type = str;
+ };
+
+ address = mkOption {
+ type = str;
+ readOnly = true;
+ };
+
+ cidr = mkOption {
+ type = str;
+ readOnly = true;
+ };
+
+ single = mkOption {
+ type = str;
+ readOnly = true;
+ };
+ };
+
+ config = {
+ address =
+ if v6config.bits != 0
+ then "${v6config.prefix}::${config.suffix}"
+ else config.suffix;
+
+ cidr = "${config.address}/${toString v6config.bits}";
+ single = "${config.address}/128";
+ };
+ }));
+ };
+ };
+ });
+ };
+
+ v4 = mkOption {
+ default = null;
+
+ type = nullOr (submodule ({ config, ... }: {
+ options = {
+ bits = mkOption {
+ type = enum [ 0 8 16 24 32 ];
+ };
+
+ prefix = mkOption {
+ type = str;
+ };
+
+ subnet = mkOption {
+ type = str;
+ readOnly = true;
+ };
+
+ cidr = mkOption {
+ type = str;
+ readOnly = true;
+ };
+ };
+
+ config = {
+ cidr = "${config.subnet}/${toString config.bits}";
+ subnet =
+ if config.bits != 0
+ then config.prefix + strings.replicate (4 - config.bits / 8) ".0"
+ else "0.0.0.0";
+ };
+ }));
+ };
+
+ v6 = mkOption {
+ default = null;
+
+ type = nullOr (submodule ({ config, ... }: {
+ options = {
+ bits = mkOption {
+ type = addCheck (ints.between 0 64) (b: mod b 4 == 0) // {
+ description = "IPv6 subnet bits at nibble boundary";
+ };
+ };
+
+ prefix = mkOption {
+ type = str;
+ };
+
+ subnet = mkOption {
+ type = str;
+ readOnly = true;
+ };
+
+ cidr = mkOption {
+ type = str;
+ readOnly = true;
+ };
+ };
+
+ config = {
+ cidr = "${config.subnet}/${toString config.bits}";
+ subnet = "${config.prefix}::";
+ };
+ }));
+ };
+ };
+ }));
+ };
+}