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