63 lines
2.1 KiB
Markdown
63 lines
2.1 KiB
Markdown
---
|
|
title: Environment Variables
|
|
tags: article software linux system configuration
|
|
created: 2024-03-07T10:09:08Z
|
|
published: true
|
|
---
|
|
|
|
Recently, I had to configure some user specific environment variables for my Linux installation.
|
|
The problem was that I needed those variables to work in all of my shells and in every desktop environment.
|
|
Since `.pam_environment` is deprecated and `.profile` isn't sourced by every shell, I needed another solution that works globally.
|
|
|
|
## User variables
|
|
|
|
One solution is to use `systemd` for this task.
|
|
You can configure user environment variables in the `~/.config/environment.d` directory.
|
|
Multiple files can be added and all of them will be processed on session start:
|
|
|
|
```
|
|
.
|
|
├── 10-xdg.conf
|
|
├── 20-dirs.conf
|
|
├── 30-general.conf
|
|
└── 40-apps.conf
|
|
```
|
|
|
|
The format inside these files is very simple:
|
|
|
|
```ini
|
|
KEY=value
|
|
KEY=value
|
|
KEY=value
|
|
```
|
|
|
|
They support variable expansion so it's possible to use things like `$HOME` in the value text.
|
|
You can take a look at my [environment.d](https://git.urbach.dev/sys/home/src/branch/main/.config/environment.d) if you need some examples.
|
|
|
|
GDM and KDE Plasma will automatically source these variables on session start.
|
|
If you don't use these or you don't want to rely on them, you need to manually load the variables in your shell config files.
|
|
|
|
### bash / zsh
|
|
|
|
```bash
|
|
export $(/usr/lib/systemd/user-environment-generators/30-systemd-environment-d-generator)
|
|
```
|
|
|
|
### fish
|
|
|
|
```bash
|
|
export (/usr/lib/systemd/user-environment-generators/30-systemd-environment-d-generator)
|
|
```
|
|
|
|
Even if you use a graphical environment it makes sense to add these to your shell startup because it allows you to see the changes made to your environment immediately. In case you're worried about execution time, it adds about 3 ms to the shell startup on my machine and is well worth the price.
|
|
|
|
## System variables
|
|
|
|
Hardware and system specific variables that do not need to live in a version controlled repository can be set in `/etc/environment`. For example, on a system with an Nvidia GPU:
|
|
|
|
```ini
|
|
GBM_BACKEND=nvidia-drm
|
|
LIBVA_DRIVER_NAME=nvidia
|
|
__GLX_VENDOR_LIBRARY_NAME=nvidia
|
|
```
|