From 896c5ff93930c1df5b767f15a3ea982d87275ab2 Mon Sep 17 00:00:00 2001 From: Yaro Kasear Date: Fri, 28 Nov 2025 16:37:18 -0600 Subject: [PATCH] Adding more structure. --- flake.nix | 83 +++++++++------------------------------- modules/metanix-core.nix | 71 ++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 65 deletions(-) create mode 100644 modules/metanix-core.nix diff --git a/flake.nix b/flake.nix index 6f990a1..f883cba 100644 --- a/flake.nix +++ b/flake.nix @@ -10,101 +10,60 @@ outputs = { self, nixpkgs, deploy-rs, disko, nixos-anywhere, ... }: let - # Default architecture if meta.nix doesn't say otherwise system = "x86_64-linux"; - pkgs = import nixpkgs { - inherit system; - }; - + pkgs = import nixpkgs { inherit system; }; lib = pkgs.lib; - # Your mkIp / mkHostsFromSpec / mkSubnetsFromSpec, etc. - # This expects: lib/addressing/default.nix addressing = import ./lib/addressing { inherit lib; }; - # User-provided world model. meta = import ./meta.nix; - # Uses your rewritten mkNetworkFromSpec that understands meta.nix shape. - network = addressing.mkNetworkFromSpec meta; - - # Optional: meta.systems = { hostName = { system = "..."; modules = [ ... ]; ... }; ...; } systemsFromMeta = meta.systems or { }; in { - ####################### - # Library-style exports - ####################### - lib = { - metanix = { - inherit meta network addressing; - }; + lib.metanix = { + inherit meta addressing; + network = addressing.mkNetworkFromSpec meta; }; - ############################# - # Per-host NixOS configs - # - # Driven by meta.systems if present. Shape example: - # meta.systems = { - # deimos = { - # system = "x86_64-linux"; - # modules = [ ./hosts/deimos.nix ]; - # diskoConfig = ./disko/deimos.nix; # optional - # deployHost = "deimos.kasear.net"; # optional - # deployUser = "root"; # optional - # }; - # }; - ############################# nixosConfigurations = lib.mapAttrs (name: sysCfg: let systemForHost = sysCfg.system or system; + in + lib.nixosSystem { + system = systemForHost; - pkgsForHost = import nixpkgs { - inherit systemForHost; + # Make meta + addressing available to all modules + specialArgs = { + inherit meta addressing; }; - hostLib = pkgsForHost.lib; - - # Optional disko module wiring if sysCfg.diskoConfig exists. - diskoModule = - if sysCfg ? diskoConfig then - { imports = [ disko.nixosModules.disko sysCfg.diskoConfig ]; } - else - { }; - in - hostLib.nixosSystem { - system = systemForHost; modules = (sysCfg.modules or [ ]) ++ [ - diskoModule - ({ ... }: { - _module.args = { - inherit meta network addressing; - hostName = name; - }; - }) + ./modules/metanix-core.nix + + # Disko wiring if present + (if sysCfg ? diskoConfig then + { imports = [ disko.nixosModules.disko sysCfg.diskoConfig ]; } + else + { }) ]; }) systemsFromMeta; - ######################################## - # deploy-rs integration - # - # Builds deploy.nodes using meta.systems + addressing. - ######################################## deploy = { nodes = lib.mapAttrs (name: sysCfg: let + network = addressing.mkNetworkFromSpec meta; hasNetworkHost = builtins.hasAttr name network.hosts; hostInfo = if hasNetworkHost then network.hosts.${name} else null; - defaultHostname = if hasNetworkHost then hostInfo.fqdn else "${name}.${meta.domain}"; in @@ -121,15 +80,9 @@ systemsFromMeta; }; - ######################################## - # deploy-rs sanity checks - ######################################## checks.${system}.deploy = deploy-rs.lib.${system}.deployChecks self.deploy; - ######################################## - # nixos-anywhere convenience app - ######################################## apps.${system}.nixos-anywhere = { type = "app"; program = diff --git a/modules/metanix-core.nix b/modules/metanix-core.nix new file mode 100644 index 0000000..7341968 --- /dev/null +++ b/modules/metanix-core.nix @@ -0,0 +1,71 @@ +{ lib, meta, addressing, ... }: + +let + inherit (lib) mkOption types; +in +{ + options.metanix = { + # Raw world spec, straight from meta.nix + meta = mkOption { + type = types.attrs; + readOnly = true; + default = { }; + description = "Raw Metanix world spec loaded from meta.nix."; + }; + + domain = mkOption { + type = types.str; + readOnly = true; + description = "Base DNS domain for this Metanix world."; + }; + + locations = mkOption { + type = types.attrs; + readOnly = true; + description = "Location tree from meta.nix."; + }; + + systems = mkOption { + type = types.attrs; + readOnly = true; + description = "System definitions from meta.nix."; + }; + + # Computed addressing + network = mkOption { + type = types.attrs; + readOnly = true; + description = "Computed addressing (hosts, subnets, etc.) from meta.nix."; + }; + + hosts = mkOption { + type = types.attrs; + readOnly = true; + description = "Shortcut for metanix.network.hosts."; + }; + + subnets = mkOption { + type = types.attrs; + readOnly = true; + description = "Shortcut for metanix.network.subnets."; + }; + }; + + config.metanix = + let + world = meta; + network = addressing.mkNetworkFromSpec world; + in + { + meta = world; + + domain = world.domain; + locations = world.locations or { }; + systems = world.systems or { }; + policy = world.policy or { }; + + inherit network; + hosts = network.hosts; + subnets = network.subnets; + }; +}