node.jsenvironment-variablessystemdpnpm

How to add user's PATH to systemd service without hardcoding it?


I have a systemd service like so:

[Unit]
Description=description here
After=database.service

[Service]
Type=simple
User=user
WorkingDirectory=/home/user/project
ExecStart=/home/user/.local/share/pnpm/pnpm start
LoadCredentialEncrypted=a:/home/user/project/.credstore/a.cred
LoadCredentialEncrypted=b:/home/user/project/.credstore/b.cred
LoadCredentialEncrypted=c:/home/user/project/.credstore/c.cred
LoadCredentialEncrypted=d:/home/user/project/.credstore/d.cred

[Install]
WantedBy=multi-user.target

And when I try to start it, I get this error:

> project@0.1.0 start /home/user/project
> node .
sh: 1: node: not found
ELIFECYCLE  Command failed.

Which makes sense, the PATH env var is incomplete. I can printenv and copy the correct PATH env var into my service like so:

Environment=PATH=paste here

And that does work without any issues. But this means I now need to edit my service whenever the PATH env var changes, and I don't want that. Is there no better way to obtain the PATH env var from the user and attach it to the service?


Solution

  • Reading .bashrc at service start time:

    ExecStart=/bin/bash -c ". ~/.bashrc ; exec /home/user/.local/share/pnpm/pnpm start"