unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob e4a296588e040269eba4f1deddb5425239beace7 12227 bytes (raw)
name: gnu/home/services/dotfiles.scm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2024 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2024 Giacomo Leidi <goodoldpaul@autistici.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu home services dotfiles)
  #:use-module (gnu home services)
  #:use-module (gnu services)
  #:use-module (gnu services configuration)
  #:autoload   (guix build utils) (find-files)
  #:use-module (guix deprecation)
  #:use-module (guix diagnostics)
  #:use-module (guix gexp)
  #:use-module (guix i18n)
  #:use-module ((guix utils) #:select (current-source-directory source-properties->location))
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 ftw)
  #:use-module (ice-9 match)
  #:use-module (ice-9 regex)
  #:export (home-dotfiles-service-type
            home-dotfiles-configuration->files
            home-dotfiles-environment->files

            home-dotfiles-configuration
            home-dotfiles-configuration?
            home-dotfiles-configuration-fields
            home-dotfiles-configuration-layout
            home-dotfiles-configuration-source-directory
            home-dotfiles-configuration-packages
            home-dotfiles-configuration-directories
            home-dotfiles-configuration-excluded

            home-dotfiles-environment
            home-dotfiles-environment?
            home-dotfiles-environment-fields
            home-dotfiles-environment-source-directory
            home-dotfiles-environment-directories
            home-dotfiles-environment-excluded

            stow-dotfiles-directory
            stow-dotfiles-directory?
            stow-dotfiles-directory-fields
            stow-dotfiles-directory-name
            stow-dotfiles-directory-packages

            plain-dotfiles-directory
            plain-dotfiles-directory?
            plain-dotfiles-directory-fields
            plain-dotfiles-directory-name))

(define %home-dotfiles-excluded
  '(".*~"
    ".*\\.swp"
    "\\.git/.*"
    "\\.gitignore"))

