-
Notifications
You must be signed in to change notification settings - Fork 249
Use envsubst
in our NGINX plugin
#1435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
plugins/nginx.json
Outdated
"readme": "nginx can be configured with env variables\n\nTo customize:\n* Use $NGINX_CONFDIR to change the configuration directory\n* Use $NGINX_LOGDIR to change the log directory\n* Use $NGINX_PIDDIR to change the pid directory\n* Use $NGINX_RUNDIR to change the run directory\n* Use $NGINX_SITESDIR to change the sites directory\n* Use $NGINX_TMPDIR to change the tmp directory. Use $NGINX_USER to change the user\n* Use $NGINX_GROUP to customize.", | ||
"version": "0.0.3", | ||
"readme": "nginx can be configured with env variables\n\nTo customize:\n* Use $NGINX_CONFDIR to change the configuration directory\n* Use $NGINX_TMPDIR to change the tmp directory. Use $NGINX_USER to change the user\n* Use $NGINX_WEB_PORT to change the port NGINX runs on. \n Note: This plugin uses envsubst when running `devbox services` to generate the nginx.conf file from the nginx.template file. To customize the nginx.conf file, edit the nginx.template file.\n", | ||
"__packages": ["envsubst"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the same as packages
in devbox.json
? Like does it go through search api, added to lockfile? Or is it just a passthrough to flake.nix, and use the default commit hash?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mikeland73 do you know if this bypasses the search index? I wasn't able to add @latest
when including the package
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this will be installed as a legacy package. We could do envsubst@latest
? or pin the version?
listen $NGINX_WEB_PORT; | ||
listen [::]:$NGINX_WEB_PORT; | ||
server_name localhost; | ||
root ../../../devbox.d/web; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Lagoja Should server_name
and root
be configurable through environment variables too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding variables for both of these
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the following to the plugin:
WEB_PORT
WEB_SERVER_NAME
WEB_ROOT
These are all set by the plugin, and can be overridden using the .env
support or via the env
section in the devbox.json. devbox services up|start|restart
or will automatically regenerate the nginx.conf
using:
envsubst < $NGINX_CONFDIR/nginx.template > $NGINX_CONFDIR/nginx.conf
If for whatever reason you want to lock the nginx.conf
, you can delete the nginx.template
file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. My only comment is whether the env vars should be namespaced with NGINX
Currently plugin environment variables are added to the entire environment. That means that if you want to run multiple custom servers we would probably want env variables to have different names. We have talked about isolating plugin environments but that's difficult to do and if we ever do that we would probably need to make it opt-in to keep backward-compatibility.
plugins/nginx.json
Outdated
"readme": "nginx can be configured with env variables\n\nTo customize:\n* Use $NGINX_CONFDIR to change the configuration directory\n* Use $NGINX_LOGDIR to change the log directory\n* Use $NGINX_PIDDIR to change the pid directory\n* Use $NGINX_RUNDIR to change the run directory\n* Use $NGINX_SITESDIR to change the sites directory\n* Use $NGINX_TMPDIR to change the tmp directory. Use $NGINX_USER to change the user\n* Use $NGINX_GROUP to customize.", | ||
"version": "0.0.3", | ||
"readme": "nginx can be configured with env variables\n\nTo customize:\n* Use $NGINX_CONFDIR to change the configuration directory\n* Use $NGINX_TMPDIR to change the tmp directory. Use $NGINX_USER to change the user\n* Use $NGINX_WEB_PORT to change the port NGINX runs on. \n Note: This plugin uses envsubst when running `devbox services` to generate the nginx.conf file from the nginx.template file. To customize the nginx.conf file, edit the nginx.template file.\n", | ||
"__packages": ["envsubst"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this will be installed as a legacy package. We could do envsubst@latest
? or pin the version?
plugins/nginx.json
Outdated
"WEB_PORT": "8081", | ||
"WEB_ROOT": "../../../devbox.d/web", | ||
"WEB_SERVER_NAME": "localhost" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these be namespaced?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be relatively safe to not namespace them -- I guess the conflict would arise if you are running multiple servers (NGINX/Apache/Caddy) in the same shell, which seems a bit unlikely?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll namespace them for now until we decide on this
Summary
NGINX does not support using environment variables in it's configuration file, which makes it difficult for our plugin to configure NGINX to run in a local devbox shell.
This update to the NGINX plugin installs envsubst, and then uses it to generate a valid
nginx.conf
from annginx.template
file. Developers can further customize their nginx.conf by adding environment variables to thenginx.template
file.Envsubst will generate the nginx.conf whenever
devbox services up
ordevbox service start
is run.How was it tested?
Using the nginx example:
devbox services up
devbox.d/nginx
folder, and run on the NGINX_WEB_PORT