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