services: xorg: Rewrite using gexps.
[jackhill/guix/guix.git] / gnu / system.scm
CommitLineData
033adfe7 1;;; GNU Guix --- Functional package management for GNU
6ce206cb 2;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
033adfe7
LC
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (gnu system)
20 #:use-module (guix store)
21 #:use-module (guix monads)
02100028 22 #:use-module (guix gexp)
033adfe7
LC
23 #:use-module (guix records)
24 #:use-module (guix packages)
25 #:use-module (guix derivations)
033adfe7
LC
26 #:use-module (gnu packages base)
27 #:use-module (gnu packages bash)
9de46ffb 28 #:use-module (gnu packages admin)
033adfe7 29 #:use-module (gnu packages package-management)
db4fdc04
LC
30 #:use-module (gnu services)
31 #:use-module (gnu services dmd)
32 #:use-module (gnu services base)
033adfe7
LC
33 #:use-module (gnu system grub)
34 #:use-module (gnu system shadow)
35 #:use-module (gnu system linux)
735c6dd7 36 #:use-module (gnu system linux-initrd)
033adfe7
LC
37 #:use-module (ice-9 match)
38 #:use-module (srfi srfi-1)
39 #:use-module (srfi srfi-26)
40 #:export (operating-system
41 operating-system?
42 operating-system-services
43 operating-system-packages
fd3bfc44
LC
44 operating-system-bootloader-entries
45 operating-system-host-name
46 operating-system-kernel
47 operating-system-initrd
48 operating-system-users
49 operating-system-groups
50 operating-system-packages
51 operating-system-timezone
52 operating-system-locale
53 operating-system-services
033adfe7 54
1aa0033b
LC
55 operating-system-derivation
56 operating-system-profile))
033adfe7
LC
57
58;;; Commentary:
59;;;
60;;; This module supports whole-system configuration.
61;;;
62;;; Code:
63
64;; System-wide configuration.
65;; TODO: Add per-field docstrings/stexi.
66(define-record-type* <operating-system> operating-system
67 make-operating-system
68 operating-system?
69 (kernel operating-system-kernel ; package
70 (default linux-libre))
71 (bootloader operating-system-bootloader ; package
72 (default grub))
73 (bootloader-entries operating-system-bootloader-entries ; list
74 (default '()))
735c6dd7
LC
75 (initrd operating-system-initrd ; monadic derivation
76 (default (gnu-system-initrd)))
033adfe7
LC
77
78 (host-name operating-system-host-name) ; string
79
80 (file-systems operating-system-file-systems ; list of fs
81 (default '()))
82
83 (users operating-system-users ; list of user accounts
84 (default '()))
85 (groups operating-system-groups ; list of user groups
86 (default (list (user-group
87 (name "root")
88 (id 0))
89 (user-group
90 (name "users")
91 (id 100)
92 (members '("guest"))))))
93
94 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
4f62d8d6
LC
95 (default (list coreutils ; or just PACKAGE
96 grep
97 sed
98 findutils
99 guile
100 bash
101 (@ (gnu packages dmd) dmd)
3141a8bd
LC
102 guix
103 tzdata)))
033adfe7
LC
104
105 (timezone operating-system-timezone) ; string
106 (locale operating-system-locale) ; string
107
108 (services operating-system-services ; list of monadic services
8b198abe 109 (default %base-services)))
033adfe7
LC
110
111
112\f
113;;;
114;;; Derivation.
115;;;
116
117(define* (union inputs
118 #:key (guile (%guile-for-build)) (system (%current-system))
119 (name "union"))
120 "Return a derivation that builds the union of INPUTS. INPUTS is a list of
121input tuples."
122 (define builder
123 '(begin
124 (use-modules (guix build union))
125
126 (setvbuf (current-output-port) _IOLBF)
127 (setvbuf (current-error-port) _IOLBF)
128
129 (let ((output (assoc-ref %outputs "out"))
130 (inputs (map cdr %build-inputs)))
131 (format #t "building union `~a' with ~a packages...~%"
132 output (length inputs))
133 (union-build output inputs))))
134
135 (mlet %store-monad
136 ((inputs (sequence %store-monad
137 (map (match-lambda
4f62d8d6 138 ((or ((? package? p)) (? package? p))
033adfe7
LC
139 (mlet %store-monad
140 ((drv (package->derivation p system)))
141 (return `(,name ,drv))))
4f62d8d6 142 (((? package? p) output)
033adfe7
LC
143 (mlet %store-monad
144 ((drv (package->derivation p system)))
145 (return `(,name ,drv ,output))))
146 (x
147 (return x)))
148 inputs))))
149 (derivation-expression name builder
150 #:system system
151 #:inputs inputs
152 #:modules '((guix build union))
6ce206cb
LC
153 #:guile-for-build guile
154 #:local-build? #t)))
033adfe7 155
23f6056b 156(define* (file-union name files)
033adfe7
LC
157 "Return a derivation that builds a directory containing all of FILES. Each
158item in FILES must be a list where the first element is the file name to use
23f6056b
LC
159in the new directory, and the second element is a gexp denoting the target
160file."
161 (define builder
162 #~(begin
163 (mkdir #$output)
164 (chdir #$output)
165 #$@(map (match-lambda
166 ((target source)
167 #~(symlink #$source #$target)))
168 files)))
033adfe7 169
23f6056b 170 (gexp->derivation name builder))
033adfe7 171
033adfe7 172(define* (etc-directory #:key
3141a8bd 173 (locale "C") (timezone "Europe/Paris")
033adfe7
LC
174 (accounts '())
175 (groups '())
176 (pam-services '())
177 (profile "/var/run/current-system/profile"))
178 "Return a derivation that builds the static part of the /etc directory."
179 (mlet* %store-monad
23f6056b 180 ((passwd (passwd-file accounts))
033adfe7
LC
181 (shadow (passwd-file accounts #:shadow? #t))
182 (group (group-file groups))
183 (pam.d (pam-services->directory pam-services))
184 (login.defs (text-file "login.defs" "# Empty for now.\n"))
9038298c
LC
185 (shells (text-file "shells" ; used by xterm and others
186 "\
187/bin/sh
188/run/current-system/bin/sh
189/run/current-system/bin/bash\n"))
033adfe7
LC
190 (issue (text-file "issue" "
191This is an alpha preview of the GNU system. Welcome.
192
193This image features the GNU Guix package manager, which was used to
194build it (http://www.gnu.org/software/guix/). The init system is
195GNU dmd (http://www.gnu.org/software/dmd/).
196
197You can log in as 'guest' or 'root' with no password.
198"))
199
200 ;; TODO: Generate bashrc from packages' search-paths.
7aec3683 201 (bashrc (text-file* "bashrc" "
033adfe7 202export PS1='\\u@\\h\\$ '
3141a8bd
LC
203
204export LC_ALL=\"" locale "\"
205export TZ=\"" timezone "\"
7aec3683 206export TZDIR=\"" tzdata "/share/zoneinfo\"
3141a8bd 207
033adfe7
LC
208export PATH=$HOME/.guix-profile/bin:" profile "/bin:" profile "/sbin
209export CPATH=$HOME/.guix-profile/include:" profile "/include
210export LIBRARY_PATH=$HOME/.guix-profile/lib:" profile "/lib
211alias ls='ls -p --color'
212alias ll='ls -l'
23f6056b
LC
213")))
214 (file-union "etc"
215 `(("services" ,#~(string-append #$net-base "/etc/services"))
216 ("protocols" ,#~(string-append #$net-base "/etc/protocols"))
217 ("rpc" ,#~(string-append #$net-base "/etc/rpc"))
218 ("pam.d" ,#~#$pam.d)
219 ("login.defs" ,#~#$login.defs)
220 ("issue" ,#~#$issue)
221 ("shells" ,#~#$shells)
222 ("profile" ,#~#$bashrc)
223 ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/"
224 #$timezone))
225 ("passwd" ,#~#$passwd)
226 ("shadow" ,#~#$shadow)
227 ("group" ,#~#$group)))))
033adfe7 228
1aa0033b 229(define (operating-system-profile os)
f6a9d048
LC
230 "Return a derivation that builds the default profile of OS."
231 ;; TODO: Replace with a real profile with a manifest.
232 (union (operating-system-packages os)
233 #:name "default-profile"))
234
0b6f49ef
LC
235(define (operating-system-accounts os)
236 "Return the user accounts for OS, including an obligatory 'root' account."
237 (mlet %store-monad ((services (sequence %store-monad
238 (operating-system-services os))))
239 (return (cons (user-account
240 (name "root")
241 (password "")
242 (uid 0) (gid 0)
243 (comment "System administrator")
244 (home-directory "/root"))
245 (append (operating-system-users os)
246 (append-map service-user-accounts
247 services))))))
248
249(define (operating-system-etc-directory os)
250 "Return that static part of the /etc directory of OS."
033adfe7 251 (mlet* %store-monad
0b6f49ef 252 ((services (sequence %store-monad (operating-system-services os)))
033adfe7
LC
253 (pam-services ->
254 ;; Services known to PAM.
255 (delete-duplicates
256 (cons %pam-other-services
257 (append-map service-pam-services services))))
0b6f49ef 258 (accounts (operating-system-accounts os))
1aa0033b 259 (profile-drv (operating-system-profile os))
033adfe7 260 (groups -> (append (operating-system-groups os)
0b6f49ef
LC
261 (append-map service-user-groups services))))
262 (etc-directory #:accounts accounts #:groups groups
263 #:pam-services pam-services
264 #:locale (operating-system-locale os)
265 #:timezone (operating-system-timezone os)
266 #:profile profile-drv)))
033adfe7 267
2106d3fc
LC
268(define (operating-system-boot-script os)
269 "Return the boot script for OS---i.e., the code started by the initrd once
270we're running in the final root."
271 (mlet* %store-monad
272 ((services (sequence %store-monad (operating-system-services os)))
273 (etc (operating-system-etc-directory os))
b5f4e686 274 (dmd-conf (dmd-configuration-file services etc)))
02100028
LC
275 (gexp->file "boot"
276 #~(execl (string-append #$dmd "/bin/dmd")
277 "dmd" "--config" #$dmd-conf))))
2106d3fc 278
0b6f49ef
LC
279(define (operating-system-derivation os)
280 "Return a derivation that builds OS."
281 (mlet* %store-monad
23f6056b
LC
282 ((profile (operating-system-profile os))
283 (etc (operating-system-etc-directory os))
0b6f49ef 284 (services (sequence %store-monad (operating-system-services os)))
f6a7b21d 285 (boot (operating-system-boot-script os))
033adfe7 286 (kernel -> (operating-system-kernel os))
735c6dd7 287 (initrd (operating-system-initrd os))
f6a7b21d 288 (initrd-file -> #~(string-append #$initrd "/initrd"))
033adfe7
LC
289 (entries -> (list (menu-entry
290 (label (string-append
291 "GNU system with "
292 (package-full-name kernel)
293 " (technology preview)"))
294 (linux kernel)
f6a7b21d
LC
295 (linux-arguments
296 (list "--root=/dev/sda1"
297 #~(string-append "--load=" #$boot)))
735c6dd7 298 (initrd initrd-file))))
b5f4e686 299 (grub.cfg (grub-configuration-file entries)))
23f6056b 300 (file-union "system"
f6a7b21d 301 `(("boot" ,#~#$boot)
23f6056b 302 ("kernel" ,#~#$kernel)
f6a7b21d 303 ("initrd" ,initrd-file)
23f6056b
LC
304 ("profile" ,#~#$profile)
305 ("grub.cfg" ,#~#$grub.cfg)
306 ("etc" ,#~#$etc)))))
033adfe7
LC
307
308;;; system.scm ends here