Environment variables: Difference between revisions
imported>SuperSamus `\${HOME}` has some issues that `$HOME` doesn't have (e.g. logging in a Fish shell via TTY) |
Added a way to fix "pam_env(sudo:session): Expandable variables must be wrapped in {}" Tags: Mobile edit Mobile web edit |
||
| (12 intermediate revisions by 8 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> | 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_DATA_HOME = "$HOME/.local/share"; | |||
XDG_DATA_HOME = "$HOME.local/share"; | |||
XDG_STATE_HOME = "$HOME/.local/state"; | 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 47: | Line 55: | ||
==== Solution or workaround ==== | ==== Solution or workaround ==== | ||
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> | |||
== nix-build sandbox == | |||
== 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, | Compared to a normal shell environment, in a nix-build sandbox, | ||
| Line 71: | Line 107: | ||
NIX_STORE=/nix/store | NIX_STORE=/nix/store | ||
</pre> | </pre> | ||
[[Category:NixOS]] | |||
[[Category:Configuration]] | |||