Skip to content

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

Merged
merged 7 commits into from
Aug 31, 2023
Merged

Use envsubst in our NGINX plugin #1435

merged 7 commits into from
Aug 31, 2023

Conversation

Lagoja
Copy link
Contributor

@Lagoja Lagoja commented Aug 29, 2023

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 an nginx.template file. Developers can further customize their nginx.conf by adding environment variables to the nginx.template file.

Envsubst will generate the nginx.conf whenever devbox services up or devbox service start is run.

How was it tested?

Using the nginx example:

  1. Change the NGINX_WEB_PORT variable
  2. Run devbox services up
  3. NGINX should generate a valid nginx.conf in the devbox.d/nginx folder, and run on the NGINX_WEB_PORT

"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"],
Copy link
Collaborator

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?

Copy link
Contributor Author

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

Copy link
Contributor

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;
Copy link

@jrseliga jrseliga Aug 29, 2023

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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.

Copy link
Contributor

@mikeland73 mikeland73 left a 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.

"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"],
Copy link
Contributor

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?

Comment on lines 11 to 13
"WEB_PORT": "8081",
"WEB_ROOT": "../../../devbox.d/web",
"WEB_SERVER_NAME": "localhost"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be namespaced?

Copy link
Contributor Author

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?

Copy link
Contributor Author

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

@Lagoja Lagoja merged commit 8e496f6 into main Aug 31, 2023
@Lagoja Lagoja deleted the jl/nginx-envsubst branch August 31, 2023 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants