Passing .env to a flake

I have a flake which outputs a nixosConfigurations.foobar entry.
I use nixos-rebuild --flake to deploy that config to a server.

Depending on whether I am pushing to prod or to my test environment I’d like to pass a different set of variables into the configs specialArgs. Preferrably through the use of a .env file, as is common in web projects.

Is there a mechanism that allows me to do that?

Don’t do that. Instead make two (or more) different nixosConfiguration entries, and call e.g. nixos-rebuild --flake path#dev and nixos-rebuild --flake path#prod depending on where you’re deploying to.

Also don’t use specialArgs to make changes between the configurations, just import a different module.

1 Like

Agreed, the point of the .env is so you can declare conditions in the environment. Just declare them in the proper first-party supported mechanism of a nixos module.