shadow.scm 15 KB

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