Revert "Merge branch 'core-updates'"
[jackhill/guix/guix.git] / gnu / system / shadow.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016 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 sets)
25 #:use-module (guix ui)
26 #:use-module (gnu services)
27 #:use-module ((gnu system file-systems)
28 #:select (%tty-gid))
29 #:use-module ((gnu packages admin)
30 #:select (shadow))
31 #:use-module (gnu packages bash)
32 #:use-module (gnu packages guile-wm)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-26)
35 #:use-module (srfi srfi-34)
36 #:use-module (srfi srfi-35)
37 #:export (user-account
38 user-account?
39 user-account-name
40 user-account-password
41 user-account-uid
42 user-account-group
43 user-account-supplementary-groups
44 user-account-comment
45 user-account-home-directory
46 user-account-shell
47 user-account-system?
48
49 user-group
50 user-group?
51 user-group-name
52 user-group-password
53 user-group-id
54 user-group-system?
55
56 default-skeletons
57 skeleton-directory
58 %base-groups
59 %base-user-accounts
60
61 account-service-type
62 account-service))
63
64 ;;; Commentary:
65 ;;;
66 ;;; Utilities for configuring the Shadow tool suite ('login', 'passwd', etc.)
67 ;;;
68 ;;; Code:
69
70 (define-record-type* <user-account>
71 user-account make-user-account
72 user-account?
73 (name user-account-name)
74 (password user-account-password (default #f))
75 (uid user-account-uid (default #f))
76 (group user-account-group) ; number | string
77 (supplementary-groups user-account-supplementary-groups
78 (default '())) ; list of strings
79 (comment user-account-comment (default ""))
80 (home-directory user-account-home-directory)
81 (shell user-account-shell ; gexp
82 (default #~(string-append #$bash "/bin/bash")))
83 (system? user-account-system? ; Boolean
84 (default #f)))
85
86 (define-record-type* <user-group>
87 user-group make-user-group
88 user-group?
89 (name user-group-name)
90 (password user-group-password (default #f))
91 (id user-group-id (default #f))
92 (system? user-group-system? ; Boolean
93 (default #f)))
94
95
96 (define %base-groups
97 ;; Default set of groups.
98 (let-syntax ((system-group (syntax-rules ()
99 ((_ args ...)
100 (user-group (system? #t) args ...)))))
101 (list (system-group (name "root") (id 0))
102 (system-group (name "wheel")) ; root-like users
103 (system-group (name "users")) ; normal users
104 (system-group (name "nogroup")) ; for daemons etc.
105
106 ;; The following groups are conventionally used by things like udev to
107 ;; control access to hardware devices.
108 (system-group (name "tty") (id %tty-gid))
109 (system-group (name "dialout"))
110 (system-group (name "kmem"))
111 (system-group (name "input")) ; input devices, from udev
112 (system-group (name "video"))
113 (system-group (name "audio"))
114 (system-group (name "netdev")) ; used in avahi-dbus.conf
115 (system-group (name "lp"))
116 (system-group (name "disk"))
117 (system-group (name "floppy"))
118 (system-group (name "cdrom"))
119 (system-group (name "tape"))
120 (system-group (name "kvm"))))) ; for /dev/kvm
121
122 (define %base-user-accounts
123 ;; List of standard user accounts. Note that "root" is a special case, so
124 ;; it's not listed here.
125 (list (user-account
126 (name "nobody")
127 (uid 65534)
128 (group "nogroup")
129 (shell #~(string-append #$shadow "/sbin/nologin"))
130 (home-directory "/nonexistent")
131 (system? #t))))
132
133 (define (default-skeletons)
134 "Return the default skeleton files for /etc/skel. These files are copied by
135 'useradd' in the home directory of newly created user accounts."
136 (define fonts.conf-content
137 ;; SXML for ~/.config/fontconfig/fonts.conf. This works around the fact
138 ;; that Fontconfig currently does not such this directory by default,
139 ;; thereby ignoring fonts installed system-wide (FIXME).
140 `(fontconfig (dir "/run/current-system/profile/share/fonts")))
141
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 [ -n \"$SSH_CLIENT\" -a -z \"`type -P cat`\" ]
161 then
162 # We are being invoked from a non-interactive SSH session
163 # (as in \"ssh host command\") but 'cat' cannot be found
164 # in $PATH. Source /etc/profile so we get $PATH and other
165 # essential variables.
166 source /etc/profile
167 fi
168
169 # Adjust the prompt depending on whether we're in 'guix environment'.
170 if [ -n \"$GUIX_ENVIRONMENT\" ]
171 then
172 PS1='\\u@\\h \\w [env]\\$ '
173 else
174 PS1='\\u@\\h \\w\\$ '
175 fi
176 alias ls='ls -p --color'
177 alias ll='ls -l'\n"))
178 (zlogin (plain-file "zlogin" "\
179 # Honor system-wide environment variables
180 source /etc/profile\n"))
181 (guile-wm (computed-file "guile-wm" copy-guile-wm))
182 (xdefaults (plain-file "Xdefaults" "\
183 XTerm*utf8: always
184 XTerm*metaSendsEscape: true\n"))
185 (fonts.conf (computed-file
186 "fonts.conf"
187 (with-imported-modules '((guix build utils))
188 #~(begin
189 (use-modules (guix build utils)
190 (sxml simple))
191
192 (define dir
193 (string-append #$output
194 "/fontconfig"))
195
196 (mkdir-p dir)
197 (call-with-output-file (string-append dir
198 "/fonts.conf")
199 (lambda (port)
200 (sxml->xml '#$fonts.conf-content port)))))))
201 (gdbinit (plain-file "gdbinit" "\
202 # Tell GDB where to look for separate debugging files.
203 set debug-file-directory ~/.guix-profile/lib/debug\n")))
204 `((".bash_profile" ,profile)
205 (".bashrc" ,bashrc)
206 (".zlogin" ,zlogin)
207 (".Xdefaults" ,xdefaults)
208 (".guile-wm" ,guile-wm)
209 (".config" ,fonts.conf)
210 (".gdbinit" ,gdbinit))))
211
212 (define (skeleton-directory skeletons)
213 "Return a directory containing SKELETONS, a list of name/derivation tuples."
214 (computed-file "skel"
215 (with-imported-modules '((guix build utils))
216 #~(begin
217 (use-modules (ice-9 match)
218 (guix build utils))
219
220 (mkdir #$output)
221 (chdir #$output)
222
223 ;; Note: copy the skeletons instead of symlinking
224 ;; them like 'file-union' does, because 'useradd'
225 ;; would just copy the symlinks as is.
226 (for-each (match-lambda
227 ((target source)
228 (copy-recursively source target)))
229 '#$skeletons)
230 #t))))
231
232 (define (assert-valid-users/groups users groups)
233 "Raise an error if USERS refer to groups not listed in GROUPS."
234 (let ((groups (list->set (map user-group-name groups))))
235 (define (validate-supplementary-group user group)
236 (unless (set-contains? groups group)
237 (raise (condition
238 (&message
239 (message
240 (format #f (_ "supplementary group '~a' \
241 of user '~a' is undeclared")
242 group
243 (user-account-name user))))))))
244
245 (for-each (lambda (user)
246 (unless (set-contains? groups (user-account-group user))
247 (raise (condition
248 (&message
249 (message
250 (format #f (_ "primary group '~a' \
251 of user '~a' is undeclared")
252 (user-account-group user)
253 (user-account-name user)))))))
254
255 (for-each (cut validate-supplementary-group user <>)
256 (user-account-supplementary-groups user)))
257 users)))
258
259 \f
260 ;;;
261 ;;; Service.
262 ;;;
263
264 (define (user-group->gexp group)
265 "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for
266 'active-groups'."
267 #~(list #$(user-group-name group)
268 #$(user-group-password group)
269 #$(user-group-id group)
270 #$(user-group-system? group)))
271
272 (define (user-account->gexp account)
273 "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for
274 'activate-users'."
275 #~`(#$(user-account-name account)
276 #$(user-account-uid account)
277 #$(user-account-group account)
278 #$(user-account-supplementary-groups account)
279 #$(user-account-comment account)
280 #$(user-account-home-directory account)
281 ,#$(user-account-shell account) ; this one is a gexp
282 #$(user-account-password account)
283 #$(user-account-system? account)))
284
285 (define (account-activation accounts+groups)
286 "Return a gexp that activates ACCOUNTS+GROUPS, a list of <user-account> and
287 <user-group> objects. Raise an error if a user account refers to a undefined
288 group."
289 (define accounts
290 (filter user-account? accounts+groups))
291
292 (define user-specs
293 (map user-account->gexp accounts))
294
295 (define groups
296 (filter user-group? accounts+groups))
297
298 (define group-specs
299 (map user-group->gexp groups))
300
301 (assert-valid-users/groups accounts groups)
302
303 ;; Add users and user groups.
304 #~(begin
305 (setenv "PATH"
306 (string-append #$(@ (gnu packages admin) shadow) "/sbin"))
307 (activate-users+groups (list #$@user-specs)
308 (list #$@group-specs))))
309
310 (define (shells-file shells)
311 "Return a file-like object that builds a shell list for use as /etc/shells
312 based on SHELLS. /etc/shells is used by xterm, polkit, and other programs."
313 (computed-file "shells"
314 #~(begin
315 (use-modules (srfi srfi-1))
316
317 (define shells
318 (delete-duplicates (list #$@shells)))
319
320 (call-with-output-file #$output
321 (lambda (port)
322 (display "\
323 /bin/sh
324 /run/current-system/profile/bin/sh
325 /run/current-system/profile/bin/bash\n" port)
326 (for-each (lambda (shell)
327 (display shell port)
328 (newline port))
329 shells))))))
330 (define (etc-files arguments)
331 "Filter out among ARGUMENTS things corresponding to skeletons, and return
332 the /etc/skel directory for those."
333 (let ((skels (filter pair? arguments))
334 (users (filter user-account? arguments)))
335 `(("skel" ,(skeleton-directory skels))
336 ("shells" ,(shells-file (map user-account-shell users))))))
337
338 (define account-service-type
339 (service-type (name 'account)
340
341 ;; Concatenate <user-account>, <user-group>, and skeleton
342 ;; lists.
343 (compose concatenate)
344 (extend append)
345
346 (extensions
347 (list (service-extension activation-service-type
348 account-activation)
349 (service-extension etc-service-type
350 etc-files)))))
351
352 (define (account-service accounts+groups skeletons)
353 "Return a <service> that takes care of user accounts and user groups, with
354 ACCOUNTS+GROUPS as its initial list of accounts and groups."
355 (service account-service-type
356 (append skeletons accounts+groups)))
357
358 ;;; shadow.scm ends here