Environment variables: Difference between revisions

imported>Symys
m Added clarification on the use of environment.variables vs other options.
Jonboh (talk | contribs)
Added a way to fix "pam_env(sudo:session): Expandable variables must be wrapped in {}"
Tags: Mobile edit Mobile web edit
 
(8 intermediate revisions by 6 users not shown)
Line 1: Line 1:
== Configuration ==
== Configuration of shell environment on NixOS ==


Environment variables can be set with [https://search.nixos.org/options?channel=unstable&show=environment.variables&from=0&size=50&sort=relevance&type=packages&query=environment.variables <code>environment.variables</code> ], [ https://search.nixos.org/options?channel=23.11&show=environment.sessionVariables&from=0&size=50&sort=relevance&type=packages&query=environment.sessionVariables <code>environment.sessionVariables</code> ], and [ https://search.nixos.org/options?channel=unstable&show=environment.profileRelativeSessionVariables&from=0&size=50&sort=relevance&type=packages&query=environment.profileRelativeSessionVariables <code>environment.profileRelativeSessionVariables</code>] . environment.variables are global variables set on shell initialization, whereas environment.sessionVariables and environment.profileRelativeSessionVariables are initialized through PAM. For example, for the [https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables XDG Base Directory Specification], the following could be set to <code>/etc/nixos/configuration.nix</code>:
Environment variables can be set with [https://search.nixos.org/options?channel=unstable&show=environment.variables&from=0&size=50&sort=relevance&type=packages&query=environment.variables environment.variables ], [https://search.nixos.org/options?channel=unstable&show=environment.sessionVariables&from=0&size=50&sort=relevance&type=packages&query=environment.sessionVariables environment.sessionVariables ], and [https://search.nixos.org/options?channel=unstable&show=environment.profileRelativeSessionVariables&from=0&size=50&sort=relevance&type=packages&query=environment.profileRelativeSessionVariables environment.profileRelativeSessionVariables ] . 
<code>environment.variables</code> are global variables set on shell initialization, whereas <code>environment.sessionVariables</code> and <code>environment.profileRelativeSessionVariables</code> are initialized through PAM (Pluggable Authentication Module).
 
Session variable sets are merged into their environment variable set counterparts. For example, <code>environment.sessionVariables</code> is merged to <code>environment.variables</code> so you can just reload your shell to reload changed variables [https://github.com/NixOS/nixpkgs/blob/5e4fbfb6b3de1aa2872b76d49fafc942626e2add/nixos/modules/config/shells-environment.nix#L166-L170].
 
For example, for the [https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables XDG Base Directory Specification], the following could be set to <code>/etc/nixos/configuration.nix</code>:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 50: Line 55:


==== Solution or workaround ====
==== Solution or workaround ====
Unknown.
Using Nix string literals can fix the problem by keeping the curly brackets after the nix evaluation.
 
<syntaxhighlight lang="nix">
Value:
{
  ...
  VARIABLE = ''''${VARIABLE}/path/to'';
  ...
}
</syntaxhighlight>
 
This will result in VARIABLE having the value <code>${VARIABLE}/path/to</code>
 
For example:
 
<syntaxhighlight lang="nix">
{
  environment.sessionVariables = rec {
    XDG_CACHE_HOME  = ''''${HOME}/.cache'';
    # ...
  };
# ...
}
</syntaxhighlight>
 
 
== Using variables from a Nix expression ==


== nix-build sandbox ==
The <code>builtins.getEnv</code> function allows for reading the environment of the Nix command which triggered the expression to be evaluated, typically <code>nix-build</code>.
 
== Variables exposed in nix-build sandbox ==


Compared to a normal shell environment, in a nix-build sandbox,
Compared to a normal shell environment, in a nix-build sandbox,
Line 74: Line 107:
NIX_STORE=/nix/store
NIX_STORE=/nix/store
</pre>
</pre>
[[Category:NixOS]]
[[Category:Configuration]]