2022-09-27 22:07:03 +02:00
|
|
|
let
|
|
|
|
|
lib = import <nixpkgs/lib>;
|
2025-12-27 22:22:54 +01:00
|
|
|
in
|
|
|
|
|
rec {
|
2022-09-27 22:07:03 +02:00
|
|
|
inherit lib;
|
|
|
|
|
|
|
|
|
|
input = [
|
|
|
|
|
{
|
2025-12-27 22:22:54 +01:00
|
|
|
x = [
|
|
|
|
|
"pool"
|
|
|
|
|
"zfs"
|
|
|
|
|
];
|
|
|
|
|
y = [
|
|
|
|
|
"mdadm"
|
|
|
|
|
"raid1"
|
|
|
|
|
];
|
2022-09-27 22:07:03 +02:00
|
|
|
}
|
|
|
|
|
{
|
2025-12-27 22:22:54 +01:00
|
|
|
x = [
|
|
|
|
|
"pool"
|
|
|
|
|
"zfs"
|
|
|
|
|
];
|
|
|
|
|
y = [
|
|
|
|
|
"disk"
|
|
|
|
|
"sda"
|
|
|
|
|
];
|
2022-09-27 22:07:03 +02:00
|
|
|
}
|
|
|
|
|
{
|
2025-12-27 22:22:54 +01:00
|
|
|
x = [
|
|
|
|
|
"mdadm"
|
|
|
|
|
"raid1"
|
|
|
|
|
];
|
|
|
|
|
y = [
|
|
|
|
|
"disk"
|
|
|
|
|
"sdb"
|
|
|
|
|
];
|
2022-09-27 22:07:03 +02:00
|
|
|
}
|
|
|
|
|
{
|
2025-12-27 22:22:54 +01:00
|
|
|
x = [
|
|
|
|
|
"mdadm"
|
|
|
|
|
"raid1"
|
|
|
|
|
];
|
|
|
|
|
y = [
|
|
|
|
|
"disk"
|
|
|
|
|
"sdc"
|
|
|
|
|
];
|
2022-09-27 22:07:03 +02:00
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
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));
|
2022-09-27 22:07:03 +02:00
|
|
|
|
2025-12-27 22:22:54 +01:00
|
|
|
vertices = graph: lib.unique (builtins.map (x: x.y) graph ++ builtins.map (x: x.x) graph);
|
2022-09-27 22:07:03 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
);
|
2022-09-27 22:07:03 +02:00
|
|
|
|
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);
|
2022-09-27 22:07:03 +02:00
|
|
|
|
|
|
|
|
output = topSort input;
|
|
|
|
|
}
|