Defining environment variables declaratively

I need to define some environment variables, to use it later for some shell scripting requirements. I’m wondering if it can be done using .nix configs or not? Shell script will be running inside NixOS default shell(Bash). One note: environment variables should be persistent!!!

1 Like

You can use environment.variables and environment.sessionVariables.

1 Like

Where can I find more detailed documentation about this? There’s no articles about this in NixOS Manual

You can use one of these methods to learn a lot about the options defined:

1 Like

What about getting value of a variable? I declared environment variables, this way:

environment.variables = {
    DOMAIN = "";
    dkim = "$( cat /var/dkim/$DOMAIN.selector.txt )";

How do I use DOMAIN variable elsewhere in .nix config?

In another module config.environment.variables.DOMAIN, in the same module you need to have the attribute set rec to be able to access “inner” values.


What’s the difference between the two?

1 Like

One are set through shells RC files, while the other are set through PAM.

I never remember which is which. Personally I try to avoid both, as I do not like leaking some systems concern into each users environment.

I generally prefer to be much more specific and often wrap individual “packages” to get their own limited view on env-vars.

Silly me - it’s all in the docs:


A set of environment variables used in the global environment. These variables will be set by PAM early in the login process.

Also, these variables are merged into environment.variables