ohmyzsh/plugins/systemd/systemd.plugin.zsh
iyzana c4d215e1dc feat(systemd): preserve editor env for sudo commands
When running `systemctl` commands that require sudo, the users'
configured editor environment variables were not preserved. This
resulted in systemctl opening a default editor.

By instructing sudo to preserve the common environment variables for
configuring an editor, systemctl can see these variables and open the
users' preferred editor.

This is mostly relevant for the `sc-edit` alias.
2025-09-05 00:37:29 +02:00

123 lines
2.1 KiB
Bash

# systemctl aliases
user_commands=(
cat
get-default
help
is-active
is-enabled
is-failed
is-system-running
list-dependencies
list-jobs
list-sockets
list-timers
list-unit-files
list-units
show
show-environment
status
)
sudo_commands=(
add-requires
add-wants
cancel
daemon-reexec
daemon-reload
default
disable
edit
emergency
enable
halt
import-environment
isolate
kexec
kill
link
list-machines
load
mask
preset
preset-all
reenable
reload
reload-or-restart
reset-failed
rescue
restart
revert
set-default
set-environment
set-property
start
stop
switch-root
try-reload-or-restart
try-restart
unmask
unset-environment
)
power_commands=(
hibernate
hybrid-sleep
poweroff
reboot
suspend
)
readonly SUDO_PRESERVE_ENV="SYSTEMD_EDITOR,SUDO_EDITOR,VISUAL,EDITOR"
for c in $user_commands; do
alias "sc-$c"="systemctl $c"
alias "scu-$c"="systemctl --user $c"
done
for c in $sudo_commands; do
alias "sc-$c"="sudo --preserve-env=$SUDO_PRESERVE_ENV systemctl $c"
alias "scu-$c"="systemctl --user $c"
done
for c in $power_commands; do
alias "sc-$c"="systemctl $c"
done
unset c user_commands sudo_commands power_commands
# --now commands
alias sc-enable-now="sc-enable --now"
alias sc-disable-now="sc-disable --now"
alias sc-mask-now="sc-mask --now"
alias scu-enable-now="scu-enable --now"
alias scu-disable-now="scu-disable --now"
alias scu-mask-now="scu-mask --now"
# --failed commands
alias scu-failed='systemctl --user --failed'
alias sc-failed='systemctl --failed'
function systemd_prompt_info {
local unit
for unit in "$@"; do
echo -n "$ZSH_THEME_SYSTEMD_PROMPT_PREFIX"
if [[ -n "$ZSH_THEME_SYSTEMD_PROMPT_CAPS" ]]; then
echo -n "${(U)unit:gs/%/%%}:"
else
echo -n "${unit:gs/%/%%}:"
fi
if systemctl is-active "$unit" &>/dev/null; then
echo -n "$ZSH_THEME_SYSTEMD_PROMPT_ACTIVE"
elif systemctl --user is-active "$unit" &>/dev/null; then
echo -n "$ZSH_THEME_SYSTEMD_PROMPT_ACTIVE"
else
echo -n "$ZSH_THEME_SYSTEMD_PROMPT_NOTACTIVE"
fi
echo -n "$ZSH_THEME_SYSTEMD_PROMPT_SUFFIX"
done
}