From patchwork Fri Feb 16 17:17:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Giacomo Leidi X-Patchwork-Id: 60439 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id C80FE27BBE9; Fri, 16 Feb 2024 17:18:56 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 190A427BBE2 for ; Fri, 16 Feb 2024 17:18:54 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rb1rI-0005HY-5k; Fri, 16 Feb 2024 12:18:44 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rb1rG-0005HA-Nz for guix-patches@gnu.org; Fri, 16 Feb 2024 12:18:42 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rb1rG-0003b8-FG for guix-patches@gnu.org; Fri, 16 Feb 2024 12:18:42 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rb1rZ-0001q9-Pd for guix-patches@gnu.org; Fri, 16 Feb 2024 12:19:01 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#68857] [PATCH v2] gnu: home: dotfiles: Properly support both plain and Stow directory layouts. References: <14bc720d-7fdc-516a-3018-58294c1cafcd@autistici.org> In-Reply-To: <14bc720d-7fdc-516a-3018-58294c1cafcd@autistici.org> Resent-From: Giacomo Leidi Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 16 Feb 2024 17:19:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68857 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: To: 68857@debbugs.gnu.org Cc: Giacomo Leidi Received: via spool by 68857-submit@debbugs.gnu.org id=B68857.17081038946986 (code B ref 68857); Fri, 16 Feb 2024 17:19:01 +0000 Received: (at 68857) by debbugs.gnu.org; 16 Feb 2024 17:18:14 +0000 Received: from localhost ([127.0.0.1]:60162 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rb1qn-0001oY-8B for submit@debbugs.gnu.org; Fri, 16 Feb 2024 12:18:14 -0500 Received: from confino.investici.org ([93.190.126.19]:28607) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rb1qk-0001oJ-Fk for 68857@debbugs.gnu.org; Fri, 16 Feb 2024 12:18:11 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=autistici.org; s=stigmate; t=1708103870; bh=KiRu0SH/JHFERLusnyhuKpHkly0uD3LO7KTuOx0iPQc=; h=From:To:Cc:Subject:Date:From; b=GjFk7X3ZIUhzI4GOIaq6yxLBu37PtzpqhsVWgG12Shjtjzq/gV/yI2vkbCtOe5aWQ 7QDFaIT91zf3pJ9tqwzGhYdW0eyzW/7kxWSsSVjtq3dcpWUU2giEOmeLCP1DYV4eGb nVEfhlYEJzZwHIlqG8WZeAvvS+A2hffemm1lRE5A= Received: from mx1.investici.org (unknown [127.0.0.1]) by confino.investici.org (Postfix) with ESMTP id 4TbzCL3gfLz1146; Fri, 16 Feb 2024 17:17:50 +0000 (UTC) Received: from [93.190.126.19] (mx1.investici.org [93.190.126.19]) (Authenticated sender: goodoldpaul@autistici.org) by localhost (Postfix) with ESMTPSA id 4TbzCL2vf2z1144; Fri, 16 Feb 2024 17:17:50 +0000 (UTC) Date: Fri, 16 Feb 2024 18:17:19 +0100 Message-ID: <20240216171724.22171-1-goodoldpaul@autistici.org> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Giacomo Leidi X-ACL-Warn: , Giacomo Leidi via Guix-patches X-Patchwork-Original-From: Giacomo Leidi via Guix-patches via From: Giacomo Leidi Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches The current implementation of the home-dotfiles-service-type contradicts the Guix manual (see https://issues.guix.gnu.org/68848 ). This patch properly implements both the plain and Stow dotfiles directory layouts. It does so by creating a new record type for each directory layout. This approach, compared to having a single field in home-dotfiles-configuration, allows for having different layouts for different directories and to carry additional information that might be required for some layouts. Implementing a new layout should just be a matter of creating a new record type, defining a strip-*-dotfile procedure and plugging them into the home-dotfiles-service-type. * gnu/home/services/dotfiles (home-dotfiles-stow-directory): New variable; (home-dotfiles-plain-directory): new variable; (home-dotfiles-configuration): migrate to (gnu services configuration); (strip-stow-dotfile): new variable; (strip-plain-dotfile): new variable; (home-dotfiles-configuration->files): use the new record types; [directory-contents]: allow for selecting a subset of application dotfile directories; * doc/guix.texi: document the new layouts. Change-Id: I2e96037608353e360828290f055ec5271cfdfd48 --- doc/guix.texi | 147 ++++++++++++++++++++++++++++----- gnu/home/services/dotfiles.scm | 143 ++++++++++++++++++++++++++------ 2 files changed, 244 insertions(+), 46 deletions(-) base-commit: bd87416648929f38c0173f047776d7675ea8a10d diff --git a/doc/guix.texi b/doc/guix.texi index 04119a5955..21adfb1fc0 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -44368,17 +44368,39 @@ directory, and some way of automatically deploy changes to their user home. @cindex Stow-like dot file management The @code{home-dotfiles-service-type} from @code{(gnu home services dotfiles)} is designed to ease the way into using Guix Home for this kind of users, -allowing them to point the service to their dotfiles directory, which must -follow the layout suggested by -@uref{https://www.gnu.org/software/stow/, GNU Stow}, -and have their dotfiles automatically deployed to their user home, without +allowing them to point the service to their dotfiles directory without migrating them to Guix native configurations. -The dotfiles directory layout is expected to be structured as follows. Please -keep in mind that it is advisable to keep your dotfiles directories under +Please keep in mind that it is advisable to keep your dotfiles directories under version control, for example in the same repository where you'd track your Guix Home configuration. +There are two supported dotfiles directory layouts, for now. The +@code{home-dotfiles-plain-directory} layout, which is structured as follows: + +@example +~$ tree -a ./dotfiles/ +dotfiles/ +├── .gitconfig +├── .gnupg +│ ├── gpg-agent.conf +│ └── gpg.conf +├── .guile +├── .config +│ ├── guix +│ │ └── channels.scm +│ └── nixpkgs +│ └── config.nix +├── .nix-channels +├── .tmux.conf +└── .vimrc +@end example + +and the @code{home-dotfiles-stow-directory} layout, which must +follow the layout suggested by +@uref{https://www.gnu.org/software/stow/, GNU Stow} and presents and additional +application specific directory layer, just like: + @example ~$ tree -a ./dotfiles/ dotfiles/ @@ -44408,8 +44430,9 @@ dotfiles/ @end example For an informal specification please refer to the Stow manual -(@pxref{Top,,, stow, Introduction}). A suitable configuration would then -be: +(@pxref{Top,,, stow, Introduction}). + +A suitable configuration with a @emph{plain} layout could be: @lisp (home-environment @@ -44417,7 +44440,10 @@ be: (services (service home-dotfiles-service-type (home-dotfiles-configuration - (directories (list "./dotfiles")))))) + (directories + (list + (home-dotfiles-plain-directory + (name "./dotfiles")))))))) @end lisp The expected home directory state would then be: @@ -44444,32 +44470,113 @@ Return a service which is very similiar to @code{home-files-service-type} (and actually extends it), but designed to ease the way into using Guix Home for users that already track their dotfiles under some kind of version control. This service allows users to point Guix Home to their dotfiles -directory and have their files automatically deployed to their home directory -just like Stow would, without migrating all of their dotfiles to Guix native +directory and have their files automatically provisioned to their home +directory, without migrating all of their dotfiles to Guix native configurations. @end defvar +@c %start of fragment + @deftp {Data Type} home-dotfiles-configuration Available @code{home-dotfiles-configuration} fields are: @table @asis -@item @code{source-directory} (default: @code{(current-source-directory)}) -The path where dotfile directories are resolved. By default dotfile directories -are resolved relative the source location where +@item @code{source-directory} (default: @code{(current-source-directory)}) (type: string) +The path where dotfile directories are resolved. By default dotfile +directories are resolved relative the source location where @code{home-dotfiles-configuration} appears. -@item @code{directories} (type: list-of-strings) -The list of dotfiles directories where @code{home-dotfiles-service-type} will -look for application dotfiles. +@item @code{directories} (default: @code{'()}) (type: list-of-plain-or-stow-directories) +The list of dotfiles directories where @code{home-dotfiles-service-type} +will look for application dotfiles. Each element of the list is supposed to be +either a @code{home-dotfiles-plain-directory} or a +@code{home-dotfiles-stow-directory} record. + +@item @code{excluded} (default: @code{'(".*~" ".*\\.swp" "\\.git" "\\.gitignore")}) (type: list-of-strings) +The list of file patterns @code{home-dotfiles-service-type} will exclude +while visiting each one of the @code{directories}. + +@end table + +@end deftp + + +@c %end of fragment + +@c %start of fragment + +@deftp {Data Type} home-dotfiles-plain-directory + +This record represents the simplest layout supported. In this layout the +dotfiles directory contains a tree of files which will be directly mapped into +the user directory: + +@example +~$ tree -a ./dotfiles/ +dotfiles/ +├── .guile +├── .config +│ └── guix +│ └── channels.scm +├── .tmux.conf +└── .vimrc +@end example + +Available @code{home-dotfiles-plain-directory} fields are: -@item @code{exclude} (default: @code{'(".*~" ".*\\.swp" "\\.git" "\\.gitignore")}) -The list of file patterns @code{home-dotfiles-service-type} will exclude while -visiting each one of the @code{directories}. +@table @asis +@item @code{name} (type: string) +The name of the dotfile directory where +@code{home-dotfiles-service-type} will look for plain dotfiles. @end table @end deftp +@c %start of fragment + +@deftp {Data Type} home-dotfiles-stow-directory + +This record represents GNU Stow's usual layout. In this layout the +dotfiles directory contains a layer of application directories, each one +containing a home file tree. The @code{home-dotfiles-service-type} will +take care of provisioning dotfiles just like Stow would. + +@example +~$ tree -a ./dotfiles/ +dotfiles/ +├── guile +│ └── .guile +├── guix +│ └── .config +│ └── guix +│ └── channels.scm +├── tmux +│ └── .tmux.conf +└── vim + └── .vimrc + +@end example + +Available @code{home-dotfiles-stow-directory} fields are: + +@table @asis +@item @code{name} (type: string) +The name of the dotfile directory where +@code{home-dotfiles-service-type} will look for application dotfiles. + +@item @code{applications} (type: maybe-list-of-strings) +A subset of the names of the application layer directories. When +provided the @code{home-dotfiles-service-type} will only provision +dotfiles from this subset of applications. + +@end table + +@end deftp + + +@c %end of fragment + @defvar home-xdg-configuration-files-service-type The service is very similar to @code{home-files-service-type} (and actually extends it), but used for defining files, which will go to diff --git a/gnu/home/services/dotfiles.scm b/gnu/home/services/dotfiles.scm index 6a740c42ce..ebe9600585 100644 --- a/gnu/home/services/dotfiles.scm +++ b/gnu/home/services/dotfiles.scm @@ -20,16 +20,33 @@ (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-stow-directory + home-dotfiles-stow-directory? + home-dotfiles-stow-directory-fields + home-dotfiles-stow-directory-name + home-dotfiles-stow-directory-applications + + home-dotfiles-plain-directory + home-dotfiles-plain-directory? + home-dotfiles-plain-directory-fields + home-dotfiles-plain-directory-name + home-dotfiles-configuration home-dotfiles-configuration? + home-dotfiles-configuration-fields home-dotfiles-configuration-source-directory home-dotfiles-configuration-directories home-dotfiles-configuration-excluded)) @@ -40,26 +57,79 @@ (define %home-dotfiles-excluded "\\.git" "\\.gitignore")) -(define-record-type* - 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 list-of-strings? + (list-of string?)) + +(define-maybe list-of-strings) + +(define-configuration/no-serialization home-dotfiles-stow-directory + (name + (string) + "The name of the dotfile directory where @code{home-dotfiles-service-type} +will look for application dotfiles.") + (applications + (maybe-list-of-strings) + "A subset of the names of the application layer directories. When provided +the @code{home-dotfiles-service-type} will only provision dotfiles from this +subset of applications.")) + +(define-configuration/no-serialization home-dotfiles-plain-directory + (name + (string) + "The name of the dotfile directory where @code{home-dotfiles-service-type} +will look for plain dotfiles.")) + +(define (list-of-plain-or-stow-directories? value) + (unless (list? value) + (raise + (formatted-message + (G_ "directories field of home-dotfiles-configuration should be a list but +~a was found.") + value))) + (for-each + (lambda (d) + (unless (or (home-dotfiles-plain-directory? d) + (home-dotfiles-stow-directory? d)) + (raise + (formatted-message + (G_ "directories field of home-dotfiles-configuration should contain +only home-dotfiles-plain-directory or home-dotfiles-stow-directory records, but +~a was found.") + d)))) + value)) + +(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.") + (directories + (list-of-plain-or-stow-directories '()) + "The list of dotfiles directories where @code{home-dotfiles-service-type} +will look for application dotfiles. Each element of the list is supposed to be +either a @code{home-dotfiles-plain-directory} or a +@code{home-dotfiles-stow-directory} record.") + (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 (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 +143,7 @@ (define (format file) file))) (map (lambda (file) - (let ((stripped (strip file))) + (let ((stripped (strip file directory))) (list stripped (local-file file (format stripped) #:recursive? #t)))) @@ -88,11 +158,18 @@ (define excluded (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 (applications #f)) + (define (filter-files directory) + (find-files directory + (lambda (file stat) + (not (regexp-exec exclusion-rx + (basename file)))))) + (if applications + (append-map filter-files + (map (lambda (app) + (string-append directory "/" app)) + applications)) + (filter-files directory))) (define (resolve directory) ;; Resolve DIRECTORY relative to the 'source-directory' field of CONFIG. @@ -101,10 +178,24 @@ (define (resolve 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))) + (append-map (lambda (record) + (let* ((name (if (home-dotfiles-plain-directory? record) + (home-dotfiles-plain-directory-name record) + (home-dotfiles-stow-directory-name record))) + (strip (if (home-dotfiles-plain-directory? record) + strip-plain-dotfile + strip-stow-dotfile)) + (applications + (and (home-dotfiles-stow-directory? record) + (let ((apps + (home-dotfiles-stow-directory-applications + record))) + (and (maybe-value-set? apps) apps)))) + (directory (resolve name)) + (contents + (directory-contents directory + #:applications applications))) + (import-dotfiles directory contents strip))) (home-dotfiles-configuration-directories config))) (define-public home-dotfiles-service-type