xorg.scm 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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 %gdm-activation
  730. ;; Ensure /var/lib/gdm is owned by the "gdm" user. This is normally the
  731. ;; case but could be wrong if the "gdm" user was created, then removed, and
  732. ;; then recreated under a different UID/GID: <https://bugs.gnu.org/37423>.
  733. (with-imported-modules '((guix build utils))
  734. #~(begin
  735. (use-modules (guix build utils))
  736. (let* ((gdm (getpwnam "gdm"))
  737. (uid (passwd:uid gdm))
  738. (gid (passwd:gid gdm))
  739. (st (stat "/var/lib/gdm" #f)))
  740. ;; Recurse into /var/lib/gdm only if it has wrong ownership.
  741. (when (and st
  742. (or (not (= uid (stat:uid st)))
  743. (not (= gid (stat:gid st)))))
  744. (for-each (lambda (file)
  745. (chown file uid gid))
  746. (find-files "/var/lib/gdm"
  747. #:directories? #t)))))))
  748. (define dbus-daemon-wrapper
  749. (program-file
  750. "gdm-dbus-wrapper"
  751. #~(begin
  752. (use-modules (srfi srfi-26))
  753. (define system-profile
  754. "/run/current-system/profile")
  755. (define user-profile
  756. (and=> (getpw (getuid))
  757. (lambda (pw)
  758. (string-append (passwd:dir pw) "/.guix-profile"))))
  759. ;; If we are able to find the user's profile, we can add it to
  760. ;; the search paths set below. We need to do this so that D-Bus
  761. ;; can start services installed by the user. This allows
  762. ;; applications that require session D-Bus services (e.g,
  763. ;; 'evolution') to work even if those services are only available
  764. ;; in the user's profile. See <https://bugs.gnu.org/35267>.
  765. (define profiles
  766. (if user-profile
  767. (list user-profile system-profile)
  768. (list system-profile)))
  769. (setenv "XDG_CONFIG_DIRS"
  770. (string-join (map (cut string-append <> "/etc/xdg") profiles)
  771. ":"))
  772. (setenv "XDG_DATA_DIRS"
  773. (string-join (map (cut string-append <> "/share") profiles)
  774. ":"))
  775. (apply execl (string-append #$dbus "/bin/dbus-daemon")
  776. (program-arguments)))))
  777. ;; Wrapper script for Wayland sessions, similar to Xsession.
  778. ;;
  779. ;; See `xinitrc`. By default, it launches the specified session through a
  780. ;; login shell. With the default Guix configuration, this should source
  781. ;; /etc/profile, setting up the Guix profile environment variables. However,
  782. ;; gdm launches its own graphical session through the same method, so we need
  783. ;; to ignore this case, since `gdm` doesn't have a login shell.
  784. (define gdm-wayland-session-wrapper
  785. (program-file
  786. "gdm-wayland-session-wrapper"
  787. #~((let* ((user (getpw (getuid)))
  788. (name (passwd:name user))
  789. (shell (passwd:shell user))
  790. (args (cdr (command-line))))
  791. (if (string=? name "gdm")
  792. (apply execl (cons (car args) args))
  793. (execl shell shell "--login" "-c" (string-join args)))))))
  794. (define-record-type* <gdm-configuration>
  795. gdm-configuration make-gdm-configuration
  796. gdm-configuration?
  797. (gdm gdm-configuration-gdm (default gdm))
  798. (allow-empty-passwords? gdm-configuration-allow-empty-passwords? (default #t))
  799. (auto-login? gdm-configuration-auto-login? (default #f))
  800. (dbus-daemon gdm-configuration-dbus-daemon (default dbus-daemon-wrapper))
  801. (debug? gdm-configuration-debug? (default #f))
  802. (default-user gdm-configuration-default-user (default #f))
  803. (gnome-shell-assets gdm-configuration-gnome-shell-assets
  804. (default (list adwaita-icon-theme font-abattis-cantarell)))
  805. (xorg-configuration gdm-configuration-xorg
  806. (default (xorg-configuration)))
  807. (x-session gdm-configuration-x-session
  808. (default (xinitrc)))
  809. (wayland? gdm-configuration-wayland? (default #f))
  810. (wayland-session gdm-configuration-wayland-session
  811. (default gdm-wayland-session-wrapper)))
  812. (define (gdm-configuration-file config)
  813. (mixed-text-file "gdm-custom.conf"
  814. "[daemon]\n"
  815. "#User=gdm\n"
  816. "#Group=gdm\n"
  817. (if (gdm-configuration-auto-login? config)
  818. (string-append
  819. "AutomaticLoginEnable=true\n"
  820. "AutomaticLogin="
  821. (or (gdm-configuration-default-user config)
  822. (error "missing default user for auto-login"))
  823. "\n")
  824. (string-append
  825. "AutomaticLoginEnable=false\n"
  826. "#AutomaticLogin=\n"))
  827. "#TimedLoginEnable=false\n"
  828. "#TimedLogin=\n"
  829. "#TimedLoginDelay=0\n"
  830. ;; Disable initial system setup inside GDM.
  831. ;; Whatever settings are set there should already be
  832. ;; taken care of through `guix system'.
  833. ;; See also
  834. ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=39281>.
  835. "InitialSetupEnable=false\n"
  836. "WaylandEnable=" (if (gdm-configuration-wayland? config)
  837. "true"
  838. "false") "\n"
  839. "\n"
  840. "[debug]\n"
  841. "Enable=" (if (gdm-configuration-debug? config)
  842. "true"
  843. "false") "\n"
  844. "\n"
  845. "[security]\n"
  846. "#DisallowTCP=true\n"
  847. "#AllowRemoteAutoLogin=false\n"))
  848. (define (gdm-pam-service config)
  849. "Return a PAM service for @command{gdm}."
  850. (list
  851. (pam-service
  852. (inherit (unix-pam-service "gdm-autologin"
  853. #:login-uid? #t))
  854. (auth (list (pam-entry
  855. (control "optional")
  856. (module (file-append (gdm-configuration-gdm config)
  857. "/lib/security/pam_gdm.so")))
  858. (pam-entry
  859. (control "sufficient")
  860. (module "pam_permit.so")))))
  861. (pam-service
  862. (inherit (unix-pam-service "gdm-launch-environment"))
  863. (auth (list (pam-entry
  864. (control "required")
  865. (module "pam_permit.so")))))
  866. (unix-pam-service "gdm-password"
  867. #:login-uid? #t
  868. #:allow-empty-passwords?
  869. (gdm-configuration-allow-empty-passwords? config))))
  870. (define (gdm-shepherd-service config)
  871. (list (shepherd-service
  872. (documentation "Xorg display server (GDM)")
  873. (provision '(xorg-server))
  874. (requirement '(dbus-system user-processes host-name udev elogind))
  875. (start #~(lambda ()
  876. (fork+exec-command
  877. (list #$(file-append (gdm-configuration-gdm config)
  878. "/bin/gdm"))
  879. #:environment-variables
  880. (list (string-append
  881. "GDM_CUSTOM_CONF="
  882. #$(gdm-configuration-file config))
  883. (string-append
  884. "GDM_DBUS_DAEMON="
  885. #$(gdm-configuration-dbus-daemon config))
  886. (string-append
  887. "GDM_X_SERVER="
  888. #$(xorg-wrapper
  889. (gdm-configuration-xorg config)))
  890. (string-append
  891. "GDM_X_SESSION="
  892. #$(gdm-configuration-x-session config))
  893. (string-append
  894. "XDG_DATA_DIRS="
  895. ((lambda (ls) (string-join ls ":"))
  896. (map (lambda (path)
  897. (string-append path "/share"))
  898. ;; XXX: Remove gnome-shell below when GDM
  899. ;; can depend on GNOME Shell directly.
  900. (cons #$gnome-shell
  901. '#$(gdm-configuration-gnome-shell-assets
  902. config)))))
  903. ;; Add XCURSOR_PATH so that mutter can find its
  904. ;; cursors. gdm doesn't login so doesn't source
  905. ;; the corresponding line in /etc/profile.
  906. "XCURSOR_PATH=/run/current-system/profile/share/icons"
  907. (string-append
  908. "GDK_PIXBUF_MODULE_FILE="
  909. #$gnome-shell "/" #$%gdk-pixbuf-loaders-cache-file)
  910. (string-append
  911. "GDM_WAYLAND_SESSION="
  912. #$(gdm-configuration-wayland-session config))))))
  913. (stop #~(make-kill-destructor))
  914. (respawn? #t))))
  915. (define gdm-service-type
  916. (handle-xorg-configuration gdm-configuration
  917. (service-type (name 'gdm)
  918. (extensions
  919. (list (service-extension shepherd-root-service-type
  920. gdm-shepherd-service)
  921. (service-extension activation-service-type
  922. (const %gdm-activation))
  923. (service-extension account-service-type
  924. (const %gdm-accounts))
  925. (service-extension pam-root-service-type
  926. gdm-pam-service)
  927. (service-extension profile-service-type
  928. gdm-configuration-gnome-shell-assets)
  929. (service-extension dbus-root-service-type
  930. (compose list
  931. gdm-configuration-gdm))
  932. (service-extension localed-service-type
  933. (compose
  934. xorg-configuration-keyboard-layout
  935. gdm-configuration-xorg))))
  936. (default-value (gdm-configuration))
  937. (description
  938. "Run the GNOME Desktop Manager (GDM), a program that allows
  939. you to log in in a graphical session, whether or not you use GNOME."))))
  940. ;; Since GDM depends on Rust (gdm -> gnome-shell -> gjs -> mozjs -> rust)
  941. ;; and Rust is currently unavailable on non-x86_64 platforms, default to
  942. ;; SDDM there (FIXME).
  943. (define* (set-xorg-configuration config
  944. #:optional
  945. (login-manager-service-type
  946. (if (target-x86-64?)
  947. gdm-service-type
  948. sddm-service-type)))
  949. "Tell the log-in manager (of type @var{login-manager-service-type}) to use
  950. @var{config}, an <xorg-configuration> record."
  951. (simple-service 'set-xorg-configuration
  952. login-manager-service-type
  953. config))
  954. ;;; xorg.scm ends here