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