1
0
mirror of https://github.com/kmein/niveum synced 2026-03-16 10:11:08 +01:00
Files
niveum/.bin/toposort.nix

82 lines
1.3 KiB
Nix
Raw Normal View History

let
lib = import <nixpkgs/lib>;
2025-12-27 22:22:54 +01:00
in
rec {
inherit lib;
input = [
{
2025-12-27 22:22:54 +01:00
x = [
"pool"
"zfs"
];
y = [
"mdadm"
"raid1"
];
}
{
2025-12-27 22:22:54 +01:00
x = [
"pool"
"zfs"
];
y = [
"disk"
"sda"
];
}
{
2025-12-27 22:22:54 +01:00
x = [
"mdadm"
"raid1"
];
y = [
"disk"
"sdb"
];
}
{
2025-12-27 22:22:54 +01:00
x = [
"mdadm"
"raid1"
];
y = [
"disk"
"sdc"
];
}
];
2025-12-27 22:22:54 +01:00
outNodes = node: graph: lib.unique (builtins.map (e: e.y) (builtins.filter (v: v.x == node) graph));
2025-12-27 22:22:54 +01:00
vertices = graph: lib.unique (builtins.map (x: x.y) graph ++ builtins.map (x: x.x) graph);
deleteVertex = node: graph: (builtins.filter (v: v.x != node && v.y != node) graph);
2025-12-27 22:22:54 +01:00
findSink =
graph:
lib.findFirst (v: outNodes v graph == [ ]) (lib.trace graph (builtins.abort "No sink found")) (
vertices graph
);
2025-12-27 22:22:54 +01:00
topSort =
graph:
if graph == [ ] then
[ ]
else if builtins.length graph == 1 then
let
only = builtins.head graph;
in
[
only.y
only.x
]
else
let
sink = findSink graph;
in
[ sink ] ++ topSort (deleteVertex sink graph);
output = topSort input;
}