Launching apps as root via dmenu

I wonder if it possible to make dmenu execute certain apps (e.g. Timeshift) as root, maybe there’s a configuration for that or some other similar way? Now I pretty much have to remember to use shortcuts (with NOPASSWD) or the terminal for such apps, which is a bit of a hassle, I’d rather prefer to use dmenu as my main app launcher. Preferably something close to vanilla i3 and (if possible) newbie friendly :slight_smile:

Don’t know about dmenu (is that a requirement?).

But you might want to start with bash aliases (don’t be afraid! It is very simple).

Simply write the aliases you want into file $HOME/.bashrc like this:

  alias update='sudo pacman -Syu'  # updates system with pacman
  alias updateall='yay -Syu'       # updates system with yay
  alias pkgcleanup='paccache -rk1' # save space and clean the package cache

and re-start a terminal. Then you’ll have commands like:

  • update
  • updateall
  • pkgcleanup

and they do exactly what you specified above. And it is very important that you know the command names and their options very well.

As you can see, the commands and their options are written there. To find out more about the commands, use the man command in terminal, e.g.:

  man pacman
  man yay

and so on.

Then, if you want a GUI for launching certain commands, you could use yad. It is quite simple to use, you’ll just write a bash script that calls yad with certain options. Here’s an example script (just modify it to your needs!):

#!/bin/bash
yad --form --title="My launcher" --width=300 \
    --field='Leafpad':fbtn 'leafpad' \
    --field='Firefox':fbtn 'firefox' \
    --field='Libreoffice':fbtn 'libreoffice --writer'

Simply write the above to a file, e.g. $HOME/bin/my-launcher. Then make it executable and put it in your PATH in order to use it later:

  chmod +x $HOME/bin/my-launcher
  export PATH=$PATH:$HOME/bin    # put this line also into $HOME/.bashrc !

Now you can start the launcher in a (new) terminal with command:

  my-launcher

Now, just click the buttons you want!

You may create a desktop icon for the launcher too. But how to do that? It is simple, so I’ll leave it to you as a further exercise. :wink:

3 Likes

In addition to the previous: making GUI programs to run as root may be really tricky and not even recommended because of security reasons.

1 Like

Thank you, @manuel , yad looks interesting I’ll read the man and try to integrate it in my workflow :slight_smile: I have been using pcmanfm as a launcher, well sort of :neutral_face: , but found myself rarely needing it.
dmenu is a dynamic menu for searching for and launching apps, which comes with i3 and that’s what I’m using the most, I’d just like it to work correctly for all apps, scripts, etc., with those requiring root access to function properly as well…

But certain apps will not function correctly (or at all) without root. If I can launch them in XFCE (for example) as root, how’s it different in this case?

X has security issues and Arch recommends not to use GUI programs as root.
Here’s much more info: https://wiki.archlinux.org/index.php/Running_GUI_applications_as_root

Terminal programs can be used with sudo/root permissions.

1 Like

you could configure sudo to not require password or even use opendoas, ayway i wouldn’t recommend to config that way,

Doesn’t timeshift come with some systemd service so you can enable and just let it run? For sure there may have some config for this on the internet.

I did that for the shortcuts (didn’t know it’s as dangerous as Arch Wiki says), but I have no idea how to make it work with dmenu…

Timeshift was just an example of a GUI app that won’t run without root access. But I’ll try to look it up, thanks

1 Like

timeshift config needs to be run as root and if i do call it from menu under i3 it asks for password without any investigation… same like gparted will do…
do you run a polkit daemon ?

╭─joekamprad@empowered64 ~  
╰─$ cat /usr/bin/timeshift-launcher                                                             1 ↵
#!/bin/bash

app_command='timeshift-gtk'

if [ `id -u` -eq 0 ]; then
	#user is admin
	${app_command}
else
	#user is not admin
	if `echo $- | grep "i" >/dev/null 2>&1`; then
		#script is running in interactive mode
		su - -c "${app_command}"
	else
		#script is running in non-interactive mode
		if [ $XDG_SESSION_TYPE = "wayland" ] ; then
			xhost +SI:localuser:root
			pkexec ${app_command}
			xhost -SI:localuser:root
			xhost
		elif command -v pkexec >/dev/null 2>&1; then
			pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY ${app_command}
		elif command -v sudo >/dev/null 2>&1; then
			x-terminal-emulator -e "sudo ${app_command}"
		elif command -v su >/dev/null 2>&1; then
			x-terminal-emulator -e "su - -c '${app_command}'"
		else
			x-terminal-emulator -e "echo 'Command must be run as root user: ${app_command}'"
		fi
	fi
fi

1 Like

I recently got Endeavour i3 :slight_smile: and it does install polkit-gnome and I see polkit.service running, is it that?

Where do I put the script?

this is the timeshift launcher installs with the package :wink:
i do have this from the files installed:

/etc/default/timeshift.json
/usr/bin/timeshift
/usr/bin/timeshift-gtk
/usr/bin/timeshift-launcher
/usr/share/appdata/timeshift.appdata.xml
/usr/share/applications/timeshift-gtk.desktop

if you use rofi menu it should show like this and calling polkit to get this:
Bildschirmfoto_2020-04-19_11-23-43

Oh, I see. The weird thing is that I can’t see timeshift from dmenu at all… Can’t find it typing neither ‘timeshift’ nor ‘System Restore Utility’ (it’s GenericName in timeshift-gtk.desktop)

edit: just tried rofi for the first time :smiley: I can see it in rofi drun. Probably will have to use it for now.

Keywords=backup;
is not inside desktop file…
is gparted shown inside dmenu?

Nope

i get it shown here on EndeavourOS i3 install:
Bildschirmfoto_2020-04-19_11-34-51

Probably something on my side then…
2020-04-19-123919_1366x768_scrot

edit: it does appear if I type ‘GP’ in capital letters! I didn’t know that it’s case-sensitive
2020-04-19-124332_1366x768_scrot

edit2: and Timeshift is there too. Still, can’t seem to launch it though. Probably because I just got it (i3EOS) from the repo, not installed normally via Calamares.

so you only take the configs and do not install from calamares?
may you need to check packages installed
wget https://raw.githubusercontent.com/endeavouros-team/i3-EndeavourOS/master/packages-repository.txt
sudo pacman -S --needed - < packages-repository.txt

Yes, I should have all of those (except terminator and pcmanfm, as I don’t use them). I’ve followed the installation instructions here. Been tweaking it here and there a little bit, so I am not surprised some things may not work a intended, it’s okay. I really appreciate all the work that has been done on i3wm, learning a lot from it.