shadow.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2016 Alex Griffin <a@ajgrf.com>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu system shadow)
  20. #:use-module (guix records)
  21. #:use-module (guix gexp)
  22. #:use-module (guix store)
  23. #:use-module (guix modules)
  24. #:use-module (guix sets)
  25. #:use-module (guix ui)
  26. #:use-module (gnu system accounts)
  27. #:use-module (gnu services)
  28. #:use-module (gnu services shepherd)
  29. #:use-module ((gnu system file-systems)
  30. #:select (%tty-gid))
  31. #:use-module ((gnu packages admin)
  32. #:select (shadow))
  33. #:use-module (gnu packages bash)
  34. #:use-module (gnu packages guile-wm)
  35. #:use-module (srfi srfi-1)
  36. #:use-module (srfi srfi-26)
  37. #:use-module (srfi srfi-34)
  38. #:use-module (srfi srfi-35)
  39. ;; Re-export these bindings for backward compatibility.
  40. #:re-export (user-account
  41. user-account?
  42. user-account-name
  43. user-account-password
  44. user-account-uid
  45. user-account-group
  46. user-account-supplementary-groups
  47. user-account-comment
  48. user-account-home-directory
  49. user-account-create-home-directory?
  50. user-account-shell
  51. user-account-system?
  52. user-group
  53. user-group?
  54. user-group-name
  55. user-group-password
  56. user-group-id
  57. user-group-system?)
  58. #:export (default-skeletons
  59. skeleton-directory
  60. %base-groups
  61. %base-user-accounts
  62. account-service-type
  63. account-service))
  64. ;;; Commentary:
  65. ;;;
  66. ;;; Utilities for configuring the Shadow tool suite ('login', 'passwd', etc.)
  67. ;;;
  68. ;;; Code:
  69. ;; Change the default shell used by new <user-account> records.
  70. (default-shell (file-append bash "/bin/bash"))
  71. (define %base-groups
  72. ;; Default set of groups.
  73. (let-syntax ((system-group (syntax-rules ()
  74. ((_ args ...)
  75. (user-group (system? #t) args ...)))))
  76. (list (system-group (name "root") (id 0))
  77. (system-group (name "wheel")) ; root-like users
  78. (system-group (name "users")) ; normal users
  79. (system-group (name "nogroup")) ; for daemons etc.
  80. ;; The following groups are conventionally used by things like udev to
  81. ;; control access to hardware devices.
  82. (system-group (name "tty") (id %tty-gid))
  83. (system-group (name "dialout"))
  84. (system-group (name "kmem"))
  85. (system-group (name "input")) ; input devices, from udev
  86. (system-group (name "video"))
  87. (system-group (name "audio"))
  88. (system-group (name "netdev")) ; used in avahi-dbus.conf
  89. (system-group (name "lp"))
  90. (system-group (name "disk"))
  91. (system-group (name "floppy"))
  92. (system-group (name "cdrom"))
  93. (system-group (name "tape"))
  94. (system-group (name "kvm"))))) ; for /dev/kvm
  95. (define %base-user-accounts
  96. ;; List of standard user accounts. Note that "root" is a special case, so
  97. ;; it's not listed here.
  98. (list (user-account
  99. (name "nobody")
  100. (uid 65534)
  101. (group "nogroup")
  102. (shell (file-append shadow "/sbin/nologin"))
  103. (home-directory "/nonexistent")
  104. (create-home-directory? #f)
  105. (system? #t))))
  106. (define (default-skeletons)
  107. "Return the default skeleton files for /etc/skel. These files are copied by
  108. 'useradd' in the home directory of newly created user accounts."
  109. (define copy-guile-wm
  110. (with-imported-modules '((guix build utils))
  111. #~(begin
  112. (use-modules (guix build utils))
  113. (copy-file (car (find-files #+guile-wm "wm-init-sample.scm"))
  114. #$output))))
  115. (let ((profile (plain-file "bash_profile" "\
  116. # Honor per-interactive-shell startup file
  117. if [ -f ~/.bashrc ]; then . ~/.bashrc; fi\n"))
  118. (bashrc (plain-file "bashrc" "\
  119. # Bash initialization for interactive non-login shells and
  120. # for remote shells (info \"(bash) Bash Startup Files\").
  121. # Export 'SHELL' to child processes. Programs such as 'screen'
  122. # honor it and otherwise use /bin/sh.
  123. export SHELL
  124. if [[ $- != *i* ]]
  125. then
  126. # We are being invoked from a non-interactive shell. If this
  127. # is an SSH session (as in \"ssh host command\"), source
  128. # /etc/profile so we get PATH and other essential variables.
  129. [[ -n \"$SSH_CLIENT\" ]] && source /etc/profile
  130. # Don't do anything else.
  131. return
  132. fi
  133. # Source the system-wide file.
  134. source /etc/bashrc
  135. # Adjust the prompt depending on whether we're in 'guix environment'.
  136. if [ -n \"$GUIX_ENVIRONMENT\" ]
  137. then
  138. PS1='\\u@\\h \\w [env]\\$ '
  139. else
  140. PS1='\\u@\\h \\w\\$ '
  141. fi
  142. alias ls='ls -p --color=auto'
  143. alias ll='ls -l'
  144. alias grep='grep --color=auto'\n"))
  145. (zprofile (plain-file "zprofile" "\
  146. # Honor system-wide environment variables
  147. source /etc/profile\n"))
  148. (guile-wm (computed-file "guile-wm" copy-guile-wm))
  149. (xdefaults (plain-file "Xdefaults" "\
  150. XTerm*utf8: always
  151. XTerm*metaSendsEscape: true\n"))
  152. (gdbinit (plain-file "gdbinit" "\
  153. # Tell GDB where to look for separate debugging files.
  154. set debug-file-directory ~/.guix-profile/lib/debug
  155. # Authorize extensions found in the store, such as the
  156. # pretty-printers of libstdc++.
  157. set auto-load safe-path /gnu/store/*/lib\n")))
  158. `((".bash_profile" ,profile)
  159. (".bashrc" ,bashrc)
  160. ;; Zsh sources ~/.zprofile before ~/.zshrc, and it sources ~/.zlogin
  161. ;; after ~/.zshrc. To avoid interfering with any customizations a user
  162. ;; may have made in their ~/.zshrc, put this in .zprofile, not .zlogin.
  163. (".zprofile" ,zprofile)
  164. (".nanorc" ,(plain-file "nanorc" "\
  165. # Include all the syntax highlighting modules.
  166. include /run/current-system/profile/share/nano/*.nanorc\n"))
  167. (".Xdefaults" ,xdefaults)
  168. (".guile" ,(plain-file "dot-guile"
  169. "(cond ((false-if-exception (resolve-interface '(ice-9 readline)))
  170. =>
  171. (lambda (module)
  172. ;; Enable completion and input history at the REPL.
  173. ((module-ref module 'activate-readline))))
  174. (else
  175. (display \"Consider installing the 'guile-readline' package for
  176. convenient interactive line editing and input history.\\n\\n\")))
  177. (unless (getenv \"INSIDE_EMACS\")
  178. (cond ((false-if-exception (resolve-interface '(ice-9 colorized)))
  179. =>
  180. (lambda (module)
  181. ;; Enable completion and input history at the REPL.
  182. ((module-ref module 'activate-colorized))))
  183. (else
  184. (display \"Consider installing the 'guile-colorized' package
  185. for a colorful Guile experience.\\n\\n\"))))\n"))
  186. (".guile-wm" ,guile-wm)
  187. (".gdbinit" ,gdbinit))))
  188. (define (skeleton-directory skeletons)
  189. "Return a directory containing SKELETONS, a list of name/derivation tuples."
  190. (computed-file "skel"
  191. (with-imported-modules '((guix build utils))
  192. #~(begin
  193. (use-modules (ice-9 match)
  194. (guix build utils))
  195. (mkdir #$output)
  196. (chdir #$output)
  197. ;; Note: copy the skeletons instead of symlinking
  198. ;; them like 'file-union' does, because 'useradd'
  199. ;; would just copy the symlinks as is.
  200. (for-each (match-lambda
  201. ((target source)
  202. (copy-recursively source target)))
  203. '#$skeletons)
  204. #t))))
  205. (define (assert-valid-users/groups users groups)
  206. "Raise an error if USERS refer to groups not listed in GROUPS."
  207. (let ((groups (list->set (map user-group-name groups))))
  208. (define (validate-supplementary-group user group)
  209. (unless (set-contains? groups group)
  210. (raise (condition
  211. (&message
  212. (message
  213. (format #f (G_ "supplementary group '~a' \
  214. of user '~a' is undeclared")
  215. group
  216. (user-account-name user))))))))
  217. (for-each (lambda (user)
  218. (unless (set-contains? groups (user-account-group user))
  219. (raise (condition
  220. (&message
  221. (message
  222. (format #f (G_ "primary group '~a' \
  223. of user '~a' is undeclared")
  224. (user-account-group user)
  225. (user-account-name user)))))))
  226. (for-each (cut validate-supplementary-group user <>)
  227. (user-account-supplementary-groups user)))
  228. users)))
  229. ;;;
  230. ;;; Service.
  231. ;;;
  232. (define (user-group->gexp group)
  233. "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for
  234. 'active-groups'."
  235. #~(list #$(user-group-name group)
  236. #$(user-group-password group)
  237. #$(user-group-id group)
  238. #$(user-group-system? group)))
  239. (define (user-account->gexp account)
  240. "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for
  241. 'activate-users'."
  242. #~`(#$(user-account-name account)
  243. #$(user-account-uid account)
  244. #$(user-account-group account)
  245. #$(user-account-supplementary-groups account)
  246. #$(user-account-comment account)
  247. #$(user-account-home-directory account)
  248. #$(user-account-create-home-directory? account)
  249. ,#$(user-account-shell account) ; this one is a gexp
  250. #$(user-account-password account)
  251. #$(user-account-system? account)))
  252. (define (account-activation accounts+groups)
  253. "Return a gexp that activates ACCOUNTS+GROUPS, a list of <user-account> and
  254. <user-group> objects. Raise an error if a user account refers to a undefined
  255. group."
  256. (define accounts
  257. (filter user-account? accounts+groups))
  258. (define user-specs
  259. (map user-account->gexp accounts))
  260. (define groups
  261. (filter user-group? accounts+groups))
  262. (define group-specs
  263. (map user-group->gexp groups))
  264. (assert-valid-users/groups accounts groups)
  265. ;; Add users and user groups.
  266. (with-imported-modules (source-module-closure '((gnu system accounts)))
  267. #~(begin
  268. (use-modules (gnu system accounts))
  269. (activate-users+groups (map sexp->user-account (list #$@user-specs))
  270. (map sexp->user-group (list #$@group-specs))))))
  271. (define (account-shepherd-service accounts+groups)
  272. "Return a Shepherd service that creates the home directories for the user
  273. accounts among ACCOUNTS+GROUPS."
  274. (define accounts
  275. (filter user-account? accounts+groups))
  276. ;; Create home directories only once 'file-systems' is up. This makes sure
  277. ;; they are created in the right place if /home lives on a separate
  278. ;; partition.
  279. ;;
  280. ;; XXX: We arrange for this service to stop right after it's done its job so
  281. ;; that 'guix system reconfigure' knows that it can reload it fearlessly
  282. ;; (and thus create new home directories). The cost of this hack is that
  283. ;; there's a small window during which first-time logins could happen before
  284. ;; the home directory has been created.
  285. (list (shepherd-service
  286. (requirement '(file-systems))
  287. (provision '(user-homes))
  288. (one-shot? #t)
  289. (modules '((gnu build activation)
  290. (gnu system accounts)))
  291. (start (with-imported-modules (source-module-closure
  292. '((gnu build activation)
  293. (gnu system accounts)))
  294. #~(lambda ()
  295. (activate-user-home
  296. (map sexp->user-account
  297. (list #$@(map user-account->gexp accounts))))
  298. #t))) ;success
  299. (documentation "Create user home directories."))))
  300. (define (shells-file shells)
  301. "Return a file-like object that builds a shell list for use as /etc/shells
  302. based on SHELLS. /etc/shells is used by xterm, polkit, and other programs."
  303. (computed-file "shells"
  304. #~(begin
  305. (use-modules (srfi srfi-1))
  306. (define shells
  307. (delete-duplicates (list #$@shells)))
  308. (call-with-output-file #$output
  309. (lambda (port)
  310. (display "\
  311. /bin/sh
  312. /run/current-system/profile/bin/sh
  313. /run/current-system/profile/bin/bash\n" port)
  314. (for-each (lambda (shell)
  315. (display shell port)
  316. (newline port))
  317. shells))))))
  318. (define (etc-files arguments)
  319. "Filter out among ARGUMENTS things corresponding to skeletons, and return
  320. the /etc/skel directory for those."
  321. (let ((skels (filter pair? arguments))
  322. (users (filter user-account? arguments)))
  323. `(("skel" ,(skeleton-directory skels))
  324. ("shells" ,(shells-file (map user-account-shell users))))))
  325. (define account-service-type
  326. (service-type (name 'account)
  327. ;; Concatenate <user-account>, <user-group>, and skeleton
  328. ;; lists.
  329. (compose concatenate)
  330. (extend append)
  331. (extensions
  332. (list (service-extension activation-service-type
  333. account-activation)
  334. (service-extension shepherd-root-service-type
  335. account-shepherd-service)
  336. (service-extension etc-service-type
  337. etc-files)))))
  338. (define (account-service accounts+groups skeletons)
  339. "Return a <service> that takes care of user accounts and user groups, with
  340. ACCOUNTS+GROUPS as its initial list of accounts and groups."
  341. (service account-service-type
  342. (append skeletons accounts+groups)))
  343. ;;; shadow.scm ends here