mirror of
https://github.com/SebastianStork/nixos-config.git
synced 2026-01-21 23:11:34 +01:00
Add uniqueness check for ports
Credit to https://lorenzbischof.ch/posts/detect-port-conflicts-in-nixos-services/
This commit is contained in:
parent
a4c3e2a829
commit
232c9aa946
21 changed files with 110 additions and 12 deletions
50
modules/system/meta/ports.nix
Normal file
50
modules/system/meta/ports.nix
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
{
|
||||
config,
|
||||
options,
|
||||
lib,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.meta.ports;
|
||||
|
||||
duplicatedPorts =
|
||||
options.meta.ports.list.definitionsWithLocations
|
||||
|> lib.concatMap (
|
||||
entry:
|
||||
map (port: {
|
||||
file = entry.file;
|
||||
port = port;
|
||||
}) entry.value
|
||||
)
|
||||
|> lib.groupBy (entry: toString entry.port)
|
||||
|> lib.filterAttrs (port: entries: lib.length entries > 1);
|
||||
|
||||
errorMessage =
|
||||
duplicatedPorts
|
||||
|> lib.mapAttrsToList (
|
||||
port: entries:
|
||||
"Duplicate port ${port} found in:\n" + lib.concatMapStrings (entry: " - ${entry.file}\n") entries
|
||||
)
|
||||
|> lib.concatStrings;
|
||||
in
|
||||
{
|
||||
options.meta.ports = {
|
||||
list = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.port;
|
||||
default = [ ];
|
||||
internal = true;
|
||||
};
|
||||
assertUnique = lib.mkEnableOption "" // {
|
||||
default = true;
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.assertUnique {
|
||||
assertions = [
|
||||
{
|
||||
assertion = duplicatedPorts == { };
|
||||
message = errorMessage;
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue