unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob fbf89069a760f9a78d830d43044f83a365f914ea 7651 bytes (raw)
name: guix/scripts/home/import.scm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
;;; Copyright © 2021 Andrew Tropin <andrew@trop.in>
;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guix scripts home import)
  #:use-module (guix profiles)
  #:use-module (guix ui)
  #:use-module (guix utils)
  #:use-module (guix packages)
  #:autoload   (guix scripts package) (manifest-entry-version-prefix)
  #:use-module (gnu packages)
  #:use-module (ice-9 match)
  #:use-module (ice-9 pretty-print)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 popen)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:export (import-manifest

            ;; For tests.
            manifest+configuration-files->code))

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

(define (basename+remove-dots file-name)
  "Remove the dot from the dotfile FILE-NAME; replace the other dots in
FILE-NAME with \"-\", and return the basename of it."
  (string-map (match-lambda
                (#\. #\-)
                (c c))
              (let ((base (basename file-name)))
                (if (string-prefix? "." base)
                    (string-drop base 1)
                    base))))

(define (generate-bash-configuration+modules destination-directory)
  (define (destination-append path)
    (string-append destination-directory "/" path))

  (define (bash-alias->pair line)
    (if (string-prefix? "alias" line)
        (let ((matched (string-match "alias (.+)=\"?'?([^\"']+)\"?'?" line)))
          `(,(match:substring matched 1) . ,(match:substring matched 2)))
        '()))
  
  (define (parse-aliases input)
    (let loop ((line (read-line input))
               (result '()))
      (if (eof-object? line)
          (reverse result)
          (loop (read-line input)
                (cons (bash-alias->pair line) result)))))

  (let ((rc (destination-append ".bashrc"))
        (profile (destination-append ".bash_profile"))
        (logout (destination-append ".bash_logout")))
    `((service home-bash-service-type
               (home-bash-configuration
                ,@(if (file-exists? rc)
                      `((aliases
                         ',(let* ((port (open-pipe* OPEN_READ "bash" "-i" "-c" "alias"))
                               (alist (parse-aliases port)))
                           (close-port port)
                           (filter (negate null?) alist))))
                      '())
                ,@(if (file-exists? rc)
                      `((bashrc
                         (list (local-file ,rc
                                           ,(basename+remove-dots rc)))))
                      '())
                ,@(if (file-exists? profile)
                      `((bash-profile
                         (list (local-file ,profile
                                           ,(basename+remove-dots profile)))))
                      '())
                ,@(if (file-exists? logout)
                      `((bash-logout
                         (list (local-file ,logout
                                           ,(basename+remove-dots logout)))))
                      '())))
      (guix gexp)
      (gnu home services shells))))

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

(define (configurations+modules configuration-directory)
  "Return a list of procedures which when called, generate code for a home
service declaration.  Copy configuration files to CONFIGURATION-DIRECTORY; the
generated service declarations will refer to those files that have been saved
in CONFIGURATION-DIRECTORY."
  (define configurations
    (delete-duplicates
     (filter-map (match-lambda
                   ((file . proc)
                    (let ((absolute-path (string-append (getenv "HOME")
                                                        "/" file)))
                      (and (file-exists? absolute-path)
                           (begin
                             (copy-file absolute-path
                                        (string-append
                                         configuration-directory "/" file))
                             proc)))))
                 %files+configurations-alist)
     eq?))

  (map (lambda (proc) (proc configuration-directory)) configurations))

(define (manifest+configuration-files->code manifest
                                            configuration-directory)
  "Read MANIFEST and the user's configuration files listed in
%FILES+CONFIGURATIONS-ALIST, and return a 'home-environment' sexp.  Copy the
user's files to CONFIGURATION-DIRECTORY; the generated sexp refers to them."
  (match (manifest->code manifest
                         #:entry-package-version
                         manifest-entry-version-prefix)
    (('begin ('use-modules profile-modules ...)
             definitions ... ('packages->manifest packages))
     (match (configurations+modules configuration-directory)
       (((services . modules) ...)
        `(begin
           (use-modules (gnu home)
                        (gnu packages)
                        (gnu services)
                        ,@(delete-duplicates
                           (append profile-modules (concatenate modules))))

           ,@definitions

           (home-environment
            (packages ,packages)
            (services (list ,@services)))))))
    (('begin ('specifications->manifest packages))
     (match (configurations+modules configuration-directory)
       (((services . modules) ...)
        `(begin
           (use-modules (gnu home)
                        (gnu packages)
                        (gnu services)
                        ,@(delete-duplicates (concatenate modules)))

           (home-environment
            (packages (map specification->package ,packages))
            (services (list ,@services)))))))))

(define* (import-manifest
          manifest destination-directory
          #:optional (port (current-output-port)))
  "Write to PORT a <home-environment> corresponding to MANIFEST."
  (match (manifest+configuration-files->code manifest
                                             destination-directory)
    (('begin exp ...)
     (format port (G_ "\
;; 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.\n"))
     (for-each (lambda (exp)
                 (newline port)
                 (pretty-print exp port))
               exp))))

debug log:

solving fbf89069a7 ...
found fbf89069a7 in https://git.savannah.gnu.org/cgit/guix.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).