Skip to content

Instantly share code, notes, and snippets.

@FrozenCow
Last active December 23, 2015 15:19
Show Gist options
  • Save FrozenCow/6654245 to your computer and use it in GitHub Desktop.
Save FrozenCow/6654245 to your computer and use it in GitHub Desktop.
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/[email protected]
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