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