refactor(registry): add stable uid registry
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
{ lib }:
|
||||
{ lib, ... }:
|
||||
{
|
||||
type = lib.types.str;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{ lib }:
|
||||
{ lib, ... }:
|
||||
{
|
||||
type = lib.types.submodule {
|
||||
options = {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{ lib }:
|
||||
{ lib, ... }:
|
||||
let
|
||||
entryType = lib.types.submodule {
|
||||
options = {
|
||||
|
||||
49
registry/stable-uid.nix
Normal file
49
registry/stable-uid.nix
Normal file
@@ -0,0 +1,49 @@
|
||||
{ lib, ... }:
|
||||
let
|
||||
inherit (lib)
|
||||
concatStringsSep
|
||||
foldl'
|
||||
groupBy
|
||||
unique
|
||||
;
|
||||
|
||||
duplicateUids = entries:
|
||||
let
|
||||
byUid = groupBy (entry: toString entry.value) entries;
|
||||
in
|
||||
foldl' (
|
||||
acc: uid:
|
||||
let
|
||||
serviceNames = unique (map (entry: entry.entry) byUid.${uid});
|
||||
in
|
||||
if builtins.length serviceNames > 1 then
|
||||
acc ++ [ {
|
||||
uid = lib.toInt uid;
|
||||
inherit serviceNames;
|
||||
} ]
|
||||
else
|
||||
acc
|
||||
) [ ] (builtins.attrNames byUid);
|
||||
|
||||
renderUidConflict = conflict:
|
||||
"${toString conflict.uid} is assigned to ${concatStringsSep ", " conflict.serviceNames}";
|
||||
in
|
||||
{
|
||||
# Next stable UID: 20003
|
||||
replicate = true;
|
||||
|
||||
type = lib.types.ints.positive;
|
||||
|
||||
validate = entries:
|
||||
let
|
||||
uidConflicts = duplicateUids entries;
|
||||
in
|
||||
[
|
||||
{
|
||||
assertion = uidConflicts == [ ];
|
||||
message = ''
|
||||
Duplicate stable UID allocation(s): ${concatStringsSep "\n" (map renderUidConflict uidConflicts)}
|
||||
'';
|
||||
}
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user