| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 |
|
| 23 | (define-module (gnu home services)
|
| 24 | #:use-module (gnu services)
|
| 25 | #:use-module ((gnu packages package-management) #:select (guix))
|
| 26 | #:use-module ((gnu packages base) #:select (coreutils))
|
| 27 | #:use-module (guix channels)
|
| 28 | #:use-module (guix monads)
|
| 29 | #:use-module (guix store)
|
| 30 | #:use-module (guix gexp)
|
| 31 | #:use-module (guix profiles)
|
| 32 | #:use-module (guix sets)
|
| 33 | #:use-module (guix ui)
|
| 34 | #:use-module (guix discovery)
|
| 35 | #:use-module (guix diagnostics)
|
| 36 | #:use-module (guix i18n)
|
| 37 | #:use-module (guix modules)
|
| 38 | #:use-module (guix memoization)
|
| 39 | #:use-module (srfi srfi-1)
|
| 40 | #:use-module (srfi srfi-9)
|
| 41 | #:use-module (srfi srfi-34)
|
| 42 | #:use-module (ice-9 match)
|
| 43 | #:use-module (ice-9 vlist)
|
| 44 |
|
| 45 | #:export (home-service-type
|
| 46 | home-profile-service-type
|
| 47 | home-environment-variables-service-type
|
| 48 | home-files-service-type
|
| 49 | home-xdg-configuration-files-service-type
|
| 50 | home-xdg-data-files-service-type
|
| 51 | home-run-on-first-login-service-type
|
| 52 | home-activation-service-type
|
| 53 | home-run-on-change-service-type
|
| 54 | home-provenance-service-type
|
| 55 | home-gc-root-service-type
|
| 56 |
|
| 57 | literal-string
|
| 58 | literal-string?
|
| 59 | literal-string-value
|
| 60 |
|
| 61 | with-shell-quotation-bindings
|
| 62 | environment-variable-shell-definitions
|
| 63 | shell-variable-definitions
|
| 64 | home-files-directory
|
| 65 | xdg-configuration-files-directory
|
| 66 | xdg-data-files-directory
|
| 67 |
|
| 68 | fold-home-service-types
|
| 69 | lookup-home-service-types
|
| 70 | home-provenance
|
| 71 |
|
| 72 | define-service-type-mapping
|
| 73 | system->home-service-type
|
| 74 |
|
| 75 | %initialize-gettext)
|
| 76 |
|
| 77 | #:re-export (service
|
| 78 | service-type
|
| 79 | service-extension
|
| 80 | simple-service
|
| 81 | for-home
|
| 82 | for-home?))
|
| 83 |
|
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 |
|
| 137 |
|
| 138 | (define (home-derivation entries mextensions)
|
| 139 | "Return as a monadic value the derivation of the 'home'
|
| 140 | directory containing the given entries."
|
| 141 | (mlet %store-monad ((extensions (mapm/accumulate-builds identity
|
| 142 | mextensions)))
|
| 143 | (lower-object
|
| 144 | (file-union "home" (append entries (concatenate extensions))))))
|
| 145 |
|
| 146 | (define home-service-type
|
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | (service-type (name 'home)
|
| 152 | (extensions '())
|
| 153 | (compose identity)
|
| 154 | (extend home-derivation)
|
| 155 | (default-value '())
|
| 156 | (description
|
| 157 | "Build the home environment top-level directory,
|
| 158 | which in turn refers to everything the home environment needs: its
|
| 159 | packages, configuration files, activation script, and so on.")))
|
| 160 |
|
| 161 | (define (packages->profile-entry packages)
|
| 162 | "Return a system entry for the profile containing PACKAGES."
|
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | (mlet %store-monad ((_ (current-target-system)))
|
| 170 | (return `(("profile" ,(profile
|
| 171 | (content (packages->manifest
|
| 172 | (map identity
|
| 173 | |
| 174 | (delete-duplicates packages eq?))))))))))
|
| 175 |
|
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | (define home-profile-service-type
|
| 181 | (service-type (name 'home-profile)
|
| 182 | (extensions
|
| 183 | (list (service-extension home-service-type
|
| 184 | packages->profile-entry)))
|
| 185 | (compose concatenate)
|
| 186 | (extend append)
|
| 187 | (description
|
| 188 | "This is the @dfn{home profile} and can be found in
|
| 189 | @file{~/.guix-home/profile}. It contains packages and
|
| 190 | configuration files that the user has declared in their
|
| 191 | @code{home-environment} record.")))
|
| 192 |
|
| 193 | |
| 194 | (define-record-type <literal-string>
|
| 195 | (literal-string str)
|
| 196 | literal-string?
|
| 197 | (str literal-string-value))
|
| 198 |
|
| 199 | (define (with-shell-quotation-bindings exp)
|
| 200 | "Insert EXP, a gexp, in a lexical environment providing the
|
| 201 | 'shell-single-quote' and 'shell-double-quote' bindings."
|
| 202 | #~(let* ((quote-string
|
| 203 | (lambda (value quoted-chars)
|
| 204 | (list->string (string-fold-right
|
| 205 | (lambda (chr lst)
|
| 206 | (if (memq chr quoted-chars)
|
| 207 | (append (list #\\ chr) lst)
|
| 208 | (cons chr lst)))
|
| 209 | '()
|
| 210 | value))))
|
| 211 | (shell-double-quote
|
| 212 | (lambda (value)
|
| 213 | |
| 214 | (string-append "\"" (quote-string value '(#\" #\\))
|
| 215 | "\"")))
|
| 216 | (shell-single-quote
|
| 217 | (lambda (value)
|
| 218 | |
| 219 | (string-append "'" (quote-string value '(#\'))
|
| 220 | "'"))))
|
| 221 | #$exp))
|
| 222 |
|
| 223 | (define (environment-variable-shell-definitions variables)
|
| 224 | "Return a gexp that evaluates to a list of POSIX shell statements defining
|
| 225 | VARIABLES, a list of environment variable name/value pairs. The returned code
|
| 226 | ensures variable values are properly quoted."
|
| 227 | (with-shell-quotation-bindings
|
| 228 | #~(string-append
|
| 229 | #$@(map (match-lambda
|
| 230 | ((key . #f)
|
| 231 | "")
|
| 232 | ((key . #t)
|
| 233 | #~(string-append "export " #$key "\n"))
|
| 234 | ((key . (or (? string? value)
|
| 235 | (? file-like? value)
|
| 236 | (? gexp? value)))
|
| 237 | #~(string-append "export " #$key "="
|
| 238 | (shell-double-quote #$value)
|
| 239 | "\n"))
|
| 240 | ((key . (? literal-string? value))
|
| 241 | #~(string-append "export " #$key "="
|
| 242 | (shell-single-quote
|
| 243 | #$(literal-string-value value))
|
| 244 | "\n")))
|
| 245 | variables))))
|
| 246 |
|
| 247 | (define (shell-variable-definitions variables)
|
| 248 | "Return a gexp that evaluates to a list of POSIX shell statements defining
|
| 249 | VARIABLES, a list of variable name/value pairs, as shell variables (not
|
| 250 | environment variables). The returned code ensures variable values are
|
| 251 | properly quoted."
|
| 252 | (with-shell-quotation-bindings
|
| 253 | #~(string-append
|
| 254 | #$@(map (match-lambda
|
| 255 | ((key . (or (? string? value)
|
| 256 | (? file-like? value)
|
| 257 | (? gexp? value)))
|
| 258 | #~(string-append #$key "="
|
| 259 | (shell-double-quote #$value)
|
| 260 | "\n"))
|
| 261 | ((key . (? literal-string? value))
|
| 262 | #~(string-append #$key "="
|
| 263 | (shell-single-quote
|
| 264 | #$(literal-string-value value))
|
| 265 | "\n")))
|
| 266 | variables))))
|
| 267 |
|
| 268 | (define (environment-variables->setup-environment-script vars)
|
| 269 | "Return a file that can be sourced by a POSIX compliant shell which
|
| 270 | initializes the environment. The file will source the home
|
| 271 | environment profile, set some default environment variables, and set
|
| 272 | environment variables provided in @code{vars}. @code{vars} is a list
|
| 273 | of pairs (@code{(key . value)}), @code{key} is a string and
|
| 274 | @code{value} is a string or gexp.
|
| 275 |
|
| 276 | If value is @code{#f} variable will be omitted.
|
| 277 | If value is @code{#t} variable will be just exported.
|
| 278 | For any other, value variable will be set to the @code{value} and
|
| 279 | exported."
|
| 280 | (define (warn-about-duplicate-definitions)
|
| 281 | (fold
|
| 282 | (lambda (x acc)
|
| 283 | (when (equal? (car x) (car acc))
|
| 284 | (warning
|
| 285 | (G_ "duplicate definition for `~a' environment variable ~%") (car x)))
|
| 286 | x)
|
| 287 | (cons "" "")
|
| 288 | (sort vars (lambda (a b)
|
| 289 | (string<? (car a) (car b))))))
|
| 290 |
|
| 291 | (warn-about-duplicate-definitions)
|
| 292 | (with-monad
|
| 293 | %store-monad
|
| 294 | (return
|
| 295 | `(("setup-environment"
|
| 296 | |
| 297 | |
| 298 | ,(computed-file "setup-environment"
|
| 299 | #~(call-with-output-file #$output
|
| 300 | (lambda (port)
|
| 301 | (set-port-encoding! port "UTF-8")
|
| 302 | (display "\
|
| 303 | # NOTE: Set HOME_ENVIRONMENT before sourcing (home-shell-profile-service-type ensures
|
| 304 | # ~/.profile does)
|
| 305 | GUIX_PROFILE=\"$HOME_ENVIRONMENT/profile\"
|
| 306 | PROFILE_FILE=\"$GUIX_PROFILE/etc/profile\"
|
| 307 | [ -f $PROFILE_FILE ] && . $PROFILE_FILE
|
| 308 |
|
| 309 | case $GUIX_LOCPATH in
|
| 310 | *$GUIX_PROFILE/lib/locale*) ;;
|
| 311 | *) export GUIX_LOCPATH=$GUIX_PROFILE/lib/locale:$GUIX_LOCPATH ;;
|
| 312 | esac
|
| 313 | case $XDG_DATA_DIRS in
|
| 314 | *$GUIX_PROFILE/share*) ;;
|
| 315 | *) export XDG_DATA_DIRS=$GUIX_PROFILE/share:$XDG_DATA_DIRS ;;
|
| 316 | esac
|
| 317 | case $MANPATH in
|
| 318 | *$GUIX_PROFILE/share/man*) ;;
|
| 319 | *) export MANPATH=$GUIX_PROFILE/share/man:$MANPATH
|
| 320 | esac
|
| 321 | case $INFOPATH in
|
| 322 | *$GUIX_PROFILE/share/info*) ;;
|
| 323 | *) export INFOPATH=$GUIX_PROFILE/share/info:$INFOPATH ;;
|
| 324 | esac
|
| 325 | case $XDG_CONFIG_DIRS in
|
| 326 | *$GUIX_PROFILE/etc/xdg*) ;;
|
| 327 | *) export XDG_CONFIG_DIRS=$GUIX_PROFILE/etc/xdg:$XDG_CONFIG_DIRS ;;
|
| 328 | esac
|
| 329 | case $XCURSOR_PATH in
|
| 330 | *$GUIX_PROFILE/share/icons*) ;;
|
| 331 | *) export XCURSOR_PATH=$GUIX_PROFILE/share/icons:$XCURSOR_PATH ;;
|
| 332 | esac
|
| 333 | case $GUIX_EXTENSIONS_PATH in
|
| 334 | *$GUIX_PROFILE/share/guix/extensions*) ;;
|
| 335 | *) export GUIX_EXTENSIONS_PATH=$GUIX_PROFILE/share/guix/extensions:$GUIX_EXTENSIONS_PATH ;;
|
| 336 | esac
|
| 337 |
|
| 338 | # Keep the shell environment clean.
|
| 339 | unset GUIX_PROFILE PROFILE_FILE
|
| 340 |
|
| 341 | " port)
|
| 342 | (display
|
| 343 | #$(environment-variable-shell-definitions vars)
|
| 344 | port)))))))))
|
| 345 |
|
| 346 | (define home-environment-variables-service-type
|
| 347 | (service-type (name 'home-environment-variables)
|
| 348 | (extensions
|
| 349 | (list (service-extension
|
| 350 | home-service-type
|
| 351 | environment-variables->setup-environment-script)))
|
| 352 | (compose concatenate)
|
| 353 | (extend append)
|
| 354 | (default-value '())
|
| 355 | (description "Set the environment variables.")))
|
| 356 |
|
| 357 | (define (files->files-directory files)
|
| 358 | "Return a @code{files} directory that contains FILES."
|
| 359 | (define (assert-no-duplicates files)
|
| 360 | (let loop ((files files)
|
| 361 | (seen (set)))
|
| 362 | (match files
|
| 363 | (() #t)
|
| 364 | (((file _) rest ...)
|
| 365 | (when (set-contains? seen file)
|
| 366 | (raise (formatted-message (G_ "duplicate '~a' entry for files/")
|
| 367 | file)))
|
| 368 | (loop rest (set-insert file seen))))))
|
| 369 |
|
| 370 | |
| 371 | |
| 372 | (assert-no-duplicates files)
|
| 373 |
|
| 374 | |
| 375 | (file-union "files" files #:dangling-symlinks? #t))
|
| 376 |
|
| 377 | |
| 378 | (define home-files-directory "files")
|
| 379 |
|
| 380 | (define (files-entry files)
|
| 381 | "Return an entry for the @file{~/.guix-home/files}
|
| 382 | directory containing FILES."
|
| 383 | (with-monad %store-monad
|
| 384 | (return `((,home-files-directory ,(files->files-directory files))))))
|
| 385 |
|
| 386 | (define home-files-service-type
|
| 387 | (service-type (name 'home-files)
|
| 388 | (extensions
|
| 389 | (list (service-extension home-service-type
|
| 390 | files-entry)))
|
| 391 | (compose concatenate)
|
| 392 | (extend append)
|
| 393 | (default-value '())
|
| 394 | (description "Files that will be put in
|
| 395 | @file{~/.guix-home/files}, and further processed during activation.")))
|
| 396 |
|
| 397 | (define xdg-configuration-files-directory ".config")
|
| 398 |
|
| 399 | (define (xdg-configuration-files files)
|
| 400 | "Add .config/ prefix to each file-path in FILES."
|
| 401 | (map (match-lambda
|
| 402 | ((file-path . rest)
|
| 403 | (cons (string-append xdg-configuration-files-directory "/" file-path)
|
| 404 | rest)))
|
| 405 | files))
|
| 406 |
|
| 407 | (define home-xdg-configuration-files-service-type
|
| 408 | (service-type (name 'home-xdg-configuration)
|
| 409 | (extensions
|
| 410 | (list (service-extension home-files-service-type
|
| 411 | xdg-configuration-files)))
|
| 412 | (compose concatenate)
|
| 413 | (extend append)
|
| 414 | (default-value '())
|
| 415 | (description "Files that will be put in
|
| 416 | @file{~/.guix-home/files/.config}, and further processed during activation.")))
|
| 417 |
|
| 418 | (define xdg-data-files-directory ".local/share")
|
| 419 |
|
| 420 | (define (xdg-data-files files)
|
| 421 | "Add .local/share prefix to each file-path in FILES."
|
| 422 | (map (match-lambda
|
| 423 | ((file-path . rest)
|
| 424 | (cons (string-append xdg-data-files-directory "/" file-path)
|
| 425 | rest)))
|
| 426 | files))
|
| 427 |
|
| 428 | (define home-xdg-data-files-service-type
|
| 429 | (service-type (name 'home-xdg-data)
|
| 430 | (extensions
|
| 431 | (list (service-extension home-files-service-type
|
| 432 | xdg-data-files)))
|
| 433 | (compose concatenate)
|
| 434 | (extend append)
|
| 435 | (default-value '())
|
| 436 | (description "Files that will be put in
|
| 437 | @file{~/.guix-home/files/.local/share}, and further processed during
|
| 438 | activation.")))
|
| 439 |
|
| 440 |
|
| 441 | (define %initialize-gettext
|
| 442 | #~(begin
|
| 443 | (bindtextdomain %gettext-domain
|
| 444 | (string-append #$guix "/share/locale"))
|
| 445 | (textdomain %gettext-domain)))
|
| 446 |
|
| 447 | (define (compute-on-first-login-script _ gexps)
|
| 448 | (program-file
|
| 449 | "on-first-login"
|
| 450 | (with-imported-modules (source-module-closure '((guix i18n)
|
| 451 | (guix diagnostics)))
|
| 452 | #~(begin
|
| 453 | (use-modules (guix i18n)
|
| 454 | (guix diagnostics))
|
| 455 |
|
| 456 | (define (claim-first-run file)
|
| 457 | (catch #t
|
| 458 | (lambda ()
|
| 459 | |
| 460 | |
| 461 | (close-fdes
|
| 462 | (open-fdes file (logior O_CREAT O_EXCL O_CLOEXEC)))
|
| 463 | #t)
|
| 464 | (const #f)))
|
| 465 |
|
| 466 | #$%initialize-gettext
|
| 467 |
|
| 468 | (let* ((xdg-runtime-dir (or (getenv "XDG_RUNTIME_DIR")
|
| 469 | (format #f "/run/user/~a" (getuid))))
|
| 470 | (flag-file-path (string-append
|
| 471 | xdg-runtime-dir "/on-first-login-executed")))
|
| 472 | |
| 473 | |
| 474 | |
| 475 | (if (file-exists? xdg-runtime-dir)
|
| 476 | (when (claim-first-run flag-file-path)
|
| 477 | |
| 478 | |
| 479 | |
| 480 | (begin *unspecified* #$@gexps))
|
| 481 | |
| 482 | |
| 483 | (warning (G_ "XDG_RUNTIME_DIR doesn't exists, on-first-login script
|
| 484 | won't execute anything. You can check if xdg runtime directory exists,
|
| 485 | XDG_RUNTIME_DIR variable is set to appropriate value and manually execute the
|
| 486 | script by running '$HOME/.guix-home/on-first-login'~%"))))))))
|
| 487 |
|
| 488 | (define (on-first-login-script-entry on-first-login)
|
| 489 | "Return, as a monadic value, an entry for the on-first-login script
|
| 490 | in the home environment directory."
|
| 491 | (with-monad %store-monad
|
| 492 | (return `(("on-first-login" ,on-first-login)))))
|
| 493 |
|
| 494 | (define home-run-on-first-login-service-type
|
| 495 | (service-type (name 'home-run-on-first-login)
|
| 496 | (extensions
|
| 497 | (list (service-extension
|
| 498 | home-service-type
|
| 499 | on-first-login-script-entry)))
|
| 500 | (compose identity)
|
| 501 | (extend compute-on-first-login-script)
|
| 502 | (default-value #f)
|
| 503 | (description "Run gexps on first user login. Can be
|
| 504 | extended with one gexp.")))
|
| 505 |
|
| 506 |
|
| 507 | (define (compute-activation-script init-gexp gexps)
|
| 508 | (gexp->script
|
| 509 | "activate"
|
| 510 | #~(let* ((he-init-file (lambda (he) (string-append he "/setup-environment")))
|
| 511 | (he-path (string-append (getenv "HOME") "/.guix-home"))
|
| 512 | (new-home-env (getenv "GUIX_NEW_HOME"))
|
| 513 | (new-home (or new-home-env
|
| 514 | |
| 515 | |
| 516 | (canonicalize-path (dirname (car (command-line))))))
|
| 517 | (old-home-env (getenv "GUIX_OLD_HOME"))
|
| 518 | (old-home (or old-home-env
|
| 519 | (if (file-exists? (he-init-file he-path))
|
| 520 | (readlink he-path)
|
| 521 | #f))))
|
| 522 | (if (file-exists? (he-init-file new-home))
|
| 523 | (let* ((port ((@ (ice-9 popen) open-input-pipe)
|
| 524 | (format #f "source ~a && ~a -0"
|
| 525 | (he-init-file new-home)
|
| 526 | #$(file-append coreutils "/bin/env"))))
|
| 527 | (result ((@ (ice-9 rdelim) read-delimited) "" port))
|
| 528 | (vars (map (lambda (x)
|
| 529 | (let ((si (string-index x #\=)))
|
| 530 | (cons (string-take x si)
|
| 531 | (string-drop x (1+ si)))))
|
| 532 | ((@ (srfi srfi-1) remove)
|
| 533 | string-null?
|
| 534 | (string-split result #\nul)))))
|
| 535 | (close-port port)
|
| 536 | (map (lambda (x) (setenv (car x) (cdr x))) vars)
|
| 537 |
|
| 538 | (setenv "GUIX_NEW_HOME" new-home)
|
| 539 | (setenv "GUIX_OLD_HOME" old-home)
|
| 540 |
|
| 541 | #$@gexps
|
| 542 |
|
| 543 | |
| 544 | (unless new-home-env (setenv "GUIX_NEW_HOME" #f))
|
| 545 | (unless old-home-env (setenv "GUIX_OLD_HOME" #f)))
|
| 546 | (format #t "\
|
| 547 | Activation script was either called or loaded by file from this directory:
|
| 548 | ~a
|
| 549 | It doesn't seem that home environment is somewhere around.
|
| 550 | Make sure that you call ./activate by symlink from -home store item.\n"
|
| 551 | new-home)))))
|
| 552 |
|
| 553 | (define (activation-script-entry m-activation)
|
| 554 | "Return, as a monadic value, an entry for the activation script
|
| 555 | in the home environment directory."
|
| 556 | (mlet %store-monad ((activation m-activation))
|
| 557 | (return `(("activate" ,activation)))))
|
| 558 |
|
| 559 | (define home-activation-service-type
|
| 560 | (service-type (name 'home-activation)
|
| 561 | (extensions
|
| 562 | (list (service-extension
|
| 563 | home-service-type
|
| 564 | activation-script-entry)))
|
| 565 | (compose identity)
|
| 566 | (extend compute-activation-script)
|
| 567 | (default-value #f)
|
| 568 | (description "Run gexps to activate the current
|
| 569 | generation of home environment and update the state of the home
|
| 570 | directory. @command{activate} script automatically called during
|
| 571 | reconfiguration or generation switching. This service can be extended
|
| 572 | with one gexp, but many times, and all gexps must be idempotent.")))
|
| 573 |
|
| 574 |
|
| 575 | |
| 576 | |
| 577 | |
| 578 |
|
| 579 | (define (service-type-mapping proc)
|
| 580 | "Return a procedure that applies PROC to map a service type graph to another
|
| 581 | one."
|
| 582 | (define (rewrite extension)
|
| 583 | (match (proc (service-extension-target extension))
|
| 584 | (#f #f)
|
| 585 | (target
|
| 586 | (service-extension target
|
| 587 | (service-extension-compute extension)))))
|
| 588 |
|
| 589 | (define replace
|
| 590 | (mlambdaq (type)
|
| 591 | (service-type
|
| 592 | (inherit type)
|
| 593 | (name (symbol-append 'home- (service-type-name type)))
|
| 594 | (location (service-type-location type))
|
| 595 | (extensions (filter-map rewrite (service-type-extensions type))))))
|
| 596 |
|
| 597 | replace)
|
| 598 |
|
| 599 | (define %system/home-service-type-mapping
|
| 600 | |
| 601 | (make-hash-table))
|
| 602 |
|
| 603 | (define system->home-service-type
|
| 604 | |
| 605 | (let ()
|
| 606 | (define (replace type)
|
| 607 | (define replacement
|
| 608 | (hashq-ref %system/home-service-type-mapping type
|
| 609 | *unspecified*))
|
| 610 |
|
| 611 | (if (eq? replacement *unspecified*)
|
| 612 | type
|
| 613 | replacement))
|
| 614 |
|
| 615 | (service-type-mapping replace)))
|
| 616 |
|
| 617 | (define-syntax define-service-type-mapping
|
| 618 | (syntax-rules (=>)
|
| 619 | ((_ system-type => home-type)
|
| 620 | (hashq-set! %system/home-service-type-mapping
|
| 621 | system-type home-type))))
|
| 622 |
|
| 623 | (define-syntax define-service-type-mappings
|
| 624 | (syntax-rules (=>)
|
| 625 | ((_ (system-type => home-type) ...)
|
| 626 | (begin
|
| 627 | (define-service-type-mapping system-type => home-type)
|
| 628 | ...))))
|
| 629 |
|
| 630 | (define-service-type-mappings
|
| 631 | (system-service-type => home-service-type)
|
| 632 | (activation-service-type => home-activation-service-type)
|
| 633 | (profile-service-type => home-profile-service-type))
|
| 634 |
|
| 635 |
|
| 636 | |
| 637 | |
| 638 | |
| 639 |
|
| 640 | (define (compute-on-change-gexp eval-gexps? pattern-gexp-tuples)
|
| 641 | (with-imported-modules (source-module-closure '((guix i18n)))
|
| 642 | #~(begin
|
| 643 | (use-modules (guix i18n))
|
| 644 |
|
| 645 | #$%initialize-gettext
|
| 646 |
|
| 647 | (define (equal-regulars? file1 file2)
|
| 648 | "Check if FILE1 and FILE2 are bit for bit identical."
|
| 649 | (let* ((cmp-binary #$(file-append
|
| 650 | (@ (gnu packages base) diffutils) "/bin/cmp"))
|
| 651 | (stats1 (lstat file1))
|
| 652 | (stats2 (lstat file2)))
|
| 653 | (cond
|
| 654 | ((= (stat:ino stats1) (stat:ino stats2)) #t)
|
| 655 | ((not (= (stat:size stats1) (stat:size stats2))) #f)
|
| 656 |
|
| 657 | (else (= (system* cmp-binary file1 file2) 0)))))
|
| 658 |
|
| 659 | (define (equal-symlinks? symlink1 symlink2)
|
| 660 | "Check if SYMLINK1 and SYMLINK2 are pointing to the same target."
|
| 661 | (string=? (readlink symlink1) (readlink symlink2)))
|
| 662 |
|
| 663 | (define (equal-directories? dir1 dir2)
|
| 664 | "Check if DIR1 and DIR2 have the same content."
|
| 665 | (define (ordinary-file file)
|
| 666 | (not (or (string=? file ".")
|
| 667 | (string=? file ".."))))
|
| 668 | (let* ((files1 (scandir dir1 ordinary-file))
|
| 669 | (files2 (scandir dir2 ordinary-file)))
|
| 670 | (if (equal? files1 files2)
|
| 671 | (map (lambda (file)
|
| 672 | (equal-files?
|
| 673 | (string-append dir1 "/" file)
|
| 674 | (string-append dir2 "/" file)))
|
| 675 | files1)
|
| 676 | #f)))
|
| 677 |
|
| 678 | (define (equal-files? file1 file2)
|
| 679 | "Compares files, symlinks or directories of the same type."
|
| 680 | (case (file-type file1)
|
| 681 | ((directory) (equal-directories? file1 file2))
|
| 682 | ((symlink) (equal-symlinks? file1 file2))
|
| 683 | ((regular) (equal-regulars? file1 file2))
|
| 684 | (else
|
| 685 | (display "The file type is unsupported by on-change service.\n")
|
| 686 | #f)))
|
| 687 |
|
| 688 | (define (file-type file)
|
| 689 | (stat:type (lstat file)))
|
| 690 |
|
| 691 | (define (something-changed? file1 file2)
|
| 692 | (cond
|
| 693 | ((and (not (file-exists? file1))
|
| 694 | (not (file-exists? file2))) #f)
|
| 695 | ((or (not (file-exists? file1))
|
| 696 | (not (file-exists? file2))) #t)
|
| 697 |
|
| 698 | ((not (eq? (file-type file1) (file-type file2))) #t)
|
| 699 |
|
| 700 | (else
|
| 701 | (not (equal-files? file1 file2)))))
|
| 702 |
|
| 703 | (define expressions-to-eval
|
| 704 | (map
|
| 705 | (lambda (x)
|
| 706 | (let* ((file1 (string-append
|
| 707 | (or (getenv "GUIX_OLD_HOME")
|
| 708 | "/gnu/store/non-existing-generation")
|
| 709 | "/" (car x)))
|
| 710 | (file2 (string-append (getenv "GUIX_NEW_HOME") "/" (car x)))
|
| 711 | (_ (format #t (G_ "Comparing ~a and\n~10t~a...") file1 file2))
|
| 712 | (any-changes? (something-changed? file1 file2))
|
| 713 | (_ (format #t (G_ " done (~a)\n")
|
| 714 | (if any-changes? "changed" "same"))))
|
| 715 | (if any-changes? (cadr x) "")))
|
| 716 | '#$pattern-gexp-tuples))
|
| 717 |
|
| 718 | (if #$eval-gexps?
|
| 719 | (begin
|
| 720 | |
| 721 | |
| 722 | (display (G_ "Evaluating on-change gexps.\n\n"))
|
| 723 | (for-each primitive-eval expressions-to-eval)
|
| 724 | (display (G_ "On-change gexps evaluation finished.\n\n")))
|
| 725 | (display "\
|
| 726 | On-change gexps won't be evaluated; evaluation has been disabled in the
|
| 727 | service configuration")))))
|
| 728 |
|
| 729 | (define home-run-on-change-service-type
|
| 730 | (service-type (name 'home-run-on-change)
|
| 731 | (extensions
|
| 732 | (list (service-extension
|
| 733 | home-activation-service-type
|
| 734 | identity)))
|
| 735 | (compose concatenate)
|
| 736 | (extend compute-on-change-gexp)
|
| 737 | (default-value #t)
|
| 738 | (description "\
|
| 739 | G-expressions to run if the specified files have changed since the
|
| 740 | last generation. The extension should be a list of lists where the
|
| 741 | first element is the pattern for file or directory that expected to be
|
| 742 | changed, and the second element is the G-expression to be evaluated.")))
|
| 743 |
|
| 744 |
|
| 745 | |
| 746 | |
| 747 | |
| 748 |
|
| 749 | (define home-provenance-service-type
|
| 750 | (service-type
|
| 751 | (name 'home-provenance)
|
| 752 | (extensions
|
| 753 | (list (service-extension
|
| 754 | home-service-type
|
| 755 | (service-extension-compute
|
| 756 | (first (service-type-extensions provenance-service-type))))))
|
| 757 | (default-value #f) |
| 758 | (description "\
|
| 759 | Store provenance information about the home environment in the home
|
| 760 | environment itself: the channels used when building the home
|
| 761 | environment, and its configuration file, when available.")))
|
| 762 |
|
| 763 | (define sexp->home-provenance sexp->system-provenance)
|
| 764 | (define home-provenance system-provenance)
|
| 765 |
|
| 766 |
|
| 767 | |
| 768 | |
| 769 | |
| 770 |
|
| 771 | (define home-gc-root-service-type
|
| 772 | (service-type
|
| 773 | (inherit (system->home-service-type gc-root-service-type))
|
| 774 | (default-value '())))
|
| 775 |
|
| 776 |
|
| 777 | |
| 778 | |
| 779 | |
| 780 |
|
| 781 | (define (parent-directory directory)
|
| 782 | "Get the parent directory of DIRECTORY"
|
| 783 | (string-join (drop-right (string-split directory #\/) 1) "/"))
|
| 784 |
|
| 785 | (define %guix-home-root-directory
|
| 786 | |
| 787 | (parent-directory
|
| 788 | (dirname (dirname (search-path %load-path "gnu/home/services.scm")))))
|
| 789 |
|
| 790 | (define %service-type-path
|
| 791 | |
| 792 | (make-parameter `((,%guix-home-root-directory . "gnu/home/services"))))
|
| 793 |
|
| 794 | (define (all-home-service-modules)
|
| 795 | "Return the default set of `home service' modules."
|
| 796 | (cons (resolve-interface '(gnu home services))
|
| 797 | (all-modules (%service-type-path)
|
| 798 | #:warn warn-about-load-error)))
|
| 799 |
|
| 800 | (define* (fold-home-service-types proc seed)
|
| 801 | (fold-service-types proc seed (all-home-service-modules)))
|
| 802 |
|
| 803 | (define lookup-home-service-types
|
| 804 | (let ((table
|
| 805 | (delay (fold-home-service-types (lambda (type result)
|
| 806 | (vhash-consq (service-type-name type)
|
| 807 | type result))
|
| 808 | vlist-null))))
|
| 809 | (lambda (name)
|
| 810 | "Return the list of services with the given NAME (a symbol)."
|
| 811 | (vhash-foldq* cons '() name (force table)))))
|