Skyler Grey | 2ca6ccd | 2023-10-14 22:56:43 +0000 | [diff] [blame] | 1 | { config, lib, pkgs, helpers, base, ... }: |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 2 | lib.recursiveUpdate { |
Skyler Grey | 2ca6ccd | 2023-10-14 22:56:43 +0000 | [diff] [blame] | 3 | options.clicks = { |
| 4 | nginx = { |
| 5 | services = lib.mkOption { |
| 6 | type = with lib.types; |
| 7 | listOf (submodule { |
| 8 | options = { |
| 9 | host = lib.mkOption { type = str; }; |
| 10 | extraHosts = lib.mkOption { type = listOf str; }; |
| 11 | secure = lib.mkOption { type = bool; }; |
| 12 | service = lib.mkOption { |
| 13 | type = let |
| 14 | validServiceTypes = { |
| 15 | "redirect" = { |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 16 | to = [ "string" str ]; |
| 17 | permanent = [ "bool" bool ]; |
Skyler Grey | 2ca6ccd | 2023-10-14 22:56:43 +0000 | [diff] [blame] | 18 | }; |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 19 | "reverseproxy" = { to = [ "string" str ]; }; |
Skyler Grey | 2ca6ccd | 2023-10-14 22:56:43 +0000 | [diff] [blame] | 20 | "php" = { |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 21 | root = [ "string" str ]; |
| 22 | socket = [ "string" str ]; |
Skyler Grey | 2ca6ccd | 2023-10-14 22:56:43 +0000 | [diff] [blame] | 23 | }; |
| 24 | "directory" = { |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 25 | private = [ "bool" bool ]; |
| 26 | root = [ "string" str ]; |
Skyler Grey | 2ca6ccd | 2023-10-14 22:56:43 +0000 | [diff] [blame] | 27 | }; |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 28 | "file" = { path = [ "string" str ]; }; |
Skyler Grey | 2ca6ccd | 2023-10-14 22:56:43 +0000 | [diff] [blame] | 29 | "path" = { |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 30 | path = [ "string" str ]; |
| 31 | service = [ "set" serviceType ]; |
Skyler Grey | 2ca6ccd | 2023-10-14 22:56:43 +0000 | [diff] [blame] | 32 | }; |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 33 | "compose" = { services = [ "list" (listOf serviceType) ]; }; |
| 34 | "status" = { statusCode = [ "int" int ]; }; |
Skyler Grey | 2ca6ccd | 2023-10-14 22:56:43 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | serviceType = mkOptionType { |
| 38 | name = "Service"; |
| 39 | |
| 40 | description = "clicks Nginx service"; |
| 41 | descriptionClass = "noun"; |
| 42 | |
| 43 | check = (x: |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 44 | if (builtins.typeOf x) != "set" then |
| 45 | lib.warn |
| 46 | "clicks nginx services must be sets but ${x} is not a set" |
| 47 | false |
| 48 | else if !(builtins.hasAttr "type" x) then |
| 49 | lib.warn |
| 50 | "clicks nginx services must have a type attribute but ${x} does not" |
| 51 | false |
| 52 | else if !(builtins.hasAttr x.type validServiceTypes) then |
| 53 | lib.warn |
| 54 | "clicks nginx services must have a valid type, but ${x.type} is not one" |
| 55 | false |
| 56 | else |
| 57 | (let |
| 58 | optionTypes = |
| 59 | (builtins.mapAttrs (n: o: builtins.elemAt o 0) |
| 60 | validServiceTypes.${x.type}) // { |
| 61 | type = "string"; |
| 62 | }; |
| 63 | in (lib.pipe x [ |
| 64 | (builtins.mapAttrs (n: o: |
| 65 | (builtins.hasAttr n optionTypes) && optionTypes.${n} |
| 66 | == (builtins.typeOf o))) |
| 67 | lib.attrValues |
| 68 | (builtins.all (x: x)) |
| 69 | ]) && (lib.pipe optionTypes [ |
| 70 | (builtins.mapAttrs (n: _: builtins.hasAttr n x)) |
| 71 | lib.attrValues |
| 72 | (builtins.all (x: x)) |
| 73 | ]))); |
Skyler Grey | 2ca6ccd | 2023-10-14 22:56:43 +0000 | [diff] [blame] | 74 | }; |
| 75 | in serviceType; |
| 76 | }; |
| 77 | type = lib.mkOption { type = strMatching "hosts"; }; |
| 78 | }; |
| 79 | }); |
| 80 | example = lib.literalExpression '' |
| 81 | with helpers.nginx; [ |
| 82 | (Host "example.clicks.codes" (ReverseProxy "generic:1001")) |
| 83 | ]''; |
| 84 | description = lib.mdDoc '' |
| 85 | Connects hostnames to services for your nginx server. We recommend using the Clicks helper to generate these |
| 86 | ''; |
| 87 | default = [ ]; |
| 88 | }; |
| 89 | serviceAliases = lib.mkOption { |
| 90 | type = with lib.types; |
| 91 | listOf (submodule { |
| 92 | options = { |
| 93 | host = lib.mkOption { |
| 94 | type = str; |
| 95 | example = "example.clicks.codes"; |
| 96 | description = '' |
| 97 | The ServerName of the server. If you override this in the nginx server block, you still need to put in the name of the attribute |
| 98 | ''; |
| 99 | }; |
| 100 | aliases = lib.mkOption { |
| 101 | type = listOf str; |
| 102 | example = [ "example2.clicks.codes" "example.coded.codes" ]; |
| 103 | description = '' |
| 104 | A list of servers to add as aliases |
| 105 | ''; |
| 106 | }; |
| 107 | type = lib.mkOption { type = strMatching "aliases"; }; |
| 108 | }; |
| 109 | }); |
| 110 | example = lib.literalExpression '' |
| 111 | with helpers.nginx; [ |
| 112 | (Host "example.clicks.codes" (ReverseProxy "generic:1001")) |
| 113 | ]''; |
| 114 | description = lib.mdDoc '' |
| 115 | Adds additional host names to your nginx server. If you're using `clicks.nginx.services` |
| 116 | you should generally use a Hosts block instead |
| 117 | ''; |
| 118 | default = [ ]; |
| 119 | }; |
| 120 | streams = lib.mkOption { |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 121 | type = with lib.types; |
| 122 | listOf (submodule { |
| 123 | options = { |
| 124 | internal = lib.mkOption { type = str; }; |
| 125 | external = lib.mkOption { type = port; }; |
| 126 | protocol = lib.mkOption { type = strMatching "^(tcp|udp)$"; }; |
Skyler Grey | 56b293d | 2023-10-22 22:53:34 +0000 | [diff] [blame^] | 127 | haproxy = lib.mkOption { type = bool; }; |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 128 | }; |
| 129 | }); |
Skyler Grey | 2ca6ccd | 2023-10-14 22:56:43 +0000 | [diff] [blame] | 130 | example = lib.literalExpression '' |
| 131 | with helpers.nginx; [ |
| 132 | (Stream 1001 "generic:1002" "tcp") |
| 133 | ]''; |
| 134 | description = lib.mdDoc '' |
| 135 | A list of servers to be placed in the nginx streams block. We recommend using the Clicks helper to generate these |
| 136 | ''; |
| 137 | default = [ ]; |
| 138 | }; |
| 139 | }; |
| 140 | }; |
| 141 | config = { |
| 142 | services.nginx = { |
| 143 | enable = true; |
| 144 | enableReload = true; |
| 145 | |
| 146 | virtualHosts = lib.recursiveUpdate (helpers.nginx.Merge |
| 147 | config.clicks.nginx.services) # clicks.nginx.services |
| 148 | (lib.pipe config.clicks.nginx.serviceAliases [ |
| 149 | (map (alias: { |
| 150 | name = alias.host; |
| 151 | value.serverAliases = alias.aliases; |
| 152 | })) |
| 153 | builtins.listToAttrs |
| 154 | ]); # clicks.nginx.serviceAliases |
| 155 | |
| 156 | streamConfig = builtins.concatStringsSep "\n" (map (stream: '' |
| 157 | server { |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 158 | listen ${builtins.toString stream.external}${ |
| 159 | lib.optionalString (stream.protocol == "udp") " udp" |
| 160 | }; |
Skyler Grey | 56b293d | 2023-10-22 22:53:34 +0000 | [diff] [blame^] | 161 | proxy_pass ${stream.internal}; |
| 162 | ${if stream.haproxy then "proxy_protocol on;" else ""} |
Skyler Grey | 2ca6ccd | 2023-10-14 22:56:43 +0000 | [diff] [blame] | 163 | } |
| 164 | '') config.clicks.nginx.streams); |
| 165 | }; |
| 166 | |
| 167 | networking.firewall.allowedTCPPorts = lib.pipe config.clicks.nginx.streams [ |
| 168 | (builtins.filter (stream: stream.protocol == "tcp")) |
| 169 | (map (stream: stream.external)) |
| 170 | ]; |
| 171 | networking.firewall.allowedUDPPorts = lib.pipe config.clicks.nginx.streams [ |
| 172 | (builtins.filter (stream: stream.protocol == "udp")) |
| 173 | (map (stream: stream.external)) |
| 174 | ]; |
| 175 | |
| 176 | security.acme.defaults = { |
| 177 | email = "admin@clicks.codes"; |
| 178 | credentialsFile = config.sops.secrets.cloudflare_cert__api_token.path; |
| 179 | }; |
| 180 | security.acme.acceptTerms = true; |
| 181 | |
| 182 | sops.secrets.cloudflare_cert__api_token = { |
| 183 | mode = "0660"; |
| 184 | owner = config.users.users.nginx.name; |
| 185 | group = config.users.users.acme.group; |
| 186 | sopsFile = ../secrets/cloudflare-cert.env.bin; |
| 187 | format = "binary"; |
| 188 | }; |
Skyler Grey | 4259e93 | 2023-10-21 21:37:03 +0000 | [diff] [blame] | 189 | |
| 190 | users.users.nginx.extraGroups = [ config.users.users.acme.group ]; |
Skyler Grey | 2ca6ccd | 2023-10-14 22:56:43 +0000 | [diff] [blame] | 191 | }; |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 192 | } (if base != null then { |
Skyler Grey | 4259e93 | 2023-10-21 21:37:03 +0000 | [diff] [blame] | 193 | config.security.acme.certs = lib.mkForce (builtins.mapAttrs (_: v: |
| 194 | (lib.filterAttrs (n: _: n != "directory") v) // { |
| 195 | webroot = null; |
| 196 | dnsProvider = "cloudflare"; |
| 197 | }) base.config.security.acme.certs); |
Skyler Grey | fe1740c | 2023-10-21 01:24:18 +0000 | [diff] [blame] | 198 | } else |
| 199 | { }) |