Subject: bug#77220: [shepherd]: Timers do not set HOME environment variable correctly

From: Tomas Volf <[email-masked]>
Date: Wed, 26 Mar 2025 23:45:17 +0100

Ludovic Courtès <[email-masked]> writes:

> Hi Tomas,
>
> Tomas Volf <[email-masked]> skribis:
>
>> Given a timer executed under some user (via #:user "git" #:group "git"
>> keyword arguments), the HOME variable is set to /.  Is that correct?  I
>> get that it might be desirable for a root user, but for regular users
>> that is surprising and causes (for example gitolite from the package of
>> the same name) to not function:
>>
>>   2025-03-23 21:09:06 FATAL: errors found but logfile could not be created
>>   2025-03-23 21:09:06 FATAL: //.gitolite/logs/gitolite-2025-03.log: No such file or directory
>>   2025-03-23 21:09:06 FATAL: die	chdir //.gitolite failed: No such file or directory<<newline>>
>>
>> 1. Is this intentional?
>> 2. If yes, is this something you would be opened to changing?
>
> Timers do not set ‘HOME’ (or any other environment variable) at all.
> That is, they take what’s given in (command … #:environment-variables …)
> and don’t touch it.
>
> It’s intentional, but the downside is that it can lead to more verbose
> timer definitions, where would have to explicitly do:
>
>   (command … #:environment-variables
>              (cons "HOME=/whatever" (default-environment-variables)))
>
> or similar.
>
> How does that sound?

Now that I have tried to actually implement it, I have noticed that I
need to do

--8<---------------cut here---------------start------------->8---
(command … #:environment-variables
           (cons "HOME=/whatever" (delete "HOME=/" (default-environment-variables))))
--8<---------------cut here---------------end--------------->8---

Since it seems last occurrence of an environment variable is used, not
first.  Still works, but looks bit ugly I guess.

In case anyone runs into this bug, below is a full version I ended up
using (user, group, environment-variables are arguments to the wrapping
function).

--8<---------------cut here---------------start------------->8---
#:user #$user
#:group #$group
#:environment-variables
(let ((pw (getpw (or #$user (getuid)))))
  (cons* (string-append "HOME=" (passwd:dir pw))
         (string-append "USER=" (passwd:name pw))
         (remove (lambda (x)
                   (or (string-prefix? "HOME=" x)
                       (string-prefix? "USER=" x)))
                 (or '#$environment-variables
                     (default-environment-variables)))))
--8<---------------cut here---------------end--------------->8---

Tomas

-- 
There are only two hard things in Computer Science:
cache invalidation, naming things and off-by-one errors.