all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Ludovic Courtès" <ludo@gnu.org>
To: Giacomo Leidi <goodoldpaul@autistici.org>
Cc: 60521@debbugs.gnu.org
Subject: [bug#60521] [PATCH v6] home: Add home-dotfiles-service.
Date: Sat, 27 Jan 2024 23:56:29 +0100	[thread overview]
Message-ID: <874jeyjsgy.fsf@gnu.org> (raw)
In-Reply-To: <20240126174850.9671-1-goodoldpaul@autistici.org> (Giacomo Leidi's message of "Fri, 26 Jan 2024 18:48:41 +0100")

[-- Attachment #1: Type: text/plain, Size: 1663 bytes --]

Hi Giacomo,

Giacomo Leidi <goodoldpaul@autistici.org> skribis:

> * gnu/home/services.scm (dotfiles-for-app): New variable;
> (home-dotfiles-configuration): new variable;
> (home-dotfiles-service-type): new variable.
> * doc/guix.texi: Document it.

Apologies again for the long delay.

The patch looks great to me and I think the functionality is there.

I gave it a try for my own config, and that has led me to make the
attached changes, which can be summarized as follows:

  • The dotfile directories are resolved relative the source location
    where ‘home-dotfiles-configuration’ appears.  The advantage is that
    users do not need to fiddle with (current-source-directory).

  • As a consequence, all ‘with-directory-excursion’ and
    ‘canonicalize-path’ calls are gone.  (Those should only be used with
    great care.)

  • The dotfile directories are traversed only once, using ‘find-files’.

  • The exclusion regexp is compiled only once (with ‘make-regexp’) and
    then reused (with ‘regexp-exec’ calls), which is more efficient
    than repeated ‘string-match’ calls.

  • Use ‘string-map’ instead of ‘string-replace-substring’ (it’s simpler
    and more efficient).

If that’s fine with you, please feel free to apply these changes.

One last thing I should have suggested earlier: how about moving it to
(gnu home services dotfiles)?  That would keep the scope of (gnu home
services) limited to essential services.

Please send one last version when you’re ready; I’m eager to use it for
my own config actually.  :-)

Thank you!

Ludo’.


[-- Attachment #2: Type: text/x-patch, Size: 7016 bytes --]

diff --git a/doc/guix.texi b/doc/guix.texi
index 3d764e6021..a796d9c8f8 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -44265,18 +44265,12 @@ Essential Home Services
 be:
 
 @lisp
-(use-modules (guix utils))
-
 (home-environment
-
-  [...]
-
+  ;; @dots{}
   (services
     (service home-dotfiles-service-type
              (home-dotfiles-configuration
-               (directories
-                 (list (string-append (current-source-directory)
-                                      "/.dotfiles")))))))
+               (directories (list "./dot-files"))))))
 @end lisp
 
 The expected home directory state would be:
diff --git a/gnu/home/services.scm b/gnu/home/services.scm
index 3e925c07c8..c7379c93c7 100644
--- a/gnu/home/services.scm
+++ b/gnu/home/services.scm
@@ -1,7 +1,7 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2021-2023 Andrew Tropin <andrew@trop.in>
 ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
-;;; Copyright © 2022-2023 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2022-2024 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2023 Carlo Zancanaro <carlo@zancanaro.id.au>
 ;;; Copyright © 2023 Giacomo Leidi <goodoldpaul@autistici.org>
 ;;;
