metanix/flake.nix
2025-11-29 14:47:32 -06:00

128 lines
3.9 KiB
Nix
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
description = "Metanix static flake: meta.nix addressing, deploy-rs, disko, nixos-anywhere";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
deploy-rs.url = "github:serokell/deploy-rs";
disko.url = "github:nix-community/disko";
nixos-anywhere.url = "github:numtide/nixos-anywhere";
};
outputs = { self, nixpkgs, deploy-rs, disko, nixos-anywhere, ... }:
let
# Default architecture if a system doesnt override it
defaultSystem = "x86_64-linux";
# pkgs per-system so cross-arch doesnt explode later
pkgsFor = system: import nixpkgs { inherit system; };
lib = nixpkgs.lib;
# World spec & addressing
meta = import ./meta.nix;
addressing = import ./lib/addressing { inherit lib; };
# Optional for now; meta.systems can be {} until the systems submodule lands
systemsFromMeta = meta.systems or { };
# Precomputed full network view
network = addressing.mkNetworkFromSpec meta;
in
{
########################
# Library exports
########################
lib.metanix = {
inherit meta addressing network;
};
########################
# NixOS configurations
########################
nixosConfigurations =
lib.mapAttrs
(name: sysCfg:
let
# System for this host, fallback to default
system = sysCfg.system or defaultSystem;
pkgs = pkgsFor system;
in
lib.nixosSystem {
inherit system;
# Make meta/addressing/world visible to modules
specialArgs = {
inherit lib pkgs meta addressing system;
modulesPath = builtins.toString <nixpkgs/nixos/modules>;
};
modules =
(sysCfg.modules or [ ]) ++ [
# Core Metanix wiring
./modules/metanix/core.nix
./modules/metanix/networkd.nix
# Identity binding: “this box is <name> in Metanix”
{
networking.hostName = sysCfg.hostName or name;
metanix.thisHost = sysCfg.metanixName or name;
}
# Optional Disko integration per host
(if sysCfg ? diskoConfig then
{
imports = [
disko.nixosModules.disko
sysCfg.diskoConfig
];
}
else
{ })
];
})
systemsFromMeta;
########################
# deploy-rs
########################
deploy = {
nodes =
lib.mapAttrs
(name: sysCfg:
let
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
{
hostname = sysCfg.deployHost or defaultHostname;
profiles.system = {
user = sysCfg.deployUser or "root";
path =
deploy-rs.lib.${defaultSystem}.activate.nixos
self.nixosConfigurations.${name};
};
})
systemsFromMeta;
};
checks.${defaultSystem}.deploy =
deploy-rs.lib.${defaultSystem}.deployChecks self.deploy;
########################
# nixos-anywhere helper
########################
apps.${defaultSystem}.nixos-anywhere = {
type = "app";
program =
"${nixos-anywhere.packages.${defaultSystem}.nixos-anywhere}/bin/nixos-anywhere";
};
};
}