services.scm 22 KB

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