How can I load .env files into my dev.nix

Hi there,

I am wondering if there is a way that I can load my .env file into my .idx/dev.nix file without adding them all verbosely as there is sensitive information in there and I hope to source control my .idx/dev.nix file in the future.

This is so that my React project can be previewed in the editor.

I have tried sourcing the .env file in the preview command as such:

command = [
            "source .env"
            "&&"
            "yarn"
            "run"
            "start"
            "--port"
            "$PORT"
            "--host"
            "0.0.0.0"
            "--disable-host-check"
          ];

But I end up with the error

2025-04-17T23:52:33Z [web] <IDX> Starting preview...

2025-04-17T23:52:33Z [web] Waiting for your web server to start serving

2025-04-17T23:52:33Z [web] Error: Error: spawn source .env ENOENT

2025-04-17T23:52:33Z [web] <IDX> ✘ Preview failed to start

Any help would be greatly appreciated

Best

@srepollock - that’s a good question. I don’t know the answer, but if anybody knows, it would be @Vova, our resident NixOS expert :slight_smile:

Hi @srepollock ,

If you want to propagate env vars into the preview, you can achieve that with something like the following:

command = [
  "bash"
  "-c"
  ''
  source .env
  exec yarn run start --port $PORT --host 0.0.0.0 --disable-host-check
  ''
];

Fantastic! That does give me a successful build

Starting the development server...

Compiled successfully!

You can now view obsidian_rhythm_ii_frontend in the browser.

  Local:            https://localhost:443
  On Your Network:  https://10.88.0.3:443

Note that the development build is not optimized.
To create a production build, use yarn build.

webpack compiled successfully
Files successfully emitted, waiting for typecheck results...
Issues checking in progress...
No issues found.
Compiling...
Compiled successfully!
webpack compiled successfully
Compiling...
No issues found.
Compiled successfully!

However, now it seems to be in a restart loop

The “Starting Preview” keeps spinning and when I go to the webpage there is no output and the page keeps refreshing periodically (approx every 3 seconds)

I will say though - when building via the console it loads without issue however