Trying to structure this to be a flake input.
This commit is contained in:
parent
9b4bd81df3
commit
2e6fbe5856
3 changed files with 89 additions and 0 deletions
38
flake.nix
Normal file
38
flake.nix
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
{
|
||||
description = "Metanix infra logic flake";
|
||||
|
||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||
|
||||
outputs = { self, nixpkgs }:
|
||||
let
|
||||
system = "x86_64-linux";
|
||||
pkgs = import nixpkgs { inherit system; };
|
||||
data = import ./meta.nix;
|
||||
metanix = import ./lib/metanix.nix { inherit data; lib = pkgs.lib; };
|
||||
in
|
||||
{
|
||||
# You get a devShell so you can :repl in peace
|
||||
devShells.${system}.default = pkgs.mkShell {
|
||||
buildInputs = [ pkgs.nix pkgs.git ];
|
||||
};
|
||||
|
||||
# Simple command to dump the system index to stdout
|
||||
apps.${system}.print-index = {
|
||||
type = "app";
|
||||
program = pkgs.writeShellScript "print-index" ''
|
||||
nix eval --impure --expr '
|
||||
let
|
||||
data = import ./meta.nix;
|
||||
lib = import ${nixpkgs}/lib;
|
||||
metanix = import ./lib/metanix.nix { inherit data lib; };
|
||||
in
|
||||
builtins.toJSON metanix.buildSystemIndex
|
||||
'
|
||||
'';
|
||||
};
|
||||
|
||||
# Or expose it as a Nix eval-friendly output
|
||||
packages.${system}.default = pkgs.writeText "system-index.json"
|
||||
(builtins.toJSON metanix.buildSystemIndex);
|
||||
};
|
||||
}
|
||||
45
lib/metanix.nix
Normal file
45
lib/metanix.nix
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# lib/metanix.nix
|
||||
{ data, lib }:
|
||||
|
||||
let
|
||||
buildSystemIndex =
|
||||
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
|
||||
);
|
||||
in
|
||||
builtins.foldl'
|
||||
(acc: entry:
|
||||
let
|
||||
existing = acc.${entry.systemName} or [ ];
|
||||
in
|
||||
acc // {
|
||||
${entry.systemName} = existing ++ [ builtins.removeAttrs entry [ "systemName" ] ];
|
||||
}
|
||||
)
|
||||
{ }
|
||||
allSystems;
|
||||
in
|
||||
{
|
||||
inherit buildSystemIndex;
|
||||
}
|
||||
6
test-index.nix
Normal file
6
test-index.nix
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# test-index.nix
|
||||
let
|
||||
data = import ./meta.nix;
|
||||
metanix = import ./lib/metanix.nix { inherit data; };
|
||||
in
|
||||
metanix.buildSystemIndex
|
||||
Loading…
Add table
Add a link
Reference in a new issue