(define (sanitize-layout value)
  (if (member value '(plain stow))
      value
      (raise
       (formatted-message
        (G_ "layout field of home-dotfiles-configuration should be either
'plain or 'stow, but ~a was found.")
        value))))

(define list-of-strings?
  (list-of string?))

(define-maybe list-of-strings)

(define-configuration/no-serialization home-dotfiles-configuration
  (source-directory
   (string (current-source-directory))
   "The path where dotfile directories are resolved.  By default dotfile
directories are resolved relative the source location where
@code{home-dotfiles-configuration} appears.")
  (layout
   (symbol 'plain)
   "The intended layout of the specified @code{directory}.  It can be either
@code{'stow} or @code{'plain}."
   (sanitizer sanitize-layout))
  (directories
   (list-of-strings '())
   "The list of dotfiles directories where @code{home-dotfiles-service-type}
will look for application dotfiles.")
  (packages
   (maybe-list-of-strings)
   "The names of a subset of the GNU Stow package layer directories.  When provided
the @code{home-dotfiles-service-type} will only provision dotfiles from this
subset of applications.  This field will be ignored if @code{layout} is set
to @code{'plain}.")
  (excluded
   (list-of-strings '(".*~" ".*\\.swp" "\\.git" "\\.gitignore"))
   "The list of file patterns @code{home-dotfiles-service-type} will exclude
while visiting @code{directory}."))

(define-configuration/no-serialization plain-dotfiles-directory
  (name
   (string)
   "The path of the dotfiles directory where @code{home-dotfiles-service-type}
will look for application dotfiles."))

(define-configuration/no-serialization stow-dotfiles-directory
  (name
   (string)
   "The path of the dotfiles directory where @code{home-dotfiles-service-type}
will look for application dotfiles.")
  (packages
   (maybe-list-of-strings)
   "The names of a subset of the GNU Stow package layer directories.  When provided
the @code{home-dotfiles-service-type} will only provision dotfiles from this
subset of applications."))

(define (list-of-dotfiles-directories? value)
  (map
   (lambda (record)
     (if (or (plain-dotfiles-directory? record)
             (stow-dotfiles-directory? record))
         value
         (raise
          (formatted-message
           (G_ "directories field of home-dotfiles-environment should be either a
plain-dotfiles-directory or stow-dotfiles-directory record, but ~a was found.")
           record))))
   value))

(define-configuration/no-serialization home-dotfiles-environment
  (source-directory
   (string (current-source-directory))
   "The path where dotfile directories are resolved.  By default dotfile
directories are resolved relative the source location where
@code{home-dotfiles-environment} appears.")
  (directories
   (list-of-dotfiles-directories '())
   "The list of dotfiles directories where @code{home-dotfiles-service-type}
will look for application dotfiles.")
  (excluded
   (list-of-strings %home-dotfiles-excluded)
   "The list of file patterns @code{home-dotfiles-service-type} will exclude
while visiting each one of the @code{directories}."))

(define (strip-stow-dotfile file-name directory)
  (let ((dotfile-name (string-drop file-name (1+ (string-length directory)))))
    (match (string-split dotfile-name #\/)
      ((package parts ...)
       (string-join parts "/")))))

(define (strip-plain-dotfile file-name directory)
  (string-drop file-name (+ 1 (string-length directory))))

(define (import-dotfiles directory files strip)
  "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 (format file)
    ;; 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 '(#\. #\/ #\space))))
                       chr
                       #\-))
                 file)))

  (map (lambda (file)
         (let ((stripped (strip file directory)))
           (list stripped
                 (local-file file (format stripped)
                             #:recursive? #t))))
       files))

;; This procedure exists only to avoid the deprecation
;; warning when compiling home-dotfiles-service-files.
;; Once the deprecation period is over this internal procedure
;; can be removed, together with home-dotfiles-service-files
;; and home-dotfiles-configuration->files.
(define (home-dotfiles-configuration->files/internal config)
  (warning (G_ "'~a' is deprecated, use '~a' instead~%")
           'home-dotfiles-configuration 'home-dotfiles-environment)
  (define stow? (eq? (home-dotfiles-configuration-layout config) 'stow))
  (define excluded
    (home-dotfiles-configuration-excluded config))
  (define exclusion-rx
    (make-regexp (string-append "^.*(" (string-join excluded "|") ")$")))

  (define* (directory-contents directory #:key (packages #f))
    (define (filter-files directory)
      (find-files directory
                  (lambda (file stat)
                    (not (regexp-exec exclusion-rx
                                      (basename file))))))
    (if (and stow? packages (maybe-value-set? packages))
        (append-map filter-files
                    (map (lambda (pkg)
                           (string-append directory "/" pkg))
                         packages))
        (filter-files directory)))

  (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))
                       (packages
                        (home-dotfiles-configuration-packages config))
                       (contents
                        (directory-contents directory
                                            #:packages packages))
                       (strip
                        (if stow? strip-stow-dotfile strip-plain-dotfile)))
                  (import-dotfiles directory contents strip)))
              (home-dotfiles-configuration-directories config)))

(define-deprecated (home-dotfiles-configuration->files config)
  home-dotfiles-environment->files
  (home-dotfiles-configuration->files/internal config))

(define (home-dotfiles-environment->files config)
  "Return a list of objects compatible with @code{home-files-service-type}'s
value, excluding files that match any of the patterns configured."
  (define excluded
    (home-dotfiles-environment-excluded config))
  (define exclusion-rx
    (make-regexp (string-append "^.*(" (string-join excluded "|") ")$")))

  (define* (directory-contents directory #:key (stow? #f) (packages #f))
    (define (filter-files directory)
      (find-files directory
                  (lambda (file stat)
                    (not (regexp-exec exclusion-rx file)))))
    (if (and stow? packages (maybe-value-set? packages))
        (append-map filter-files
                    (map (lambda (pkg)
                           (string-append directory "/" pkg))
                         packages))
        (filter-files directory)))

  (define (resolve directory)
    ;; Resolve DIRECTORY relative to the 'source-directory' field of CONFIG.
    (if (string-prefix? "/" directory)
        directory
        (in-vicinity (home-dotfiles-environment-source-directory config)
                     directory)))

  (append-map (lambda (record)
                (let* ((stow? (stow-dotfiles-directory? record))
                       (name
                        (if stow?
                            (stow-dotfiles-directory-name record)
                            (plain-dotfiles-directory-name record)))
                       (directory (resolve name))
                       (packages
                        (and stow?
                             (stow-dotfiles-directory-packages record)))
                       (contents
                        (directory-contents directory
                                            #:stow? stow?
                                            #:packages packages))
                       (strip
                        (if stow? strip-stow-dotfile strip-plain-dotfile)))
                  (import-dotfiles directory contents strip)))
              (home-dotfiles-environment-directories config)))

(define (home-dotfiles-service-files config)
  (if (home-dotfiles-environment? config)
      (home-dotfiles-environment->files config)
      (home-dotfiles-configuration->files/internal config)))

(define-public home-dotfiles-service-type
  (service-type (name 'home-dotfiles)
                (extensions
                 (list (service-extension home-files-service-type
                                          home-dotfiles-service-files)))
                (default-value (home-dotfiles-environment))
                (description "Files that will be put in the user's home directory
following GNU Stow's algorithm, and further processed during activation.")))

debug log:

solving e4a296588e ...
found e4a296588e in https://yhetil.org/guix-patches/cc1befa8e426e98ae7803e94aa7186f89a49b15b.1733004187.git.goodoldpaul@autistici.org/
found 823bdb03fb in https://git.savannah.gnu.org/cgit/guix.git
preparing index
index prepared:
100644 823bdb03fb475e924af65cdcb0664639b94a8788	gnu/home/services/dotfiles.scm

applying [1/1] https://yhetil.org/guix-patches/cc1befa8e426e98ae7803e94aa7186f89a49b15b.1733004187.git.goodoldpaul@autistici.org/
diff --git a/gnu/home/services/dotfiles.scm b/gnu/home/services/dotfiles.scm
index 823bdb03fb..e4a296588e 100644

Checking patch gnu/home/services/dotfiles.scm...
Applied patch gnu/home/services/dotfiles.scm cleanly.

index at:
100644 e4a296588e040269eba4f1deddb5425239beace7	gnu/home/services/dotfiles.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).