guix-home-manager/guix/scripts/home.scm

home.scm

1
;;; GNU Guix --- Functional package management for GNU
2
;;; Copyright © 2019 Julien Lepiller <julien@lepiller.eu>
3
;;;
4
;;; This file is part of GNU Guix.
5
;;;
6
;;; GNU Guix is free software; you can redistribute it and/or modify it
7
;;; under the terms of the GNU General Public License as published by
8
;;; the Free Software Foundation; either version 3 of the License, or (at
9
;;; your option) any later version.
10
;;;
11
;;; GNU Guix is distributed in the hope that it will be useful, but
12
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
;;; GNU General Public License for more details.
15
;;;
16
;;; You should have received a copy of the GNU General Public License
17
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
18
19
(define-module (guix scripts home)
20
  #:use-module (guix derivations)
21
  #:use-module (guix grafts)
22
  #:use-module (guix monads)
23
  #:use-module (guix packages)
24
  #:use-module (guix profiles)
25
  #:use-module (guix scripts)
26
  #:use-module (guix scripts build)
27
  #:use-module (guix status)
28
  #:use-module (guix store)
29
  #:use-module (guix ui)
30
  #:use-module (guix utils)
31
  #:use-module (home)
32
  #:use-module (ice-9 match)
33
  #:use-module (srfi srfi-1)
34
  #:use-module (srfi srfi-26)
35
  #:use-module (srfi srfi-35)
36
  #:use-module (srfi srfi-37)
37
  #:export (guix-home))
38
39
;;;
40
;;; Command-line options.
41
;;;
42
43
(define %options
44
  (list (option '(#\h "help") #f #f
45
                (lambda args
46
                  (show-help)
47
                  (exit 0)))
48
        (option '(#\V "version") #f #f
49
                (lambda args
50
                  (show-version-and-exit "guix edit")))))
51
52
(define (show-help)
53
  (display (G_ "Usage: guix home [OPTION ...] ACTION [ARG ...] [FILE]
54
Manage a user home environment according to FILE and ACTION.  Some actions
55
support additional ARGs.\n"))
56
  (display (G_ "The valid values for ACTION are:\n"))
57
  (newline)
58
  (display (G_ "\
59
   reconfigure      switch to or create a new home configuration\n"))
60
  (display (G_ "\
61
   build            build the home configuration without installing anything\n"))
62
  (display (G_ "\
63
   list-generations list the home generations\n"))
64
  (show-build-options-help)
65
  (display (G_ "
66
  -h, --help             display this help and exit"))
67
  (display (G_ "
68
  -V, --version          display version information and exit"))
69
  (newline)
70
  (show-bug-report-information))
71
72
(define %options
73
  ;; Specifications of the command-line options.
74
  (cons* (option '(#\h "help") #f #f
75
                 (lambda args
76
                   (show-help)
77
                   (exit 0)))
78
         (option '(#\V "version") #f #f
79
                 (lambda args
80
                   (show-version-and-exit "guix system")))
81
         (option '(#\e "expression") #t #f
82
                 (lambda (opt name arg result)
83
                   (alist-cons 'expression arg result)))
84
         (option '(#\d "derivation") #f #f
85
                 (lambda (opt name arg result)
86
                   (alist-cons 'derivations-only? #t result)))
87
         (option '("on-error") #t #f
88
                 (lambda (opt name arg result)
89
                   (alist-cons 'on-error (string->symbol arg)
90
                               result)))
91
         (option '(#\t "file-system-type") #t #f
92
                 (lambda (opt name arg result)
93
                   (alist-cons 'file-system-type arg
94
                               result)))
95
         (option '("image-size") #t #f
96
                 (lambda (opt name arg result)
97
                   (alist-cons 'image-size (size->number arg)
98
                               result)))
99
         (option '(#\N "network") #f #f
100
                 (lambda (opt name arg result)
101
                   (alist-cons 'container-shared-network? #t result)))
102
         (option '("no-bootloader" "no-grub") #f #f
103
                 (lambda (opt name arg result)
104
                   (alist-cons 'install-bootloader? #f result)))
105
         (option '("full-boot") #f #f
106
                 (lambda (opt name arg result)
107
                   (alist-cons 'full-boot? #t result)))
108
         (option '("skip-checks") #f #f
109
                 (lambda (opt name arg result)
110
                   (alist-cons 'skip-safety-checks? #t result)))
111
112
         (option '(#\n "dry-run") #f #f
113
                 (lambda (opt name arg result)
114
                   (alist-cons 'dry-run? #t (alist-cons 'graft? #f result))))
115
         (option '(#\v "verbosity") #t #f
116
                 (lambda (opt name arg result)
117
                   (let ((level (string->number* arg)))
118
                     (alist-cons 'verbosity level
119
                                 (alist-delete 'verbosity result)))))
120
         (option '(#\s "system") #t #f
121
                 (lambda (opt name arg result)
122
                   (alist-cons 'system arg
123
                               (alist-delete 'system result eq?))))
124
         (option '(#\r "root") #t #f
125
                 (lambda (opt name arg result)
126
                   (alist-cons 'gc-root arg result)))
127
         %standard-build-options))
128
129
(define %default-options
130
  ;; Alist of default option values.
131
  `((system . ,(%current-system))
132
    (substitutes? . #t)
133
    (build-hook? . #t)
134
    (print-build-trace? . #t)
135
    (print-extended-build-trace? . #t)
136
    (multiplexed-build-output? . #t)
137
    (graft? . #t)
138
    (debug . 0)
139
    (verbosity . #f)                              ;default
140
    (file-system-type . "ext4")
141
    (image-size . guess)
142
    (install-bootloader? . #t)))
143
144
;;;
145
;;; Profiles
146
;;;
147
148
(define %user-module
149
  ;; Module in which the machine description file is loaded.
150
  (make-user-module '()))
151
152
(define %home (getenv "HOME"))
153
154
(define %current-home
155
  (string-append %profile-directory "/home"))
156
157
(define (ensure-home-profile data-directory)
158
  "Ensures $HOME is a symlink to the profile.  If it is not yet the case, move
159
it to the @var{data-directory} directory, unless it already exists, in which case
160
report an error."
161
  (ensure-profile-directory)
162
163
  (when (and %home %current-home)
164
    (let ((home (false-if-exception (lstat %home))))
165
      (cond
166
        ((equal? (stat:type home) 'symlink)
167
         (unless (equal? (readlink %home) %current-home)
168
           (delete-file %home)
169
           (symlink %current-home %home)))
170
        ((false-if-exception (lstat data-directory))
171
         (leave (G_ "Your $HOME directory (~a) is not a symlink to the home
172
profile, and it cannot be moved as ~a already exists on the filesystem.~%")
173
                %home data-directory))
174
        ((not home)
175
         (symlink %current-home %home))
176
        (else
177
         (rename-file %home data-directory)
178
         (symlink %current-home %home))))))
179
180
(define (list-generations pattern)
181
  "Display in a human-readable format all the home generations matching
182
PATTERN, a string.  When PATTERN is #f, display all the home generations."
183
  (cond ((not (file-exists? %current-home))             ; XXX: race condition
184
	 (raise (condition (&profile-not-found-error
185
			     (profile %current-home)))))
186
	((not pattern)
187
	 (for-each (lambda (number) (display-generation %current-home number)) (profile-generations %current-home)))
188
	((matching-generations pattern %current-home)
189
	 =>
190
	 (lambda (numbers)
191
	   (if (null-list? numbers)
192
	       (exit 1)
193
	       (leave-on-EPIPE
194
		 (for-each (lambda (number) (display-generation %current-home number)) numbers)))))))
195
196
197
;;;
198
;;; Entry point.
199
;;;
200
201
(define* (perform-action action home
202
                         #:key
203
                         dry-run? derivations-only?
204
                         use-substitutes?)
205
  "Perform ACTION for HOME.  When DERIVATIONS-ONLY? is true, print the
206
derivation file name(s) without building anything."
207
  (define println
208
    (cut format #t "~a~%" <>))
209
210
  (with-store store
211
    (let* ((drv      (run-with-store store (home->derivation home)))
212
           (profile  (derivation->output-path drv)))
213
      (show-what-to-build store (list drv)
214
                          #:use-substitutes? use-substitutes?
215
                          #:dry-run? dry-run?)
216
217
      (unless (or dry-run? derivations-only?)
218
          (begin
219
            (build-derivations store (list drv))
220
            (case action
221
              ((reconfigure)
222
               (newline)
223
               (format #t (G_ "Activating home...~%"))
224
               (ensure-home-profile (home-data-directory home))
225
               (let* ((number (generation-number %current-home))
226
                      (generation (generation-file-name %current-home (+ 1 number))))
227
                 (switch-symlinks generation profile)
228
                 (switch-symlinks %current-home generation))
229
                 (format #t (G_ "Your home directory has been reconfigured.~%")))
230
              (else
231
                (display profile)
232
                (newline))))))))
233
234
(define (process-action action args opts)
235
  "Process ACTION, a sub-command, with the arguments are listed in ARGS.
236
ACTION must be one of the sub-commands that takes an operating system
237
declaration as an argument (a file name.)  OPTS is the raw alist of options
238
resulting from command-line parsing."
239
  (define (ensure-home-configuration file-or-exp obj)
240
    (unless (home? obj)
241
      (leave (G_ "'~a' does not return a home configuration~%")
242
             file-or-exp))
243
    obj)
244
245
  (let* ((file        (match args
246
                        (() #f)
247
                        ((x . _) x)))
248
         (expr        (assoc-ref opts 'expression))
249
         (system      (assoc-ref opts 'system))
250
         (home        (ensure-home-configuration
251
                       (or file expr)
252
                       (cond
253
                        ((and expr file)
254
                         (leave
255
                          (G_ "both file and expression cannot be specified~%")))
256
                        (expr
257
                         (read/eval expr))
258
                        (file
259
                         (load* file %user-module
260
                                #:on-error (assoc-ref opts 'on-error)))
261
                        (else
262
                         (leave (G_ "no configuration specified~%"))))))
263
264
         (dry?        (assoc-ref opts 'dry-run?)))
265
266
    (with-store store
267
      (set-build-options-from-command-line store opts)
268
269
      (set-guile-for-build (default-guile))
270
271
      (case action
272
        (else
273
         (unless (eq? action 'build)
274
           (warn-about-old-distro #:suggested-command
275
                                  "guix home reconfigure"))
276
277
         (perform-action action home
278
                         #:dry-run? dry?
279
                         #:derivations-only? (assoc-ref opts
280
                                                        'derivations-only?)
281
                         #:use-substitutes? (assoc-ref opts 'substitutes?)))))
282
    (warn-about-disk-space)))
283
284
(define (resolve-subcommand name)
285
  (let ((module (resolve-interface
286
                 `(guix scripts home ,(string->symbol name))))
287
        (proc (string->symbol (string-append "guix-home-" name))))
288
    (module-ref module proc)))
289
290
(define (process-command command args opts)
291
  "Process COMMAND, one of the 'guix system' sub-commands.  ARGS is its
292
argument list and OPTS is the option alist."
293
  (case command
294
    ;; The following commands do not need to use the store, and they do not need
295
    ;; an operating system configuration file.
296
    ((list-generations)
297
     (let ((pattern (match args
298
			   (() #f)
299
			   ((pattern) pattern)
300
			   (x (leave (G_ "wrong number of arguments~%"))))))
301
       (list-generations pattern)))
302
    ;; The following commands need to use the store, but they do not need an
303
    ;; operating system configuration file.
304
    ;; The following commands need to use the store, and they also
305
    ;; need an operating system configuration file.
306
    (else (process-action command args opts))))
307
308
(define (guix-home . args)
309
  (define (parse-sub-command arg result)
310
    ;; Parse sub-command ARG and augment RESULT accordingly.
311
    (if (assoc-ref result 'action)
312
        (alist-cons 'argument arg result)
313
        (let ((action (string->symbol arg)))
314
          (case action
315
            ((build reconfigure list-generations)
316
             (alist-cons 'action action result))
317
            (else (leave (G_ "~a: unknown action~%") action))))))
318
319
  (define (match-pair car)
320
    ;; Return a procedure that matches a pair with CAR.
321
    (match-lambda
322
      ((head . tail)
323
       (and (eq? car head) tail))
324
      (_ #f)))
325
326
  (define (option-arguments opts)
327
    ;; Extract the plain arguments from OPTS.
328
    (let* ((args   (reverse (filter-map (match-pair 'argument) opts)))
329
           (count  (length args))
330
           (action (assoc-ref opts 'action))
331
           (expr   (assoc-ref opts 'expression)))
332
      (define (fail)
333
        (leave (G_ "wrong number of arguments for action '~a'~%")
334
               action))
335
336
      (unless action
337
        (format (current-error-port)
338
                (G_ "guix home: missing command name~%"))
339
        (format (current-error-port)
340
                (G_ "Try 'guix home --help' for more information.~%"))
341
        (exit 1))
342
343
      (case action
344
        ((build reconfigure)
345
         (unless (= count 1)
346
           (fail))))
347
      args))
348
349
  (with-error-handling
350
    (let* ((opts     (parse-command-line args %options
351
                                         (list %default-options)
352
                                         #:argument-handler
353
                                         parse-sub-command))
354
           (args     (option-arguments opts))
355
           (command  (assoc-ref opts 'action)))
356
      (parameterize ((%graft? (assoc-ref opts 'graft?)))
357
        (with-status-verbosity (or (assoc-ref opts 'verbosity)
358
                                   (if (eq? command 'build) 2 1))
359
          (process-command command args opts))))))
360
361
;;; home.scm ends here
362