0

I'm using systemd to start the Emacs daemon, as explained in the Emacs wiki.

I also use my ~/.profile file to add my user-installed TeX Live installation to the PATH variable, so it contains the line

PATH="$HOME/.local/texlive/2023/bin/x86_64-linux:$PATH"

Apparently, the emacs daemon is started before that line is executed. [M-x] getenv [RET] PATH reveals that the texlive directory is not part of the PATH emacs is aware of.

Note that

systemctl --user show-environment

gives the correct path, so that executing

systemctl restart --user emacs

resolves the situation for the current session.

Is there a way to make systemd wait for the .profile file to be executed before starting a user service?

5
  • Back up - your problem is NOT getting systemd to deal with .profile, its that the PATH is not setup for systemd (which amongst other things isolates the environment of the programs it tuns from its own environment) - see unix.stackexchange.com/questions/347873/…
    – symcbean
    Apr 5, 2023 at 8:59
  • 3
    Does this answer your question? Set PATH for a systemd unit
    – symcbean
    Apr 5, 2023 at 8:59
  • @symcbean Not really. I dislike the idea of having to set the environment variable separately for every systemd unit that might want to use it. Ideally, there is a single config file that sets my user PATH for everything. I always thought .profile is that place. Apr 5, 2023 at 9:41
  • Then use a different service manager than systemd (although the ones I'm familiar with are all the same in this regard).
    – symcbean
    Apr 5, 2023 at 9:49
  • Maybe some variant of unix.stackexchange.com/questions/728058/… will help?
    – imabug
    May 17, 2023 at 12:56

2 Answers 2

3

The standard way to set the environment for user services is described in man environment.d. Typically, you would create a file ~/.config/environment.d/90texlive.conf, for example, holding the line

PATH="$HOME/.local/texlive/2023/bin/x86_64-linux:$PATH"

This file is parsed by systemd, so beware the syntax is limited, but it can usually be made compatible with the shell, so you could source it from your .profile if you do not want to repeat the line there. Remember to do a systemctl --user daemon-reload when testing.

Note that systemd initialises the PATH to something that may not be as extensive as what your shell normally gets, so you may want to have other config lines or files too.

How does your .profile PATH eventually find its way into systemd's environment? Possibly some part of the desktop startup does something like

systemctl --user import-environment

or

dbus-update-activation-environment --systemd --all 
3
  • 1
    You can't really just source an environment.d conf because you can't use export in it, while you need that keyword in shell. A read loop should do though.
    – Tom Yan
    Apr 5, 2023 at 13:59
  • 1
    Yeah that works. I had to use 991-path.conf though, because for some reason, /etc/environment is linked to /usr/lib/environment.d/99-environment.conf, so that my own config got overwritten when first trying this. Apr 5, 2023 at 14:17
  • 1
    @TomYan you are right. Some variables like PATH are usually already marked for export anyway, and just giving them a new value through source will work in those particular circumstances.
    – meuh
    Apr 5, 2023 at 15:25
1

Waiting for .profile to be executed is not going to help in your problem, because the user services are not child processes of the shell that executes ~/.profile, and so cannot inherit any environment variable settings from it.

If you want a customized $PATH for your emacs.service, you will have to do it in the service file (or an override file), probably with an Environment= option in the [Service] section. Note that environment variable expansion will not be performed for the Environment= option, so you won't be able to use $HOME or $PATH, but you can use %h to refer to the user's home directory.

So, the Environment= line you'd need might be something like this:

Environment="PATH=%h/.local/texlive/2023/bin/x86_64-linux:%h/bin:/usr/local/bin:/usr/bin:/bin"
3
  • 1
    Your first sentence is not entirely correct: If I restart the service from my command line, it does indeed inherit the environment variables set in .profile. Interestingly, the same happens when I log out and log in again. Apr 5, 2023 at 9:18
  • @red_trumpet "If I restart the service from my command line" how do you do that restarting, specifically? Apr 5, 2023 at 9:22
  • @MarcusMüller As written in my post, using systemctl restart --user emacs. Also, systemctl --user show-environment seems to know the correct PATH. Apr 5, 2023 at 9:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .