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>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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)
29 #:use-module ((gnu packages admin)
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
43 user-account-supplementary-groups
45 user-account-home-directory
66 ;;; Utilities for configuring the Shadow tool suite ('login', 'passwd', etc.)
70 (define-record-type* <user-account>
71 user-account make-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
86 (define-record-type* <user-group>
87 user-group make-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
97 ;; Default set of groups.
98 (let-syntax ((system-group (syntax-rules ()
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.
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
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.
129 (shell #~(string-append #$shadow "/sbin/nologin"))
130 (home-directory "/nonexistent")
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 copy-guile-wm
137 (with-imported-modules '((guix build utils))
139 (use-modules (guix build utils))
140 (copy-file (car (find-files #$guile-wm "wm-init-sample.scm"))
143 (let ((profile (plain-file "bash_profile" "\
144 # Honor per-interactive-shell startup file
145 if [ -f ~/.bashrc ]; then . ~/.bashrc; fi\n"))
146 (bashrc (plain-file "bashrc" "\
147 # Bash initialization for interactive non-login shells and
148 # for remote shells (info \"(bash) Bash Startup Files\").
150 # Export 'SHELL' to child processes. Programs such as 'screen'
151 # honor it and otherwise use /bin/sh.
154 if [ -n \"$SSH_CLIENT\" -a -z \"`type -P cat`\" ]
156 # We are being invoked from a non-interactive SSH session
157 # (as in \"ssh host command\") but 'cat' cannot be found
158 # in $PATH. Source /etc/profile so we get $PATH and other
159 # essential variables.
163 # Adjust the prompt depending on whether we're in 'guix environment'.
164 if [ -n \"$GUIX_ENVIRONMENT\" ]
166 PS1='\\u@\\h \\w [env]\\$ '
168 PS1='\\u@\\h \\w\\$ '
170 alias ls='ls -p --color'
171 alias ll='ls -l'\n"))
172 (zlogin (plain-file "zlogin" "\
173 # Honor system-wide environment variables
174 source /etc/profile\n"))
175 (guile-wm (computed-file "guile-wm" copy-guile-wm))
176 (xdefaults (plain-file "Xdefaults" "\
178 XTerm*metaSendsEscape: true\n"))
179 (gdbinit (plain-file "gdbinit" "\
180 # Tell GDB where to look for separate debugging files.
181 set debug-file-directory ~/.guix-profile/lib/debug\n")))
182 `((".bash_profile" ,profile)
185 (".Xdefaults" ,xdefaults)
186 (".guile-wm" ,guile-wm)
187 (".gdbinit" ,gdbinit))))
189 (define (skeleton-directory skeletons)
190 "Return a directory containing SKELETONS, a list of name/derivation tuples."
191 (computed-file "skel"
192 (with-imported-modules '((guix build utils))
194 (use-modules (ice-9 match)
200 ;; Note: copy the skeletons instead of symlinking
201 ;; them like 'file-union' does, because 'useradd'
202 ;; would just copy the symlinks as is.
203 (for-each (match-lambda
205 (copy-recursively source target)))
209 (define (assert-valid-users/groups users groups)
210 "Raise an error if USERS refer to groups not listed in GROUPS."
211 (let ((groups (list->set (map user-group-name groups))))
212 (define (validate-supplementary-group user group)
213 (unless (set-contains? groups group)
217 (format #f (_ "supplementary group '~a' \
218 of user '~a' is undeclared")
220 (user-account-name user))))))))
222 (for-each (lambda (user)
223 (unless (set-contains? groups (user-account-group user))
227 (format #f (_ "primary group '~a' \
228 of user '~a' is undeclared")
229 (user-account-group user)
230 (user-account-name user)))))))
232 (for-each (cut validate-supplementary-group user <>)
233 (user-account-supplementary-groups user)))
241 (define (user-group->gexp group)
242 "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for
244 #~(list #$(user-group-name group)
245 #$(user-group-password group)
246 #$(user-group-id group)
247 #$(user-group-system? group)))
249 (define (user-account->gexp account)
250 "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for
252 #~`(#$(user-account-name account)
253 #$(user-account-uid account)
254 #$(user-account-group account)
255 #$(user-account-supplementary-groups account)
256 #$(user-account-comment account)
257 #$(user-account-home-directory account)
258 ,#$(user-account-shell account) ; this one is a gexp
259 #$(user-account-password account)
260 #$(user-account-system? account)))
262 (define (account-activation accounts+groups)
263 "Return a gexp that activates ACCOUNTS+GROUPS, a list of <user-account> and
264 <user-group> objects. Raise an error if a user account refers to a undefined
267 (filter user-account? accounts+groups))
270 (map user-account->gexp accounts))
273 (filter user-group? accounts+groups))
276 (map user-group->gexp groups))
278 (assert-valid-users/groups accounts groups)
280 ;; Add users and user groups.
283 (string-append #$(@ (gnu packages admin) shadow) "/sbin"))
284 (activate-users+groups (list #$@user-specs)
285 (list #$@group-specs))))
287 (define (shells-file shells)
288 "Return a file-like object that builds a shell list for use as /etc/shells
289 based on SHELLS. /etc/shells is used by xterm, polkit, and other programs."
290 (computed-file "shells"
292 (use-modules (srfi srfi-1))
295 (delete-duplicates (list #$@shells)))
297 (call-with-output-file #$output
301 /run/current-system/profile/bin/sh
302 /run/current-system/profile/bin/bash\n" port)
303 (for-each (lambda (shell)
307 (define (etc-files arguments)
308 "Filter out among ARGUMENTS things corresponding to skeletons, and return
309 the /etc/skel directory for those."
310 (let ((skels (filter pair? arguments))
311 (users (filter user-account? arguments)))
312 `(("skel" ,(skeleton-directory skels))
313 ("shells" ,(shells-file (map user-account-shell users))))))
315 (define account-service-type
316 (service-type (name 'account)
318 ;; Concatenate <user-account>, <user-group>, and skeleton
320 (compose concatenate)
324 (list (service-extension activation-service-type
326 (service-extension etc-service-type
329 (define (account-service accounts+groups skeletons)
330 "Return a <service> that takes care of user accounts and user groups, with
331 ACCOUNTS+GROUPS as its initial list of accounts and groups."
332 (service account-service-type
333 (append skeletons accounts+groups)))
335 ;;; shadow.scm ends here