Environment variables: Difference between revisions

imported>Yuu
No edit summary
Pigs (talk | contribs)
m Add categories nixos and configuration
 
(15 intermediate revisions by 9 users not shown)
Line 1: Line 1:
== Configuration ==
== Configuration of shell environment on NixOS ==


Environment variables can be set with <code>environment.variables</code>, <code>environment.sessionVariables</code>, and <code>environment.profileRelativeSessionVariables</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>:
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">
{
{
# ...
  # ...
 
 
  # This is using a rec (recursive) expression to set and access XDG_BIN_HOME within the expression
  # For more on rec expressions see https://nix.dev/tutorials/first-steps/nix-language#recursive-attribute-set-rec
   environment.sessionVariables = rec {
   environment.sessionVariables = rec {
     XDG_CACHE_HOME  = "\${HOME}/.cache";
     XDG_CACHE_HOME  = "$HOME/.cache";
     XDG_CONFIG_HOME = "\${HOME}/.config";
     XDG_CONFIG_HOME = "$HOME/.config";
     XDG_BIN_HOME    = "\${HOME}/.local/bin";
     XDG_DATA_HOME  = "$HOME/.local/share";
     XDG_DATA_HOME  = "\${HOME}/.local/share";
     XDG_STATE_HOME  = "$HOME/.local/state";


    # Not officially in the specification
    XDG_BIN_HOME    = "$HOME/.local/bin";
     PATH = [  
     PATH = [  
       "\${XDG_BIN_HOME}"
       "${XDG_BIN_HOME}"
     ];
     ];
   };
   };
Line 24: Line 33:
== Troubleshooting ==
== Troubleshooting ==


=== sudo[3424]: pam_env(sudo:session): Expandable variables must be wrapped in {} <$VARIABLE/path/to> - ignoring ===
=== pam_env(sudo:session): Expandable variables must be wrapped in {} <$VARIABLE/path/to> ===


Error logs may be found with <code>journalctl -xb -p3</code> regarding the no presence of curly braces <code>{}</code> for variable expansion.
Error logs may be found with <code>journalctl -xb -p3</code> regarding the no presence of curly braces <code>{}</code> for variable expansion.
Line 47: Line 56:
==== Solution or workaround ====
==== Solution or workaround ====
Unknown.
Unknown.
== Using variables from a Nix expression ==
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,
Nix will set some environment variables, for example:
<pre>
NIX_BINTOOLS=/nix/store/lvg99f3zni6zw4cvlci6wpmzlls0nsn4-binutils-wrapper-2.38
NIX_BINTOOLS_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu=1
NIX_BUILD_CORES=3
NIX_BUILD_TOP=/build
NIX_CC=/nix/store/61zfi5pmhb0d91422f186x26v7b52y5k-gcc-wrapper-11.3.0
NIX_CC_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu=1
NIX_CFLAGS_COMPILE= -frandom-seed=8cnrgjjflj
NIX_ENFORCE_NO_NATIVE=1
NIX_ENFORCE_PURITY=1
NIX_HARDENING_ENABLE=fortify stackprotector pic strictoverflow format relro bindnow
NIX_INDENT_MAKE=1
NIX_LDFLAGS=-rpath /nix/store/8cnrgjjflj3dyppz299w50l9yydgnqkp-x/lib64 -rpath /nix/store/8cnrgjjflj3dyppz299w50l9yydgnqkp-x/lib
NIX_LOG_FD=2
NIX_SSL_CERT_FILE=/no-cert-file.crt
NIX_STORE=/nix/store
</pre>
[[Category:NixOS]]
[[Category:Configuration]]