Files
nixos/modules/nixos/proxy.nix
T

97 lines
2.4 KiB
Nix
Raw Normal View History

2024-10-03 15:21:56 -05:00
{ config, lib, ... }:
2022-04-20 11:11:35 -05:00
let
cfg = config.greg.proxies;
2022-04-20 11:11:35 -05:00
alias = name: with builtins; head (split "\\." name);
2022-04-29 08:47:52 -05:00
makeHost = name: dest: {
forceSSL = dest.ssl;
enableACME = dest.ssl;
locations."${dest.path}" = {
proxyPass = dest.target;
2024-10-19 01:19:59 -05:00
extraConfig =
''
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
''
+ dest.extraConfig;
};
serverAliases = lib.mkIf dest.genAliases [ "${alias name}" ];
};
2022-04-20 11:11:35 -05:00
in
2024-10-19 01:19:59 -05:00
with lib;
{
options = {
greg.proxies = mkOption {
default = { };
example = literalExpression ''
2024-10-19 01:19:59 -05:00
{ host-name = {
target = proxyLocation;
ssl = true;
};
'';
description = ''
2024-10-19 01:19:59 -05:00
Quick and simple Nginx proxy configurations.
Use this to configure a very simple proxy that does not
need any extra customization options other than SSL
enablement.
'';
2022-04-20 11:11:35 -05:00
2024-10-19 01:19:59 -05:00
type =
with types;
attrsOf (
submodule (
{ ... }:
{
options = {
genAliases = mkOption {
type = types.bool;
description = "Whether to auto-generate short alias name";
default = true;
};
2022-06-12 04:58:34 +00:00
2024-10-19 01:19:59 -05:00
target = mkOption {
type = types.str;
description = ''The destination that is being proxied.'';
example = "http://localhost:8080";
};
2022-04-20 11:11:35 -05:00
2024-10-19 01:19:59 -05:00
ssl = mkOption {
type = types.bool;
description = "Whether to enable SSL in front of the proxy";
default = false;
};
2022-04-20 11:11:35 -05:00
2024-10-19 01:19:59 -05:00
path = mkOption {
type = types.str;
description = "The path prefix for this proxy";
default = "/";
};
2023-12-19 19:07:02 +00:00
2024-10-19 01:19:59 -05:00
extraConfig = mkOption {
type = types.str;
description = "Extra nginx config options";
default = "";
};
};
}
)
);
};
};
2022-04-20 11:11:35 -05:00
config.services.nginx = mkIf ((attrValues cfg) != [ ]) {
enable = true;
2022-04-20 11:11:35 -05:00
recommendedGzipSettings = true;
recommendedOptimisation = true;
recommendedProxySettings = true;
recommendedTlsSettings = true;
2022-04-20 11:11:35 -05:00
virtualHosts = mapAttrs makeHost cfg;
};
2022-04-20 11:11:35 -05:00
}