| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 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 | |
| 50 | |
| 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
|
| 63 | or '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
|
| 72 | directories 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}
|
| 82 | will 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
|
| 86 | the @code{home-dotfiles-service-type} will only provision dotfiles from this
|
| 87 | subset of applications. This field will be ignored if @code{layout} is set
|
| 88 | to @code{'plain}.")
|
| 89 | (excluded
|
| 90 | (list-of-strings %home-dotfiles-excluded)
|
| 91 | "The list of file patterns @code{home-dotfiles-service-type} will exclude
|
| 92 | while 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
|
| 105 | value. Each object is a pair where the first element is the relative path
|
| 106 | of a file and the second is a gexp representing the file content. Objects are
|
| 107 | generated by recursively visiting DIRECTORY and mapping its contents to the
|
| 108 | user's home directory, excluding files that match any of the patterns in EXCLUDED."
|
| 109 | (define (format file)
|
| 110 | |
| 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
|
| 130 | value, 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 | |
| 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
|
| 180 | following GNU Stow's algorithm, and further processed during activation.")))
|