From 96cc5e9501975e2497531fdc54946fbaa0675f10 Mon Sep 17 00:00:00 2001 From: Yaro Kasear Date: Sun, 20 Jul 2025 12:45:51 -0500 Subject: [PATCH] Starting out simple. Small. --- README.md | 40 ++++++++++++++++--------- flake.lock | 16 ++++------ lib/functions/buildLocationList.nix | 1 + lib/metanix.nix | 45 ++++------------------------- test/test-buildLocationList.nix | 7 +++++ 5 files changed, 47 insertions(+), 62 deletions(-) create mode 100644 lib/functions/buildLocationList.nix create mode 100644 test/test-buildLocationList.nix diff --git a/README.md b/README.md index 2cd02ff..628f251 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,39 @@ # Metanix -*Declare your infrastructure!* + +_Declare your infrastructure!_ **THIS IS STILL IN DESIGN AND EXPERIMENTATION PHASE** ## Description + Metanix is a nix library designed to allow a central, no-nonsense nix file to be written to allow for a Source of Truth leading to deterministic configuration identifiers like IP addresses, UIDs, GIDs, and enable quickly establishing frameworks for NixOS systems existing within an ecosystem. As such, I am trying to design this system to enable a would-be admin to answer very simple one-word questions. Sort of. ## What? + Or, in other words, what systems do you want Metanix to manage or at least be aware of? ## Where? + Or, in other words, what networks and subnets do you have and where do your systems/users belong? ## Who? -Or, in other words, who are your users? + +Or, in other words, who are your users? ## How? + Or, in other words, what kind of access control groups do you want or need to best control how everything is used. ## Enter: THINGS I use the term "thing" as an overall blanket classification for the four main types in the library: Locations, Systems, Users, and Groups. -**Locations** are networks with their own Internet access, or at the very least are networks that are supposedly physically distinct from other networks. It could be your house, your office building, an apartment, whatever. Typically, Metanix considers the fundamental defining aspects of these to be routers and subnets. +**Locations** are networks with their own Internet access, or at the very least are networks that are supposedly physically distinct from other networks. It could be your house, your office building, an apartment, whatever. Typically, Metanix considers the fundamental defining aspects of these to be routers and subnets. -**Systems** are devices in any location. It can be any kind of device. PCs, servers, smart phones, game consoles. Depending on how its defined, Metanix will manage the device in some way, either through NixOS/Nix/Home Manager configurations, or through services it configures through Nix like DHCP, DNS, Headscale, etc. +**Systems** are devices in any location. It can be any kind of device. PCs, servers, smart phones, game consoles. Depending on how its defined, Metanix will manage the device in some way, either through NixOS/Nix/Home Manager configurations, or through services it configures through Nix like DHCP, DNS, Headscale, etc. -**Users** are (usually) the people who are using the systems and participating in your networks. Depending on the context, the user will have an account on the system, perhaps a network share with their data, and specific access controls granted or restricted to them on Headscale. +**Users** are (usually) the people who are using the systems and participating in your networks. Depending on the context, the user will have an account on the system, perhaps a network share with their data, and specific access controls granted or restricted to them on Headscale. **Groups** are, well, collections of things. Useful for providing a common configuration or access control to those things without having to do a bunch of needless extra legwork. This makes access control and security more straightforward, allows one to classify entire systems as being under a particular responsibility, etc. @@ -119,28 +125,32 @@ More on each of these categories of things later. role = "infrastructure"; tags = [ "router" "linode" ]; services = [ "nginx-proxy" "headscale" ]; - config = {...}: { # NixOS config here, or use a path to one. }; + config = {...}: { # NixOS config here, or use a path to one. + }; }; metatron = { owner = "yaro"; nixOS = true; role = "server"; tags = ["server" "linode" "upstream"]; - config = {...}: { # NixOS config here, or use a path to one. }; + config = {...}: { # NixOS config here, or use a path to one. + }; }; io = { owner = "yaro"; nixOS = true; role = "infrastructure"; tags = ["router" "downstream"]; - config = {...}: { # NixOS config here, or use a path to one. }; + config = {...}: { # NixOS config here, or use a path to one. + }; }; europa = { owner = "yaro"; nixOS = true; role = "infrastructure"; tags = ["router" "downstream"]; - config = {...}: { # NixOS config here, or use a path to one. }; + config = {...}: { # NixOS config here, or use a path to one. + }; }; deimos = { owner = "yaro"; @@ -157,7 +167,8 @@ More on each of these categories of things later. "forgejo" "headscale" ]; - config = {...}: { # NixOS config here, or use a path to one. }; + config = {...}: { # NixOS config here, or use a path to one. + }; }; phobos = { owner = "yaro"; @@ -171,7 +182,8 @@ More on each of these categories of things later. "sandbox" "node-red" ]; - config = {...}: { # NixOS config here, or use a path to one. }; + config = {...}: { # NixOS config here, or use a path to one. + }; }; terra = { owner = "yaro"; @@ -189,7 +201,8 @@ More on each of these categories of things later. group = { userGroups = { programmers = { - config = { ... }: { # Home-manager config here, or use a path to one. }; + config = { ... }: { # Home-manager config here, or use a path to one. + }; }; }; systemGroups = { @@ -198,7 +211,8 @@ More on each of these categories of things later. "nfs" "isci" ]; - config = { ... }: { # Home-manager config here, or use a path to one. }; + config = { ... }: { # Home-manager config here, or use a path to one. + }; }; }; }; diff --git a/flake.lock b/flake.lock index 8530de9..a0f88f6 100644 --- a/flake.lock +++ b/flake.lock @@ -2,18 +2,14 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1752950548, - "narHash": "sha256-NS6BLD0lxOrnCiEOcvQCDVPXafX1/ek1dfJHX1nUIzc=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "c87b95e25065c028d31a94f06a62927d18763fdf", - "type": "github" + "lastModified": 0, + "narHash": "sha256-NV4S2Lf2hYmZQ3Qf4t/YyyBaJNuxLPyjzvDma0zPp/M=", + "path": "/nix/store/4878dzkl1bdxv6wqavzy7bdracvl9q16-source", + "type": "path" }, "original": { - "owner": "NixOS", - "ref": "nixos-unstable", - "repo": "nixpkgs", - "type": "github" + "id": "nixpkgs", + "type": "indirect" } }, "root": { diff --git a/lib/functions/buildLocationList.nix b/lib/functions/buildLocationList.nix new file mode 100644 index 0000000..012706c --- /dev/null +++ b/lib/functions/buildLocationList.nix @@ -0,0 +1 @@ +locations: builtins.attrNames locations diff --git a/lib/metanix.nix b/lib/metanix.nix index 4325112..a2d338c 100644 --- a/lib/metanix.nix +++ b/lib/metanix.nix @@ -1,45 +1,12 @@ -# lib/metanix.nix { data, lib }: let - buildSystemIndex = + indexOf = name: names: let - flattenSystems = locationName: subnetName: systems: - lib.mapAttrsToList - (systemName: systemCfg: { - inherit systemName locationName subnetName; - primary = systemCfg.primary or false; - macs = systemCfg.macAddresses or [ ]; - }) - systems; - - allSystems = - builtins.concatLists ( - lib.mapAttrsToList - (locationName: location: - builtins.concatLists ( - lib.mapAttrsToList - (subnetName: subnet: - flattenSystems locationName subnetName (subnet.systems or { }) - ) - (location.subnets or { }) - ) - ) - data.locations - ); + indexed = builtins.imap0 (i: v: { inherit i v; }) names; + matches = builtins.filter (x: x.v == name) indexed; in - builtins.foldl' - (acc: entry: - let - existing = acc.${entry.systemName} or [ ]; - in - acc // { - ${entry.systemName} = existing ++ [ builtins.removeAttrs entry [ "systemName" ] ]; - } - ) - { } - allSystems; + if matches == [ ] then null else (builtins.head matches).i; + in -{ - inherit buildSystemIndex; -} +{ indexOf = indexOf; } diff --git a/test/test-buildLocationList.nix b/test/test-buildLocationList.nix new file mode 100644 index 0000000..855d45c --- /dev/null +++ b/test/test-buildLocationList.nix @@ -0,0 +1,7 @@ +let + buildLocationList = import ../lib/functions/buildLocationList.nix; + sampleData = import ./meta.nix; +in +{ + result = buildLocationList sampleData.locations; +}