Environment variables: Difference between revisions

fix outdated link
Jonboh (talk | contribs)
Added a way to fix "pam_env(sudo:session): Expandable variables must be wrapped in {}"
Tags: Mobile edit Mobile web edit
 
(2 intermediate revisions by 2 users not shown)
Line 2: Line 2:


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 ] .   
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).  
<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>:
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>:
Line 53: 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 ==
== Using variables from a Nix expression ==
Line 81: Line 107:
NIX_STORE=/nix/store
NIX_STORE=/nix/store
</pre>
</pre>
[[Category:NixOS]]
[[Category:Configuration]]