@@ -24,7 +24,7 @@ (define-module (gnu home services)
   #:use-module (gnu services)
   #:use-module ((gnu packages package-management) #:select (guix))
   #:use-module ((gnu packages base) #:select (coreutils))
-  #:use-module (guix build utils)
+  #:autoload   (guix build utils) (find-files)
   #:use-module (guix channels)
   #:use-module (guix monads)
   #:use-module (guix store)
@@ -32,6 +32,7 @@ (define-module (gnu home services)
   #:use-module (guix profiles)
   #:use-module (guix sets)
   #:use-module (guix ui)
+  #:use-module ((guix utils) #:select (current-source-directory))
   #:use-module (guix discovery)
   #:use-module (guix diagnostics)
   #:use-module (guix i18n)
@@ -43,7 +44,6 @@ (define-module (gnu home services)
   #:use-module (ice-9 ftw)
   #:use-module (ice-9 match)
   #:use-module (ice-9 regex)
-  #:use-module (ice-9 string-fun)
   #:use-module (ice-9 vlist)
 
   #:export (home-service-type
@@ -375,65 +375,69 @@ (define %home-dotfiles-excluded
 (define-record-type* <home-dotfiles-configuration>
   home-dotfiles-configuration make-home-dotfiles-configuration
   home-dotfiles-configuration?
+  (source-directory  home-dotfiles-configuration-source-directory
+                     (default (current-source-directory))
+                     (innate))
   (directories       home-dotfiles-configuration-directories       ;list of strings
                      (default '()))
   (excluded          home-dotfiles-configuration-excluded          ;list of strings
                      (default %home-dotfiles-excluded)))
 
-(define* (import-dotfiles directory excluded)
+(define (import-dotfiles directory files)
   "Return a list of objects compatible with @code{home-files-service-type}'s
 value.  Each object is a pair where the first element is the relative path
 of a file and the second is a gexp representing the file content.  Objects are
 generated by recursively visiting DIRECTORY and mapping its contents to the
 user's home directory, excluding files that match any of the patterns in EXCLUDED."
-  (define filtered
-    (find-files directory
-                (lambda (file stat)
-                  (not (string-match
-                        (string-append
-                         "^.*(" (string-join excluded "|") ")$") file)))))
   (define (strip file)
-     (string-drop file (+ 1 (string-length directory))))
-  (define (resolve file)
-    (if (eq? 'symlink (stat:type (lstat file)))
-        (let ((resolved (readlink file)))
-          (with-directory-excursion (dirname file)
-            (canonicalize-path resolved)))
-        file))
+    (string-drop file (+ 1 (string-length directory))))
+
   (define (format file)
-    (let* ((without-spaces
-           (string-replace-substring file " " "_"))
-          (without-slashes-and-spaces
-           (string-replace-substring without-spaces "/" "-")))
-      (string-append "home-dotfiles-" without-slashes-and-spaces)))
+    ;; Remove from FILE characters that cannot be used in the store.
+    (string-append
+     "home-dotfiles-"
+     (string-map (lambda (chr)
+                   (if (and (char-set-contains? char-set:ascii chr)
+                            (char-set-contains? char-set:graphic chr)
+                            (not (memv chr '(#\. #\/))))
+                       chr
+                       #\-))
+                 file)))
 
   (map (lambda (file)
-        (let* ((stripped (strip file)))
+        (let ((stripped (strip file)))
           (list stripped
-                (local-file (resolve file) (format stripped)
+                (local-file file (format stripped)
                             #:recursive? #t))))
-       filtered))
+       files))
 
 (define (home-dotfiles-configuration->files config)
-   "Return a list of objects compatible with @code{home-files-service-type}'s
+  "Return a list of objects compatible with @code{home-files-service-type}'s
 value, generated following GNU Stow's algorithm for each of the
 directories in CONFIG, excluding files that match any of the patterns configured."
-   (define (directory-contents directories)
-     (append-map
-      (lambda (directory)
-        (map
-         (lambda (content)
-           (with-directory-excursion directory
-               (canonicalize-path content)))
-         (scandir directory
-           (lambda (name)
-             (not (member name '("." "..")))))))
-      directories))
-   (append-map
-    (lambda (app)
-      (import-dotfiles app (home-dotfiles-configuration-excluded config)))
-    (directory-contents
-     (home-dotfiles-configuration-directories config))))
+  (define excluded
+    (home-dotfiles-configuration-excluded config))
+  (define exclusion-rx
+    (make-regexp (string-append "^.*(" (string-join excluded "|") ")$")))
+
+  (define (directory-contents directory)
+    (find-files directory
+                (lambda (file stat)
+                  (not (regexp-exec exclusion-rx
+                                    (basename file))))))
+
+  (define (resolve directory)
+    ;; Resolve DIRECTORY relative to the 'source-directory' field of CONFIG.
+    (if (string-prefix? "/" directory)
+        directory
+        (in-vicinity (home-dotfiles-configuration-source-directory config)
+                     directory)))
+
+  (append-map (lambda (directory)
+                (let* ((directory (resolve directory))
+                       (contents  (directory-contents directory)))
+                  (import-dotfiles directory contents)))
+              (home-dotfiles-configuration-directories config)))
 
 (define-public home-dotfiles-service-type
   (service-type (name 'home-dotfiles)

  reply	other threads:[~2024-01-27 22:57 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-03 16:51 [bug#60521] [PATCH] home: Add home-stow-migration-service goodoldpaul--- via Guix-patches via
2023-01-03 16:55 ` Giacomo Leidi via Guix-patches via
2023-01-17 13:09   ` Ludovic Courtès
2023-01-17 15:21     ` Andrew Tropin
2023-01-23 10:23       ` Ludovic Courtès
2023-01-25  6:32         ` Andrew Tropin
2023-01-17 17:09   ` Bruno Victal
2023-02-12 17:36 ` goodoldpaul--- via Guix-patches via
2023-04-12 20:31   ` goodoldpaul--- via Guix-patches via
2023-02-12 17:36 ` [bug#60521] [v2] home: Add home-dotfiles-service Giacomo Leidi via Guix-patches via
2023-04-12 20:32 ` [bug#60521] [v3] " Giacomo Leidi via Guix-patches via
2023-04-24 20:33   ` [bug#60521] [PATCH] home: Add home-stow-migration-service Ludovic Courtès
2023-06-24 15:47 ` paul via Guix-patches via
2023-08-26  9:34   ` goodoldpaul--- via Guix-patches via
2023-09-22 12:59     ` paul via Guix-patches via
2023-06-24 16:01 ` [bug#60521] [PATCH-v4] home: Add home-dotfiles-service Giacomo Leidi via Guix-patches via
2023-08-26  9:39 ` Giacomo Leidi via Guix-patches via
2023-09-22 13:01 ` [bug#60521] [PATCH] " Giacomo Leidi via Guix-patches via
2023-10-02  2:19 ` [bug#60521] Nicolas Odermatt-Lemay
2023-10-06 21:17 ` [bug#60521] [PATCH] home: Add home-dotfiles-service paul via Guix-patches via
2023-10-06 21:22 ` Giacomo Leidi via Guix-patches via
2023-10-29 12:58 ` [bug#60521] [60521] Add home-dotfiles-service-type - Rebased on master paul via Guix-patches via
2024-01-21 17:06   ` paul via Guix-patches via
2024-01-22  0:16     ` [bug#60521] " tumashu
2024-01-22  8:12       ` Giacomo via Guix-patches via
2024-01-22 12:36     ` [bug#60521] " Feng Shu
2024-01-22 16:45       ` paul via Guix-patches via
2024-01-23 12:14         ` Feng Shu
2024-01-24 11:58     ` Feng Shu
2024-01-26 17:47       ` paul via Guix-patches via
2024-01-27  2:54         ` Feng Shu
2024-01-21 17:07   ` paul via Guix-patches via
2023-10-29 12:59 ` [bug#60521] [PATCH v4] home: Add home-dotfiles-service Giacomo Leidi via Guix-patches via
2023-11-06  0:55 ` [bug#60521] [PATCH] home: Add home-stow-migration-service Feng Shu
2023-11-07  8:58 ` Feng Shu
2023-11-09  0:59 ` Feng Shu
2024-01-21 17:08 ` [bug#60521] [PATCH v5] home: Add home-dotfiles-service Giacomo Leidi via Guix-patches via
2024-01-26 17:48 ` [bug#60521] [PATCH v6] " Giacomo Leidi via Guix-patches via
2024-01-27 22:56   ` Ludovic Courtès [this message]
2024-01-28 15:36     ` paul via Guix-patches via
2024-01-27 20:21 ` [bug#60521] [PATCH] home: Add home-stow-migration-service Sergey Trofimov
2024-01-29 13:20   ` Ludovic Courtès
2024-01-29 13:40     ` Sergey Trofimov
2024-01-29 14:23       ` Giacomo via Guix-patches via
2024-01-29 15:19         ` Sergey Trofimov
2024-01-29 16:09           ` Giacomo via Guix-patches via
2024-01-29 18:34             ` Sergey Trofimov
2024-02-07 22:17         ` [bug#60521] Dot file layout for ‘home-dotfiles-service’ Ludovic Courtès
2024-02-09  0:44           ` paul via Guix-patches via
2024-02-09  0:45             ` paul via Guix-patches via
2024-02-10 10:03             ` Ludovic Courtès
2024-02-10 10:47               ` Janneke Nieuwenhuizen
2024-01-29 16:10       ` [bug#60521] [PATCH] home: Add home-stow-migration-service Ludovic Courtès
2024-01-27 20:21 ` Sergey Trofimov
2024-01-28 15:37 ` [bug#60521] [PATCH v7] home: Add home-dotfiles-service Giacomo Leidi via Guix-patches via
2024-01-28 21:02   ` bug#60521: " Ludovic Courtès
2024-01-28 21:14     ` [bug#60521] " paul via Guix-patches via
2024-01-28 21:22       ` paul via Guix-patches via

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874jeyjsgy.fsf@gnu.org \
    --to=ludo@gnu.org \
    --cc=60521@debbugs.gnu.org \
    --cc=goodoldpaul@autistici.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.