xorg.scm 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Andy Wingo <wingo@igalia.com>
  3. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2015 Sou Bunnbu <iyzsong@gmail.com>
  5. ;;; Copyright © 2018, 2019 Timothy Sample <samplet@ngyro.com>
  6. ;;; Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  7. ;;; Copyright © 2019 Tim Gesthuizen <tim.gesthuizen@yahoo.de>
  8. ;;; Copyright © 2020 shtwzrd <shtwzrd@protonmail.com>
  9. ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
  10. ;;; Copyright © 2020 Alex Griffin <a@ajgrf.com>
  11. ;;; Copyright © 2021 Brice Waegeneire <brice@waegenei.re>
  12. ;;; Copyright © 2021 Oleg Pykhalov <go.wigust@gmail.com>
  13. ;;; Copyright © 2021 Josselin Poiret <josselin.poiret@protonmail.ch>
  14. ;;; Copyright © 2022 Chris Marusich <cmmarusich@gmail.com>
  15. ;;;
  16. ;;; This file is part of GNU Guix.
  17. ;;;
  18. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  19. ;;; under the terms of the GNU General Public License as published by
  20. ;;; the Free Software Foundation; either version 3 of the License, or (at
  21. ;;; your option) any later version.
  22. ;;;
  23. ;;; GNU Guix is distributed in the hope that it will be useful, but
  24. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  25. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. ;;; GNU General Public License for more details.
  27. ;;;
  28. ;;; You should have received a copy of the GNU General Public License
  29. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  30. (define-module (gnu services xorg)
  31. #:autoload (gnu services sddm) (sddm-service-type)
  32. #:use-module (gnu artwork)
  33. #:use-module (gnu services)
  34. #:use-module (gnu services shepherd)
  35. #:use-module (gnu system pam)
  36. #:use-module (gnu system setuid)
  37. #:use-module (gnu system keyboard)
  38. #:use-module (gnu services base)
  39. #:use-module (gnu services dbus)
  40. #:use-module (gnu packages base)
  41. #:use-module (gnu packages guile)
  42. #:use-module (gnu packages xorg)
  43. #:use-module (gnu packages fonts)
  44. #:use-module (gnu packages gl)
  45. #:use-module (gnu packages glib)
  46. #:use-module (gnu packages display-managers)
  47. #:use-module (gnu packages freedesktop)
  48. #:use-module (gnu packages gnustep)
  49. #:use-module (gnu packages gnome)
  50. #:use-module (gnu packages admin)
  51. #:use-module (gnu packages bash)
  52. #:use-module (gnu system shadow)
  53. #:use-module (guix build-system glib-or-gtk)
  54. #:use-module (guix build-system trivial)
  55. #:use-module (guix gexp)
  56. #:use-module (guix store)
  57. #:use-module (guix packages)
  58. #:use-module (guix derivations)
  59. #:use-module (guix records)
  60. #:use-module (guix deprecation)
  61. #:use-module (guix utils)
  62. #:use-module (srfi srfi-1)
  63. #:use-module (srfi srfi-9)
  64. #:use-module (srfi srfi-26)
  65. #:use-module (ice-9 match)
  66. #:export (xorg-configuration
  67. xorg-configuration?
  68. xorg-configuration-modules
  69. xorg-configuration-fonts
  70. xorg-configuration-drivers
  71. xorg-configuration-resolutions
  72. xorg-configuration-extra-config
  73. xorg-configuration-server
  74. xorg-configuration-server-arguments
  75. %default-xorg-modules
  76. %default-xorg-fonts
  77. %default-xorg-server-arguments
  78. xorg-wrapper
  79. xorg-start-command
  80. xinitrc
  81. xorg-server-service-type
  82. %default-slim-theme
  83. %default-slim-theme-name
  84. slim-configuration
  85. slim-configuration?
  86. slim-configuration-slim
  87. slim-configuration-allow-empty-passwords?
  88. slim-configuration-auto-login?
  89. slim-configuration-default-user
  90. slim-configuration-theme
  91. slim-configuration-theme-name
  92. slim-configuration-xauth
  93. slim-configuration-shepherd
  94. slim-configuration-auto-login-session
  95. slim-configuration-xorg
  96. slim-configuration-display
  97. slim-configuration-vt
  98. slim-configuration-sessreg
  99. slim-service-type
  100. screen-locker
  101. screen-locker?
  102. screen-locker-service-type
  103. screen-locker-service
  104. localed-configuration
  105. localed-configuration?
  106. localed-service-type
  107. gdm-configuration
  108. gdm-service-type
  109. handle-xorg-configuration
  110. set-xorg-configuration))
  111. ;;; Commentary:
  112. ;;;
  113. ;;; Services that relate to the X Window System.
  114. ;;;
  115. ;;; Code:
  116. (define %default-xorg-modules
  117. ;; Default list of modules loaded by the server. When multiple drivers
  118. ;; match, the first one in the list is loaded.
  119. (list xf86-video-vesa
  120. xf86-video-fbdev
  121. xf86-video-amdgpu
  122. xf86-video-ati
  123. xf86-video-cirrus
  124. xf86-video-intel
  125. xf86-video-mach64
  126. xf86-video-nouveau
  127. xf86-video-nv
  128. xf86-video-sis
  129. ;; Libinput is the new thing and is recommended over evdev/synaptics:
  130. ;; <http://who-t.blogspot.fr/2015/01/xf86-input-libinput-compatibility-with.html>.
  131. xf86-input-libinput
  132. xf86-input-evdev
  133. xf86-input-keyboard
  134. xf86-input-mouse))
  135. (define %default-xorg-fonts
  136. ;; Default list of fonts available to the X server.
  137. (list (file-append font-alias "/share/fonts/X11/75dpi")
  138. (file-append font-alias "/share/fonts/X11/100dpi")
  139. (file-append font-alias "/share/fonts/X11/misc")
  140. (file-append font-alias "/share/fonts/X11/cyrillic")
  141. (file-append font-misc-misc ;default fonts for xterm
  142. "/share/fonts/X11/misc")
  143. (file-append font-adobe75dpi "/share/fonts/X11/75dpi")))
  144. (define %default-xorg-server-arguments
  145. ;; Default command-line arguments for X.
  146. '("-nolisten" "tcp"))
  147. ;; Configuration of an Xorg server.
  148. (define-record-type* <xorg-configuration>
  149. xorg-configuration make-xorg-configuration
  150. xorg-configuration?
  151. (modules xorg-configuration-modules ;list of file-like
  152. (thunked)
  153. ; filter out modules not supported on current system
  154. (default (filter
  155. (lambda (p)
  156. (member (%current-system)
  157. (package-supported-systems p)))
  158. %default-xorg-modules)))
  159. (fonts xorg-configuration-fonts ;list of packges
  160. (default %default-xorg-fonts))
  161. (drivers xorg-configuration-drivers ;list of strings
  162. (default '()))
  163. (resolutions xorg-configuration-resolutions ;list of tuples
  164. (default '()))
  165. (keyboard-layout xorg-configuration-keyboard-layout ;#f | <keyboard-layout>
  166. (default #f))
  167. (extra-config xorg-configuration-extra-config ;list of strings
  168. (default '()))
  169. (server xorg-configuration-server ;file-like
  170. (default xorg-server))
  171. (server-arguments xorg-configuration-server-arguments ;list of strings
  172. (default %default-xorg-server-arguments)))
  173. (define (xorg-configuration->file config)
  174. "Compute an Xorg configuration file corresponding to CONFIG, an
  175. <xorg-configuration> record."
  176. (let ((xorg-server (xorg-configuration-server config)))
  177. (define all-modules
  178. ;; 'xorg-server' provides 'fbdevhw.so' etc.
  179. (append (xorg-configuration-modules config)
  180. (list xorg-server)))
  181. (define build
  182. #~(begin
  183. (use-modules (ice-9 match)
  184. (srfi srfi-1)
  185. (srfi srfi-26))
  186. (call-with-output-file #$output
  187. (lambda (port)
  188. (define drivers
  189. '#$(xorg-configuration-drivers config))
  190. (define (device-section driver)
  191. (string-append "
  192. Section \"Device\"
  193. Identifier \"device-" driver "\"
  194. Driver \"" driver "\"
  195. EndSection"))
  196. (define (screen-section driver resolutions)
  197. (string-append "
  198. Section \"Screen\"
  199. Identifier \"screen-" driver "\"
  200. Device \"device-" driver "\"
  201. SubSection \"Display\"
  202. Modes "
  203. (string-join (map (match-lambda
  204. ((x y)
  205. (string-append "\"" (number->string x)
  206. "x" (number->string y) "\"")))
  207. resolutions)) "
  208. EndSubSection
  209. EndSection"))
  210. (define (input-class-section layout variant model options)
  211. (string-append "
  212. Section \"InputClass\"
  213. Identifier \"evdev keyboard catchall\"
  214. MatchIsKeyboard \"on\"
  215. Option \"XkbLayout\" " (object->string layout)
  216. (if variant
  217. (string-append " Option \"XkbVariant\" \""
  218. variant "\"")
  219. "")
  220. (if model
  221. (string-append " Option \"XkbModel\" \""
  222. model "\"")
  223. "")
  224. (match options
  225. (()
  226. "")
  227. (_
  228. (string-append " Option \"XkbOptions\" \""
  229. (string-join options ",") "\""))) "
  230. MatchDevicePath \"/dev/input/event*\"
  231. Driver \"evdev\"
  232. EndSection\n"))
  233. (define (expand modules)
  234. ;; Append to MODULES the relevant /lib/xorg/modules
  235. ;; sub-directories.
  236. (append-map (lambda (module)
  237. (filter-map (lambda (directory)
  238. (let ((full (string-append module
  239. directory)))
  240. (and (file-exists? full)
  241. full)))
  242. '("/lib/xorg/modules/drivers"
  243. "/lib/xorg/modules/input"
  244. "/lib/xorg/modules/multimedia"
  245. "/lib/xorg/modules/extensions")))
  246. modules))
  247. (display "Section \"Files\"\n" port)
  248. (for-each (lambda (font)
  249. (format port " FontPath \"~a\"~%" font))
  250. '#$(xorg-configuration-fonts config))
  251. (for-each (lambda (module)
  252. (format port
  253. " ModulePath \"~a\"~%"
  254. module))
  255. (append (expand '#$all-modules)
  256. ;; For fbdevhw.so and so on.
  257. (list #$(file-append xorg-server
  258. "/lib/xorg/modules"))))
  259. (display "EndSection\n" port)
  260. (display "
  261. Section \"ServerFlags\"
  262. Option \"AllowMouseOpenFail\" \"on\"
  263. EndSection\n" port)
  264. (display (string-join (map device-section drivers) "\n")
  265. port)
  266. (newline port)
  267. (display (string-join
  268. (map (cut screen-section <>
  269. '#$(xorg-configuration-resolutions config))
  270. drivers)
  271. "\n")
  272. port)
  273. (newline port)
  274. (let ((layout #$(and=> (xorg-configuration-keyboard-layout config)
  275. keyboard-layout-name))
  276. (variant #$(and=> (xorg-configuration-keyboard-layout config)
  277. keyboard-layout-variant))
  278. (model #$(and=> (xorg-configuration-keyboard-layout config)
  279. keyboard-layout-model))
  280. (options '#$(and=> (xorg-configuration-keyboard-layout config)
  281. keyboard-layout-options)))
  282. (when layout
  283. (display (input-class-section layout variant model options)
  284. port)
  285. (newline port)))
  286. (for-each (lambda (config)
  287. (display config port))
  288. '#$(xorg-configuration-extra-config config))))))
  289. (computed-file "xserver.conf" build)))
  290. (define (xorg-configuration-directory modules)
  291. "Return a directory that contains the @code{.conf} files for X.org that
  292. includes the @code{share/X11/xorg.conf.d} directories of each package listed
  293. in @var{modules}."
  294. (with-imported-modules '((guix build utils))
  295. (computed-file "xorg.conf.d"
  296. #~(begin
  297. (use-modules (guix build utils)
  298. (srfi srfi-1))
  299. (define files
  300. (append-map (lambda (module)
  301. (find-files (string-append
  302. module
  303. "/share/X11/xorg.conf.d")
  304. "\\.conf$"))
  305. (list #$@modules)))
  306. (mkdir #$output)
  307. (for-each (lambda (file)
  308. (symlink file
  309. (string-append #$output "/"
  310. (basename file))))
  311. files)
  312. #t))))
  313. (define* (xorg-wrapper #:optional (config (xorg-configuration)))
  314. "Return a derivation that builds a script to start the X server with the
  315. given @var{config}. The resulting script should be used in place of
  316. @code{/usr/bin/X}."
  317. (define exp
  318. ;; Write a small wrapper around the X server.
  319. #~(begin
  320. (setenv "XORG_DRI_DRIVER_PATH" (string-append #$mesa "/lib/dri"))
  321. (setenv "XKB_BINDIR" (string-append #$xkbcomp "/bin"))
  322. (let ((X (string-append #$(xorg-configuration-server config) "/bin/X")))
  323. (apply execl X X
  324. "-xkbdir" (string-append #$xkeyboard-config "/share/X11/xkb")
  325. "-config" #$(xorg-configuration->file config)
  326. "-configdir" #$(xorg-configuration-directory
  327. (xorg-configuration-modules config))
  328. (cdr (command-line))))))
  329. (program-file "X-wrapper" exp))
  330. (define* (xorg-start-command #:optional (config (xorg-configuration)))
  331. "Return a @code{startx} script in which the modules, fonts, etc. specified
  332. in @var{config}, are available. The result should be used in place of
  333. @code{startx}."
  334. (define X
  335. (xorg-wrapper config))
  336. (define exp
  337. ;; Write a small wrapper around the X server.
  338. #~(apply execl #$X #$X ;; Second #$X is for argv[0].
  339. "-logverbose" "-verbose" "-terminate"
  340. #$@(xorg-configuration-server-arguments config)
  341. (cdr (command-line))))
  342. (program-file "startx" exp))
  343. (define* (xinitrc #:key fallback-session)
  344. "Return a system-wide xinitrc script that starts the specified X session,
  345. which should be passed to this script as the first argument. If not, the
  346. @var{fallback-session} will be used or, if @var{fallback-session} is false, a
  347. desktop session from the system or user profile will be used."
  348. (define builder
  349. #~(begin
  350. (use-modules (ice-9 match)
  351. (ice-9 regex)
  352. (ice-9 ftw)
  353. (ice-9 rdelim)
  354. (srfi srfi-1)
  355. (srfi srfi-26))
  356. (define (close-all-fdes)
  357. ;; Close all the open file descriptors except 0 to 2.
  358. (let loop ((fd 3))
  359. (when (< fd 4096) ;FIXME: use sysconf + _SC_OPEN_MAX
  360. (false-if-exception (close-fdes fd))
  361. (loop (+ 1 fd)))))
  362. (define (exec-from-login-shell command . args)
  363. ;; Run COMMAND from a login shell so that it gets to see the same
  364. ;; environment variables that one gets when logging in on a tty, for
  365. ;; instance.
  366. (let* ((pw (getpw (getuid)))
  367. (shell (passwd:shell pw)))
  368. ;; Close any open file descriptors. This is all the more
  369. ;; important that SLiM itself exec's us directly without closing
  370. ;; its own file descriptors!
  371. (close-all-fdes)
  372. ;; The '--login' option is supported at least by Bash and zsh.
  373. (execl shell shell "--login" "-c"
  374. (string-join (cons command args)))))
  375. (define system-profile
  376. "/run/current-system/profile")
  377. (define user-profile
  378. (and=> (getpw (getuid))
  379. (lambda (pw)
  380. (string-append (passwd:dir pw) "/.guix-profile"))))
  381. (define (xsession-command desktop-file)
  382. ;; Read from DESKTOP-FILE its X session command and return it as a
  383. ;; list.
  384. (define exec-regexp
  385. (make-regexp "^[[:blank:]]*Exec=(.*)$"))
  386. (call-with-input-file desktop-file
  387. (lambda (port)
  388. (let loop ()
  389. (match (read-line port)
  390. ((? eof-object?) #f)
  391. ((= (cut regexp-exec exec-regexp <>) result)
  392. (if result
  393. (string-tokenize (match:substring result 1))
  394. (loop))))))))
  395. (define (find-session profile)
  396. ;; Return an X session command from PROFILE or #f if none was found.
  397. (let ((directory (string-append profile "/share/xsessions")))
  398. (match (scandir directory
  399. (cut string-suffix? ".desktop" <>))
  400. ((or () #f)
  401. #f)
  402. ((sessions ...)
  403. (any xsession-command
  404. (map (cut string-append directory "/" <>)
  405. sessions))))))
  406. (let* ((home (getenv "HOME"))
  407. (xsession-file (string-append home "/.xsession"))
  408. (session (match (command-line)
  409. ((_)
  410. #$(if fallback-session
  411. #~(list #$fallback-session)
  412. #f))
  413. ((_ x ..1)
  414. x))))
  415. (if (file-exists? xsession-file)
  416. ;; Run ~/.xsession when it exists.
  417. (apply exec-from-login-shell xsession-file
  418. (or session '()))
  419. ;; Otherwise, start the specified session or a fallback.
  420. (apply exec-from-login-shell
  421. (or session
  422. (find-session user-profile)
  423. (find-session system-profile)))))))
  424. (program-file "xinitrc" builder))
  425. (define-syntax handle-xorg-configuration
  426. (syntax-rules ()
  427. "Generate the `compose' and `extend' entries of a login manager
  428. `service-type' to handle specifying the `xorg-configuration' through
  429. a `service-extension', as used by `set-xorg-configuration'."
  430. ((_ configuration-record service-type-definition)
  431. (service-type
  432. (inherit service-type-definition)
  433. (compose (lambda (extensions)
  434. (match extensions
  435. (() #f)
  436. ((config . _) config))))
  437. (extend (lambda (config xorg-configuration)
  438. (if xorg-configuration
  439. (configuration-record
  440. (inherit config)
  441. (xorg-configuration xorg-configuration))
  442. config)))))))
  443. (define (xorg-server-profile-service config)
  444. ;; XXX: profile-service-type only accepts <package> objects.
  445. (list
  446. (package
  447. (name "xorg-wrapper")
  448. (version (package-version xorg-server))
  449. (source (xorg-wrapper config))
  450. (build-system trivial-build-system)
  451. (arguments
  452. '(#:modules ((guix build utils))
  453. #:builder
  454. (begin
  455. (use-modules (guix build utils))
  456. (let* ((source (assoc-ref %build-inputs "source"))
  457. (out (assoc-ref %outputs "out"))
  458. (bin (string-append out "/bin")))
  459. (mkdir-p bin)
  460. (symlink source (string-append bin "/X"))
  461. (symlink source (string-append bin "/Xorg"))
  462. #t))))
  463. (home-page (package-home-page xorg-server))
  464. (synopsis (package-synopsis xorg-server))
  465. (description (package-description xorg-server))
  466. (license (package-license xorg-server)))))
  467. (define xorg-server-service-type
  468. (service-type
  469. (name 'xorg-server)
  470. (extensions
  471. (list (service-extension profile-service-type
  472. xorg-server-profile-service)))
  473. (default-value (xorg-configuration))
  474. (description "Add @command{X} to the system profile, to be used with
  475. @command{sx} or @command{xinit}.")))
  476. ;;;
  477. ;;; SLiM log-in manager.
  478. ;;;
  479. (define %default-slim-theme
  480. ;; Theme based on work by Felipe López.
  481. (file-append %artwork-repository "/slim"))
  482. (define %default-slim-theme-name
  483. ;; This must be the name of the sub-directory in %DEFAULT-SLIM-THEME that
  484. ;; contains the actual theme files.
  485. "1.x")
  486. (define-record-type* <slim-configuration>
  487. slim-configuration make-slim-configuration
  488. slim-configuration?
  489. (slim slim-configuration-slim
  490. (default slim))
  491. (allow-empty-passwords? slim-configuration-allow-empty-passwords?
  492. (default #t))
  493. (gnupg? slim-configuration-gnupg?
  494. (default #f))
  495. (auto-login? slim-configuration-auto-login?
  496. (default #f))
  497. (default-user slim-configuration-default-user
  498. (default ""))
  499. (theme slim-configuration-theme
  500. (default %default-slim-theme))
  501. (theme-name slim-configuration-theme-name
  502. (default %default-slim-theme-name))
  503. (xauth slim-configuration-xauth
  504. (default xauth))
  505. (shepherd slim-configuration-shepherd
  506. (default shepherd))
  507. (auto-login-session slim-configuration-auto-login-session
  508. (default #f))
  509. (xorg-configuration slim-configuration-xorg
  510. (default (xorg-configuration)))
  511. (display slim-configuration-display
  512. (default ":0"))
  513. (vt slim-configuration-vt
  514. (default "vt7"))
  515. (sessreg slim-configuration-sessreg
  516. (default sessreg)))
  517. (define (slim-pam-service config)
  518. "Return a PAM service for @command{slim}."
  519. (list (unix-pam-service
  520. "slim"
  521. #:login-uid? #t
  522. #:allow-empty-passwords?
  523. (slim-configuration-allow-empty-passwords? config)
  524. #:gnupg?
  525. (slim-configuration-gnupg? config))))
  526. (define (slim-shepherd-service config)
  527. (let* ((xinitrc (xinitrc #:fallback-session
  528. (slim-configuration-auto-login-session config)))
  529. (xauth (slim-configuration-xauth config))
  530. (startx (xorg-start-command (slim-configuration-xorg config)))
  531. (display (slim-configuration-display config))
  532. (vt (slim-configuration-vt config))
  533. (shepherd (slim-configuration-shepherd config))
  534. (theme-name (slim-configuration-theme-name config))
  535. (sessreg (slim-configuration-sessreg config))
  536. (lockfile (string-append "/var/run/slim-" vt ".lock")))
  537. (define slim.cfg
  538. (mixed-text-file "slim.cfg" "
  539. default_path /run/current-system/profile/bin
  540. default_xserver " startx "
  541. display_name " display "
  542. xserver_arguments " vt "
  543. xauth_path " xauth "/bin/xauth
  544. authfile /var/run/slim-" vt ".auth
  545. lockfile " lockfile "
  546. logfile /var/log/slim-" vt ".log
  547. # The login command. '%session' is replaced by the chosen session name, one
  548. # of the names specified in the 'sessions' setting: 'wmaker', 'xfce', etc.
  549. login_cmd exec " xinitrc " %session
  550. sessiondir /run/current-system/profile/share/xsessions
  551. session_msg session (F1 to change):
  552. sessionstart_cmd " sessreg "/bin/sessreg -a -l $DISPLAY %user
  553. sessionstop_cmd " sessreg "/bin/sessreg -d -l $DISPLAY %user
  554. halt_cmd " shepherd "/sbin/halt
  555. reboot_cmd " shepherd "/sbin/reboot\n"
  556. (if (slim-configuration-auto-login? config)
  557. (string-append "auto_login yes\ndefault_user "
  558. (slim-configuration-default-user config) "\n")
  559. "")
  560. (if theme-name
  561. (string-append "current_theme " theme-name "\n")
  562. "")))
  563. (define theme
  564. (slim-configuration-theme config))
  565. (list (shepherd-service
  566. (documentation "Xorg display server")
  567. (provision (append
  568. ;; For compatibility, also provide 'xorg-server'.
  569. (if (string=? vt "vt7")
  570. '(xorg-server)
  571. '())
  572. (list (symbol-append 'xorg-server-
  573. (string->symbol vt)))))
  574. (requirement '(user-processes host-name udev))
  575. (start
  576. #~(lambda ()
  577. ;; A stale lock file can prevent SLiM from starting, so remove it to
  578. ;; be on the safe side.
  579. (false-if-exception (delete-file lockfile))
  580. (fork+exec-command
  581. (list (string-append #$(slim-configuration-slim config)
  582. "/bin/slim")
  583. "-nodaemon")
  584. #:environment-variables
  585. (list (string-append "SLIM_CFGFILE=" #$slim.cfg)
  586. #$@(if theme
  587. (list #~(string-append "SLIM_THEMESDIR=" #$theme))
  588. #~())))))
  589. (stop #~(make-kill-destructor))
  590. (respawn? #t)))))
  591. (define slim-service-type
  592. (handle-xorg-configuration slim-configuration
  593. (service-type (name 'slim)
  594. (extensions
  595. (list (service-extension shepherd-root-service-type
  596. slim-shepherd-service)
  597. (service-extension pam-root-service-type
  598. slim-pam-service)
  599. ;; Unconditionally add xterm to the system profile, to
  600. ;; avoid bad surprises.
  601. (service-extension profile-service-type
  602. (const (list xterm)))))
  603. (default-value (slim-configuration))
  604. (description
  605. "Run the SLiM graphical login manager for X11."))))
  606. ;;;
  607. ;;; Screen lockers & co.
  608. ;;;
  609. (define-record-type <screen-locker>
  610. (screen-locker name program empty?)
  611. screen-locker?
  612. (name screen-locker-name) ;string
  613. (program screen-locker-program) ;gexp
  614. (empty? screen-locker-allows-empty-passwords?)) ;Boolean
  615. (define screen-locker-pam-services
  616. (match-lambda
  617. (($ <screen-locker> name _ empty?)
  618. (list (unix-pam-service name
  619. #:allow-empty-passwords? empty?)))))
  620. (define screen-locker-setuid-programs
  621. (compose list file-like->setuid-program screen-locker-program))
  622. (define screen-locker-service-type
  623. (service-type (name 'screen-locker)
  624. (extensions
  625. (list (service-extension pam-root-service-type
  626. screen-locker-pam-services)
  627. (service-extension setuid-program-service-type
  628. screen-locker-setuid-programs)))
  629. (description
  630. "Allow the given program to be used as a screen locker for
  631. the graphical server by making it setuid-root, so it can authenticate users,
  632. and by creating a PAM service for it.")))
  633. (define* (screen-locker-service package
  634. #:optional
  635. (program (package-name package))
  636. #:key allow-empty-passwords?)
  637. "Add @var{package}, a package for a screen locker or screen saver whose
  638. command is @var{program}, to the set of setuid programs and add a PAM entry
  639. for it. For example:
  640. @lisp
  641. (screen-locker-service xlockmore \"xlock\")
  642. @end lisp
  643. makes the good ol' XlockMore usable."
  644. (service screen-locker-service-type
  645. (screen-locker program
  646. (file-append package "/bin/" program)
  647. allow-empty-passwords?)))
  648. ;;;
  649. ;;; Locale service.
  650. ;;;
  651. (define-record-type* <localed-configuration>
  652. localed-configuration make-localed-configuration
  653. localed-configuration?
  654. (localed localed-configuration-localed
  655. (default localed))
  656. (keyboard-layout localed-configuration-keyboard-layout
  657. (default #f)))
  658. (define (localed-dbus-service config)
  659. "Return the 'localed' D-Bus service for @var{config}, a
  660. @code{<localed-configuration>} record."
  661. (define keyboard-layout
  662. (localed-configuration-keyboard-layout config))
  663. ;; The primary purpose of 'localed' is to tell GDM what the "current" Xorg
  664. ;; keyboard layout is. If 'localed' is missing, or if it's unable to
  665. ;; determine the current XKB layout, then GDM forcefully installs its
  666. ;; default XKB config (US English). Here we communicate the configured
  667. ;; layout through environment variables.
  668. (if keyboard-layout
  669. (let* ((layout (keyboard-layout-name keyboard-layout))
  670. (variant (keyboard-layout-variant keyboard-layout))
  671. (model (keyboard-layout-model keyboard-layout))
  672. (options (keyboard-layout-options keyboard-layout)))
  673. (list (wrapped-dbus-service
  674. (localed-configuration-localed config)
  675. "libexec/localed/localed"
  676. `(("GUIX_XKB_LAYOUT" ,layout)
  677. ,@(if variant
  678. `(("GUIX_XKB_VARIANT" ,variant))
  679. '())
  680. ,@(if model
  681. `(("GUIX_XKB_MODEL" ,model))
  682. '())
  683. ,@(if (null? options)
  684. '()
  685. `(("GUIX_XKB_OPTIONS"
  686. ,(string-join options ","))))))))
  687. '()))
  688. (define localed-service-type
  689. (let ((package (lambda (config)
  690. ;; Don't bother if the user didn't specify any keyboard
  691. ;; layout.
  692. (if (localed-configuration-keyboard-layout config)
  693. (list (localed-configuration-localed config))
  694. '()))))
  695. (service-type (name 'localed)
  696. (extensions
  697. (list (service-extension dbus-root-service-type
  698. localed-dbus-service)
  699. (service-extension udev-service-type package)
  700. (service-extension polkit-service-type package)
  701. ;; Add 'localectl' to the profile.
  702. (service-extension profile-service-type package)))
  703. ;; This service can be extended, typically by the X login
  704. ;; manager, to communicate the chosen Xorg keyboard layout.
  705. (compose (lambda (extensions)
  706. (find keyboard-layout? extensions)))
  707. (extend (lambda (config keyboard-layout)
  708. (localed-configuration
  709. (inherit config)
  710. (keyboard-layout keyboard-layout))))
  711. (description
  712. "Run the locale daemon, @command{localed}, which can be used
  713. to control the system locale and keyboard mapping from user programs such as
  714. the GNOME desktop environment.")
  715. (default-value (localed-configuration)))))
  716. ;;;
  717. ;;; GNOME Desktop Manager.
  718. ;;;
  719. (define %gdm-accounts
  720. (list (user-group (name "gdm") (system? #t))
  721. (user-account
  722. (name "gdm")
  723. (group "gdm")
  724. (supplementary-groups '("video"))
  725. (system? #t)
  726. (comment "GNOME Display Manager user")
  727. (home-directory "/var/lib/gdm")
  728. (shell (file-append shadow "/sbin/nologin")))))
  729. (define dbus-daemon-wrapper
  730. (program-file
  731. "gdm-dbus-wrapper"
  732. #~(begin
  733. (use-modules (srfi srfi-26))
  734. (define system-profile
  735. "/run/current-system/profile")
  736. (define user-profile
  737. (and=> (getpw (getuid))
  738. (lambda (pw)
  739. (string-append (passwd:dir pw) "/.guix-profile"))))
  740. ;; If we are able to find the user's profile, we can add it to
  741. ;; the search paths set below. We need to do this so that D-Bus
  742. ;; can start services installed by the user. This allows
  743. ;; applications that require session D-Bus services (e.g,
  744. ;; 'evolution') to work even if those services are only available
  745. ;; in the user's profile. See <https://bugs.gnu.org/35267>.
  746. (define profiles
  747. (if user-profile
  748. (list user-profile system-profile)
  749. (list system-profile)))
  750. (setenv "XDG_CONFIG_DIRS"
  751. (string-join (map (cut string-append <> "/etc/xdg") profiles)
  752. ":"))
  753. (setenv "XDG_DATA_DIRS"
  754. (string-join (map (cut string-append <> "/share") profiles)
  755. ":"))
  756. (apply execl (string-append #$dbus "/bin/dbus-daemon")
  757. (program-arguments)))))
  758. ;; Wrapper script for Wayland sessions, similar to Xsession.
  759. ;;
  760. ;; See `xinitrc`. By default, it launches the specified session through a
  761. ;; login shell. With the default Guix configuration, this should source
  762. ;; /etc/profile, setting up the Guix profile environment variables. However,
  763. ;; gdm launches its own graphical session through the same method, so we need
  764. ;; to ignore this case, since `gdm` doesn't have a login shell.
  765. (define gdm-wayland-session-wrapper
  766. (program-file
  767. "gdm-wayland-session-wrapper"
  768. #~((let* ((user (getpw (getuid)))
  769. (name (passwd:name user))
  770. (shell (passwd:shell user))
  771. (args (cdr (command-line))))
  772. (if (string=? name "gdm")
  773. (apply execl (cons (car args) args))
  774. (execl shell shell "--login" "-c" (string-join args)))))))
  775. (define-record-type* <gdm-configuration>
  776. gdm-configuration make-gdm-configuration
  777. gdm-configuration?
  778. (gdm gdm-configuration-gdm (default gdm))
  779. (allow-empty-passwords? gdm-configuration-allow-empty-passwords? (default #t))
  780. (auto-login? gdm-configuration-auto-login? (default #f))
  781. (dbus-daemon gdm-configuration-dbus-daemon (default dbus-daemon-wrapper))
  782. (debug? gdm-configuration-debug? (default #f))
  783. (default-user gdm-configuration-default-user (default #f))
  784. (gnome-shell-assets gdm-configuration-gnome-shell-assets
  785. (default (list adwaita-icon-theme font-abattis-cantarell)))
  786. (xorg-configuration gdm-configuration-xorg
  787. (default (xorg-configuration)))
  788. (x-session gdm-configuration-x-session
  789. (default (xinitrc)))
  790. (wayland? gdm-configuration-wayland? (default #f))
  791. (wayland-session gdm-configuration-wayland-session
  792. (default gdm-wayland-session-wrapper)))
  793. (define (gdm-configuration-file config)
  794. (mixed-text-file "gdm-custom.conf"
  795. "[daemon]\n"
  796. "#User=gdm\n"
  797. "#Group=gdm\n"
  798. (if (gdm-configuration-auto-login? config)
  799. (string-append
  800. "AutomaticLoginEnable=true\n"
  801. "AutomaticLogin="
  802. (or (gdm-configuration-default-user config)
  803. (error "missing default user for auto-login"))
  804. "\n")
  805. (string-append
  806. "AutomaticLoginEnable=false\n"
  807. "#AutomaticLogin=\n"))
  808. "#TimedLoginEnable=false\n"
  809. "#TimedLogin=\n"
  810. "#TimedLoginDelay=0\n"
  811. ;; Disable initial system setup inside GDM.
  812. ;; Whatever settings are set there should already be
  813. ;; taken care of through `guix system'.
  814. ;; See also
  815. ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=39281>.
  816. "InitialSetupEnable=false\n"
  817. "WaylandEnable=" (if (gdm-configuration-wayland? config)
  818. "true"
  819. "false") "\n"
  820. "\n"
  821. "[debug]\n"
  822. "Enable=" (if (gdm-configuration-debug? config)
  823. "true"
  824. "false") "\n"
  825. "\n"
  826. "[security]\n"
  827. "#DisallowTCP=true\n"
  828. "#AllowRemoteAutoLogin=false\n"))
  829. (define (gdm-pam-service config)
  830. "Return a PAM service for @command{gdm}."
  831. (list
  832. (pam-service
  833. (inherit (unix-pam-service "gdm-autologin"
  834. #:login-uid? #t))
  835. (auth (list (pam-entry
  836. (control "optional")
  837. (module (file-append (gdm-configuration-gdm config)
  838. "/lib/security/pam_gdm.so")))
  839. (pam-entry
  840. (control "sufficient")
  841. (module "pam_permit.so")))))
  842. (pam-service
  843. (inherit (unix-pam-service "gdm-launch-environment"))
  844. (auth (list (pam-entry
  845. (control "required")
  846. (module "pam_permit.so")))))
  847. (unix-pam-service "gdm-password"
  848. #:login-uid? #t
  849. #:allow-empty-passwords?
  850. (gdm-configuration-allow-empty-passwords? config))))
  851. (define (gdm-shepherd-service config)
  852. (list (shepherd-service
  853. (documentation "Xorg display server (GDM)")
  854. (provision '(xorg-server))
  855. (requirement '(dbus-system user-processes host-name udev elogind))
  856. (start #~(lambda ()
  857. (fork+exec-command
  858. (list #$(file-append (gdm-configuration-gdm config)
  859. "/bin/gdm"))
  860. #:environment-variables
  861. (list (string-append
  862. "GDM_CUSTOM_CONF="
  863. #$(gdm-configuration-file config))
  864. (string-append
  865. "GDM_DBUS_DAEMON="
  866. #$(gdm-configuration-dbus-daemon config))
  867. (string-append
  868. "GDM_X_SERVER="
  869. #$(xorg-wrapper
  870. (gdm-configuration-xorg config)))
  871. (string-append
  872. "GDM_X_SESSION="
  873. #$(gdm-configuration-x-session config))
  874. (string-append
  875. "XDG_DATA_DIRS="
  876. ((lambda (ls) (string-join ls ":"))
  877. (map (lambda (path)
  878. (string-append path "/share"))
  879. ;; XXX: Remove gnome-shell below when GDM
  880. ;; can depend on GNOME Shell directly.
  881. (cons #$gnome-shell
  882. '#$(gdm-configuration-gnome-shell-assets
  883. config)))))
  884. ;; Add XCURSOR_PATH so that mutter can find its
  885. ;; cursors. gdm doesn't login so doesn't source
  886. ;; the corresponding line in /etc/profile.
  887. "XCURSOR_PATH=/run/current-system/profile/share/icons"
  888. (string-append
  889. "GDK_PIXBUF_MODULE_FILE="
  890. #$gnome-shell "/" #$%gdk-pixbuf-loaders-cache-file)
  891. (string-append
  892. "GDM_WAYLAND_SESSION="
  893. #$(gdm-configuration-wayland-session config))))))
  894. (stop #~(make-kill-destructor))
  895. (respawn? #t))))
  896. (define gdm-service-type
  897. (handle-xorg-configuration gdm-configuration
  898. (service-type (name 'gdm)
  899. (extensions
  900. (list (service-extension shepherd-root-service-type
  901. gdm-shepherd-service)
  902. (service-extension account-service-type
  903. (const %gdm-accounts))
  904. (service-extension pam-root-service-type
  905. gdm-pam-service)
  906. (service-extension profile-service-type
  907. gdm-configuration-gnome-shell-assets)
  908. (service-extension dbus-root-service-type
  909. (compose list
  910. gdm-configuration-gdm))
  911. (service-extension localed-service-type
  912. (compose
  913. xorg-configuration-keyboard-layout
  914. gdm-configuration-xorg))))
  915. (default-value (gdm-configuration))
  916. (description
  917. "Run the GNOME Desktop Manager (GDM), a program that allows
  918. you to log in in a graphical session, whether or not you use GNOME."))))
  919. ;; Since GDM depends on Rust (gdm -> gnome-shell -> gjs -> mozjs -> rust)
  920. ;; and Rust is currently unavailable on non-x86_64 platforms, default to
  921. ;; SDDM there (FIXME).
  922. (define* (set-xorg-configuration config
  923. #:optional
  924. (login-manager-service-type
  925. (if (target-x86-64?)
  926. gdm-service-type
  927. sddm-service-type)))
  928. "Tell the log-in manager (of type @var{login-manager-service-type}) to use
  929. @var{config}, an <xorg-configuration> record."
  930. (simple-service 'set-xorg-configuration
  931. login-manager-service-type
  932. config))
  933. ;;; xorg.scm ends here