xorg.scm 42 KB

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