services.scm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 Andrew Tropin <andrew@trop.in>
  3. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu home services)
  20. #:use-module (gnu services)
  21. #:use-module (guix channels)
  22. #:use-module (guix monads)
  23. #:use-module (guix store)
  24. #:use-module (guix gexp)
  25. #:use-module (guix profiles)
  26. #:use-module (guix sets)
  27. #:use-module (guix ui)
  28. #:use-module (guix discovery)
  29. #:use-module (guix diagnostics)
  30. #:use-module (srfi srfi-1)
  31. #:use-module (ice-9 match)
  32. #:export (home-service-type
  33. home-profile-service-type
  34. home-environment-variables-service-type
  35. home-files-service-type
  36. home-run-on-first-login-service-type
  37. home-activation-service-type
  38. home-run-on-change-service-type
  39. home-provenance-service-type
  40. fold-home-service-types)
  41. #:re-export (service
  42. service-type
  43. service-extension))
  44. ;;; Comment:
  45. ;;;
  46. ;;; This module is similar to (gnu system services) module, but
  47. ;;; provides Home Services, which are supposed to be used for building
  48. ;;; home-environment.
  49. ;;;
  50. ;;; Home Services use the same extension as System Services. Consult
  51. ;;; (gnu system services) module or manual for more information.
  52. ;;;
  53. ;;; home-service-type is a root of home services DAG.
  54. ;;;
  55. ;;; home-profile-service-type is almost the same as profile-service-type, at least
  56. ;;; for now.
  57. ;;;
  58. ;;; home-environment-variables-service-type generates a @file{setup-environment}
  59. ;;; shell script, which is expected to be sourced by login shell or other program,
  60. ;;; which starts early and spawns all other processes. Home services for shells
  61. ;;; automatically add code for sourcing this file, if person do not use those home
  62. ;;; services they have to source this script manually in their's shell *profile
  63. ;;; file (details described in the manual).
  64. ;;;
  65. ;;; home-files-service-type is similar to etc-service-type, but doesn't extend
  66. ;;; home-activation, because deploy mechanism for config files is pluggable and
  67. ;;; can be different for different home environments: The default one is called
  68. ;;; symlink-manager (will be introudced in a separate patch series), which creates
  69. ;;; links for various dotfiles (like $XDG_CONFIG_HOME/$APP/...) to store, but is
  70. ;;; possible to implement alternative approaches like read-only home from Julien's
  71. ;;; guix-home-manager.
  72. ;;;
  73. ;;; home-run-on-first-login-service-type provides an @file{on-first-login} guile
  74. ;;; script, which runs provided gexps once, when user makes first login. It can
  75. ;;; be used to start user's Shepherd and maybe some other process. It relies on
  76. ;;; assumption that /run/user/$UID will be created on login by some login
  77. ;;; manager (elogind for example).
  78. ;;;
  79. ;;; home-activation-service-type provides an @file{activate} guile script, which
  80. ;;; do three main things:
  81. ;;;
  82. ;;; - Sets environment variables to the values declared in
  83. ;;; @file{setup-environment} shell script. It's necessary, because user can set
  84. ;;; for example XDG_CONFIG_HOME and it should be respected by activation gexp of
  85. ;;; symlink-manager.
  86. ;;;
  87. ;;; - Sets GUIX_NEW_HOME and possibly GUIX_OLD_HOME vars to paths in the store.
  88. ;;; Later those variables can be used by activation gexps, for example by
  89. ;;; symlink-manager or run-on-change services.
  90. ;;;
  91. ;;; - Run all activation gexps provided by other home services.
  92. ;;;
  93. ;;; home-run-on-change-service-type allows to trigger actions during
  94. ;;; activation if file or directory specified by pattern is changed.
  95. ;;;
  96. ;;; Code:
  97. (define (home-derivation entries mextensions)
  98. "Return as a monadic value the derivation of the 'home'
  99. directory containing the given entries."
  100. (mlet %store-monad ((extensions (mapm/accumulate-builds identity
  101. mextensions)))
  102. (lower-object
  103. (file-union "home" (append entries (concatenate extensions))))))
  104. (define home-service-type
  105. ;; This is the ultimate service type, the root of the home service
  106. ;; DAG. The service of this type is extended by monadic name/item
  107. ;; pairs. These items end up in the "home-environment directory" as
  108. ;; returned by 'home-environment-derivation'.
  109. (service-type (name 'home)
  110. (extensions '())
  111. (compose identity)
  112. (extend home-derivation)
  113. (default-value '())
  114. (description
  115. "Build the home environment top-level directory,
  116. which in turn refers to everything the home environment needs: its
  117. packages, configuration files, activation script, and so on.")))
  118. (define (packages->profile-entry packages)
  119. "Return a system entry for the profile containing PACKAGES."
  120. ;; XXX: 'mlet' is needed here for one reason: to get the proper
  121. ;; '%current-target' and '%current-target-system' bindings when
  122. ;; 'packages->manifest' is called, and thus when the 'package-inputs'
  123. ;; etc. procedures are called on PACKAGES. That way, conditionals in those
  124. ;; inputs see the "correct" value of these two parameters. See
  125. ;; <https://issues.guix.gnu.org/44952>.
  126. (mlet %store-monad ((_ (current-target-system)))
  127. (return `(("profile" ,(profile
  128. (content (packages->manifest
  129. (map identity
  130. ;;(options->transformation transformations)
  131. (delete-duplicates packages eq?))))))))))
  132. ;; MAYBE: Add a list of transformations for packages. It's better to
  133. ;; place it in home-profile-service-type to affect all profile
  134. ;; packages and prevent conflicts, when other packages relies on
  135. ;; non-transformed version of package.
  136. (define home-profile-service-type
  137. (service-type (name 'home-profile)
  138. (extensions
  139. (list (service-extension home-service-type
  140. packages->profile-entry)))
  141. (compose concatenate)
  142. (extend append)
  143. (description
  144. "This is the @dfn{home profile} and can be found in
  145. @file{~/.guix-home/profile}. It contains packages and
  146. configuration files that the user has declared in their
  147. @code{home-environment} record.")))
  148. (define (environment-variables->setup-environment-script vars)
  149. "Return a file that can be sourced by a POSIX compliant shell which
  150. initializes the environment. The file will source the home
  151. environment profile, set some default environment variables, and set
  152. environment variables provided in @code{vars}. @code{vars} is a list
  153. of pairs (@code{(key . value)}), @code{key} is a string and
  154. @code{value} is a string or gexp.
  155. If value is @code{#f} variable will be omitted.
  156. If value is @code{#t} variable will be just exported.
  157. For any other, value variable will be set to the @code{value} and
  158. exported."
  159. (define (warn-about-duplicate-defenitions)
  160. (fold
  161. (lambda (x acc)
  162. (when (equal? (car x) (car acc))
  163. (warning
  164. (G_ "duplicate definition for `~a' environment variable ~%") (car x)))
  165. x)
  166. (cons "" "")
  167. (sort vars (lambda (a b)
  168. (string<? (car a) (car b))))))
  169. (warn-about-duplicate-defenitions)
  170. (with-monad
  171. %store-monad
  172. (return
  173. `(("setup-environment"
  174. ;; TODO: It's necessary to source ~/.guix-profile too
  175. ;; on foreign distros
  176. ,(apply mixed-text-file "setup-environment"
  177. "\
  178. HOME_ENVIRONMENT=$HOME/.guix-home
  179. GUIX_PROFILE=\"$HOME_ENVIRONMENT/profile\"
  180. PROFILE_FILE=\"$HOME_ENVIRONMENT/profile/etc/profile\"
  181. [ -f $PROFILE_FILE ] && . $PROFILE_FILE
  182. case $XDG_DATA_DIRS in
  183. *$HOME_ENVIRONMENT/profile/share*) ;;
  184. *) export XDG_DATA_DIRS=$HOME_ENVIRONMENT/profile/share:$XDG_DATA_DIRS ;;
  185. esac
  186. case $MANPATH in
  187. *$HOME_ENVIRONMENT/profile/share/man*) ;;
  188. *) export MANPATH=$HOME_ENVIRONMENT/profile/share/man:$MANPATH
  189. esac
  190. case $INFOPATH in
  191. *$HOME_ENVIRONMENT/profile/share/info*) ;;
  192. *) export INFOPATH=$HOME_ENVIRONMENT/profile/share/info:$INFOPATH ;;
  193. esac
  194. case $XDG_CONFIG_DIRS in
  195. *$HOME_ENVIRONMENT/profile/etc/xdg*) ;;
  196. *) export XDG_CONFIG_DIRS=$HOME_ENVIRONMENT/profile/etc/xdg:$XDG_CONFIG_DIRS ;;
  197. esac
  198. case $XCURSOR_PATH in
  199. *$HOME_ENVIRONMENT/profile/share/icons*) ;;
  200. *) export XCURSOR_PATH=$HOME_ENVIRONMENT/profile/share/icons:$XCURSOR_PATH ;;
  201. esac
  202. "
  203. (append-map
  204. (match-lambda
  205. ((key . #f)
  206. '())
  207. ((key . #t)
  208. (list "export " key "\n"))
  209. ((key . value)
  210. (list "export " key "=" value "\n")))
  211. vars)))))))
  212. (define home-environment-variables-service-type
  213. (service-type (name 'home-environment-variables)
  214. (extensions
  215. (list (service-extension
  216. home-service-type
  217. environment-variables->setup-environment-script)))
  218. (compose concatenate)
  219. (extend append)
  220. (default-value '())
  221. (description "Set the environment variables.")))
  222. (define (files->files-directory files)
  223. "Return a @code{files} directory that contains FILES."
  224. (define (assert-no-duplicates files)
  225. (let loop ((files files)
  226. (seen (set)))
  227. (match files
  228. (() #t)
  229. (((file _) rest ...)
  230. (when (set-contains? seen file)
  231. (raise (formatted-message (G_ "duplicate '~a' entry for files/")
  232. file)))
  233. (loop rest (set-insert file seen))))))
  234. ;; Detect duplicates early instead of letting them through, eventually
  235. ;; leading to a build failure of "files.drv".
  236. (assert-no-duplicates files)
  237. (file-union "files" files))
  238. (define (files-entry files)
  239. "Return an entry for the @file{~/.guix-home/files}
  240. directory containing FILES."
  241. (with-monad %store-monad
  242. (return `(("files" ,(files->files-directory files))))))
  243. (define home-files-service-type
  244. (service-type (name 'home-files)
  245. (extensions
  246. (list (service-extension home-service-type
  247. files-entry)))
  248. (compose concatenate)
  249. (extend append)
  250. (default-value '())
  251. (description "Configuration files for programs that
  252. will be put in @file{~/.guix-home/files}.")))
  253. (define (compute-on-first-login-script _ gexps)
  254. (program-file
  255. "on-first-login"
  256. #~(let* ((xdg-runtime-dir (or (getenv "XDG_RUNTIME_DIR")
  257. (format #f "/run/user/~a" (getuid))))
  258. (flag-file-path (string-append
  259. xdg-runtime-dir "/on-first-login-executed"))
  260. (touch (lambda (file-name)
  261. (call-with-output-file file-name (const #t)))))
  262. ;; XDG_RUNTIME_DIR dissapears on logout, that means such trick
  263. ;; allows to launch on-first-login script on first login only
  264. ;; after complete logout/reboot.
  265. (if (file-exists? xdg-runtime-dir)
  266. (unless (file-exists? flag-file-path)
  267. (begin #$@gexps (touch flag-file-path)))
  268. (display "XDG_RUNTIME_DIR doesn't exists, on-first-login script
  269. won't execute anything. You can check if xdg runtime directory exists,
  270. XDG_RUNTIME_DIR variable is set to appropriate value and manually execute the
  271. script by running '$HOME/.guix-home/on-first-login'")))))
  272. (define (on-first-login-script-entry on-first-login)
  273. "Return, as a monadic value, an entry for the on-first-login script
  274. in the home environment directory."
  275. (with-monad %store-monad
  276. (return `(("on-first-login" ,on-first-login)))))
  277. (define home-run-on-first-login-service-type
  278. (service-type (name 'home-run-on-first-login)
  279. (extensions
  280. (list (service-extension
  281. home-service-type
  282. on-first-login-script-entry)))
  283. (compose identity)
  284. (extend compute-on-first-login-script)
  285. (default-value #f)
  286. (description "Run gexps on first user login. Can be
  287. extended with one gexp.")))
  288. (define (compute-activation-script init-gexp gexps)
  289. (gexp->script
  290. "activate"
  291. #~(let* ((he-init-file (lambda (he) (string-append he "/setup-environment")))
  292. (he-path (string-append (getenv "HOME") "/.guix-home"))
  293. (new-home-env (getenv "GUIX_NEW_HOME"))
  294. (new-home (or new-home-env
  295. ;; Path of the activation file if called interactively
  296. (dirname (car (command-line)))))
  297. (old-home-env (getenv "GUIX_OLD_HOME"))
  298. (old-home (or old-home-env
  299. (if (file-exists? (he-init-file he-path))
  300. (readlink he-path)
  301. #f))))
  302. (if (file-exists? (he-init-file new-home))
  303. (let* ((port ((@ (ice-9 popen) open-input-pipe)
  304. (format #f "source ~a && env -0"
  305. (he-init-file new-home))))
  306. (result ((@ (ice-9 rdelim) read-delimited) "" port))
  307. (vars (map (lambda (x)
  308. (let ((si (string-index x #\=)))
  309. (cons (string-take x si)
  310. (string-drop x (1+ si)))))
  311. ((@ (srfi srfi-1) remove)
  312. string-null?
  313. (string-split result #\nul)))))
  314. (close-port port)
  315. (map (lambda (x) (setenv (car x) (cdr x))) vars)
  316. (setenv "GUIX_NEW_HOME" new-home)
  317. (setenv "GUIX_OLD_HOME" old-home)
  318. #$@gexps
  319. ;; Do not unset env variable if it was set outside.
  320. (unless new-home-env (setenv "GUIX_NEW_HOME" #f))
  321. (unless old-home-env (setenv "GUIX_OLD_HOME" #f)))
  322. (format #t "\
  323. Activation script was either called or loaded by file from this directory:
  324. ~a
  325. It doesn't seem that home environment is somewhere around.
  326. Make sure that you call ./activate by symlink from -home store item.\n"
  327. new-home)))))
  328. (define (activation-script-entry m-activation)
  329. "Return, as a monadic value, an entry for the activation script
  330. in the home environment directory."
  331. (mlet %store-monad ((activation m-activation))
  332. (return `(("activate" ,activation)))))
  333. (define home-activation-service-type
  334. (service-type (name 'home-activation)
  335. (extensions
  336. (list (service-extension
  337. home-service-type
  338. activation-script-entry)))
  339. (compose identity)
  340. (extend compute-activation-script)
  341. (default-value #f)
  342. (description "Run gexps to activate the current
  343. generation of home environment and update the state of the home
  344. directory. @command{activate} script automatically called during
  345. reconfiguration or generation switching. This service can be extended
  346. with one gexp, but many times, and all gexps must be idempotent.")))
  347. ;;;
  348. ;;; On-change.
  349. ;;;
  350. (define (compute-on-change-gexp eval-gexps? pattern-gexp-tuples)
  351. #~(begin
  352. (define (equal-regulars? file1 file2)
  353. "Check if FILE1 and FILE2 are bit for bit identical."
  354. (let* ((cmp-binary #$(file-append
  355. (@ (gnu packages base) diffutils) "/bin/cmp"))
  356. (stats1 (lstat file1))
  357. (stats2 (lstat file2)))
  358. (cond
  359. ((= (stat:ino stats1) (stat:ino stats2)) #t)
  360. ((not (= (stat:size stats1) (stat:size stats2))) #f)
  361. (else (= (system* cmp-binary file1 file2) 0)))))
  362. (define (equal-symlinks? symlink1 symlink2)
  363. "Check if SYMLINK1 and SYMLINK2 are pointing to the same target."
  364. (string=? (readlink symlink1) (readlink symlink2)))
  365. (define (equal-directories? dir1 dir2)
  366. "Check if DIR1 and DIR2 have the same content."
  367. (define (ordinary-file file)
  368. (not (or (string=? file ".")
  369. (string=? file ".."))))
  370. (let* ((files1 (scandir dir1 ordinary-file))
  371. (files2 (scandir dir2 ordinary-file)))
  372. (if (equal? files1 files2)
  373. (map (lambda (file)
  374. (equal-files?
  375. (string-append dir1 "/" file)
  376. (string-append dir2 "/" file)))
  377. files1)
  378. #f)))
  379. (define (equal-files? file1 file2)
  380. "Compares files, symlinks or directories of the same type."
  381. (case (file-type file1)
  382. ((directory) (equal-directories? file1 file2))
  383. ((symlink) (equal-symlinks? file1 file2))
  384. ((regular) (equal-regulars? file1 file2))
  385. (else
  386. (display "The file type is unsupported by on-change service.\n")
  387. #f)))
  388. (define (file-type file)
  389. (stat:type (lstat file)))
  390. (define (something-changed? file1 file2)
  391. (cond
  392. ((and (not (file-exists? file1))
  393. (not (file-exists? file2))) #f)
  394. ((or (not (file-exists? file1))
  395. (not (file-exists? file2))) #t)
  396. ((not (eq? (file-type file1) (file-type file2))) #t)
  397. (else
  398. (not (equal-files? file1 file2)))))
  399. (define expressions-to-eval
  400. (map
  401. (lambda (x)
  402. (let* ((file1 (string-append
  403. (or (getenv "GUIX_OLD_HOME")
  404. "/gnu/store/non-existing-generation")
  405. "/" (car x)))
  406. (file2 (string-append (getenv "GUIX_NEW_HOME") "/" (car x)))
  407. (_ (format #t "Comparing ~a and\n~10t~a..." file1 file2))
  408. (any-changes? (something-changed? file1 file2))
  409. (_ (format #t " done (~a)\n"
  410. (if any-changes? "changed" "same"))))
  411. (if any-changes? (cadr x) "")))
  412. '#$pattern-gexp-tuples))
  413. (if #$eval-gexps?
  414. (begin
  415. (display "Evaling on-change gexps.\n\n")
  416. (for-each primitive-eval expressions-to-eval)
  417. (display "On-change gexps evaluation finished.\n\n"))
  418. (display "\
  419. On-change gexps won't be evaluated, disabled by service
  420. configuration.\n"))))
  421. (define home-run-on-change-service-type
  422. (service-type (name 'home-run-on-change)
  423. (extensions
  424. (list (service-extension
  425. home-activation-service-type
  426. identity)))
  427. (compose concatenate)
  428. (extend compute-on-change-gexp)
  429. (default-value #t)
  430. (description "\
  431. G-expressions to run if the specified files have changed since the
  432. last generation. The extension should be a list of lists where the
  433. first element is the pattern for file or directory that expected to be
  434. changed, and the second element is the G-expression to be evaluated.")))
  435. ;;;
  436. ;;; Provenance tracking.
  437. ;;;
  438. (define home-provenance-service-type
  439. (service-type
  440. (name 'home-provenance)
  441. (extensions
  442. (list (service-extension
  443. home-service-type
  444. (service-extension-compute
  445. (first (service-type-extensions provenance-service-type))))))
  446. (default-value #f) ;the HE config file
  447. (description "\
  448. Store provenance information about the home environment in the home
  449. environment itself: the channels used when building the home
  450. environment, and its configuration file, when available.")))
  451. (define sexp->home-provenance sexp->system-provenance)
  452. (define home-provenance system-provenance)
  453. ;;;
  454. ;;; Searching
  455. ;;;
  456. (define (parent-directory directory)
  457. "Get the parent directory of DIRECTORY"
  458. (string-join (drop-right (string-split directory #\/) 1) "/"))
  459. (define %guix-home-root-directory
  460. ;; Absolute file name of the module hierarchy.
  461. (parent-directory
  462. (dirname (dirname (search-path %load-path "gnu/home/services.scm")))))
  463. (define %service-type-path
  464. ;; Search path for service types.
  465. (make-parameter `((,%guix-home-root-directory . "gnu/home/services"))))
  466. (define (all-home-service-modules)
  467. "Return the default set of `home service' modules."
  468. (cons (resolve-interface '(gnu home services))
  469. (all-modules (%service-type-path)
  470. #:warn warn-about-load-error)))
  471. (define* (fold-home-service-types proc seed)
  472. (fold-service-types proc seed (all-home-service-modules)))