Skip to content

Instantly share code, notes, and snippets.

@FrozenCow FrozenCow/example.sh
Last active Dec 23, 2015

Embed
What would you like to do?
Systemd env.conf generation using profile.d
sudo mkdir -p /etc/systemd/system/user@${UID}.service.d/
./generate-systemd-envconf-from-profile.sh | sudo tee /etc/systemd/system/user@${UID}.service.d/env.conf
#!/bin/sh
if [ -z "$EXPORTING" ]
then
# This hack is used to clear the environment to bare minimum.
env -i EXPORTING=1 HOME=$HOME $0
exit
fi
BEFORE=$(mktemp)
AFTER=$(mktemp)
# Store the environment variables before we load profile
env > $BEFORE
export DISPLAY=:0
source /etc/profile
source $HOME/.profile
# Store the environment variables after we have loaded profile
env > $AFTER
#echo .include /usr/lib/systemd/system/user@.service
echo [Service]
# Use the difference of the 2 stored environment variables
# Extract the added (or modified) lines: the ones that are only in $AFTER
# Put it in the Systemd-form: Environment="NAME=VALUE"
diff $BEFORE $AFTER | grep '^>' | sed 's/^> \(.*\)=\(.*\)$/Environment="\1=\2"/g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.