Skip to content
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

Supports reading configuration from environment variables at runtime #2991

Open
1 task
medz opened this issue Jan 13, 2025 · 2 comments
Open
1 task

Supports reading configuration from environment variables at runtime #2991

medz opened this issue Jan 13, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@medz
Copy link

medz commented Jan 13, 2025

Describe the feature

I noticed that the configuration of nitro's nitro.config.ts will eventually be serialized and read as JSON.

This creates the following problems:

  1. When using Docker for image packaging, sensitive information must be configured in the code (such as database account passwords and some cloud service keys, etc.)

  2. It is not possible to distinguish the configuration well for dev/test/prod, etc.

Expected goals

For example, when configuring the value of nitro.config.ts, we are allowed to configure environment variables, which will not be serialized and will only be read from the environment variables when the app starts.

Simple example:

export default defineNitroConfig({
  compatibilityDate: "2025-01-11",
  storage: {
    cache: {
      driver: "redis",
      host: process.env.REDIS_HOST, // or env("REDIS_HOST")
      port: 6379,
    },
  },

Additional information

  • Would you be willing to help implement this feature?
@pqnet
Copy link

pqnet commented Feb 12, 2025

This is relevant to be able to use the database effectively in production, as the password is rarely hardcodable in nitro.config file

@medz
Copy link
Author

medz commented Feb 13, 2025

@pqnet
This is relevant to be able to use the database effectively in production, as the password is rarely hardcodable in nitro.config file

This is very common, for example, it can be perfectly solved #1161 (comment)

It is worth mentioning that, for example, SvelteKit's solution is to type the KV of .env and then export it from $env.private.

The reason I like Nitro/Nuxt is the typing of runtimeConfig, which allows TS to detect the configuration I am using.

There are also some other problems that need this kind of situation, such as:

  • I have a CORS middleware that has different configurations for different environments.
  • Solve the configuration of Storage and DB in different environments

There is also the most important problem:
Nitro uses c12 for serialization configuration, but there is a serious problem. The configuration method of $env.<name> is supported in the configuration file. But I had to implement the NODE_ENV setting at the stage of building the program, which resulted in the built program not being able to be directly deployed to the corresponding environment, and could only be built for NODE_ENV

Not everyone uses platforms such as Vercel/Netifty/cloudflare for deployment

For example, I use docker and use actions to automatically deploy and build, so my configuration must be like this:

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
RUN bun run build
- id: env
  run: |
    if [ "${{ github.ref }}" = "refs/heads/main" ]; then
    echo "NODE_ENV=staging" >> $GITHUB_OUTPUT
    elif [ "${{ github.ref }}" = "refs/heads/stable" ]; then
    echo "NODE_ENV=production" >> $GITHUB_OUTPUT fi 
- RUN: docker build --build-arg NODE_ENV=${{ steps.env.outputs.NODE_ENV }} -t $TAG .

@pi0 pi0 added enhancement New feature or request and removed pending triage labels Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants