Grocy undefined env var error (how to debug PHP FPM in NixOS)

Viewing grocy.$domain.tld in browser or curl shows:

Fatal error: Uncaught Error: Failed opening required ''
Stack trace:
#0 /nix/store/89z2zmy2pgdxfzs74pv6ifva8vl8kgdq-grocy-4.2.0/grocy-4.2.0/public/index.php(45): require\_once()
#1 {main} thrown in /nix/store/89z2zmy2pgdxfzs74pv6ifva8vl8kgdq-grocy-4.2.0/app.php on line 15

So I looked in /nix/store/89z2zmy2pgdxfzs74pv6ifva8vl8kgdq-grocy-4.2.0/app.php at line 15, and it says

require_once getenv('GROCY_CONFIG_FILE');

So this environment variable is probably not being defined, which is why we fail to open required ''. I did systemctl show grocy.service | grep ExecStart which shows the exact commandline:

/nix/store/dpdpnmjisq6349404bkl5vzpbc2da07j-php-with-extensions-8.2.24/bin/php-fpm -y /nix/store/zgjas7fmlyh3m1r91mi8vz8i6nvb9spj-phpfpm-grocy.conf -c /nix/store/2kzix8dlxyqfv3p9fvs931rmc7y71sdz-php.ini

I did cat /nix/store/zgjas7fmlyh3m1r91mi8vz8i6nvb9spj-phpfpm-grocy.conf and it contains the lines:

[grocy]
....
env[GROCY_CONFIG_FILE] = /etc/grocy/config.php
...

Why is the environment variable undefined? What should I try next? The problem persists after restarting the service and rebooting the machine.

I am using Caddy instead of Nginx, but I don’t think that should matter, since this bug appears to be regarding environment variables set by PHP FPM.

I have little experience debugging PHP FPM. I think the errors are only going into journald (error_log = syslog); Journald shows no logs after startup. What would be the best way to “ask” PHP fpm to print its env? What other logs should I check?

This may be non-Nix related (I already asked in the upstream community), but the environment variable stuff does come from a Nix patch.

Here is the relevant configuration module in my NixOS flake. If creating a minimal example will help, I will do that (but it is some non-trivial effort). I was hoping someone who understands more than me could shed some light on how environment variables in the pool come to be set (or unset) in a mostly normal/default NixOS config. I also uploade several config files into a GitHub Gist so you can see what’s going on.

It turns out I’m an idiot.

I was pointing Caddy to the wrong phpfpm pool with the path/to/grocy web root. I initially thought the environment variables weren’t being passed through, when actually they were fine and the request was going to a different PHP FPM pool. Always question your assumptions (and sleep helps!).

My working Grocy on Caddy config is here.