You are not logged in.

#1 2022-08-05 19:55:55

Condor
Member
Registered: 2017-12-01
Posts: 54

Environment variables: Replacing ~/.pam_environment

Hello all,

after reading about the intended deprecation of ~/.pam_environment, I am looking for an equally comfortable solution.

The intention is to have one central place to define user environment variables, which are exported regardless whether I log in through a TTY or graphical session manager. ~/.pam_environment covered that use case nicely, without having to rely on different script-like solutions such as ~/.profile or ~/.bash_profile. To make matters more complicated, I use the fish shell which does not directly parse any profile files. (I am aware of the workaround by adding to ~/.config/fish/config.fish, but that seems like an ugly hack.)

The discussion concerning the pull request to abandon ~/.pam_environment mentions “systemd based user sessions”. I am hitting dead ends trawling the web for an explanation how that might solve my issue, actually also how that entire concept works. Perhaps my search terms are not suitable.

What I understand is that files in ~.config/environment.d/*.conf are pulled in for systemd user units to provide environment variables. They show up with systemctl --user show-environment, thus are processed. But they are neither available after logging in through a TTY nor graphical session manager.

Can someone point me in the right direction? How do I replace ~/.pam_environment with a central solution to provide environment variables?

Offline

#2 2022-10-24 08:13:41

Felixoid
Member
Registered: 2012-05-23
Posts: 22

Re: Environment variables: Replacing ~/.pam_environment

Hello, after the recent boot I've ended up with the same question.

Sure, I can just add the same vars to shell-specific places, but it looks rather like an ugly hack than a workaround

Offline

#3 2022-10-24 08:25:40

Living
Member
Registered: 2022-10-24
Posts: 1

Re: Environment variables: Replacing ~/.pam_environment

I have a very similar setup like Condor. My work-around is to define all my variables in ~/.config/environment.d/*.conf and then load them in my ~/.bashrc with these lines:

# Load environment variables from the systemd files. This way I don’t need to
# declare them in different places again.
set -a # All variables declared will also be exported.
for f in $(find ${XDG_CONFIG_HOME:-~/.config}/environment.d -name "*.conf"); do
  source "$f"
done
set +a

# ... some other things ...
exec fish

Offline

#4 2022-10-25 01:25:46

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,134

Re: Environment variables: Replacing ~/.pam_environment

man environment.d
man systemd-environment-d-generator
man systemd.environment-generator

Apparently terminal emulators have to run a systemd service which then runs the user's shell ...?

But, hey, it will apparently work in gnome.


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#5 2022-10-25 06:59:04

seth
Member
Registered: 2012-09-03
Posts: 51,498

Re: Environment variables: Replacing ~/.pam_environment

https://wiki.archlinux.org/title/System … _variables

At any time, use systemctl --user set-environment or systemctl --user import-environment. Affects all user units started after setting the environment variables, but not the units that were already running.
Using the dbus-update-activation-environment --systemd --all command provided by dbus. Has the same effect as systemctl --user import-environment, but also affects the D-Bus session. You can add this to the end of your shell initialization file.

cat /etc/X11/xinit/xinitrc.d/50-systemd-user.sh

and see the last link below.

Offline

#6 2022-10-25 10:41:35

Felixoid
Member
Registered: 2012-05-23
Posts: 22

Re: Environment variables: Replacing ~/.pam_environment

Thank you @seth and @cfr

It looks like DMs imply the systemd variables under the hood, so this works fine for me https://github.com/Felixoid/dotfiles/co … 6f4e23cfd0

Unfortunately, it doesn't work in non-graphic sessions, so I guess, I'll add something to import ENVs from systemd into shell.

upd
it looks like it just works

set -a
eval $(/lib/systemd/user-environment-generators/30-systemd-environment-d-generator)
set +a

Last edited by Felixoid (2022-10-25 10:56:39)

Offline

Board footer

Powered by FluxBB