aboutsummaryrefslogtreecommitdiff
path: root/gnu/home/services
diff options
context:
space:
mode:
authorLiliana Marie Prikler <liliana.prikler@gmail.com>2024-03-09 07:46:25 +0100
committerLiliana Marie Prikler <liliana.prikler@gmail.com>2024-03-09 07:46:25 +0100
commit058532bdd8953feefbd84fd7514822886c7f1a7e (patch)
treedd29b23b04df1e30a03b93d53a339f6fcd817285 /gnu/home/services
parent19fc252ab7e86ad1443a8d16f68467c61bf23179 (diff)
parent1bc05c6f6d56ad9e0e31d7f1cc75545a65e3d0f3 (diff)
downloadguix-058532bdd8953feefbd84fd7514822886c7f1a7e.tar.gz
guix-058532bdd8953feefbd84fd7514822886c7f1a7e.zip
Merge branch 'master' into emacs-team
Diffstat (limited to 'gnu/home/services')
-rw-r--r--gnu/home/services/dotfiles.scm115
1 files changed, 88 insertions, 27 deletions
diff --git a/gnu/home/services/dotfiles.scm b/gnu/home/services/dotfiles.scm
index 6a740c42ce..823bdb03fb 100644
--- a/gnu/home/services/dotfiles.scm
+++ b/gnu/home/services/dotfiles.scm
@@ -20,17 +20,25 @@
(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 diagnostics)
#:use-module (guix gexp)
- #:use-module (guix records)
+ #:use-module (guix i18n)
#:use-module ((guix utils) #:select (current-source-directory))
#: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-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))
@@ -40,26 +48,64 @@
"\\.git"
"\\.gitignore"))
-(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 files)
+(define %home-dotfiles-layouts
+ '(plain stow))
+
+(define (sanitize-layout value)
+ (if (member value %home-dotfiles-layouts)
+ 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 %home-dotfiles-excluded)
+ "The list of file patterns @code{home-dotfiles-service-type} will exclude
+while visiting @code{directory}."))
+
+(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 (strip file)
- (string-drop file (+ 1 (string-length directory))))
-
(define (format file)
;; Remove from FILE characters that cannot be used in the store.
(string-append
@@ -73,7 +119,7 @@ user's home directory, excluding files that match any of the patterns in EXCLUDE
file)))
(map (lambda (file)
- (let ((stripped (strip file)))
+ (let ((stripped (strip file directory)))
(list stripped
(local-file file (format stripped)
#:recursive? #t))))
@@ -81,18 +127,25 @@ user's home directory, excluding files that match any of the patterns in EXCLUDE
(define (home-dotfiles-configuration->files config)
"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."
+value, excluding files that match any of the patterns configured."
+ (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)
- (find-files directory
- (lambda (file stat)
- (not (regexp-exec exclusion-rx
- (basename file))))))
+ (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.
@@ -103,15 +156,23 @@ directories in CONFIG, excluding files that match any of the patterns configured
(append-map (lambda (directory)
(let* ((directory (resolve directory))
- (contents (directory-contents directory)))
- (import-dotfiles directory contents)))
+ (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-public home-dotfiles-service-type
(service-type (name 'home-dotfiles)
(extensions
(list (service-extension home-files-service-type
- home-dotfiles-configuration->files)))
+ (lambda (config)
+ (when config
+ (home-dotfiles-configuration->files config))))))
(default-value (home-dotfiles-configuration))
(description "Files that will be put in the user's home directory
following GNU Stow's algorithm, and further processed during activation.")))