guix: Add ability to convert "dotfiles" to home services. v1 PROPOSED

Xinglu Chen: 1
 guix: Add ability to convert "dotfiles" to home services.

 2 files changed, 78 insertions(+), 14 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~abcdw/rde-devel/patches/23603/mbox | git am -3
Learn more about email & git
View this thread in the archives

[RFC PATCH] guix: Add ability to convert "dotfiles" to home services. Export this patch

* guix/home-services-import.scm: New file.
* guix/scripts/home.scm (manifest->code): Generate <home-environment>
that contains home services.
(home-environment-template): Add ‘services’ keyword argument.
This will automatically generate the ‘services’ field in the
‘home-environment’ record and generate the appropriate service
configurations based on dotfiles found in ~/.  It will also
automatically import the needed modules for the services, so the user
doesn’t have to go hunt for the correct to import.

For now, only Bash is supported; more services will hopefully come in
the future.  :)

Try it out:

--8<---------------cut here---------------start------------->8---
$ ./pre-inst-env guix home import
;; This "home-environment" file can be passed to 'guix home reconfigure'
;; to reproduce the content of your profile.  This is "symbolic": it only
;; specifies package names.  To reproduce the exact same profile, you also
;; need to capture the channels being used, as returned by "guix describe".
;; See the "Replicating Guix" section in the manual.

  (gnu home)
  (gnu packages)
  (gnu home-services bash))

    (map specification->package
         (list "glibc-locales" "nss-certs" "nss")))
    (list (service
                (list (slurp-file-gexp
                        (local-file "/home/yoctocell/.bashrc"))))
                (list (slurp-file-gexp
                        (local-file "/home/yoctocell/.bash_profile")))))))))
--8<---------------cut here---------------end--------------->8---

 guix/home-services-import.scm | 53 +++++++++++++++++++++++++++++++++++
 guix/scripts/home.scm         | 39 +++++++++++++++++---------
 2 files changed, 78 insertions(+), 14 deletions(-)
 create mode 100644 guix/home-services-import.scm

diff --git a/guix/home-services-import.scm b/guix/home-services-import.scm
new file mode 100644
index 0000000..c6a1b1d
--- /dev/null
+++ b/guix/home-services-import.scm
@@ -0,0 +1,53 @@
(define-module (guix home-services-import)
  #:use-module (gnu home-services-utils)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:export (modules+configurations))

;;; Commentary:
;;; This module provides utilities for generating home service
;;; configurations from existing "dotfiles".
;;; Code:

(define (generate-bash-module+configuration)
  (let ((rc (string-append (getenv "HOME") "/.bashrc"))
        (profile (string-append (getenv "HOME") "/.bash_profile"))
        (logout (string-append (getenv "HOME") "/.bash_logout")))
    `((gnu home-services bash)
      (service home-bash-service-type
                  ,@(optional (file-exists? rc)
                                 (list (slurp-file-gexp (local-file ,rc))))))
                  ,@(optional (file-exists? profile)
                                 (list (slurp-file-gexp
                                        (local-file ,profile))))))
                  ,@(optional (file-exists? logout)
                                 (list (slurp-file-gexp
                                        (local-file ,logout)))))))))))

(define %files-configurations-alist
  `((".bashrc" . ,generate-bash-module+configuration)
    (".bash_profile" . ,generate-bash-module+configuration)
    (".bash_logout" . ,generate-bash-module+configuration)))

(define (modules+configurations)
  (let ((configurations (delete-duplicates
                         (filter-map (match-lambda
                                ((file . proc)
                                 (if (file-exists?
                                      (string-append (getenv "HOME") "/" file))
                         (lambda (x y)
                           (equal? (procedure-name x) (procedure-name y))))))
    (map (lambda (proc) (proc)) configurations)))
diff --git a/guix/scripts/home.scm b/guix/scripts/home.scm
index 5cb6328..3ced74c 100644
--- a/guix/scripts/home.scm
+++ b/guix/scripts/home.scm
@@ -4,6 +4,8 @@
  #:use-module (gnu packages)
  #:use-module (gnu home)
  #:use-module (gnu home-services)
  #:use-module ((gnu home-services-utils) #:select (optional))
  #:use-module (guix home-services-import)
  #:use-module (guix channels)
  #:autoload   (guix scripts pull) (channel-commit-hyperlink)
  #:use-module (guix derivations)
@@ -537,10 +539,14 @@ available."
                                                   ":" output))))
                        (manifest-entries manifest))))
        (if home-environment?
            (let ((modules+configurations (modules+configurations)))
               (use-modules (gnu home)
                            (gnu packages))
               ,(home-environment-template #:specs specs))
                            (gnu packages)
                            ,@(map first modules+configurations))
                 #:specs specs
                 #:services (map second modules+configurations))))
               (use-modules (gnu packages))

@@ -575,14 +581,18 @@ available."
                             (options->transformation ',options))))
        (if home-environment?
               (use-modules (guix transformations)
                            (gnu home)
                            (gnu packages))


               ,(home-environment-template #:packages packages))
            (let ((modules+configurations (modules+configurations)))
                 (use-modules (guix transformations)
                              (gnu home)
                              (gnu packages)
                              ,@(map first modules+configurations))


                   #:packages packages
                   #:services (map second modules+configurations))))
               (use-modules (guix transformations)
                            (gnu packages))
@@ -592,15 +602,16 @@ available."
                 (list ,@packages)))))))

(define* (home-environment-template #:key (packages #f) (specs #f))
(define* (home-environment-template #:key (packages #f) (specs #f) services)
  "Return an S-exp containing a <home-environment> declaration
containing PACKAGES, or SPECS (package specifications)."
containing PACKAGES, or SPECS (package specifications), and SERVICES."
      ,@(if packages
            `((list ,@packages))
            `((map specification->package
               (list ,@specs)))))))
                   (list ,@specs)))))
     (services (list ,@services))))

(define* (import-manifest

base-commit: d56810ad8f4d24000872a9ba25dcf3d7e13e320c
Xinglu Chen <public@yoctocell.xyz> writes: