Issue with setting environment variables in Org-mode shell blocks via TRAMP

Bartosz Kaczyński <[email protected]> Sat, 28 Dec 2024 10:47:23 +0000
Newsgroups gmane.emacs.tramp
Message-ID <[email protected]>
Hello TRAMP users,

I hope this message finds you well. I am encountering an issue when 
trying to execute shell commands via TRAMP in Org-mode blocks, where the 
environment variables (such as XDG_RUNTIME_DIR) are not being loaded as 
expected.

I am using TRAMP to connect to a remote system over SSH and execute 
shell commands with sudo. However, the shell environment on the remote 
system does not load certain environment variables (specifically those 
defined in ~/.bashrc), which causes commands like systemctl --user 
daemon-reload to fail.

My ~/.bashrc file in the serviceuser homedir:

export XDG_RUNTIME_DIR=/run/user/$(id -u)

Here is the Org-mode block I am trying to execute:

#+begin_src shell :dir /ssh:user@remotesrv|sudo:serviceuser@remotesrv:
systemctl --user daemon-reload
#+end_src

To work around this, I have tried forcing the remote shell to be 
interactive by modifying the TRAMP settings:

(custom-set-variables
  '(tramp-encoding-shell "/bin/bash")

(add-to-list 'tramp-connection-properties
              (list ".*"
                    "remote-shell" "/bin/bash"
                    "remote-shell-args" '("-i" "-c")))

I also tried sourcing ~/.bashrc explicitly in the Org-mode shell block, 
which works, but I'd prefer to have the environment variables set 
automatically without this extra step.

I’m wondering if this issue is related to the way TRAMP handles shell 
sessions or if it might be specific to how Org-mode executes code 
blocks. Do you have any advice on how I might ensure that environment 
variables like XDG_RUNTIME_DIR are automatically loaded when executing 
commands in Org-mode shell blocks via TRAMP?

Any guidance or suggestions would be greatly appreciated.

Thank you for your time!

bartk