services.scm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. (gexp->script
  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. (when (not (file-exists? flag-file-path))
  266. (begin #$@gexps (touch flag-file-path))))))
  267. (define (on-first-login-script-entry m-on-first-login)
  268. "Return, as a monadic value, an entry for the on-first-login script
  269. in the home environment directory."
  270. (mlet %store-monad ((on-first-login m-on-first-login))
  271. (return `(("on-first-login" ,on-first-login)))))
  272. (define home-run-on-first-login-service-type
  273. (service-type (name 'home-run-on-first-login)
  274. (extensions
  275. (list (service-extension
  276. home-service-type
  277. on-first-login-script-entry)))
  278. (compose identity)
  279. (extend compute-on-first-login-script)
  280. (default-value #f)
  281. (description "Run gexps on first user login. Can be
  282. extended with one gexp.")))
  283. (define (compute-activation-script init-gexp gexps)
  284. (gexp->script
  285. "activate"
  286. #~(let* ((he-init-file (lambda (he) (string-append he "/setup-environment")))
  287. (he-path (string-append (getenv "HOME") "/.guix-home"))
  288. (new-home-env (getenv "GUIX_NEW_HOME"))
  289. (new-home (or new-home-env
  290. ;; Path of the activation file if called interactively
  291. (dirname (car (command-line)))))
  292. (old-home-env (getenv "GUIX_OLD_HOME"))
  293. (old-home (or old-home-env
  294. (if (file-exists? (he-init-file he-path))
  295. (readlink he-path)
  296. #f))))
  297. (if (file-exists? (he-init-file new-home))
  298. (let* ((port ((@ (ice-9 popen) open-input-pipe)
  299. (format #f "source ~a && env -0"
  300. (he-init-file new-home))))
  301. (result ((@ (ice-9 rdelim) read-delimited) "" port))
  302. (vars (map (lambda (x)
  303. (let ((si (string-index x #\=)))
  304. (cons (string-take x si)
  305. (string-drop x (1+ si)))))
  306. ((@ (srfi srfi-1) remove)
  307. string-null?
  308. (string-split result #\nul)))))
  309. (close-port port)
  310. (map (lambda (x) (setenv (car x) (cdr x))) vars)
  311. (setenv "GUIX_NEW_HOME" new-home)
  312. (setenv "GUIX_OLD_HOME" old-home)
  313. #$@gexps
  314. ;; Do not unset env variable if it was set outside.
  315. (unless new-home-env (setenv "GUIX_NEW_HOME" #f))
  316. (unless old-home-env (setenv "GUIX_OLD_HOME" #f)))
  317. (format #t "\
  318. Activation script was either called or loaded by file from this direcotry:
  319. ~a
  320. It doesn't seem that home environment is somewhere around.
  321. Make sure that you call ./activate by symlink from -home store item.\n"
  322. new-home)))))
  323. (define (activation-script-entry m-activation)
  324. "Return, as a monadic value, an entry for the activation script
  325. in the home environment directory."
  326. (mlet %store-monad ((activation m-activation))
  327. (return `(("activate" ,activation)))))
  328. (define home-activation-service-type
  329. (service-type (name 'home-activation)
  330. (extensions
  331. (list (service-extension
  332. home-service-type
  333. activation-script-entry)))
  334. (compose identity)
  335. (extend compute-activation-script)
  336. (default-value #f)
  337. (description "Run gexps to activate the current
  338. generation of home environment and update the state of the home
  339. directory. @command{activate} script automatically called during
  340. reconfiguration or generation switching. This service can be extended
  341. with one gexp, but many times, and all gexps must be idempotent.")))
  342. ;;;
  343. ;;; On-change.
  344. ;;;
  345. (define (compute-on-change-gexp eval-gexps? pattern-gexp-tuples)
  346. #~(begin
  347. (define (equal-regulars? file1 file2)
  348. "Check if FILE1 and FILE2 are bit for bit identical."
  349. (let* ((cmp-binary #$(file-append
  350. (@ (gnu packages base) diffutils) "/bin/cmp"))
  351. (stats1 (lstat file1))
  352. (stats2 (lstat file2)))
  353. (cond
  354. ((= (stat:ino stats1) (stat:ino stats2)) #t)
  355. ((not (= (stat:size stats1) (stat:size stats2))) #f)
  356. (else (= (system* cmp-binary file1 file2) 0)))))
  357. (define (equal-symlinks? symlink1 symlink2)
  358. "Check if SYMLINK1 and SYMLINK2 are pointing to the same target."
  359. (string=? (readlink symlink1) (readlink symlink2)))
  360. (define (equal-directories? dir1 dir2)
  361. "Check if DIR1 and DIR2 have the same content."
  362. (define (ordinary-file file)
  363. (not (or (string=? file ".")
  364. (string=? file ".."))))
  365. (let* ((files1 (scandir dir1 ordinary-file))
  366. (files2 (scandir dir2 ordinary-file)))
  367. (if (equal? files1 files2)
  368. (map (lambda (file)
  369. (equal-files?
  370. (string-append dir1 "/" file)
  371. (string-append dir2 "/" file)))
  372. files1)
  373. #f)))
  374. (define (equal-files? file1 file2)
  375. "Compares files, symlinks or directories of the same type."
  376. (case (file-type file1)
  377. ((directory) (equal-directories? file1 file2))
  378. ((symlink) (equal-symlinks? file1 file2))
  379. ((regular) (equal-regulars? file1 file2))
  380. (else
  381. (display "The file type is unsupported by on-change service.\n")
  382. #f)))
  383. (define (file-type file)
  384. (stat:type (lstat file)))
  385. (define (something-changed? file1 file2)
  386. (cond
  387. ((and (not (file-exists? file1))
  388. (not (file-exists? file2))) #f)
  389. ((or (not (file-exists? file1))
  390. (not (file-exists? file2))) #t)
  391. ((not (eq? (file-type file1) (file-type file2))) #t)
  392. (else
  393. (not (equal-files? file1 file2)))))
  394. (define expressions-to-eval
  395. (map
  396. (lambda (x)
  397. (let* ((file1 (string-append
  398. (or (getenv "GUIX_OLD_HOME")
  399. "/gnu/store/non-existing-generation")
  400. "/" (car x)))
  401. (file2 (string-append (getenv "GUIX_NEW_HOME") "/" (car x)))
  402. (_ (format #t "Comparing ~a and\n~10t~a..." file1 file2))
  403. (any-changes? (something-changed? file1 file2))
  404. (_ (format #t " done (~a)\n"
  405. (if any-changes? "changed" "same"))))
  406. (if any-changes? (cadr x) "")))
  407. '#$pattern-gexp-tuples))
  408. (if #$eval-gexps?
  409. (begin
  410. (display "Evaling on-change gexps.\n\n")
  411. (for-each primitive-eval expressions-to-eval)
  412. (display "On-change gexps evaluation finished.\n\n"))
  413. (display "\
  414. On-change gexps won't be evaluated, disabled by service
  415. configuration.\n"))))
  416. (define home-run-on-change-service-type
  417. (service-type (name 'home-run-on-change)
  418. (extensions
  419. (list (service-extension
  420. home-activation-service-type
  421. identity)))
  422. (compose concatenate)
  423. (extend compute-on-change-gexp)
  424. (default-value #t)
  425. (description "\
  426. G-expressions to run if the specified files have changed since the
  427. last generation. The extension should be a list of lists where the
  428. first element is the pattern for file or directory that expected to be
  429. changed, and the second element is the G-expression to be evaluated.")))
  430. ;;;
  431. ;;; Provenance tracking.
  432. ;;;
  433. (define home-provenance-service-type
  434. (service-type
  435. (name 'home-provenance)
  436. (extensions
  437. (list (service-extension
  438. home-service-type
  439. (service-extension-compute
  440. (first (service-type-extensions provenance-service-type))))))
  441. (default-value #f) ;the HE config file
  442. (description "\
  443. Store provenance information about the home environment in the home
  444. environment itself: the channels used when building the home
  445. environment, and its configuration file, when available.")))
  446. (define sexp->home-provenance sexp->system-provenance)
  447. (define home-provenance system-provenance)
  448. ;;;
  449. ;;; Searching
  450. ;;;
  451. (define (parent-directory directory)
  452. "Get the parent directory of DIRECTORY"
  453. (string-join (drop-right (string-split directory #\/) 1) "/"))
  454. (define %guix-home-root-directory
  455. ;; Absolute file name of the module hierarchy.
  456. (parent-directory
  457. (dirname (dirname (search-path %load-path "gnu/home/services.scm")))))
  458. (define %service-type-path
  459. ;; Search path for service types.
  460. (make-parameter `((,%guix-home-root-directory . "gnu/home/services"))))
  461. (define (all-home-service-modules)
  462. "Return the default set of `home service' modules."
  463. (cons (resolve-interface '(gnu home services))
  464. (all-modules (%service-type-path)
  465. #:warn warn-about-load-error)))
  466. (define* (fold-home-service-types proc seed)
  467. (fold-service-types proc seed (all-home-service-modules)))