From 0876f102648b5b8fb273b34f7bce9b24b25417fc Mon Sep 17 00:00:00 2001 From: Randommisha13 <52196819+Randommisha13@users.noreply.github.com> Date: Thu, 26 Nov 2026 20:20:17 +0500 Subject: [PATCH] feat(systemd): support other privilege elevation tools (#13695) --- plugins/systemd/README.md | 24 +++++++++++++++++++++++- plugins/systemd/systemd.plugin.zsh | 21 ++++++++++++++++----- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/plugins/systemd/README.md b/plugins/systemd/README.md index 1980ae373..7566e5cf0 100644 --- a/plugins/systemd/README.md +++ b/plugins/systemd/README.md @@ -24,7 +24,7 @@ plugins=(... systemd) | `sc-show-environment` | `systemctl show-environment` | Dump the systemd manager environment block | | `sc-cat` | `systemctl cat` | Show backing files of one or more units | | `sc-list-timers` | `systemctl list-timers` | List timer units currently in memory | -| **Aliases with sudo** ||| +| **Privileged aliases** ||| | `sc-start` | `sudo systemctl start` | Start Unit(s) | | `sc-stop` | `sudo systemctl stop` | Stop Unit(s) | | `sc-reload` | `sudo systemctl reload` | Reload Unit(s) | @@ -50,6 +50,28 @@ plugins=(... systemd) | `sc-disable-now` | `sudo systemctl disable --now` | Disable and stop unit(s) | | `sc-mask-now` | `sudo systemctl mask --now` | Mask and stop unit(s) | +### Privileged aliases + +systemd plugin supports alternative privilege elevation tools. You can prepend an arbitrary string, +replacing `sudo`, to the privileged aliases by setting `$ZSH_SYSTEMD_SUDO` variable. You can leave +authorization to systemd by setting `$ZSH_SYSTEMD_SUDO` to an empty string. + +To use `doas` as your elevation tool you will need to add the following line to your zshrc; + +```zsh +ZSH_SYSTEMD_SUDO="doas" +``` + +After shell restart, you will see that alias for `sc-enable` is changed: + +```sh +$ which sc-enable +sc-enable: aliased to doas systemctl enable +``` + +These aliases are evaluated during oh-my-zsh startup, and can't be changed interactively (you will +need to restart shell to see changes). + ### User aliases You can use the above aliases as `--user` by using the prefix `scu` instead of `sc`. diff --git a/plugins/systemd/systemd.plugin.zsh b/plugins/systemd/systemd.plugin.zsh index 842174df0..59469e75e 100644 --- a/plugins/systemd/systemd.plugin.zsh +++ b/plugins/systemd/systemd.plugin.zsh @@ -67,15 +67,27 @@ power_commands=( suspend ) +if ! [[ -v ZSH_SYSTEMD_SUDO ]]; then + ZSH_SYSTEMD_SUDO="sudo" +fi + 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 systemctl $c" - alias "scu-$c"="systemctl --user $c" -done +# Avoids leading space in alias when using polkit +if [[ -n "${ZSH_SYSTEMD_SUDO}" ]]; then + for c in $sudo_commands; do + alias "sc-$c"="$ZSH_SYSTEMD_SUDO systemctl $c" + alias "scu-$c"="systemctl --user $c" + done +else + for c in $sudo_commands; do + alias "sc-$c"="systemctl $c" + alias "scu-$c"="systemctl --user $c" + done +fi for c in $power_commands; do alias "sc-$c"="systemctl $c" @@ -83,7 +95,6 @@ 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"