Making Emacs tramp work with doas under Gentoo Linux

posted on 2023-05-07

Recently I have switched to doas (portable fork of the OpenBSD doas command, a simpler alternative to sudo) and on my Gentoo workstation Emacs tramp didn’t like the switch. It went into a infinite loop as it couldn’t recognize the shell’s prompt. The difference is how bash shell spawned by sudo was behaving in comparison to when spawned by doas (by default tramp spawns the shell using doas -u user -s) Fiddling with tramp-shell-prompt-pattern didn’t helped me much so as a workaround I just changed how the shell is spawned by making doas execute su instead of the shell directly.

Snippet below when put in init.el will make tramp to work with doas:

;; you may want to skip the "sudo" if you don't use doas on
;; remote machines as well.
(dolist (method '("doas" "sudo"))
  (add-to-list 'tramp-methods
     `(,method
       (tramp-login-program        "doas")
       (tramp-login-args           (("-u" "%u") ("su - %u")))
       (tramp-remote-shell         ,tramp-default-remote-shell)
       (tramp-remote-shell-args    ("-c"))
       (tramp-connection-timeout   10)
       (tramp-session-timeout      300)
       (tramp-password-previous-hop t))))

More of a workaround than a solution, but it works reliably. Happy hacking!