create nginx magic module
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Vivian 2023-05-05 10:04:38 +02:00
parent e34c892fc2
commit 64bdee8a12
12 changed files with 111 additions and 64 deletions

View file

@ -1,3 +1,3 @@
{ ... }: {
imports = [ ./meta.nix ./dns.nix ./flood.nix ./gnome ./unpackerr.nix ./vault.nix ];
imports = [ ./meta.nix ./nginx.nix ./dns.nix ./flood.nix ./gnome ./unpackerr.nix ./vault.nix ];
}

View file

@ -12,6 +12,7 @@ let
};
port = mkOption {
type = types.int;
default = 80;
example = 4242;
description = ''
The port under which the service runs on the host
@ -21,6 +22,7 @@ let
};
in {
options.meta = {
exposes = mkOption {
type = with types; attrsOf (submodule exposesOpts);
description = ''
@ -37,6 +39,5 @@ in {
};
config = {
};
}

View file

@ -0,0 +1,32 @@
{ lib, hosts, config, ... }:
with lib;
let cfg = config.services.v.nginx;
in {
options.services.v.nginx.generateVirtualHosts =
mkEnableOption "generate vhosts";
config = let
proxy = url: {
enableACME = true;
forceSSL = true;
locations."/" = {
proxyPass = url;
proxyWebsockets = true;
};
};
hosts' =
filter (hasAttr "exposes") (attrValues hosts.${config.networking.domain});
exposes = { ip, exposes, ... }:
map ({ domain, port ? 80}: { inherit ip domain port; }) (attrValues exposes);
mkVhost = { ip, domain, port}: {
"${domain}" = proxy "http://${ip}:${toString port}";
};
vhosts = foldr (el: acc: acc // mkVhost el) { } (concatMap exposes hosts');
in mkIf cfg.generateVirtualHosts {
services.nginx.virtualHosts = vhosts;
};
}