# systemd environment directives

Date: 2021-08-07 19:58:29 +08:00Date modified: 2022-04-16 20:19:15 +08:00

systemd enables setting the environment through environment directives. For some, this is a nice shell-agnostic way of setting environment variables and could replace setting through shell profiles (i.e., `.bashrc`, `.profile`).

It needs a `*.conf` file in one of the load paths (seen from the `environment.d.5` manual page). Just like how systemd at user-level, you can set it at user-level by placing them in certain user-level load paths (e.g., `$XDG_CONFIG_HOME/environment.d`).

Keep in mind it does not use the shell directly and instead makes use of shell-like syntax. The syntax takes variable substitutions and parameter expansion seen from GNU Bash.

The following code block is an example of setting Nix-related environment variables to enable desktop integrations.

    # Enable desktop integration with Nix-installed applications.
    NIX_PATH=$HOME/.nix-defexpr/channels${NIX_PATH:+:}$NIX_PATH
    PATH=${PATH:+$PATH:}$HOME/.nix-profile/bin
    XDG_DATA_DIRS=${XDG_DATA_DIRS:+$XDG_DATA_DIRS:}$HOME/.nix-profile/share/

## Backlinks

- systemd