projects/guix·blob python-team · refs

180 lines · 7.3 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2024 Ludovic Courtès <[email protected]>
3;;; Copyright © 2024, 2026 Giacomo Leidi <[email protected]>
4;;; Copyright © 2025 Brice Waegeneire <[email protected]>
5;;;
6;;; This file is part of GNU Guix.
7;;;
8;;; GNU Guix is free software; you can redistribute it and/or modify it
9;;; under the terms of the GNU General Public License as published by
10;;; the Free Software Foundation; either version 3 of the License, or (at
11;;; your option) any later version.
12;;;
13;;; GNU Guix is distributed in the hope that it will be useful, but
14;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; You should have received a copy of the GNU General Public License
19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21(define-module (gnu home services dotfiles)
22 #:use-module (gnu home services)
23 #:use-module (gnu services)
24 #:use-module (gnu services configuration)
25 #:autoload (guix build utils) (find-files)
26 #:use-module (guix diagnostics)
27 #:use-module (guix gexp)
28 #:use-module (guix i18n)
29 #:use-module ((guix utils) #:select (current-source-directory))
30 #:use-module (srfi srfi-1)
31 #:use-module (ice-9 ftw)
32 #:use-module (ice-9 match)
33 #:use-module (ice-9 regex)
34 #:export (home-dotfiles-service-type
35 home-dotfiles-configuration->files
36
37 home-dotfiles-configuration
38 home-dotfiles-configuration?
39 home-dotfiles-configuration-fields
40 home-dotfiles-configuration-layout
41 home-dotfiles-configuration-source-directory
42 home-dotfiles-configuration-packages
43 home-dotfiles-configuration-directories
44 home-dotfiles-configuration-excluded))
45
46(define %home-dotfiles-excluded
47 '(".*~"
48 ".*\\.swp"
49 ;; This first / is safe until regexp-exec in directory-contents
50 ;; is passed an absolute file name.
51 "/\\.git/.*"
52 "\\.gitignore"))
53
54(define %home-dotfiles-layouts
55 '(plain stow))
56
57(define (sanitize-layout value)
58 (if (member value %home-dotfiles-layouts)
59 value
60 (raise
61 (formatted-message
62 (G_ "layout field of home-dotfiles-configuration should be either 'plain
63or 'stow, but ~a was found.")
64 value))))
65
66(define-maybe/no-serialization list-of-strings)
67
68(define-configuration/no-serialization home-dotfiles-configuration
69 (source-directory
70 (string (current-source-directory))
71 "The path where dotfile directories are resolved. By default dotfile
72directories are resolved relative the source location where
73@code{home-dotfiles-configuration} appears.")
74 (layout
75 (symbol 'plain)
76 "The intended layout of the specified @code{directory}. It can be either
77@code{'stow} or @code{'plain}."
78 (sanitizer sanitize-layout))
79 (directories
80 (list-of-strings '())
81 "The list of dotfiles directories where @code{home-dotfiles-service-type}
82will look for application dotfiles.")
83 (packages
84 (maybe-list-of-strings)
85 "The names of a subset of the GNU Stow package layer directories. When provided
86the @code{home-dotfiles-service-type} will only provision dotfiles from this
87subset of applications. This field will be ignored if @code{layout} is set
88to @code{'plain}.")
89 (excluded
90 (list-of-strings %home-dotfiles-excluded)
91 "The list of file patterns @code{home-dotfiles-service-type} will exclude
92while visiting @code{directory}."))
93
94(define (strip-stow-dotfile file-name directory)
95 (let ((dotfile-name (string-drop file-name (1+ (string-length directory)))))
96 (match (string-split dotfile-name #\/)
97 ((package parts ...)
98 (string-join parts "/")))))
99
100(define (strip-plain-dotfile file-name directory)
101 (string-drop file-name (+ 1 (string-length directory))))
102
103(define (import-dotfiles directory files strip)
104 "Return a list of objects compatible with @code{home-files-service-type}'s
105value. Each object is a pair where the first element is the relative path
106of a file and the second is a gexp representing the file content. Objects are
107generated by recursively visiting DIRECTORY and mapping its contents to the
108user's home directory, excluding files that match any of the patterns in EXCLUDED."
109 (define (format file)
110 ;; Remove from FILE characters that cannot be used in the store.
111 (string-append
112 "home-dotfiles-"
113 (string-map (lambda (chr)
114 (if (and (char-set-contains? char-set:ascii chr)
115 (char-set-contains? char-set:graphic chr)
116 (not (memv chr '(#\. #\/ #\space))))
117 chr
118 #\-))
119 file)))
120
121 (map (lambda (file)
122 (let ((stripped (strip file directory)))
123 (list stripped
124 (local-file file (format stripped)
125 #:recursive? #t))))
126 files))
127
128(define (home-dotfiles-configuration->files config)
129 "Return a list of objects compatible with @code{home-files-service-type}'s
130value, excluding files and directories that match any of the patterns configured."
131 (define stow? (eq? (home-dotfiles-configuration-layout config) 'stow))
132 (define excluded
133 (home-dotfiles-configuration-excluded config))
134 (define exclusion-rx
135 (make-regexp (string-append "^.*(" (string-join excluded "|") ")$")))
136 (define* (excluded-file? file stat #:optional result)
137 (not (regexp-exec exclusion-rx file)))
138
139 (define* (directory-contents directory #:key (packages #f))
140 (define (filter-files directory)
141 (find-files directory
142 excluded-file?
143 #:enter?
144 excluded-file?))
145 (if (and stow? packages (maybe-value-set? packages))
146 (append-map filter-files
147 (map (lambda (pkg)
148 (string-append directory "/" pkg))
149 packages))
150 (filter-files directory)))
151
152 (define (resolve directory)
153 ;; Resolve DIRECTORY relative to the 'source-directory' field of CONFIG.
154 (if (string-prefix? "/" directory)
155 directory
156 (in-vicinity (home-dotfiles-configuration-source-directory config)
157 directory)))
158
159 (append-map (lambda (directory)
160 (let* ((directory (resolve directory))
161 (packages
162 (home-dotfiles-configuration-packages config))
163 (contents
164 (directory-contents directory
165 #:packages packages))
166 (strip
167 (if stow? strip-stow-dotfile strip-plain-dotfile)))
168 (import-dotfiles directory contents strip)))
169 (home-dotfiles-configuration-directories config)))
170
171(define-public home-dotfiles-service-type
172 (service-type (name 'home-dotfiles)
173 (extensions
174 (list (service-extension home-files-service-type
175 (lambda (config)
176 (when config
177 (home-dotfiles-configuration->files config))))))
178 (default-value (home-dotfiles-configuration))
179 (description "Files that will be put in the user's home directory
180following GNU Stow's algorithm, and further processed during activation.")))