Skip to content

Instantly share code, notes, and snippets.

@LunarLambda
Last active August 28, 2024 12:40
Show Gist options
  • Save LunarLambda/9f1bc21493c443b865865474874ad19c to your computer and use it in GitHub Desktop.
Save LunarLambda/9f1bc21493c443b865865474874ad19c to your computer and use it in GitHub Desktop.
I wrote a ZSH oneliner that generates a drop-in configuration for user@.service for setting the XDG base directory environment variables.
#!/usr/bin/zsh -f
print -- "Setting XDG_DATA_HOME to \"foo bar\" for demonstration purposes"
XDG_DATA_HOME="foo bar"
print -l -- "Base expansion" \
"\$(typeset -m 'XDG_*_HOME')" "" \
$(typeset -m 'XDG_*_HOME') ""
print -l -- "Add newline splitting and sorting flags: (fo)" \
"\${(fo)\"\$(typeset -m 'XDG_*_HOME')\"}" "" \
${(fo)"$(typeset -m 'XDG_*_HOME')"} ""
print -l -- "Prefix Environment=" \
"Environment=\${(fo)\"\$(typeset -m 'XDG_*_HOME')\"}" "" \
Environment=${(fo)"$(typeset -m 'XDG_*_HOME')"} ""
print -l -- "Add RC_EXPAND_PARAM flag: ^" \
"Environment=\${(fo)^\"\$(typeset -m 'XDG_*_HOME')\"}" "" \
Environment=${(fo)^"$(typeset -m 'XDG_*_HOME')"} ""
print -l -- "Fix quoting to work with systemd's configuration format:" \
"Environment=\${(q-)^\${(foQ)\"\$(typeset -m 'XDG_*_HOME')\"}}" "" \
"[Service]" \
Environment=${(q-)^${(foQ)"$(typeset -m 'XDG_*_HOME')"}} ""
print -l -- "Single line version:" \
"Environment=\${(j: :)\${(q-)\${(foQ)\"\$(typeset -m 'XDG_*_HOME')\"}}}" "" \
"[Service]" \
Environment=${(j: :)${(q-)${(foQ)"$(typeset -m 'XDG_*_HOME')"}}} ""
Setting XDG_DATA_HOME to "foo bar" for demonstration purposes
Base expansion
$(typeset -m 'XDG_*_HOME')
XDG_DATA_HOME='foo
bar'
XDG_CACHE_HOME=/home/luna/.cache
XDG_CONFIG_HOME=/home/luna/cfg
XDG_STATE_HOME=/home/luna/.local/state
Add newline splitting and sorting flags: (fo)
${(fo)"$(typeset -m 'XDG_*_HOME')"}
XDG_CACHE_HOME=/home/luna/.cache
XDG_CONFIG_HOME=/home/luna/cfg
XDG_DATA_HOME='foo bar'
XDG_STATE_HOME=/home/luna/.local/state
Prefix Environment=
Environment=${(fo)"$(typeset -m 'XDG_*_HOME')"}
Environment=XDG_CACHE_HOME=/home/luna/.cache
XDG_CONFIG_HOME=/home/luna/cfg
XDG_DATA_HOME='foo bar'
XDG_STATE_HOME=/home/luna/.local/state
Add RC_EXPAND_PARAM flag: ^
Environment=${(fo)^"$(typeset -m 'XDG_*_HOME')"}
Environment=XDG_CACHE_HOME=/home/luna/.cache
Environment=XDG_CONFIG_HOME=/home/luna/cfg
Environment=XDG_DATA_HOME='foo bar'
Environment=XDG_STATE_HOME=/home/luna/.local/state
Fix quoting to work with systemd's configuration format:
Environment=${(q-)^${(foQ)"$(typeset -m 'XDG_*_HOME')"}}
[Service]
Environment=XDG_CACHE_HOME=/home/luna/.cache
Environment=XDG_CONFIG_HOME=/home/luna/cfg
Environment='XDG_DATA_HOME=foo bar'
Environment=XDG_STATE_HOME=/home/luna/.local/state
Single line version:
Environment=${(j: :)${(q-)${(foQ)"$(typeset -m 'XDG_*_HOME')"}}}
[Service]
Environment=XDG_CACHE_HOME=/home/luna/.cache XDG_CONFIG_HOME=/home/luna/cfg 'XDG_DATA_HOME=foo bar' XDG_STATE_HOME=/home/luna/.local/state
$ print -C1 -- '[Service]' Environment=${(j: :)${(q-)${(foQ)"$(typeset -m 'XDG_*_HOME')"}}} | sudo tee /etc/systemd/system/user@$UID.service.d/xdg.conf
[Service]
Environment=XDG_CACHE_HOME=/home/luna/.cache XDG_CONFIG_HOME=/home/luna/cfg XDG_DATA_HOME=/home/luna/.local/share XDG_STATE_HOME=/home/luna/.local/state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment