mirror of
https://github.com/oddlama/nix-config.git
synced 2025-10-10 14:50:40 +02:00
refactor(topology): split node options
This commit is contained in:
parent
d4a932923d
commit
b20376f2e4
8 changed files with 138 additions and 157 deletions
|
@ -1,7 +0,0 @@
|
|||
f: {
|
||||
lib,
|
||||
config,
|
||||
...
|
||||
}:
|
||||
f {
|
||||
}
|
32
topology/options/disks.nix
Normal file
32
topology/options/disks.nix
Normal file
|
@ -0,0 +1,32 @@
|
|||
f: {
|
||||
lib,
|
||||
config,
|
||||
...
|
||||
}: let
|
||||
inherit
|
||||
(lib)
|
||||
mkOption
|
||||
types
|
||||
;
|
||||
in
|
||||
f {
|
||||
options.nodes = mkOption {
|
||||
type = types.attrsOf (types.submodule {
|
||||
options = {
|
||||
disks = mkOption {
|
||||
default = {};
|
||||
type = types.attrsOf (types.submodule (submod: {
|
||||
options = {
|
||||
name = mkOption {
|
||||
description = "The name of this disk";
|
||||
default = submod.config._module.args.name;
|
||||
readOnly = true;
|
||||
type = types.str;
|
||||
};
|
||||
};
|
||||
}));
|
||||
};
|
||||
};
|
||||
});
|
||||
};
|
||||
}
|
38
topology/options/firewall.nix
Normal file
38
topology/options/firewall.nix
Normal file
|
@ -0,0 +1,38 @@
|
|||
f: {
|
||||
lib,
|
||||
config,
|
||||
...
|
||||
}: let
|
||||
inherit
|
||||
(lib)
|
||||
mkOption
|
||||
types
|
||||
;
|
||||
in
|
||||
f {
|
||||
options.nodes = mkOption {
|
||||
type = types.attrsOf (types.submodule {
|
||||
options = {
|
||||
firewallRules = mkOption {
|
||||
description = "TODO";
|
||||
default = {};
|
||||
type = types.attrsOf (types.submodule (submod: {
|
||||
options = {
|
||||
name = mkOption {
|
||||
description = "The name of this firewall rule";
|
||||
type = types.str;
|
||||
readOnly = true;
|
||||
default = submod.config._module.args.name;
|
||||
};
|
||||
|
||||
contents = mkOption {
|
||||
description = "A human readable summary of this rule's effects";
|
||||
type = types.lines;
|
||||
};
|
||||
};
|
||||
}));
|
||||
};
|
||||
};
|
||||
});
|
||||
};
|
||||
}
|
53
topology/options/interfaces.nix
Normal file
53
topology/options/interfaces.nix
Normal file
|
@ -0,0 +1,53 @@
|
|||
f: {
|
||||
lib,
|
||||
config,
|
||||
...
|
||||
}: let
|
||||
inherit
|
||||
(lib)
|
||||
mkOption
|
||||
types
|
||||
;
|
||||
in
|
||||
f {
|
||||
options.nodes = mkOption {
|
||||
type = types.attrsOf (types.submodule {
|
||||
options = {
|
||||
interfaces = mkOption {
|
||||
description = "TODO";
|
||||
default = {};
|
||||
type = types.attrsOf (types.submodule (submod: {
|
||||
options = {
|
||||
name = mkOption {
|
||||
description = "The name of this interface";
|
||||
type = types.str;
|
||||
readOnly = true;
|
||||
default = submod.config._module.args.name;
|
||||
};
|
||||
|
||||
mac = mkOption {
|
||||
description = "The MAC address of this interface, if known.";
|
||||
default = null;
|
||||
type = types.nullOr types.str;
|
||||
};
|
||||
|
||||
addresses = mkOption {
|
||||
description = "The configured address(es), or a descriptive string (like DHCP).";
|
||||
type = types.listOf types.str;
|
||||
};
|
||||
|
||||
network = mkOption {
|
||||
description = ''
|
||||
The global name of the attached/spanned network.
|
||||
If this is given, this interface can be shown in the network graph.
|
||||
'';
|
||||
default = null;
|
||||
type = types.nullOr types.str;
|
||||
};
|
||||
};
|
||||
}));
|
||||
};
|
||||
};
|
||||
});
|
||||
};
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
f: {
|
||||
lib,
|
||||
config,
|
||||
...
|
||||
}:
|
||||
f {
|
||||
}
|
|
@ -30,84 +30,11 @@ in
|
|||
};
|
||||
|
||||
parent = mkOption {
|
||||
description = "TODO guests ids (topology.node.<name>.id) ensure exists";
|
||||
default = [];
|
||||
type = types.listOf types.str;
|
||||
};
|
||||
|
||||
disks = mkOption {
|
||||
default = {};
|
||||
type = types.attrsOf (types.submodule (submod: {
|
||||
options = {
|
||||
name = mkOption {
|
||||
description = "The name of this disk";
|
||||
default = submod.config._module.args.name;
|
||||
readOnly = true;
|
||||
type = types.str;
|
||||
};
|
||||
};
|
||||
}));
|
||||
};
|
||||
|
||||
interfaces = mkOption {
|
||||
description = "TODO";
|
||||
default = {};
|
||||
type = types.attrsOf (types.submodule (submod: {
|
||||
options = {
|
||||
name = mkOption {
|
||||
description = "The name of this interface";
|
||||
type = types.str;
|
||||
readOnly = true;
|
||||
default = submod.config._module.args.name;
|
||||
};
|
||||
|
||||
mac = mkOption {
|
||||
description = "The MAC address of this interface, if known.";
|
||||
default = null;
|
||||
type = types.nullOr types.str;
|
||||
};
|
||||
|
||||
addresses = mkOption {
|
||||
description = "The configured address(es), or a descriptive string (like DHCP).";
|
||||
type = types.listOf types.str;
|
||||
};
|
||||
|
||||
network = mkOption {
|
||||
description = ''
|
||||
The global name of the attached/spanned network.
|
||||
If this is given, this interface can be shown in the network graph.
|
||||
'';
|
||||
default = null;
|
||||
type = types.nullOr types.str;
|
||||
};
|
||||
};
|
||||
}));
|
||||
};
|
||||
|
||||
firewallRules = mkOption {
|
||||
description = "TODO";
|
||||
default = {};
|
||||
type = types.attrsOf (types.submodule (submod: {
|
||||
options = {
|
||||
name = mkOption {
|
||||
description = "The name of this firewall rule";
|
||||
type = types.str;
|
||||
readOnly = true;
|
||||
default = submod.config._module.args.name;
|
||||
};
|
||||
|
||||
contents = mkOption {
|
||||
description = "A human readable summary of this rule's effects";
|
||||
type = types.lines;
|
||||
};
|
||||
};
|
||||
}));
|
||||
description = "The id of the parent node, if this node has a parent.";
|
||||
default = null;
|
||||
type = types.nullOr types.str;
|
||||
};
|
||||
};
|
||||
}));
|
||||
};
|
||||
|
||||
config = {
|
||||
# TODO: assertions = []
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
{
|
||||
lib,
|
||||
config,
|
||||
pkgs,
|
||||
...
|
||||
}: let
|
||||
} @ args: let
|
||||
inherit
|
||||
(lib)
|
||||
mkOption
|
||||
|
@ -20,9 +19,6 @@ in {
|
|||
|
||||
config.renderers.d2.output = pkgs.runCommand "build-d2-topology" {} ''
|
||||
mkdir -p $out
|
||||
cp ${import ./network.nix {
|
||||
inherit pkgs;
|
||||
inherit (config) nixosConfigurations;
|
||||
}} $out/network.d2
|
||||
cp ${import ./network.nix args} $out/network.d2
|
||||
'';
|
||||
}
|
||||
|
|
|
@ -1,81 +1,30 @@
|
|||
{
|
||||
lib,
|
||||
config,
|
||||
pkgs,
|
||||
nixosConfigurations,
|
||||
...
|
||||
}: let
|
||||
inherit
|
||||
(pkgs.lib)
|
||||
any
|
||||
attrNames
|
||||
attrValues
|
||||
(lib)
|
||||
concatLines
|
||||
concatStringsSep
|
||||
elem
|
||||
escapeXML
|
||||
flip
|
||||
filterAttrs
|
||||
imap0
|
||||
mapAttrs'
|
||||
nameValuePair
|
||||
mapAttrsToList
|
||||
optional
|
||||
optionalAttrs
|
||||
optionalString
|
||||
;
|
||||
|
||||
# global = {
|
||||
# # global entities;
|
||||
# };
|
||||
|
||||
# asjson = builtins.toFile "topology.dot" (
|
||||
# builtins.toJSON (map (x: x.config.topology) (attrValues nixosConfigurations))
|
||||
# );
|
||||
|
||||
colors.base00 = "#101419";
|
||||
colors.base01 = "#171B20";
|
||||
colors.base02 = "#21262e";
|
||||
colors.base03 = "#242931";
|
||||
colors.base03b = "#353c48";
|
||||
colors.base04 = "#485263";
|
||||
colors.base05 = "#b6beca";
|
||||
colors.base06 = "#dee1e6";
|
||||
colors.base07 = "#e3e6eb";
|
||||
colors.base08 = "#e05f65";
|
||||
colors.base09 = "#f9a872";
|
||||
colors.base0A = "#f1cf8a";
|
||||
colors.base0B = "#78dba9";
|
||||
colors.base0C = "#74bee9";
|
||||
colors.base0D = "#70a5eb";
|
||||
colors.base0E = "#c68aee";
|
||||
colors.base0F = "#9378de";
|
||||
|
||||
nodesById = mapAttrs' (_: node: nameValuePair node.config.topology.id node) nixosConfigurations;
|
||||
|
||||
isGuestOfAny = node: any (x: elem node x.config.topology.guests) (attrValues nodesById);
|
||||
rootNodes = filterAttrs (n: _: !(isGuestOfAny n)) nodesById;
|
||||
|
||||
toD2 = node: let
|
||||
topo = node.config.topology;
|
||||
in ''
|
||||
${topo.id}: |md
|
||||
# ${topo.id}
|
||||
|
||||
## Guests:
|
||||
${concatLines (map (x: "- ${x}") topo.guests)}
|
||||
toD2 = _nodeName: node: ''
|
||||
${node.name}: |md
|
||||
# ${node.name}
|
||||
|
||||
## Disks:
|
||||
${concatLines (mapAttrsToList (_: v: "- ${v.name}") topo.disks)}
|
||||
${concatLines (mapAttrsToList (_: v: "- ${v.name}") node.disks)}
|
||||
|
||||
## Interfaces:
|
||||
${concatLines (mapAttrsToList (_: v: "- ${v.name}, mac ${toString v.mac}, addrs ${toString v.addresses}, network ${toString v.network}") topo.interfaces)}
|
||||
${concatLines (mapAttrsToList (_: v: "- ${v.name}, mac ${toString v.mac}, addrs ${toString v.addresses}, network ${toString v.network}") node.interfaces)}
|
||||
|
||||
## Firewall Zones:
|
||||
${concatLines (mapAttrsToList (_: v: "- ${v.name}, mac ${toString v.mac}, addrs ${toString v.addresses}, network ${toString v.network}") topo.firewallRules)}
|
||||
${concatLines (mapAttrsToList (_: v: "- ${v.name}, mac ${toString v.mac}, addrs ${toString v.addresses}, network ${toString v.network}") node.firewallRules)}
|
||||
|
|
||||
'';
|
||||
|
||||
d2ForNodes = mapAttrs' (_: node: nameValuePair node.config.topology.id (toD2 node)) nodesById;
|
||||
in
|
||||
pkgs.writeText "network.d2" ''
|
||||
${concatLines (map (x: d2ForNodes.${x}) (attrNames rootNodes))}
|
||||
${concatLines (mapAttrsToList toD2 config.nodes)}
|
||||
''
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue