|
| 1 | +{ |
| 2 | + pkgs, |
| 3 | + config, |
| 4 | + lib, |
| 5 | + ... |
| 6 | +}: |
| 7 | +let |
| 8 | + inherit (lib) mkOption types; |
| 9 | + cfg = config.services.ingress; |
| 10 | + |
| 11 | + tenants = lib.mapAttrsToList (name: t: t // { inherit name; }) cfg.tenants; |
| 12 | + |
| 13 | + # binds the public port; routes each /<name>/* to a tenant's loopback caddy |
| 14 | + # with the prefix stripped. |
| 15 | + rootCaddyfile = pkgs.writeText "ingress-root.Caddyfile" ( |
| 16 | + lib.concatStringsSep "\n" ( |
| 17 | + [ |
| 18 | + "{" |
| 19 | + "\tadmin off" |
| 20 | + "\tauto_https off" |
| 21 | + "}" |
| 22 | + "" |
| 23 | + ":${toString cfg.publicPort} {" |
| 24 | + ] |
| 25 | + ++ map ( |
| 26 | + t: "\thandle_path /${t.name}/* {\n\t\treverse_proxy 127.0.0.1:${toString t.upstreamPort}\n\t}" |
| 27 | + ) tenants |
| 28 | + ++ [ |
| 29 | + "\thandle {" |
| 30 | + "\t\trespond \"not found\" 404" |
| 31 | + "\t}" |
| 32 | + "}" |
| 33 | + "" |
| 34 | + ] |
| 35 | + ) |
| 36 | + ); |
| 37 | + |
| 38 | + # tab-indent each non-blank route line so the rendered seed is caddy-fmt clean. |
| 39 | + indent = |
| 40 | + text: |
| 41 | + lib.concatStringsSep "\n" (map (l: if l == "" then l else "\t${l}") (lib.splitString "\n" text)); |
| 42 | + |
| 43 | + # runtime dir (0700, owned by the tenant) holding the caddy admin socket. |
| 44 | + rundirOf = t: "/run/ingress-${t.name}"; |
| 45 | + |
| 46 | + # the tenant's Caddyfile, rendered when absent; user-owned afterward. The |
| 47 | + # admin socket in the 0700 rundir lets the user `caddy reload` in place, and |
| 48 | + # no other non-root user can reach it. |
| 49 | + seedFile = |
| 50 | + t: |
| 51 | + pkgs.writeText "ingress-${t.name}-seed.Caddyfile" ( |
| 52 | + lib.concatStringsSep "\n" [ |
| 53 | + "{" |
| 54 | + "\tadmin unix/${rundirOf t}/admin.sock" |
| 55 | + "\tauto_https off" |
| 56 | + "}" |
| 57 | + "" |
| 58 | + ":${toString t.upstreamPort} {" |
| 59 | + (indent (lib.removeSuffix "\n" t.routes)) |
| 60 | + "}" |
| 61 | + "" |
| 62 | + ] |
| 63 | + ); |
| 64 | +in |
| 65 | +{ |
| 66 | + options.services.ingress = { |
| 67 | + enable = lib.mkEnableOption "the public path-routed ingress (root caddy + per-user self-serve tenant caddies)"; |
| 68 | + |
| 69 | + publicPort = mkOption { |
| 70 | + type = types.port; |
| 71 | + default = 8080; |
| 72 | + description = '' |
| 73 | + Port the root caddy binds and the image exposes. Meant to be exe.dev's |
| 74 | + primary (root-URL) port; the deploy pins it with `share port` and |
| 75 | + publishes it with `share set-public`. |
| 76 | + ''; |
| 77 | + }; |
| 78 | + |
| 79 | + tenants = mkOption { |
| 80 | + default = { }; |
| 81 | + description = '' |
| 82 | + Per-user subtrees under /<name>/* on the public port. Each runs a caddy |
| 83 | + as its user, reading a Caddyfile the user owns and can reload, so a user |
| 84 | + can expose new services at runtime without a rebuild or root. |
| 85 | + ''; |
| 86 | + type = types.attrsOf ( |
| 87 | + types.submodule ( |
| 88 | + { name, ... }: |
| 89 | + { |
| 90 | + options = { |
| 91 | + user = mkOption { |
| 92 | + type = types.str; |
| 93 | + default = name; |
| 94 | + description = "User the tenant caddy runs as (must be a declared user)."; |
| 95 | + }; |
| 96 | + upstreamPort = mkOption { |
| 97 | + type = types.port; |
| 98 | + description = "Loopback port the tenant caddy binds; root forwards /<name>/* here (prefix stripped)."; |
| 99 | + }; |
| 100 | + routes = mkOption { |
| 101 | + type = types.lines; |
| 102 | + default = "respond \"${name}: no routes configured yet\" 404"; |
| 103 | + description = "Seed route body spliced into the tenant's site block on first boot; user-owned after."; |
| 104 | + }; |
| 105 | + }; |
| 106 | + } |
| 107 | + ) |
| 108 | + ); |
| 109 | + }; |
| 110 | + }; |
| 111 | + |
| 112 | + config = lib.mkIf cfg.enable { |
| 113 | + image.exposedPorts.tcp = [ cfg.publicPort ]; |
| 114 | + |
| 115 | + # tenants manage their routes with `caddy reload`, so put caddy on their PATH. |
| 116 | + users.users = lib.listToAttrs ( |
| 117 | + map (t: lib.nameValuePair t.user { packages = [ pkgs.caddy ]; }) tenants |
| 118 | + ); |
| 119 | + |
| 120 | + s6.services = lib.mkMerge ( |
| 121 | + [ |
| 122 | + { |
| 123 | + ingress-root = { |
| 124 | + dependencies = [ "base" ]; |
| 125 | + run = '' |
| 126 | + mkdir -p /run/ingress-root |
| 127 | + exec env HOME=/run/ingress-root XDG_DATA_HOME=/run/ingress-root XDG_CONFIG_HOME=/run/ingress-root \ |
| 128 | + ${pkgs.caddy}/bin/caddy run --config ${rootCaddyfile} --adapter caddyfile |
| 129 | + ''; |
| 130 | + }; |
| 131 | + } |
| 132 | + ] |
| 133 | + ++ map ( |
| 134 | + t: |
| 135 | + let |
| 136 | + u = config.users.users.${t.user}; |
| 137 | + gid = toString config.users.groups.${u.group}.gid; |
| 138 | + caddyDir = "${u.home}/.caddy"; |
| 139 | + caddyfile = "${caddyDir}/Caddyfile"; |
| 140 | + rundir = rundirOf t; |
| 141 | + in |
| 142 | + { |
| 143 | + # write the Caddyfile only when absent. |
| 144 | + "ingress-${t.name}-setup" = { |
| 145 | + type = "oneshot"; |
| 146 | + dependencies = [ |
| 147 | + "base" |
| 148 | + ] |
| 149 | + ++ lib.optional config.services.backup.enable "backup-restore"; |
| 150 | + run = '' |
| 151 | + mkdir -p ${caddyDir} |
| 152 | + [ -e ${caddyfile} ] || cp ${seedFile t} ${caddyfile} |
| 153 | + chown -R ${toString u.uid}:${gid} ${caddyDir} |
| 154 | + ''; |
| 155 | + }; |
| 156 | + "ingress-${t.name}" = { |
| 157 | + dependencies = [ |
| 158 | + "base" |
| 159 | + "ingress-${t.name}-setup" |
| 160 | + ]; |
| 161 | + run = '' |
| 162 | + mkdir -p ${rundir} |
| 163 | + chown ${toString u.uid}:${gid} ${rundir} |
| 164 | + chmod 700 ${rundir} |
| 165 | + exec /command/s6-setuidgid ${t.user} \ |
| 166 | + env HOME=${u.home} XDG_DATA_HOME=${rundir} XDG_CONFIG_HOME=${rundir} \ |
| 167 | + ${pkgs.caddy}/bin/caddy run --config ${caddyfile} --adapter caddyfile |
| 168 | + ''; |
| 169 | + }; |
| 170 | + } |
| 171 | + ) tenants |
| 172 | + ); |
| 173 | + }; |
| 174 | +} |
0 commit comments