~abcdw/rde-devel

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

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

Details
Message ID
<0be9fdd26dee553232c4daca98e3bad5007e2660.1625409903.git.public@yoctocell.xyz>
DKIM signature
pass
Download raw message
Patch: +78 -14
* 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.

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

(home-environment
  (packages
    (map specification->package
         (list "glibc-locales" "nss-certs" "nss")))
  (services
    (list (service
            home-bash-service-type
            (home-bash-configuration
              (bashrc
                (list (slurp-file-gexp
                        (local-file "/home/yoctocell/.bashrc"))))
              (bash-profile
                (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
                 (home-bash-configuration
                  ,@(optional (file-exists? rc)
                              `((bashrc
                                 (list (slurp-file-gexp (local-file ,rc))))))
                  ,@(optional (file-exists? profile)
                              `((bash-profile
                                 (list (slurp-file-gexp
                                        (local-file ,profile))))))
                  ,@(optional (file-exists? logout)
                              `((bash-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))
                                     proc
                                     #f)))
                                     %files-configurations-alist)
                         (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?
            `(begin
            (let ((modules+configurations (modules+configurations)))
              `(begin
               (use-modules (gnu home)
                            (gnu packages))
               ,(home-environment-template #:specs specs))
                            (gnu packages)
                            ,@(map first modules+configurations))
               ,(home-environment-template
                 #:specs specs
                 #:services (map second modules+configurations))))
            `(begin
               (use-modules (gnu packages))

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

               ,@transformations

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

                 ,@transformations

                 ,(home-environment-template
                   #:packages packages
                   #:services (map second modules+configurations))))
            `(begin
               (use-modules (guix transformations)
                            (gnu packages))
@@ -592,15 +602,16 @@ available."
                (packages->manifest
                 (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."
  `(home-environment
     (packages
      ,@(if packages
            `((list ,@packages))
            `((map specification->package
               (list ,@specs)))))))
                   (list ,@specs)))))
     (services (list ,@services))))

(define* (import-manifest
          manifest

base-commit: d56810ad8f4d24000872a9ba25dcf3d7e13e320c
-- 
2.32.0
Details
Message ID
<87eec6rajf.fsf@trop.in>
In-Reply-To
<0be9fdd26dee553232c4daca98e3bad5007e2660.1625409903.git.public@yoctocell.xyz> (view parent)
DKIM signature
missing
Download raw message
Xinglu Chen <public@yoctocell.xyz> writes:

> * 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.
>
> (use-modules
>   (gnu home)
>   (gnu packages)
>   (gnu home-services bash))
>
> (home-environment
>   (packages
>     (map specification->package
>          (list "glibc-locales" "nss-certs" "nss")))
>   (services
>     (list (service
>             home-bash-service-type
>             (home-bash-configuration
>               (bashrc
>                 (list (slurp-file-gexp
>                         (local-file "/home/yoctocell/.bashrc"))))
>               (bash-profile
>                 (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)
Some applications can require more than 1 import.
> +      (service home-bash-service-type
> +                 (home-bash-configuration
> +                  ,@(optional (file-exists? rc)
> +                              `((bashrc
> +                                 (list (slurp-file-gexp (local-file ,rc))))))
> +                  ,@(optional (file-exists? profile)
> +                              `((bash-profile
> +                                 (list (slurp-file-gexp
> +                                        (local-file ,profile))))))
> +                  ,@(optional (file-exists? logout)
> +                              `((bash-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))
> +                                     proc
> +                                     #f)))
> +                                     %files-configurations-alist)
> +                         (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?
> -            `(begin
> +            (let ((modules+configurations (modules+configurations)))
> +              `(begin
>                 (use-modules (gnu home)
> -                            (gnu packages))
> -               ,(home-environment-template #:specs specs))
> +                            (gnu packages)
> +                            ,@(map first modules+configurations))
> +               ,(home-environment-template
> +                 #:specs specs
> +                 #:services (map second modules+configurations))))
>              `(begin
>                 (use-modules (gnu packages))
>  
> @@ -575,14 +581,18 @@ available."
>                               (options->transformation ',options))))
>                         transformation-procedures)))
>          (if home-environment?
> -            `(begin
> -               (use-modules (guix transformations)
> -                            (gnu home)
> -                            (gnu packages))
> -
> -               ,@transformations
> -
> -               ,(home-environment-template #:packages packages))
> +            (let ((modules+configurations (modules+configurations)))
> +              `(begin
> +                 (use-modules (guix transformations)
> +                              (gnu home)
> +                              (gnu packages)
> +                              ,@(map first modules+configurations))
> +
> +                 ,@transformations
> +
> +                 ,(home-environment-template
> +                   #:packages packages
> +                   #:services (map second modules+configurations))))
>              `(begin
>                 (use-modules (guix transformations)
>                              (gnu packages))
> @@ -592,15 +602,16 @@ available."
>                  (packages->manifest
>                   (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."
>    `(home-environment
>       (packages
>        ,@(if packages
>              `((list ,@packages))
>              `((map specification->package
> -               (list ,@specs)))))))
> +                   (list ,@specs)))))
> +     (services (list ,@services))))
>  
>  (define* (import-manifest
>            manifest
>
> base-commit: d56810ad8f4d24000872a9ba25dcf3d7e13e320c

Overall looks good, applied.  I'll review it more preciesely, when will
be upstreaming home.scm to Guix.

Thank you for the patch)
Reply to thread Export thread (mbox)