Commit | Line | Data |
---|---|---|
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) | |
29fce8b6 | 26 | #:use-module (guix profiles) |
033adfe7 LC |
27 | #:use-module (gnu packages base) |
28 | #:use-module (gnu packages bash) | |
bdb36958 | 29 | #:use-module (gnu packages guile) |
89a0d00a | 30 | #:use-module (gnu packages which) |
9de46ffb | 31 | #:use-module (gnu packages admin) |
8a07c289 | 32 | #:use-module (gnu packages linux) |
033adfe7 | 33 | #:use-module (gnu packages package-management) |
6f436c54 LC |
34 | #:use-module (gnu packages which) |
35 | #:use-module (gnu packages less) | |
36 | #:use-module (gnu packages zile) | |
55e70e65 LC |
37 | #:use-module (gnu packages nano) |
38 | #:use-module (gnu packages lsof) | |
a68c6967 LC |
39 | #:use-module (gnu packages gawk) |
40 | #:use-module (gnu packages compression) | |
41 | #:autoload (gnu packages cryptsetup) (cryptsetup) | |
db4fdc04 LC |
42 | #:use-module (gnu services) |
43 | #:use-module (gnu services dmd) | |
44 | #:use-module (gnu services base) | |
033adfe7 LC |
45 | #:use-module (gnu system grub) |
46 | #:use-module (gnu system shadow) | |
47 | #:use-module (gnu system linux) | |
735c6dd7 | 48 | #:use-module (gnu system linux-initrd) |
c5df1839 | 49 | #:use-module (gnu system file-systems) |
033adfe7 LC |
50 | #:use-module (ice-9 match) |
51 | #:use-module (srfi srfi-1) | |
52 | #:use-module (srfi srfi-26) | |
53 | #:export (operating-system | |
54 | operating-system? | |
d5b429ab LC |
55 | |
56 | operating-system-bootloader | |
033adfe7 | 57 | operating-system-services |
217a5b85 | 58 | operating-system-user-services |
033adfe7 | 59 | operating-system-packages |
fd3bfc44 | 60 | operating-system-host-name |
c65e1834 | 61 | operating-system-hosts-file |
fd3bfc44 LC |
62 | operating-system-kernel |
63 | operating-system-initrd | |
64 | operating-system-users | |
65 | operating-system-groups | |
548d4c13 | 66 | operating-system-issue |
fd3bfc44 LC |
67 | operating-system-packages |
68 | operating-system-timezone | |
69 | operating-system-locale | |
5dae0186 | 70 | operating-system-mapped-devices |
83bcd0b8 | 71 | operating-system-file-systems |
b25937e3 | 72 | operating-system-activation-script |
033adfe7 | 73 | |
1aa0033b | 74 | operating-system-derivation |
83bcd0b8 | 75 | operating-system-profile |
6f436c54 LC |
76 | operating-system-grub.cfg |
77 | ||
568841d4 | 78 | local-host-aliases |
df5ce088 | 79 | %setuid-programs |
5dae0186 LC |
80 | %base-packages |
81 | ||
82 | luks-device-mapping)) | |
033adfe7 LC |
83 | |
84 | ;;; Commentary: | |
85 | ;;; | |
86 | ;;; This module supports whole-system configuration. | |
87 | ;;; | |
88 | ;;; Code: | |
89 | ||
90 | ;; System-wide configuration. | |
91 | ;; TODO: Add per-field docstrings/stexi. | |
92 | (define-record-type* <operating-system> operating-system | |
93 | make-operating-system | |
94 | operating-system? | |
95 | (kernel operating-system-kernel ; package | |
96 | (default linux-libre)) | |
d5b429ab LC |
97 | (bootloader operating-system-bootloader) ; <grub-configuration> |
98 | ||
83bcd0b8 | 99 | (initrd operating-system-initrd ; (list fs) -> M derivation |
060238ae | 100 | (default base-initrd)) |
033adfe7 LC |
101 | |
102 | (host-name operating-system-host-name) ; string | |
c65e1834 LC |
103 | (hosts-file operating-system-hosts-file ; M item | #f |
104 | (default #f)) | |
033adfe7 | 105 | |
5dae0186 LC |
106 | (mapped-devices operating-system-mapped-devices ; list of <mapped-device> |
107 | (default '())) | |
8a6d2731 | 108 | (file-systems operating-system-file-systems) ; list of fs |
2a13d05e LC |
109 | (swap-devices operating-system-swap-devices ; list of strings |
110 | (default '())) | |
033adfe7 LC |
111 | |
112 | (users operating-system-users ; list of user accounts | |
113 | (default '())) | |
114 | (groups operating-system-groups ; list of user groups | |
773e956d | 115 | (default %base-groups)) |
033adfe7 | 116 | |
40281c54 LC |
117 | (skeletons operating-system-skeletons ; list of name/monadic value |
118 | (default (default-skeletons))) | |
548d4c13 LC |
119 | (issue operating-system-issue ; string |
120 | (default %default-issue)) | |
40281c54 | 121 | |
033adfe7 | 122 | (packages operating-system-packages ; list of (PACKAGE OUTPUT...) |
6f436c54 | 123 | (default %base-packages)) ; or just PACKAGE |
033adfe7 LC |
124 | |
125 | (timezone operating-system-timezone) ; string | |
8a6d2731 LC |
126 | (locale operating-system-locale ; string |
127 | (default "en_US.UTF-8")) | |
033adfe7 | 128 | |
217a5b85 | 129 | (services operating-system-user-services ; list of monadic services |
09e028f4 LC |
130 | (default %base-services)) |
131 | ||
132 | (pam-services operating-system-pam-services ; list of PAM services | |
133 | (default (base-pam-services))) | |
134 | (setuid-programs operating-system-setuid-programs | |
69689380 | 135 | (default %setuid-programs)) ; list of string-valued gexps |
033adfe7 | 136 | |
69689380 LC |
137 | (sudoers operating-system-sudoers ; /etc/sudoers contents |
138 | (default %sudoers-specification))) | |
033adfe7 | 139 | |
2717a89a | 140 | \f |
033adfe7 LC |
141 | ;;; |
142 | ;;; Derivation. | |
143 | ;;; | |
144 | ||
23f6056b | 145 | (define* (file-union name files) |
033adfe7 LC |
146 | "Return a derivation that builds a directory containing all of FILES. Each |
147 | item in FILES must be a list where the first element is the file name to use | |
23f6056b LC |
148 | in the new directory, and the second element is a gexp denoting the target |
149 | file." | |
150 | (define builder | |
151 | #~(begin | |
152 | (mkdir #$output) | |
153 | (chdir #$output) | |
154 | #$@(map (match-lambda | |
155 | ((target source) | |
156 | #~(symlink #$source #$target))) | |
157 | files))) | |
033adfe7 | 158 | |
23f6056b | 159 | (gexp->derivation name builder)) |
033adfe7 | 160 | |
40281c54 LC |
161 | \f |
162 | ;;; | |
163 | ;;; Services. | |
164 | ;;; | |
165 | ||
722554a3 | 166 | (define (open-luks-device source target) |
5dae0186 LC |
167 | "Return a gexp that maps SOURCE to TARGET as a LUKS device, using |
168 | 'cryptsetup'." | |
169 | #~(zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup") | |
170 | "open" "--type" "luks" | |
171 | #$source #$target))) | |
172 | ||
722554a3 LC |
173 | (define (close-luks-device source target) |
174 | "Return a gexp that closes TARGET, a LUKS device." | |
175 | #~(zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup") | |
176 | "close" #$target))) | |
177 | ||
178 | (define luks-device-mapping | |
179 | ;; The type of LUKS mapped devices. | |
180 | (mapped-device-kind | |
181 | (open open-luks-device) | |
182 | (close close-luks-device))) | |
183 | ||
023f391c LC |
184 | (define (other-file-system-services os) |
185 | "Return file system services for the file systems of OS that are not marked | |
186 | as 'needed-for-boot'." | |
187 | (define file-systems | |
188 | (remove (lambda (fs) | |
189 | (or (file-system-needed-for-boot? fs) | |
190 | (string=? "/" (file-system-mount-point fs)))) | |
191 | (operating-system-file-systems os))) | |
192 | ||
5dae0186 LC |
193 | (define (device-mappings fs) |
194 | (filter (lambda (md) | |
195 | (string=? (string-append "/dev/mapper/" | |
196 | (mapped-device-target md)) | |
197 | (file-system-device fs))) | |
198 | (operating-system-mapped-devices os))) | |
199 | ||
200 | (define (requirements fs) | |
201 | (map (lambda (md) | |
202 | (symbol-append 'device-mapping- | |
203 | (string->symbol (mapped-device-target md)))) | |
204 | (device-mappings fs))) | |
205 | ||
023f391c | 206 | (sequence %store-monad |
5dae0186 LC |
207 | (map (lambda (fs) |
208 | (match fs | |
209 | (($ <file-system> device title target type flags opts | |
210 | #f check? create?) | |
211 | (file-system-service device target type | |
212 | #:title title | |
213 | #:requirements (requirements fs) | |
214 | #:check? check? | |
215 | #:create-mount-point? create? | |
216 | #:options opts | |
217 | #:flags flags)))) | |
023f391c LC |
218 | file-systems))) |
219 | ||
de1c158f LC |
220 | (define (mapped-device-user device file-systems) |
221 | "Return a file system among FILE-SYSTEMS that uses DEVICE, or #f." | |
222 | (let ((target (string-append "/dev/mapper/" (mapped-device-target device)))) | |
223 | (find (lambda (fs) | |
224 | (string=? (file-system-device fs) target)) | |
225 | file-systems))) | |
226 | ||
227 | (define (operating-system-user-mapped-devices os) | |
228 | "Return the subset of mapped devices that can be installed in | |
229 | user-land--i.e., those not needed during boot." | |
230 | (let ((devices (operating-system-mapped-devices os)) | |
231 | (file-systems (operating-system-file-systems os))) | |
232 | (filter (lambda (md) | |
233 | (let ((user (mapped-device-user md file-systems))) | |
234 | (or (not user) | |
235 | (not (file-system-needed-for-boot? user))))) | |
236 | devices))) | |
237 | ||
238 | (define (operating-system-boot-mapped-devices os) | |
239 | "Return the subset of mapped devices that must be installed during boot, | |
240 | from the initrd." | |
241 | (let ((devices (operating-system-mapped-devices os)) | |
242 | (file-systems (operating-system-file-systems os))) | |
243 | (filter (lambda (md) | |
244 | (let ((user (mapped-device-user md file-systems))) | |
245 | (and user (file-system-needed-for-boot? user)))) | |
246 | devices))) | |
247 | ||
5dae0186 LC |
248 | (define (device-mapping-services os) |
249 | "Return the list of device-mapping services for OS as a monadic list." | |
250 | (sequence %store-monad | |
251 | (map (lambda (md) | |
722554a3 LC |
252 | (let* ((source (mapped-device-source md)) |
253 | (target (mapped-device-target md)) | |
254 | (type (mapped-device-type md)) | |
255 | (open (mapped-device-kind-open type)) | |
256 | (close (mapped-device-kind-close type))) | |
5dae0186 | 257 | (device-mapping-service target |
722554a3 LC |
258 | (open source target) |
259 | (close source target)))) | |
de1c158f | 260 | (operating-system-user-mapped-devices os)))) |
5dae0186 | 261 | |
2a13d05e LC |
262 | (define (swap-services os) |
263 | "Return the list of swap services for OS as a monadic list." | |
264 | (sequence %store-monad | |
265 | (map swap-service (operating-system-swap-devices os)))) | |
266 | ||
217a5b85 LC |
267 | (define (essential-services os) |
268 | "Return the list of essential services for OS. These are special services | |
269 | that implement part of what's declared in OS are responsible for low-level | |
270 | bookkeeping." | |
5dae0186 LC |
271 | (mlet* %store-monad ((mappings (device-mapping-services os)) |
272 | (root-fs (root-file-system-service)) | |
023f391c | 273 | (other-fs (other-file-system-services os)) |
2a13d05e | 274 | (swaps (swap-services os)) |
023f391c LC |
275 | (procs (user-processes-service |
276 | (map (compose first service-provision) | |
277 | other-fs))) | |
278 | (host-name (host-name-service | |
279 | (operating-system-host-name os)))) | |
5dae0186 | 280 | (return (cons* host-name procs root-fs |
2a13d05e | 281 | (append other-fs mappings swaps))))) |
217a5b85 LC |
282 | |
283 | (define (operating-system-services os) | |
284 | "Return all the services of OS, including \"internal\" services that do not | |
285 | explicitly appear in OS." | |
286 | (mlet %store-monad | |
287 | ((user (sequence %store-monad (operating-system-user-services os))) | |
288 | (essential (essential-services os))) | |
289 | (return (append essential user)))) | |
290 | ||
40281c54 LC |
291 | \f |
292 | ;;; | |
293 | ;;; /etc. | |
294 | ;;; | |
295 | ||
6f436c54 LC |
296 | (define %base-packages |
297 | ;; Default set of packages globally visible. It should include anything | |
298 | ;; required for basic administrator tasks. | |
55e70e65 | 299 | (cons* procps psmisc which less zile nano |
bdb36958 | 300 | (@ (gnu packages admin) dmd) guix |
55e70e65 | 301 | lsof ;for Guix's 'list-runtime-roots' |
a68c6967 | 302 | util-linux inetutils isc-dhcp wireless-tools |
9b762b8d | 303 | net-tools ; XXX: remove when Inetutils suffices |
03e9998f LC |
304 | |
305 | ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev | |
306 | ;; already depends on it anyway. | |
8a7330fd | 307 | kmod eudev |
03e9998f | 308 | |
b63dbd44 | 309 | e2fsprogs kbd |
9b762b8d LC |
310 | |
311 | ;; The packages below are also in %FINAL-INPUTS, so take them from | |
312 | ;; there to avoid duplication. | |
313 | (map canonical-package | |
a68c6967 | 314 | (list guile-2.0 bash coreutils findutils grep sed |
4b164c45 | 315 | diffutils patch gawk tar gzip bzip2 xz lzip)))) |
6f436c54 | 316 | |
548d4c13 LC |
317 | (define %default-issue |
318 | ;; Default contents for /etc/issue. | |
319 | " | |
320 | This is the GNU system. Welcome.\n") | |
321 | ||
568841d4 LC |
322 | (define (local-host-aliases host-name) |
323 | "Return aliases for HOST-NAME, to be used in /etc/hosts." | |
324 | (string-append "127.0.0.1 localhost " host-name "\n" | |
325 | "::1 localhost " host-name "\n")) | |
326 | ||
c65e1834 LC |
327 | (define (default-/etc/hosts host-name) |
328 | "Return the default /etc/hosts file." | |
568841d4 | 329 | (text-file "hosts" (local-host-aliases host-name))) |
c65e1834 | 330 | |
033adfe7 | 331 | (define* (etc-directory #:key |
3141a8bd | 332 | (locale "C") (timezone "Europe/Paris") |
548d4c13 | 333 | (issue "Hello!\n") |
40281c54 | 334 | (skeletons '()) |
033adfe7 | 335 | (pam-services '()) |
b4140694 | 336 | (profile "/run/current-system/profile") |
c65e1834 | 337 | hosts-file |
69689380 | 338 | (sudoers "")) |
033adfe7 LC |
339 | "Return a derivation that builds the static part of the /etc directory." |
340 | (mlet* %store-monad | |
ab6a279a | 341 | ((pam.d (pam-services->directory pam-services)) |
69689380 | 342 | (sudoers (text-file "sudoers" sudoers)) |
033adfe7 | 343 | (login.defs (text-file "login.defs" "# Empty for now.\n")) |
9038298c LC |
344 | (shells (text-file "shells" ; used by xterm and others |
345 | "\ | |
346 | /bin/sh | |
b4140694 LC |
347 | /run/current-system/profile/bin/sh |
348 | /run/current-system/profile/bin/bash\n")) | |
548d4c13 | 349 | (issue (text-file "issue" issue)) |
033adfe7 | 350 | |
07b08343 LC |
351 | ;; For now, generate a basic config so that /etc/hosts is honored. |
352 | (nsswitch (text-file "nsswitch.conf" | |
353 | "hosts: files dns\n")) | |
354 | ||
033adfe7 | 355 | ;; TODO: Generate bashrc from packages' search-paths. |
7aec3683 | 356 | (bashrc (text-file* "bashrc" " |
c851400b | 357 | export PS1='\\u@\\h \\w\\$ ' |
3141a8bd LC |
358 | |
359 | export LC_ALL=\"" locale "\" | |
360 | export TZ=\"" timezone "\" | |
7aec3683 | 361 | export TZDIR=\"" tzdata "/share/zoneinfo\" |
3141a8bd | 362 | |
d3bbe992 | 363 | # Tell 'modprobe' & co. where to look for modules. |
62f0a479 | 364 | export LINUX_MODULE_DIRECTORY=/run/booted-system/kernel/lib/modules |
d3bbe992 | 365 | |
585c6519 LC |
366 | export PATH=$HOME/.guix-profile/bin:/run/current-system/profile/bin |
367 | export PATH=/run/setuid-programs:/run/current-system/profile/sbin:$PATH | |
033adfe7 LC |
368 | export CPATH=$HOME/.guix-profile/include:" profile "/include |
369 | export LIBRARY_PATH=$HOME/.guix-profile/lib:" profile "/lib | |
370 | alias ls='ls -p --color' | |
371 | alias ll='ls -l' | |
40281c54 LC |
372 | ")) |
373 | (skel (skeleton-directory skeletons))) | |
23f6056b LC |
374 | (file-union "etc" |
375 | `(("services" ,#~(string-append #$net-base "/etc/services")) | |
376 | ("protocols" ,#~(string-append #$net-base "/etc/protocols")) | |
377 | ("rpc" ,#~(string-append #$net-base "/etc/rpc")) | |
378 | ("pam.d" ,#~#$pam.d) | |
379 | ("login.defs" ,#~#$login.defs) | |
380 | ("issue" ,#~#$issue) | |
07b08343 | 381 | ("nsswitch.conf" ,#~#$nsswitch) |
40281c54 | 382 | ("skel" ,#~#$skel) |
23f6056b LC |
383 | ("shells" ,#~#$shells) |
384 | ("profile" ,#~#$bashrc) | |
c65e1834 | 385 | ("hosts" ,#~#$hosts-file) |
23f6056b LC |
386 | ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/" |
387 | #$timezone)) | |
69689380 | 388 | ("sudoers" ,#~#$sudoers))))) |
033adfe7 | 389 | |
1aa0033b | 390 | (define (operating-system-profile os) |
29fce8b6 LC |
391 | "Return a derivation that builds the system profile of OS." |
392 | (profile-derivation (manifest (map package->manifest-entry | |
393 | (operating-system-packages os))))) | |
f6a9d048 | 394 | |
ab6a279a LC |
395 | (define %root-account |
396 | ;; Default root account. | |
397 | (user-account | |
398 | (name "root") | |
399 | (password "") | |
400 | (uid 0) (group "root") | |
401 | (comment "System administrator") | |
402 | (home-directory "/root"))) | |
403 | ||
0b6f49ef LC |
404 | (define (operating-system-accounts os) |
405 | "Return the user accounts for OS, including an obligatory 'root' account." | |
ab6a279a LC |
406 | (define users |
407 | ;; Make sure there's a root account. | |
408 | (if (find (lambda (user) | |
409 | (and=> (user-account-uid user) zero?)) | |
410 | (operating-system-users os)) | |
411 | (operating-system-users os) | |
412 | (cons %root-account (operating-system-users os)))) | |
413 | ||
217a5b85 | 414 | (mlet %store-monad ((services (operating-system-services os))) |
ab6a279a LC |
415 | (return (append users |
416 | (append-map service-user-accounts services))))) | |
0b6f49ef LC |
417 | |
418 | (define (operating-system-etc-directory os) | |
419 | "Return that static part of the /etc directory of OS." | |
033adfe7 | 420 | (mlet* %store-monad |
217a5b85 | 421 | ((services (operating-system-services os)) |
033adfe7 LC |
422 | (pam-services -> |
423 | ;; Services known to PAM. | |
424 | (delete-duplicates | |
09e028f4 LC |
425 | (append (operating-system-pam-services os) |
426 | (append-map service-pam-services services)))) | |
40281c54 | 427 | (profile-drv (operating-system-profile os)) |
c65e1834 LC |
428 | (skeletons (operating-system-skeletons os)) |
429 | (/etc/hosts (or (operating-system-hosts-file os) | |
430 | (default-/etc/hosts (operating-system-host-name os))))) | |
62f0a479 | 431 | (etc-directory #:pam-services pam-services |
40281c54 | 432 | #:skeletons skeletons |
548d4c13 | 433 | #:issue (operating-system-issue os) |
0b6f49ef LC |
434 | #:locale (operating-system-locale os) |
435 | #:timezone (operating-system-timezone os) | |
c65e1834 | 436 | #:hosts-file /etc/hosts |
69689380 | 437 | #:sudoers (operating-system-sudoers os) |
0b6f49ef | 438 | #:profile profile-drv))) |
033adfe7 | 439 | |
09e028f4 LC |
440 | (define %setuid-programs |
441 | ;; Default set of setuid-root programs. | |
442 | (let ((shadow (@ (gnu packages admin) shadow))) | |
443 | (list #~(string-append #$shadow "/bin/passwd") | |
444 | #~(string-append #$shadow "/bin/su") | |
69689380 | 445 | #~(string-append #$inetutils "/bin/ping") |
8a07c289 LC |
446 | #~(string-append #$sudo "/bin/sudo") |
447 | #~(string-append #$fuse "/bin/fusermount")))) | |
69689380 LC |
448 | |
449 | (define %sudoers-specification | |
450 | ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel' | |
451 | ;; group can do anything. See | |
452 | ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>. | |
453 | ;; TODO: Add a declarative API. | |
454 | "root ALL=(ALL) ALL | |
455 | %wheel ALL=(ALL) ALL\n") | |
09e028f4 | 456 | |
ab6a279a LC |
457 | (define (user-group->gexp group) |
458 | "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for | |
459 | 'active-groups'." | |
460 | #~(list #$(user-group-name group) | |
461 | #$(user-group-password group) | |
c8fa3426 LC |
462 | #$(user-group-id group) |
463 | #$(user-group-system? group))) | |
ab6a279a LC |
464 | |
465 | (define (user-account->gexp account) | |
466 | "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for | |
467 | 'activate-users'." | |
468 | #~`(#$(user-account-name account) | |
469 | #$(user-account-uid account) | |
470 | #$(user-account-group account) | |
471 | #$(user-account-supplementary-groups account) | |
472 | #$(user-account-comment account) | |
473 | #$(user-account-home-directory account) | |
474 | ,#$(user-account-shell account) ; this one is a gexp | |
459dd9ea LC |
475 | #$(user-account-password account) |
476 | #$(user-account-system? account))) | |
ab6a279a | 477 | |
484a2b3a LC |
478 | (define (operating-system-activation-script os) |
479 | "Return the activation script for OS---i.e., the code that \"activates\" the | |
480 | stateful part of OS, including user accounts and groups, special directories, | |
481 | etc." | |
09e028f4 | 482 | (define %modules |
548f7a8f | 483 | '((gnu build activation) |
8a9e21d1 | 484 | (gnu build linux-boot) |
e2f4b305 | 485 | (gnu build file-systems) |
548f7a8f | 486 | (guix build utils))) |
09e028f4 | 487 | |
55ccc388 LC |
488 | (define (service-activations services) |
489 | ;; Return the activation scripts for SERVICES. | |
490 | (let ((gexps (filter-map service-activate services))) | |
491 | (sequence %store-monad (map (cut gexp->file "activate-service.scm" <>) | |
492 | gexps)))) | |
493 | ||
ab6a279a | 494 | (mlet* %store-monad ((services (operating-system-services os)) |
55ccc388 | 495 | (actions (service-activations services)) |
ab6a279a LC |
496 | (etc (operating-system-etc-directory os)) |
497 | (modules (imported-modules %modules)) | |
498 | (compiled (compiled-modules %modules)) | |
ab6a279a | 499 | (accounts (operating-system-accounts os))) |
09e028f4 LC |
500 | (define setuid-progs |
501 | (operating-system-setuid-programs os)) | |
502 | ||
ab6a279a LC |
503 | (define user-specs |
504 | (map user-account->gexp accounts)) | |
505 | ||
506 | (define groups | |
507 | (append (operating-system-groups os) | |
508 | (append-map service-user-groups services))) | |
509 | ||
510 | (define group-specs | |
511 | (map user-group->gexp groups)) | |
512 | ||
4654439b | 513 | (gexp->file "activate" |
4dfe6c58 LC |
514 | #~(begin |
515 | (eval-when (expand load eval) | |
516 | ;; Make sure 'use-modules' below succeeds. | |
517 | (set! %load-path (cons #$modules %load-path)) | |
518 | (set! %load-compiled-path | |
519 | (cons #$compiled %load-compiled-path))) | |
520 | ||
548f7a8f | 521 | (use-modules (gnu build activation)) |
4dfe6c58 | 522 | |
ee248b6a LC |
523 | ;; Make sure /bin/sh is valid and current. |
524 | (activate-/bin/sh | |
525 | (string-append #$(canonical-package bash) | |
526 | "/bin/sh")) | |
527 | ||
4dfe6c58 LC |
528 | ;; Populate /etc. |
529 | (activate-etc #$etc) | |
530 | ||
ab6a279a LC |
531 | ;; Add users and user groups. |
532 | (setenv "PATH" | |
533 | (string-append #$(@ (gnu packages admin) shadow) | |
534 | "/sbin")) | |
535 | (activate-users+groups (list #$@user-specs) | |
536 | (list #$@group-specs)) | |
537 | ||
09e028f4 LC |
538 | ;; Activate setuid programs. |
539 | (activate-setuid-programs (list #$@setuid-progs)) | |
540 | ||
55ccc388 LC |
541 | ;; Run the services' activation snippets. |
542 | ;; TODO: Use 'load-compiled'. | |
543 | (for-each primitive-load '#$actions) | |
544 | ||
b4140694 | 545 | ;; Set up /run/current-system. |
484a2b3a LC |
546 | (activate-current-system))))) |
547 | ||
548 | (define (operating-system-boot-script os) | |
549 | "Return the boot script for OS---i.e., the code started by the initrd once | |
550 | we're running in the final root." | |
551 | (mlet* %store-monad ((services (operating-system-services os)) | |
552 | (activate (operating-system-activation-script os)) | |
553 | (dmd-conf (dmd-configuration-file services))) | |
554 | (gexp->file "boot" | |
555 | #~(begin | |
556 | ;; Activate the system. | |
557 | ;; TODO: Use 'load-compiled'. | |
558 | (primitive-load #$activate) | |
559 | ||
560 | ;; Keep track of the booted system. | |
561 | (false-if-exception (delete-file "/run/booted-system")) | |
562 | (symlink (readlink "/run/current-system") | |
563 | "/run/booted-system") | |
b4140694 | 564 | |
26a728eb LC |
565 | ;; Close any remaining open file descriptors to be on the |
566 | ;; safe side. This must be the very last thing we do, | |
567 | ;; because Guile has internal FDs such as 'sleep_pipe' | |
568 | ;; that need to be alive. | |
569 | (let loop ((fd 3)) | |
570 | (when (< fd 1024) | |
571 | (false-if-exception (close-fdes fd)) | |
572 | (loop (+ 1 fd)))) | |
573 | ||
4dfe6c58 LC |
574 | ;; Start dmd. |
575 | (execl (string-append #$dmd "/bin/dmd") | |
576 | "dmd" "--config" #$dmd-conf))))) | |
2106d3fc | 577 | |
83bcd0b8 LC |
578 | (define (operating-system-root-file-system os) |
579 | "Return the root file system of OS." | |
580 | (find (match-lambda | |
d4c87617 | 581 | (($ <file-system> _ _ "/") #t) |
83bcd0b8 LC |
582 | (_ #f)) |
583 | (operating-system-file-systems os))) | |
584 | ||
b4140694 LC |
585 | (define (operating-system-initrd-file os) |
586 | "Return a gexp denoting the initrd file of OS." | |
83bcd0b8 LC |
587 | (define boot-file-systems |
588 | (filter (match-lambda | |
d4c87617 | 589 | (($ <file-system> device title "/") |
3c05b4bc | 590 | #t) |
d4c87617 LC |
591 | (($ <file-system> device title mount-point type flags |
592 | options boot?) | |
3c05b4bc | 593 | boot?)) |
83bcd0b8 LC |
594 | (operating-system-file-systems os))) |
595 | ||
de1c158f LC |
596 | (define mapped-devices |
597 | (operating-system-boot-mapped-devices os)) | |
598 | ||
599 | (define make-initrd | |
600 | (operating-system-initrd os)) | |
601 | ||
602 | (mlet %store-monad ((initrd (make-initrd boot-file-systems | |
603 | #:mapped-devices mapped-devices))) | |
b4140694 LC |
604 | (return #~(string-append #$initrd "/initrd")))) |
605 | ||
2d23e6f0 LC |
606 | (define (kernel->grub-label kernel) |
607 | "Return a label for the GRUB menu entry that boots KERNEL." | |
608 | (string-append "GNU system with " | |
609 | (string-titlecase (package-name kernel)) " " | |
610 | (package-version kernel) | |
611 | " (technology preview)")) | |
612 | ||
fe6e3fe2 LC |
613 | (define* (operating-system-grub.cfg os #:optional (old-entries '())) |
614 | "Return the GRUB configuration file for OS. Use OLD-ENTRIES to populate the | |
615 | \"old entries\" menu." | |
0b6f49ef | 616 | (mlet* %store-monad |
b4140694 | 617 | ((system (operating-system-derivation os)) |
83bcd0b8 | 618 | (root-fs -> (operating-system-root-file-system os)) |
b4140694 | 619 | (kernel -> (operating-system-kernel os)) |
033adfe7 | 620 | (entries -> (list (menu-entry |
2d23e6f0 | 621 | (label (kernel->grub-label kernel)) |
033adfe7 | 622 | (linux kernel) |
f6a7b21d | 623 | (linux-arguments |
83bcd0b8 LC |
624 | (list (string-append "--root=" |
625 | (file-system-device root-fs)) | |
b4140694 LC |
626 | #~(string-append "--system=" #$system) |
627 | #~(string-append "--load=" #$system | |
628 | "/boot"))) | |
629 | (initrd #~(string-append #$system "/initrd")))))) | |
fe6e3fe2 LC |
630 | (grub-configuration-file (operating-system-bootloader os) entries |
631 | #:old-entries old-entries))) | |
b4140694 | 632 | |
64e40dbb LC |
633 | (define (operating-system-parameters-file os) |
634 | "Return a file that describes the boot parameters of OS. The primary use of | |
635 | this file is the reconstruction of GRUB menu entries for old configurations." | |
636 | (mlet %store-monad ((initrd (operating-system-initrd-file os)) | |
637 | (root -> (operating-system-root-file-system os)) | |
638 | (label -> (kernel->grub-label | |
639 | (operating-system-kernel os)))) | |
640 | (gexp->file "parameters" | |
641 | #~(boot-parameters (version 0) | |
642 | (label #$label) | |
643 | (root-device #$(file-system-device root)) | |
644 | (kernel #$(operating-system-kernel os)) | |
645 | (initrd #$initrd))))) | |
646 | ||
b4140694 LC |
647 | (define (operating-system-derivation os) |
648 | "Return a derivation that builds OS." | |
649 | (mlet* %store-monad | |
650 | ((profile (operating-system-profile os)) | |
651 | (etc (operating-system-etc-directory os)) | |
652 | (boot (operating-system-boot-script os)) | |
653 | (kernel -> (operating-system-kernel os)) | |
64e40dbb LC |
654 | (initrd (operating-system-initrd-file os)) |
655 | (params (operating-system-parameters-file os))) | |
23f6056b | 656 | (file-union "system" |
f6a7b21d | 657 | `(("boot" ,#~#$boot) |
23f6056b | 658 | ("kernel" ,#~#$kernel) |
64e40dbb | 659 | ("parameters" ,#~#$params) |
b4140694 | 660 | ("initrd" ,initrd) |
23f6056b | 661 | ("profile" ,#~#$profile) |
23f6056b | 662 | ("etc" ,#~#$etc))))) |
033adfe7 LC |
663 | |
664 | ;;; system.scm ends here |