diff mbox series

[bug#61982,2/2] home: services: xdg-base-directories: Deprecate XDG_LOG_HOME.

Message ID 0a0764da3fa04f9a02f37a7960a0c9608782854d.1678029530.git.mirai@makinata.eu
State New
Headers show
Series Respect XDG Base Directory Specification 0.8. | expand

Commit Message

Bruno Victal March 5, 2023, 3:19 p.m. UTC
XDG_LOG_HOME is non-standard and log files should go to XDG_STATE_HOME
according to the XDG Base Directory Specification.

Fixes <https://issues.guix.gnu.org/61809>.

* gnu/home/services/desktop.scm (home-dbus-shepherd-services): Log to XDG_STATE_HOME.
* gnu/home/services/mcron.scm (home-mcron-shepherd-services): Ditto.
* gnu/home/services/pm.scm (home-batsignal-shepherd-services): Ditto.
* gnu/home/services/shepherd.scm (launch-shepherd-gexp): Ditto.
* gnu/home/services/xdg.scm
(home-xdg-base-directories-configuration)[log-home]: Deprecate and unset default value.
(home-xdg-base-directories-environment-variables-service)
(ensure-xdg-base-dirs-on-activation): Handle field deprecation.
(home-xdg-base-directories-service-type): Update description.
---
 gnu/home/services/desktop.scm  |  4 ++--
 gnu/home/services/mcron.scm    |  4 ++--
 gnu/home/services/pm.scm       |  6 +++---
 gnu/home/services/shepherd.scm |  4 ++--
 gnu/home/services/xdg.scm      | 36 +++++++++++++++++++++++++---------
 5 files changed, 36 insertions(+), 18 deletions(-)

Comments

Andrew Tropin April 7, 2023, 8:45 a.m. UTC | #1
On 2023-03-05 15:19, Bruno Victal wrote:

> XDG_LOG_HOME is non-standard and log files should go to XDG_STATE_HOME
> according to the XDG Base Directory Specification.
>
> Fixes <https://issues.guix.gnu.org/61809>.
>
> * gnu/home/services/desktop.scm (home-dbus-shepherd-services): Log to XDG_STATE_HOME.
> * gnu/home/services/mcron.scm (home-mcron-shepherd-services): Ditto.
> * gnu/home/services/pm.scm (home-batsignal-shepherd-services): Ditto.
> * gnu/home/services/shepherd.scm (launch-shepherd-gexp): Ditto.
> * gnu/home/services/xdg.scm
> (home-xdg-base-directories-configuration)[log-home]: Deprecate and unset default value.
> (home-xdg-base-directories-environment-variables-service)
> (ensure-xdg-base-dirs-on-activation): Handle field deprecation.
> (home-xdg-base-directories-service-type): Update description.
> ---
>  gnu/home/services/desktop.scm  |  4 ++--
>  gnu/home/services/mcron.scm    |  4 ++--
>  gnu/home/services/pm.scm       |  6 +++---
>  gnu/home/services/shepherd.scm |  4 ++--
>  gnu/home/services/xdg.scm      | 36 +++++++++++++++++++++++++---------
>  5 files changed, 36 insertions(+), 18 deletions(-)
>
> diff --git a/gnu/home/services/desktop.scm b/gnu/home/services/desktop.scm
> index cb25b03b64..76df7b30a2 100644
> --- a/gnu/home/services/desktop.scm
> +++ b/gnu/home/services/desktop.scm
> @@ -206,8 +206,8 @@ (define (home-dbus-shepherd-services config)
>                           (default-environment-variables))
>                     #:log-file
>                     (format #f "~a/dbus.log"
> -                           (or (getenv "XDG_LOG_HOME")
> -                               (format #f "~a/.local/var/log"
> +                           (or (getenv "XDG_STATE_HOME")
> +                               (format #f "~a/.local/state"

If we deprecate XDG_LOG_HOME, maybe it's a good idea to make a helper
function, which can be reused across different services?  

Also, how about appending /log subdirectory by default?

WDYT?

>                                         (getenv "HOME"))))))
>           (stop #~(make-kill-destructor)))))
>  
> diff --git a/gnu/home/services/mcron.scm b/gnu/home/services/mcron.scm
> index 5f35bfe054..69c7f9faa6 100644
> --- a/gnu/home/services/mcron.scm
> +++ b/gnu/home/services/mcron.scm
> @@ -99,8 +99,8 @@ (define (home-mcron-shepherd-services config)
>                                          #~())
>                                   #$@files)
>                             #:log-file (string-append
> -                                       (or (getenv "XDG_LOG_HOME")
> -                                           (format #f "~a/.local/var/log"
> +                                       (or (getenv "XDG_STATE_HOME")
> +                                           (format #f "~a/.local/state"
>                                                     (getenv "HOME")))
>                                         "/mcron.log")))
>                   (stop #~(make-kill-destructor))
> diff --git a/gnu/home/services/pm.scm b/gnu/home/services/pm.scm
> index 5f09941827..274ab2303c 100644
> --- a/gnu/home/services/pm.scm
> +++ b/gnu/home/services/pm.scm
> @@ -128,9 +128,9 @@ (define (home-batsignal-shepherd-services config)
>                                   (list "-i")
>                                   (list)))
>                       #:log-file (string-append
> -                                 (or (getenv "XDG_LOG_HOME")
> -                                     (format #f "~a/.local/var/log"
> -                                               (getenv "HOME")))
> +                                 (or (getenv "XDG_STATE_HOME")
> +                                     (format #f "~a/.local/state"
> +                                             (getenv "HOME")))
>                                   "/batsignal.log")))
>             (stop #~(make-kill-destructor))))))
>  
> diff --git a/gnu/home/services/shepherd.scm b/gnu/home/services/shepherd.scm
> index 1a70a220f0..825cf66f05 100644
> --- a/gnu/home/services/shepherd.scm
> +++ b/gnu/home/services/shepherd.scm
> @@ -108,8 +108,8 @@ (define (launch-shepherd-gexp config)
>                        (or (getenv "XDG_RUNTIME_DIR")
>                            (format #f "/run/user/~a" (getuid)))
>                        "/shepherd/socket"))
> -              (let ((log-dir (or (getenv "XDG_LOG_HOME")
> -                                 (format #f "~a/.local/var/log"
> +              (let ((log-dir (or (getenv "XDG_STATE_HOME")
> +                                 (format #f "~a/.local/state"
>                                           (getenv "HOME")))))
>                  ;; TODO: Remove it, 0.9.2 creates it automatically?
>                  ((@ (guix build utils) mkdir-p) log-dir)
> diff --git a/gnu/home/services/xdg.scm b/gnu/home/services/xdg.scm
> index ac557b4c3d..958772696b 100644
> --- a/gnu/home/services/xdg.scm
> +++ b/gnu/home/services/xdg.scm
> @@ -1,6 +1,7 @@
>  ;;; GNU Guix --- Functional package management for GNU
>  ;;; Copyright © 2021, 2022 Andrew Tropin <andrew@trop.in>
>  ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
> +;;; Copyright © 2023 Bruno Victal <mirai@makinata.eu>
>  ;;;
>  ;;; This file is part of GNU Guix.
>  ;;;
> @@ -22,6 +23,7 @@ (define-module (gnu home services xdg)
>    #:use-module (gnu home services)
>    #:use-module (gnu packages freedesktop)
>    #:use-module (gnu home services utils)
> +  #:use-module (guix deprecation)
>    #:use-module (guix gexp)
>    #:use-module (guix modules)
>    #:use-module (guix records)
> @@ -39,7 +41,7 @@ (define-module (gnu home services xdg)
>              home-xdg-base-directories-configuration-config-home
>              home-xdg-base-directories-configuration-data-home
>              home-xdg-base-directories-configuration-state-home
> -            home-xdg-base-directories-configuration-log-home
> +            home-xdg-base-directories-configuration-log-home  ; deprecated
>              home-xdg-base-directories-configuration-runtime-dir
>  
>              home-xdg-user-directories-service-type
> @@ -77,6 +79,7 @@ (define-module (gnu home services xdg)
>  
>  (define (serialize-path field-name val) "")
>  (define path? string?)
> +(define-maybe path)
>  
>  (define-configuration home-xdg-base-directories-configuration
>    (cache-home
> @@ -97,12 +100,17 @@ (define-configuration home-xdg-base-directories-configuration
>     (path "${XDG_RUNTIME_DIR:-/run/user/$UID}")
>     "Base directory for programs to store user-specific runtime files,
>  like sockets.")
> +  ;; TODO: deprecated field, use $XDG_STATE_HOME(/log) instead.
>    (log-home
> -   (path "$HOME/.local/var/log")
> +   maybe-path
>     "Base directory for programs to store log files, analogus to
>  @file{/var/log}, but for user.  It is not a part of XDG Base Directory
>  Specification, but helps to make implementation of home services more
> -consistent.")
> +consistent."
> +   (lambda (field-name val)
> +     (when (maybe-value-set? val)
> +       (warn-about-deprecation field-name #f #:replacement 'state-home))
> +     (serialize-path field-name val)))
>    (state-home
>     (path "$HOME/.local/state")
>     "Base directory for programs to store state data that should persist
> @@ -117,7 +125,13 @@ (define (home-xdg-base-directories-environment-variables-service config)
>              #f "XDG_~a"
>              (object->snake-case-string (configuration-field-name field) 'upper))
>             ((configuration-field-getter field) config)))
> -   home-xdg-base-directories-configuration-fields))
> +   ;; XXX: deprecated field, remove later
> +   (if (maybe-value-set?
> +        (home-xdg-base-directories-configuration-log-home config))
> +       home-xdg-base-directories-configuration-fields
> +       (filter-configuration-fields
> +        home-xdg-base-directories-configuration-fields
> +        '(log-home) #t))))
>  
>  (define (ensure-xdg-base-dirs-on-activation config)
>    (with-imported-modules '((guix build utils))
> @@ -138,7 +152,14 @@ (define (ensure-xdg-base-dirs-on-activation config)
>                       ;; and will be provided by elogind or other service.
>                       (and (not (string=? "XDG_RUNTIME_DIR" variable))
>                            variable)))
> -                 home-xdg-base-directories-configuration-fields)))))
> +                 ;; XXX: deprecated field, remove later
> +                 (if (maybe-value-set?
> +                      (home-xdg-base-directories-configuration-log-home
> +                       config))
> +                     home-xdg-base-directories-configuration-fields
> +                     (filter-configuration-fields
> +                      home-xdg-base-directories-configuration-fields
> +                      '(log-home) #t)))))))
>  
>  (define (last-extension-or-cfg config extensions)
>    "Picks configuration value from last provided extension.  If there
> @@ -157,10 +178,7 @@ (define home-xdg-base-directories-service-type
>                  (default-value (home-xdg-base-directories-configuration))
>                  (compose identity)
>                  (extend last-extension-or-cfg)
> -                (description "Configure XDG base directories.  This
> -service introduces an additional @env{XDG_LOG_HOME} variable.  It's not
> -a part of XDG specification, at least yet, but are convenient to have,
> -it improves the consistency between different home services.  The
> +                (description "Configure XDG base directories.  The
>  services of this service-type is instantiated by default, to provide
>  non-default value, extend the service-type (using @code{simple-service}
>  for example).")))

Overall LGTM, let me know what you think on a few comments above.
Bruno Victal April 7, 2023, 11:47 a.m. UTC | #2
Hi Andrew,

On 2023-04-07 09:45, Andrew Tropin wrote:
> On 2023-03-05 15:19, Bruno Victal wrote:
>> --- a/gnu/home/services/desktop.scm
>> +++ b/gnu/home/services/desktop.scm
>> @@ -206,8 +206,8 @@ (define (home-dbus-shepherd-services config)
>>                           (default-environment-variables))
>>                     #:log-file
>>                     (format #f "~a/dbus.log"
>> -                           (or (getenv "XDG_LOG_HOME")
>> -                               (format #f "~a/.local/var/log"
>> +                           (or (getenv "XDG_STATE_HOME")
>> +                               (format #f "~a/.local/state"
> 
> If we deprecate XDG_LOG_HOME, maybe it's a good idea to make a helper
> function, which can be reused across different services?  
> 
> Also, how about appending /log subdirectory by default?
>> WDYT?

I'm wondering if this continued usage of XDG_LOG_HOME was a good idea,
maybe the service should just start using XDG_STATE_HOME as soon as possible?
(in which case, this patch will have to be reworked to reflect that)
This way we avoid introducing yet more code to handle deprecated code.

It shouldn't result in any kind of problems, XDG_LOG_HOME will still be set in the meantime
for scripts and code that happen to use it and are not part of (upstream) guix.

WDYT?


Cheers,
Bruno
Andrew Tropin April 7, 2023, 12:46 p.m. UTC | #3
On 2023-04-07 12:47, Bruno Victal wrote:

> Hi Andrew,
>
> On 2023-04-07 09:45, Andrew Tropin wrote:
>> On 2023-03-05 15:19, Bruno Victal wrote:
>>> --- a/gnu/home/services/desktop.scm
>>> +++ b/gnu/home/services/desktop.scm
>>> @@ -206,8 +206,8 @@ (define (home-dbus-shepherd-services config)
>>>                           (default-environment-variables))
>>>                     #:log-file
>>>                     (format #f "~a/dbus.log"
>>> -                           (or (getenv "XDG_LOG_HOME")
>>> -                               (format #f "~a/.local/var/log"
>>> +                           (or (getenv "XDG_STATE_HOME")
>>> +                               (format #f "~a/.local/state"
>> 
>> If we deprecate XDG_LOG_HOME, maybe it's a good idea to make a helper
>> function, which can be reused across different services?  
>> 
>> Also, how about appending /log subdirectory by default?
>>> WDYT?
>
> I'm wondering if this continued usage of XDG_LOG_HOME was a good idea,
> maybe the service should just start using XDG_STATE_HOME as soon as possible?
> (in which case, this patch will have to be reworked to reflect that)
> This way we avoid introducing yet more code to handle deprecated code.
>
> It shouldn't result in any kind of problems, XDG_LOG_HOME will still be set in the meantime
> for scripts and code that happen to use it and are not part of (upstream) guix.
>
> WDYT?

I meant introducing something like this
(define (get-log-dir)
 (string-append
  (getenv "XDG_STATE_HOME") "/log"))

and use it instead of
(or (getenv "XDG_STATE_HOME")
 (format #f "~a/.local/state" (getenv "HOME")))
Philip McGrath April 9, 2023, 7:09 p.m. UTC | #4
Hi,

On Fri, Apr 7, 2023, at 8:46 AM, Andrew Tropin wrote:
> On 2023-04-07 12:47, Bruno Victal wrote:
>
>> Hi Andrew,
>>
>> On 2023-04-07 09:45, Andrew Tropin wrote:
>>> On 2023-03-05 15:19, Bruno Victal wrote:
>>>> --- a/gnu/home/services/desktop.scm
>>>> +++ b/gnu/home/services/desktop.scm
>>>> @@ -206,8 +206,8 @@ (define (home-dbus-shepherd-services config)
>>>>                           (default-environment-variables))
>>>>                     #:log-file
>>>>                     (format #f "~a/dbus.log"
>>>> -                           (or (getenv "XDG_LOG_HOME")
>>>> -                               (format #f "~a/.local/var/log"
>>>> +                           (or (getenv "XDG_STATE_HOME")
>>>> +                               (format #f "~a/.local/state"
>>> 
>>> If we deprecate XDG_LOG_HOME, maybe it's a good idea to make a helper
>>> function, which can be reused across different services?  
>>> 
>>> Also, how about appending /log subdirectory by default?
>>>> WDYT?
>>
>> I'm wondering if this continued usage of XDG_LOG_HOME was a good idea,
>> maybe the service should just start using XDG_STATE_HOME as soon as possible?
>> (in which case, this patch will have to be reworked to reflect that)
>> This way we avoid introducing yet more code to handle deprecated code.
>>
>> It shouldn't result in any kind of problems, XDG_LOG_HOME will still be set in the meantime
>> for scripts and code that happen to use it and are not part of (upstream) guix.
>>
>> WDYT?
>
> I meant introducing something like this
> (define (get-log-dir)
>  (string-append
>   (getenv "XDG_STATE_HOME") "/log"))
>

Within the XDG directories, I think (unless perhaps you only use a single file) you are supposed to keep everything in a subdirectory named for your application (or with a well-known name for things shared among multiple applications). For example, on my system, I can find log files in "$XDG_DATA_HOME/akonadi/Akonadi.error", "$XDG_DATA_HOME/akonadi/Akonadi.error.old", "$XDG_DATA_HOME/sddm/wayland-session.log", and "$XDG_DATA_HOME/sddm/xorg-session.log". (Apparently SDDM and Akonadi haven't been updated to use $XDG_STATE_HOME.) I think Guix Home services should follow that convention, rather than putting all logs together in "$XDG_STATE_HOME/log/".

>
> and use it instead of
> (or (getenv "XDG_STATE_HOME")
>  (format #f "~a/.local/state" (getenv "HOME")))
>

Perhaps my least-favorite part of the XDG Base Directory specification is having to repeat the default values so many times. But I understood the conclusion from <https://issues.guix.gnu.org/56050#3> to be that Guix should be able to assume that the XDG variables are all explicitly set, either by Guix System or by the installation process for Guix on a foreign distro.

-Philip
\( April 9, 2023, 7:57 p.m. UTC | #5
On Sun Apr 9, 2023 at 8:09 PM BST, Philip McGrath wrote:
> > I meant introducing something like this
> > (define (get-log-dir)
> >  (string-append
> >   (getenv "XDG_STATE_HOME") "/log"))
> >
>
> Within the XDG directories, I think (unless perhaps you only use a single file) you are supposed to keep everything in a subdirectory named for your application (or with a well-known name for things shared among multiple applications). For example, on my system, I can find log files in "$XDG_DATA_HOME/akonadi/Akonadi.error", "$XDG_DATA_HOME/akonadi/Akonadi.error.old", "$XDG_DATA_HOME/sddm/wayland-session.log", and "$XDG_DATA_HOME/sddm/xorg-session.log". (Apparently SDDM and Akonadi haven't been updated to use $XDG_STATE_HOME.) I think Guix Home services should follow that convention, rather than putting all logs together in "$XDG_STATE_HOME/log/".

So it turns out Shepherd provides these gems squirreled away inside its
(shepherd support) module:

```
(define %user-config-dir
  ;; Default config directory if shepherd is run as a normal user.
  (string-append (or (getenv "XDG_CONFIG_HOME")
                     (string-append user-homedir "/.config"))
                 "/shepherd"))

(define %user-log-dir
  ;; Default log directory if shepherd is run as a normal user.
  (string-append (or (getenv "XDG_STATE_HOME")
                     (string-append user-homedir "/.local/state"))
                 "/shepherd"))

(define %user-runtime-dir
  ;; Default runtime directory if shepherd is run as a normal user.
  (string-append (or (getenv "XDG_RUNTIME_DIR")
                     (format #f "/run/user/~s" (getuid)))))
```

Shepherd already provides us with a %USER-LOG-DIR variable :D

    -- (
\( April 9, 2023, 8:01 p.m. UTC | #6
On Sun Apr 9, 2023 at 8:57 PM BST, ( via Guix-patches via wrote:
> Shepherd already provides us with a %USER-LOG-DIR variable :D

One thing I will note is that it might be good if (shepherd support) was one
of the modules imported into shepherd service gexps by default.

    -- (
Andrew Tropin June 15, 2023, 9:35 a.m. UTC | #7
On 2023-03-05 15:19, Bruno Victal wrote:

> XDG_LOG_HOME is non-standard and log files should go to XDG_STATE_HOME
> according to the XDG Base Directory Specification.
>
> Fixes <https://issues.guix.gnu.org/61809>.
>
> * gnu/home/services/desktop.scm (home-dbus-shepherd-services): Log to XDG_STATE_HOME.
> * gnu/home/services/mcron.scm (home-mcron-shepherd-services): Ditto.
> * gnu/home/services/pm.scm (home-batsignal-shepherd-services): Ditto.
> * gnu/home/services/shepherd.scm (launch-shepherd-gexp): Ditto.
> * gnu/home/services/xdg.scm
> (home-xdg-base-directories-configuration)[log-home]: Deprecate and unset default value.
> (home-xdg-base-directories-environment-variables-service)
> (ensure-xdg-base-dirs-on-activation): Handle field deprecation.
> (home-xdg-base-directories-service-type): Update description.
> ---
>  gnu/home/services/desktop.scm  |  4 ++--
>  gnu/home/services/mcron.scm    |  4 ++--
>  gnu/home/services/pm.scm       |  6 +++---
>  gnu/home/services/shepherd.scm |  4 ++--
>  gnu/home/services/xdg.scm      | 36 +++++++++++++++++++++++++---------
>  5 files changed, 36 insertions(+), 18 deletions(-)
>
> diff --git a/gnu/home/services/desktop.scm b/gnu/home/services/desktop.scm
> index cb25b03b64..76df7b30a2 100644
> --- a/gnu/home/services/desktop.scm
> +++ b/gnu/home/services/desktop.scm
> @@ -206,8 +206,8 @@ (define (home-dbus-shepherd-services config)
>                           (default-environment-variables))
>                     #:log-file
>                     (format #f "~a/dbus.log"
> -                           (or (getenv "XDG_LOG_HOME")
> -                               (format #f "~a/.local/var/log"
> +                           (or (getenv "XDG_STATE_HOME")
> +                               (format #f "~a/.local/state"
>                                         (getenv "HOME"))))))
>           (stop #~(make-kill-destructor)))))
>  
> diff --git a/gnu/home/services/mcron.scm b/gnu/home/services/mcron.scm
> index 5f35bfe054..69c7f9faa6 100644
> --- a/gnu/home/services/mcron.scm
> +++ b/gnu/home/services/mcron.scm
> @@ -99,8 +99,8 @@ (define (home-mcron-shepherd-services config)
>                                          #~())
>                                   #$@files)
>                             #:log-file (string-append
> -                                       (or (getenv "XDG_LOG_HOME")
> -                                           (format #f "~a/.local/var/log"
> +                                       (or (getenv "XDG_STATE_HOME")
> +                                           (format #f "~a/.local/state"
>                                                     (getenv "HOME")))
>                                         "/mcron.log")))
>                   (stop #~(make-kill-destructor))
> diff --git a/gnu/home/services/pm.scm b/gnu/home/services/pm.scm
> index 5f09941827..274ab2303c 100644
> --- a/gnu/home/services/pm.scm
> +++ b/gnu/home/services/pm.scm
> @@ -128,9 +128,9 @@ (define (home-batsignal-shepherd-services config)
>                                   (list "-i")
>                                   (list)))
>                       #:log-file (string-append
> -                                 (or (getenv "XDG_LOG_HOME")
> -                                     (format #f "~a/.local/var/log"
> -                                               (getenv "HOME")))
> +                                 (or (getenv "XDG_STATE_HOME")
> +                                     (format #f "~a/.local/state"
> +                                             (getenv "HOME")))
>                                   "/batsignal.log")))
>             (stop #~(make-kill-destructor))))))
>  
> diff --git a/gnu/home/services/shepherd.scm b/gnu/home/services/shepherd.scm
> index 1a70a220f0..825cf66f05 100644
> --- a/gnu/home/services/shepherd.scm
> +++ b/gnu/home/services/shepherd.scm
> @@ -108,8 +108,8 @@ (define (launch-shepherd-gexp config)
>                        (or (getenv "XDG_RUNTIME_DIR")
>                            (format #f "/run/user/~a" (getuid)))
>                        "/shepherd/socket"))
> -              (let ((log-dir (or (getenv "XDG_LOG_HOME")
> -                                 (format #f "~a/.local/var/log"
> +              (let ((log-dir (or (getenv "XDG_STATE_HOME")
> +                                 (format #f "~a/.local/state"
>                                           (getenv "HOME")))))
>                  ;; TODO: Remove it, 0.9.2 creates it automatically?
>                  ((@ (guix build utils) mkdir-p) log-dir)
> diff --git a/gnu/home/services/xdg.scm b/gnu/home/services/xdg.scm
> index ac557b4c3d..958772696b 100644
> --- a/gnu/home/services/xdg.scm
> +++ b/gnu/home/services/xdg.scm
> @@ -1,6 +1,7 @@
>  ;;; GNU Guix --- Functional package management for GNU
>  ;;; Copyright © 2021, 2022 Andrew Tropin <andrew@trop.in>
>  ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
> +;;; Copyright © 2023 Bruno Victal <mirai@makinata.eu>
>  ;;;
>  ;;; This file is part of GNU Guix.
>  ;;;
> @@ -22,6 +23,7 @@ (define-module (gnu home services xdg)
>    #:use-module (gnu home services)
>    #:use-module (gnu packages freedesktop)
>    #:use-module (gnu home services utils)
> +  #:use-module (guix deprecation)
>    #:use-module (guix gexp)
>    #:use-module (guix modules)
>    #:use-module (guix records)
> @@ -39,7 +41,7 @@ (define-module (gnu home services xdg)
>              home-xdg-base-directories-configuration-config-home
>              home-xdg-base-directories-configuration-data-home
>              home-xdg-base-directories-configuration-state-home
> -            home-xdg-base-directories-configuration-log-home
> +            home-xdg-base-directories-configuration-log-home  ; deprecated
>              home-xdg-base-directories-configuration-runtime-dir
>  
>              home-xdg-user-directories-service-type
> @@ -77,6 +79,7 @@ (define-module (gnu home services xdg)
>  
>  (define (serialize-path field-name val) "")
>  (define path? string?)
> +(define-maybe path)
>  
>  (define-configuration home-xdg-base-directories-configuration
>    (cache-home
> @@ -97,12 +100,17 @@ (define-configuration home-xdg-base-directories-configuration
>     (path "${XDG_RUNTIME_DIR:-/run/user/$UID}")
>     "Base directory for programs to store user-specific runtime files,
>  like sockets.")
> +  ;; TODO: deprecated field, use $XDG_STATE_HOME(/log) instead.
>    (log-home
> -   (path "$HOME/.local/var/log")
> +   maybe-path
>     "Base directory for programs to store log files, analogus to
>  @file{/var/log}, but for user.  It is not a part of XDG Base Directory
>  Specification, but helps to make implementation of home services more
> -consistent.")
> +consistent."
> +   (lambda (field-name val)
> +     (when (maybe-value-set? val)
> +       (warn-about-deprecation field-name #f #:replacement 'state-home))
> +     (serialize-path field-name val)))
>    (state-home
>     (path "$HOME/.local/state")
>     "Base directory for programs to store state data that should persist
> @@ -117,7 +125,13 @@ (define (home-xdg-base-directories-environment-variables-service config)
>              #f "XDG_~a"
>              (object->snake-case-string (configuration-field-name field) 'upper))
>             ((configuration-field-getter field) config)))
> -   home-xdg-base-directories-configuration-fields))
> +   ;; XXX: deprecated field, remove later
> +   (if (maybe-value-set?
> +        (home-xdg-base-directories-configuration-log-home config))
> +       home-xdg-base-directories-configuration-fields
> +       (filter-configuration-fields
> +        home-xdg-base-directories-configuration-fields
> +        '(log-home) #t))))
>  
>  (define (ensure-xdg-base-dirs-on-activation config)
>    (with-imported-modules '((guix build utils))
> @@ -138,7 +152,14 @@ (define (ensure-xdg-base-dirs-on-activation config)
>                       ;; and will be provided by elogind or other service.
>                       (and (not (string=? "XDG_RUNTIME_DIR" variable))
>                            variable)))
> -                 home-xdg-base-directories-configuration-fields)))))
> +                 ;; XXX: deprecated field, remove later
> +                 (if (maybe-value-set?
> +                      (home-xdg-base-directories-configuration-log-home
> +                       config))
> +                     home-xdg-base-directories-configuration-fields
> +                     (filter-configuration-fields
> +                      home-xdg-base-directories-configuration-fields
> +                      '(log-home) #t)))))))
>  
>  (define (last-extension-or-cfg config extensions)
>    "Picks configuration value from last provided extension.  If there
> @@ -157,10 +178,7 @@ (define home-xdg-base-directories-service-type
>                  (default-value (home-xdg-base-directories-configuration))
>                  (compose identity)
>                  (extend last-extension-or-cfg)
> -                (description "Configure XDG base directories.  This
> -service introduces an additional @env{XDG_LOG_HOME} variable.  It's not
> -a part of XDG specification, at least yet, but are convenient to have,
> -it improves the consistency between different home services.  The
> +                (description "Configure XDG base directories.  The
>  services of this service-type is instantiated by default, to provide
>  non-default value, extend the service-type (using @code{simple-service}
>  for example).")))

Hi Bruno,

Applied, added log subdirectory, pushed, thank you for working on it.


Hi Ludo,

WDYT about adding (home-log-dir) function to (guix build utils)?
So we can prevent copypasting the same code all over home modules.

Something like:
--8<---------------cut here---------------start------------->8---
(define (home-log-dir)
  "Returns a directory for user applications logs."
  (string-append
   (or (getenv "XDG_STATE_HOME")
       (format #f "~a/.local/state"
               (getenv "HOME")))
   "/log"))
--8<---------------cut here---------------end--------------->8---
Philip McGrath June 15, 2023, 6:09 p.m. UTC | #8
Hi,

On Thu, Jun 15, 2023, at 5:35 AM, Andrew Tropin wrote:
>
> WDYT about adding (home-log-dir) function to (guix build utils)?
> So we can prevent copypasting the same code all over home modules.
>
> Something like:
> --8<---------------cut here---------------start------------->8---
> (define (home-log-dir)
>   "Returns a directory for user applications logs."
>   (string-append
>    (or (getenv "XDG_STATE_HOME")
>        (format #f "~a/.local/state"
>                (getenv "HOME")))
>    "/log"))
> --8<---------------cut here---------------end--------------->8---
>

I still think idiomatic XDG structure is NOT to have a "/log" subdirectory like this, as I explained in <https://issues.guix.gnu.org/61982#7>.

Aside from that, though, I thought the conclusion from <https://issues.guix.gnu.org/56050#3> was that it is the responsibility of Guix System or the installation mechanism for Guix on a foreign distribution (e.g. "/etc/profile.d/zzz-guix.sh") to initialize all of the XDG variables, so code like this can use `(getenv "XDG_STATE_HOME")` unconditionally (or indeed with a checked assertion). Maybe there's some context I'm forgetting, though. I've been looking into these things again as I attempt to solve <https://lists.gnu.org/archive/html/help-guix/2023-06/msg00031.html>: I noticed in the attached environment variables that entries under /home/philip/.guix-home/profile/ are duplicated in many search paths.

-Philip
Andrew Tropin June 16, 2023, 3:28 a.m. UTC | #9
On 2023-06-15 14:09, Philip McGrath wrote:

> Hi,
>
> On Thu, Jun 15, 2023, at 5:35 AM, Andrew Tropin wrote:
>>
>> WDYT about adding (home-log-dir) function to (guix build utils)?
>> So we can prevent copypasting the same code all over home modules.
>>
>> Something like:
>> --8<---------------cut here---------------start------------->8---
>> (define (home-log-dir)
>>   "Returns a directory for user applications logs."
>>   (string-append
>>    (or (getenv "XDG_STATE_HOME")
>>        (format #f "~a/.local/state"
>>                (getenv "HOME")))
>>    "/log"))
>> --8<---------------cut here---------------end--------------->8---
>>
>
> I still think idiomatic XDG structure is NOT to have a "/log"
> subdirectory like this, as I explained in
> <https://issues.guix.gnu.org/61982#7>.

With a brief look I didn't find information about this idiom in
specification, but you can highlight it with quote or link.

Anyway, I'm ok with storing logs in app subdirectory or "/log", not so
ok with storing them in the root of state home.

>
> Aside from that, though, I thought the conclusion from
> <https://issues.guix.gnu.org/56050#3> was that it is the
> responsibility of Guix System or the installation mechanism for Guix
> on a foreign distribution (e.g. "/etc/profile.d/zzz-guix.sh") to
> initialize all of the XDG variables, so code like this can use
> `(getenv "XDG_STATE_HOME")` unconditionally (or indeed with a checked
> assertion). Maybe there's some context I'm forgetting, though. I've
> been looking into these things again as I attempt to solve
> <https://lists.gnu.org/archive/html/help-guix/2023-06/msg00031.html>:
> I noticed in the attached environment variables that entries under
> /home/philip/.guix-home/profile/ are duplicated in many search paths.

home-state-dir function can also be useful, which can later become just
(getenv ("XDG_STATE_HOME")).
Philip McGrath June 17, 2023, 6:22 p.m. UTC | #10
Hi,

On Thu, Jun 15, 2023, at 11:28 PM, Andrew Tropin wrote:
> On 2023-06-15 14:09, Philip McGrath wrote:
>
>> Hi,
>>
>> On Thu, Jun 15, 2023, at 5:35 AM, Andrew Tropin wrote:
>>>
>>> WDYT about adding (home-log-dir) function to (guix build utils)?
>>> So we can prevent copypasting the same code all over home modules.
>>>
>>> Something like:
>>> --8<---------------cut here---------------start------------->8---
>>> (define (home-log-dir)
>>>   "Returns a directory for user applications logs."
>>>   (string-append
>>>    (or (getenv "XDG_STATE_HOME")
>>>        (format #f "~a/.local/state"
>>>                (getenv "HOME")))
>>>    "/log"))
>>> --8<---------------cut here---------------end--------------->8---
>>>
>>
>> I still think idiomatic XDG structure is NOT to have a "/log"
>> subdirectory like this, as I explained in
>> <https://issues.guix.gnu.org/61982#7>.
>
> With a brief look I didn't find information about this idiom in
> specification, but you can highlight it with quote or link.
>
> Anyway, I'm ok with storing logs in app subdirectory or "/log", not so
> ok with storing them in the root of state home.
>

You are right that the idiomatic usage isn't made very explicit in the specification. In the specification itself, the idiom is implicit in the "Referencing this specification" section, which always uses a subdirectory:

>
> Other specifications may reference this specification by specifying the location of a data file as $XDG_DATA_DIRS/subdir/filename. This implies that:
>
> - Such file should be installed to $datadir/subdir/filename with $datadir defaulting to /usr/share.
>
> - A user-specific version of the data file may be created in $XDG_DATA_HOME/subdir/filename, taking into account the default value for $XDG_DATA_HOME if $XDG_DATA_HOME is not set.
>
> - Lookups of the data file should search for ./subdir/filename relative to all base directories specified by $XDG_DATA_HOME and $XDG_DATA_DIRS . If an environment variable is either not set or empty, its default value as defined by this specification should be used instead. 
>
> Specifications may reference this specification by specifying the location of a configuration file as $XDG_CONFIG_DIRS/subdir/filename. This implies that:
>
> - Default configuration files should be installed to $sysconfdir/xdg/subdir/filename with $sysconfdir defaulting to /etc.
>
> - A user-specific version of the configuration file may be created in $XDG_CONFIG_HOME/subdir/filename, taking into account the default value for $XDG_CONFIG_HOME if $XDG_CONFIG_HOME is not set.
>
> - Lookups of the configuration file should search for ./subdir/filename relative to all base directories indicated by $XDG_CONFIG_HOME and $XDG_CONFIG_DIRS . If an environment variable is either not set or empty, its default value as defined by this specification should be used instead. 
>

(Obviously $HOME/.local/bin is a special case, since using subdirectories there would defeat the point.)

More than the text of the specification, I'd look at actual observed usage and documented best practices. The relevant Gnome Goal [1] (despite outbound linkrot) is consistent with every other developer guide I've seen in showing the use of subdirectories, e.g. in the Glib example code:

```
const char *cache_dir = g_get_user_cache_dir ();
char *mydir = g_build_filename (cache_dir, "myapp", NULL);
/* use it */
g_free (mydir);
```

(Glib now provides g_get_user_state_dir, too. [2])

The Arch wiki has a survey of XDG support status [3] that consistently shows applications using appropriate subdirectories.

[1]: https://wiki.gnome.org/Initiatives/GnomeGoals/XDGConfigFolders
[2]: https://docs.gtk.org/glib/func.get_user_state_dir.html
[3]: https://wiki.archlinux.org/title/XDG_Base_Directory

>>
>> Aside from that, though, I thought the conclusion from
>> <https://issues.guix.gnu.org/56050#3> was that it is the
>> responsibility of Guix System or the installation mechanism for Guix
>> on a foreign distribution (e.g. "/etc/profile.d/zzz-guix.sh") to
>> initialize all of the XDG variables, so code like this can use
>> `(getenv "XDG_STATE_HOME")` unconditionally (or indeed with a checked
>> assertion). Maybe there's some context I'm forgetting, though. I've
>> been looking into these things again as I attempt to solve
>> <https://lists.gnu.org/archive/html/help-guix/2023-06/msg00031.html>:
>> I noticed in the attached environment variables that entries under
>> /home/philip/.guix-home/profile/ are duplicated in many search paths.
>
> home-state-dir function can also be useful, which can later become just
> (getenv ("XDG_STATE_HOME")).
>

I'm not at all opposed to providing functions rather that using getenv everywhere: they could guard against typos, check invariants, and all the other usual helpful things. But---though maybe I'm missing some context---how often is it actually needed to pass these default values? As long as the upstream software supports XDG directories properly, I would think it should just do the right thing without having to pass a bunch of explicit configuration options.

-Philip
diff mbox series

Patch

diff --git a/gnu/home/services/desktop.scm b/gnu/home/services/desktop.scm
index cb25b03b64..76df7b30a2 100644
--- a/gnu/home/services/desktop.scm
+++ b/gnu/home/services/desktop.scm
@@ -206,8 +206,8 @@  (define (home-dbus-shepherd-services config)
                          (default-environment-variables))
                    #:log-file
                    (format #f "~a/dbus.log"
-                           (or (getenv "XDG_LOG_HOME")
-                               (format #f "~a/.local/var/log"
+                           (or (getenv "XDG_STATE_HOME")
+                               (format #f "~a/.local/state"
                                        (getenv "HOME"))))))
          (stop #~(make-kill-destructor)))))
 
diff --git a/gnu/home/services/mcron.scm b/gnu/home/services/mcron.scm
index 5f35bfe054..69c7f9faa6 100644
--- a/gnu/home/services/mcron.scm
+++ b/gnu/home/services/mcron.scm
@@ -99,8 +99,8 @@  (define (home-mcron-shepherd-services config)
                                         #~())
                                  #$@files)
                            #:log-file (string-append
-                                       (or (getenv "XDG_LOG_HOME")
-                                           (format #f "~a/.local/var/log"
+                                       (or (getenv "XDG_STATE_HOME")
+                                           (format #f "~a/.local/state"
                                                    (getenv "HOME")))
                                        "/mcron.log")))
                  (stop #~(make-kill-destructor))
diff --git a/gnu/home/services/pm.scm b/gnu/home/services/pm.scm
index 5f09941827..274ab2303c 100644
--- a/gnu/home/services/pm.scm
+++ b/gnu/home/services/pm.scm
@@ -128,9 +128,9 @@  (define (home-batsignal-shepherd-services config)
                                  (list "-i")
                                  (list)))
                      #:log-file (string-append
-                                 (or (getenv "XDG_LOG_HOME")
-                                     (format #f "~a/.local/var/log"
-                                               (getenv "HOME")))
+                                 (or (getenv "XDG_STATE_HOME")
+                                     (format #f "~a/.local/state"
+                                             (getenv "HOME")))
                                  "/batsignal.log")))
            (stop #~(make-kill-destructor))))))
 
diff --git a/gnu/home/services/shepherd.scm b/gnu/home/services/shepherd.scm
index 1a70a220f0..825cf66f05 100644
--- a/gnu/home/services/shepherd.scm
+++ b/gnu/home/services/shepherd.scm
@@ -108,8 +108,8 @@  (define (launch-shepherd-gexp config)
                       (or (getenv "XDG_RUNTIME_DIR")
                           (format #f "/run/user/~a" (getuid)))
                       "/shepherd/socket"))
-              (let ((log-dir (or (getenv "XDG_LOG_HOME")
-                                 (format #f "~a/.local/var/log"
+              (let ((log-dir (or (getenv "XDG_STATE_HOME")
+                                 (format #f "~a/.local/state"
                                          (getenv "HOME")))))
                 ;; TODO: Remove it, 0.9.2 creates it automatically?
                 ((@ (guix build utils) mkdir-p) log-dir)
diff --git a/gnu/home/services/xdg.scm b/gnu/home/services/xdg.scm
index ac557b4c3d..958772696b 100644
--- a/gnu/home/services/xdg.scm
+++ b/gnu/home/services/xdg.scm
@@ -1,6 +1,7 @@ 
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2021, 2022 Andrew Tropin <andrew@trop.in>
 ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
+;;; Copyright © 2023 Bruno Victal <mirai@makinata.eu>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -22,6 +23,7 @@  (define-module (gnu home services xdg)
   #:use-module (gnu home services)
   #:use-module (gnu packages freedesktop)
   #:use-module (gnu home services utils)
+  #:use-module (guix deprecation)
   #:use-module (guix gexp)
   #:use-module (guix modules)
   #:use-module (guix records)
@@ -39,7 +41,7 @@  (define-module (gnu home services xdg)
             home-xdg-base-directories-configuration-config-home
             home-xdg-base-directories-configuration-data-home
             home-xdg-base-directories-configuration-state-home
-            home-xdg-base-directories-configuration-log-home
+            home-xdg-base-directories-configuration-log-home  ; deprecated
             home-xdg-base-directories-configuration-runtime-dir
 
             home-xdg-user-directories-service-type
@@ -77,6 +79,7 @@  (define-module (gnu home services xdg)
 
 (define (serialize-path field-name val) "")
 (define path? string?)
+(define-maybe path)
 
 (define-configuration home-xdg-base-directories-configuration
   (cache-home
@@ -97,12 +100,17 @@  (define-configuration home-xdg-base-directories-configuration
    (path "${XDG_RUNTIME_DIR:-/run/user/$UID}")
    "Base directory for programs to store user-specific runtime files,
 like sockets.")
+  ;; TODO: deprecated field, use $XDG_STATE_HOME(/log) instead.
   (log-home
-   (path "$HOME/.local/var/log")
+   maybe-path
    "Base directory for programs to store log files, analogus to
 @file{/var/log}, but for user.  It is not a part of XDG Base Directory
 Specification, but helps to make implementation of home services more
-consistent.")
+consistent."
+   (lambda (field-name val)
+     (when (maybe-value-set? val)
+       (warn-about-deprecation field-name #f #:replacement 'state-home))
+     (serialize-path field-name val)))
   (state-home
    (path "$HOME/.local/state")
    "Base directory for programs to store state data that should persist
@@ -117,7 +125,13 @@  (define (home-xdg-base-directories-environment-variables-service config)
             #f "XDG_~a"
             (object->snake-case-string (configuration-field-name field) 'upper))
            ((configuration-field-getter field) config)))
-   home-xdg-base-directories-configuration-fields))
+   ;; XXX: deprecated field, remove later
+   (if (maybe-value-set?
+        (home-xdg-base-directories-configuration-log-home config))
+       home-xdg-base-directories-configuration-fields
+       (filter-configuration-fields
+        home-xdg-base-directories-configuration-fields
+        '(log-home) #t))))
 
 (define (ensure-xdg-base-dirs-on-activation config)
   (with-imported-modules '((guix build utils))
@@ -138,7 +152,14 @@  (define (ensure-xdg-base-dirs-on-activation config)
                      ;; and will be provided by elogind or other service.
                      (and (not (string=? "XDG_RUNTIME_DIR" variable))
                           variable)))
-                 home-xdg-base-directories-configuration-fields)))))
+                 ;; XXX: deprecated field, remove later
+                 (if (maybe-value-set?
+                      (home-xdg-base-directories-configuration-log-home
+                       config))
+                     home-xdg-base-directories-configuration-fields
+                     (filter-configuration-fields
+                      home-xdg-base-directories-configuration-fields
+                      '(log-home) #t)))))))
 
 (define (last-extension-or-cfg config extensions)
   "Picks configuration value from last provided extension.  If there
@@ -157,10 +178,7 @@  (define home-xdg-base-directories-service-type
                 (default-value (home-xdg-base-directories-configuration))
                 (compose identity)
                 (extend last-extension-or-cfg)
-                (description "Configure XDG base directories.  This
-service introduces an additional @env{XDG_LOG_HOME} variable.  It's not
-a part of XDG specification, at least yet, but are convenient to have,
-it improves the consistency between different home services.  The
+                (description "Configure XDG base directories.  The
 services of this service-type is instantiated by default, to provide
 non-default value, extend the service-type (using @code{simple-service}
 for example).")))