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 %home %current-home
164
    (let ((home (false-if-exception (lstat %home))))
165
      (cond
166
        ((equal? (stat:type home) 'symlink)
167
         (symlink %current-home %home))
168
        ((false-if-exception (lstat data-directory))
169
         (leave (G_ "Your $HOME directory (~a) is not a symlink to the home
170
profile, and it cannot be moved as ~a already exists on the filesystem.~%")
171
                %home data-directory))
172
        ((not home)
173
         (symlink %current-home %home))
174
        (else
175
         (rename-file %home data-directory)
176
         (symlink %current-home %home))))))
177
178
(define (list-generations pattern)
179
  "Display in a human-readable format all the home generations matching
180
PATTERN, a string.  When PATTERN is #f, display all the home generations."
181
  (cond ((not (file-exists? %current-home))             ; XXX: race condition
182
	 (raise (condition (&profile-not-found-error
183
			     (profile %current-home)))))
184
	((not pattern)
185
	 (for-each (lambda (number) (display-generation %current-home number)) (profile-generations %current-home)))
186
	((matching-generations pattern %current-home)
187
	 =>
188
	 (lambda (numbers)
189
	   (if (null-list? numbers)
190
	       (exit 1)
191
	       (leave-on-EPIPE
192
		 (for-each (lambda (number) (display-generation %current-home number)) numbers)))))))
193
194
195
;;;
196
;;; Entry point.
197
;;;
198
199
(define* (perform-action action home
200
                         #:key
201
                         dry-run? derivations-only?
202
                         use-substitutes?)
203
  "Perform ACTION for HOME.  When DERIVATIONS-ONLY? is true, print the
204
derivation file name(s) without building anything."
205
  (define println
206
    (cut format #t "~a~%" <>))
207
208
  (with-store store
209
    (let* ((drv      (run-with-store store (home->derivation home)))
210
           (profile  (derivation->output-path drv)))
211
      (show-what-to-build store (list drv)
212
                          #:use-substitutes? use-substitutes?
213
                          #:dry-run? dry-run?)
214
215
      (unless (or dry-run? derivations-only?)
216
          (begin
217
            (build-derivations store (list drv))
218
            (case action
219
              ((reconfigure)
220
               (newline)
221
               (format #t (G_ "Activating home...~%"))
222
               (ensure-home-profile (home-data-directory home))
223
               (let* ((number (generation-number %current-home))
224
                      (generation (generation-file-name %current-home (+ 1 number))))
225
                 (switch-symlinks generation profile)
226
                 (switch-symlinks %current-home generation))
227
                 (format #t (G_ "Your home directory has been reconfigured.~%")))
228
              (else
229
                (display profile)
230
                (newline))))))))
231
232
(define (process-action action args opts)
233
  "Process ACTION, a sub-command, with the arguments are listed in ARGS.
234
ACTION must be one of the sub-commands that takes an operating system
235
declaration as an argument (a file name.)  OPTS is the raw alist of options
236
resulting from command-line parsing."
237
  (define (ensure-home-configuration file-or-exp obj)
238
    (unless (home? obj)
239
      (leave (G_ "'~a' does not return a home configuration~%")
240
             file-or-exp))
241
    obj)
242
243
  (let* ((file        (match args
244
                        (() #f)
245
                        ((x . _) x)))
246
         (expr        (assoc-ref opts 'expression))
247
         (system      (assoc-ref opts 'system))
248
         (home        (ensure-home-configuration
249
                       (or file expr)
250
                       (cond
251
                        ((and expr file)
252
                         (leave
253
                          (G_ "both file and expression cannot be specified~%")))
254
                        (expr
255
                         (read/eval expr))
256
                        (file
257
                         (load* file %user-module
258
                                #:on-error (assoc-ref opts 'on-error)))
259
                        (else
260
                         (leave (G_ "no configuration specified~%"))))))
261
262
         (dry?        (assoc-ref opts 'dry-run?)))
263
264
    (with-store store
265
      (set-build-options-from-command-line store opts)
266
267
      (set-guile-for-build (default-guile))
268
269
      (case action
270
        (else
271
         (unless (eq? action 'build)
272
           (warn-about-old-distro #:suggested-command
273
                                  "guix home reconfigure"))
274
275
         (perform-action action home
276
                         #:dry-run? dry?
277
                         #:derivations-only? (assoc-ref opts
278
                                                        'derivations-only?)
279
                         #:use-substitutes? (assoc-ref opts 'substitutes?)))))
280
    (warn-about-disk-space)))
281
282
(define (resolve-subcommand name)
283
  (let ((module (resolve-interface
284
                 `(guix scripts home ,(string->symbol name))))
285
        (proc (string->symbol (string-append "guix-home-" name))))
286
    (module-ref module proc)))
287
288
(define (process-command command args opts)
289
  "Process COMMAND, one of the 'guix system' sub-commands.  ARGS is its
290
argument list and OPTS is the option alist."
291
  (case command
292
    ;; The following commands do not need to use the store, and they do not need
293
    ;; an operating system configuration file.
294
    ((list-generations)
295
     (let ((pattern (match args
296
			   (() #f)
297
			   ((pattern) pattern)
298
			   (x (leave (G_ "wrong number of arguments~%"))))))
299
       (list-generations pattern)))
300
    ;; The following commands need to use the store, but they do not need an
301
    ;; operating system configuration file.
302
    ;; The following commands need to use the store, and they also
303
    ;; need an operating system configuration file.
304
    (else (process-action command args opts))))
305
306
(define (guix-home . args)
307
  (define (parse-sub-command arg result)
308
    ;; Parse sub-command ARG and augment RESULT accordingly.
309
    (if (assoc-ref result 'action)
310
        (alist-cons 'argument arg result)
311
        (let ((action (string->symbol arg)))
312
          (case action
313
            ((build reconfigure list-generations)
314
             (alist-cons 'action action result))
315
            (else (leave (G_ "~a: unknown action~%") action))))))
316
317
  (define (match-pair car)
318
    ;; Return a procedure that matches a pair with CAR.
319
    (match-lambda
320
      ((head . tail)
321
       (and (eq? car head) tail))
322
      (_ #f)))
323
324
  (define (option-arguments opts)
325
    ;; Extract the plain arguments from OPTS.
326
    (let* ((args   (reverse (filter-map (match-pair 'argument) opts)))
327
           (count  (length args))
328
           (action (assoc-ref opts 'action))
329
           (expr   (assoc-ref opts 'expression)))
330
      (define (fail)
331
        (leave (G_ "wrong number of arguments for action '~a'~%")
332
               action))
333
334
      (unless action
335
        (format (current-error-port)
336
                (G_ "guix home: missing command name~%"))
337
        (format (current-error-port)
338
                (G_ "Try 'guix home --help' for more information.~%"))
339
        (exit 1))
340
341
      (case action
342
        ((build reconfigure)
343
         (unless (= count 1)
344
           (fail))))
345
      args))
346
347
  (with-error-handling
348
    (let* ((opts     (parse-command-line args %options
349
                                         (list %default-options)
350
                                         #:argument-handler
351
                                         parse-sub-command))
352
           (args     (option-arguments opts))
353
           (command  (assoc-ref opts 'action)))
354
      (parameterize ((%graft? (assoc-ref opts 'graft?)))
355
        (with-status-verbosity (or (assoc-ref opts 'verbosity)
356
                                   (if (eq? command 'build) 2 1))
357
          (process-command command args opts))))))
358
359
;;; home.scm ends here
360