90 lines
1.8 KiB
Nix
90 lines
1.8 KiB
Nix
{
|
|
config,
|
|
lib,
|
|
...
|
|
}:
|
|
let
|
|
mkHost =
|
|
{
|
|
name,
|
|
kind,
|
|
traits ? [ ],
|
|
displays ? { },
|
|
users ? { },
|
|
imports ? [ ],
|
|
stateVersion ? "24.05",
|
|
}:
|
|
{
|
|
meta.host = {
|
|
inherit
|
|
displays
|
|
kind
|
|
name
|
|
traits
|
|
users
|
|
;
|
|
};
|
|
|
|
inherit imports;
|
|
|
|
networking.hostName = name;
|
|
system.stateVersion = stateVersion;
|
|
};
|
|
|
|
mkCaddyReverseProxy =
|
|
{
|
|
domain,
|
|
port,
|
|
extraHeaders ? [ ],
|
|
extraConfigText ? "",
|
|
}:
|
|
let
|
|
headerLines = map (header: " header_up ${header.name} ${header.value}") extraHeaders;
|
|
extraConfigLines = map (line: " ${line}") (
|
|
lib.filter (line: line != "") (lib.splitString "\n" extraConfigText)
|
|
);
|
|
bodyLines = headerLines ++ extraConfigLines;
|
|
body = lib.concatStringsSep "\n" bodyLines;
|
|
in
|
|
{
|
|
services.caddy.virtualHosts.${domain}.extraConfig =
|
|
if body == "" then
|
|
"reverse_proxy :${toString port}"
|
|
else
|
|
''
|
|
reverse_proxy :${toString port} {
|
|
${body}
|
|
}
|
|
'';
|
|
};
|
|
in
|
|
{
|
|
options.meta.lib.mkHost = lib.mkOption {
|
|
type = lib.types.raw;
|
|
description = "Internal host constructor shared between flake-parts modules.";
|
|
internal = true;
|
|
readOnly = true;
|
|
};
|
|
|
|
options.meta.lib.mkCaddyReverseProxy = lib.mkOption {
|
|
type = lib.types.raw;
|
|
description = "Internal Caddy reverse proxy helper shared between flake-parts modules.";
|
|
internal = true;
|
|
readOnly = true;
|
|
};
|
|
|
|
options.meta.lib.users = lib.mkOption {
|
|
type = lib.types.attrs;
|
|
description = "Canonical user attrsets shared by host definitions.";
|
|
internal = true;
|
|
readOnly = true;
|
|
};
|
|
|
|
config.meta.lib = {
|
|
inherit
|
|
mkCaddyReverseProxy
|
|
mkHost
|
|
;
|
|
};
|
|
}
|