Create semi-dynamic reverse proxy with nginx (possibly with lua)

I have syncthing running for each user on my system. Basically, what I do is to use a constant number + each user’s user id as the port for the respective web ui. (If somebody has a better approach that may also be worth exploring.)

Now I have to remember the appropriate port for each user. I’d prefer to set this using a reverse proxy.

However, the user id is only known at runtime. So I need nginx to check user ids before setting up the proxy. I’ve tried this with lua, but I get the error

nginx: [emerg] unknown directive "set_by_lua" in /nix/store/0hpa...

leading me to believe that either my lua knowledge is that sorely lacking (it is), or I have not succesfully enabled lua, or something even more basic. Here’s a section of my config

{
  services.nginx = {
      enable = true;
      recommendedProxySettings = true;
      additionalModules = [pkgs.nginxPackages.lua];
      virtualHosts = builtins.listToAttrs (map (name: {
        name = "${name}.syncthing.${infrastructure.homelab.publicDomain}";
        value = {
          locations."/" = {
            proxyPass = "http://localhost:$user_port";
            extraConfig = ''
              set_by_lua_block $user_port {
                local handle = io.popen("id -u " .. "${name}")
                local result = handle:read("*a")
                handle:close()
                # portOffset is a constant number
                return tonumber(result) + ${toString portOffset}
              }
            '';
          };
        };
      # users ++ preexistingUsers is a list of { name = "username"; } attrsets
      }) (users ++ preexistingUsers));
    };
}

Can somebody help?