linux-container: Use 'source-module-closure' when generating the script.
[jackhill/guix/guix.git] / gnu / system / linux-container.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
3 ;;; Copyright © 2016 Ludovic Courtès <ludo@gnu.org>
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 linux-container)
21 #:use-module (ice-9 match)
22 #:use-module (srfi srfi-1)
23 #:use-module (guix config)
24 #:use-module (guix store)
25 #:use-module (guix gexp)
26 #:use-module (guix derivations)
27 #:use-module (guix monads)
28 #:use-module (guix modules)
29 #:use-module (gnu build linux-container)
30 #:use-module (gnu services)
31 #:use-module (gnu system)
32 #:use-module (gnu system file-systems)
33 #:export (mapping->file-system
34 system-container
35 containerized-operating-system
36 container-script))
37
38 (define (mapping->file-system mapping)
39 "Return a file system that realizes MAPPING."
40 (match mapping
41 (($ <file-system-mapping> source target writable?)
42 (file-system
43 (mount-point target)
44 (device source)
45 (type "none")
46 (flags (if writable?
47 '(bind-mount)
48 '(bind-mount read-only)))
49 (check? #f)
50 (create-mount-point? #t)))))
51
52 (define (containerized-operating-system os mappings)
53 "Return an operating system based on OS for use in a Linux container
54 environment. MAPPINGS is a list of <file-system-mapping> to realize in the
55 containerized OS."
56 (define user-file-systems
57 (remove (lambda (fs)
58 (let ((target (file-system-mount-point fs))
59 (source (file-system-device fs)))
60 (or (string=? target (%store-prefix))
61 (string=? target "/")
62 (and (string? source)
63 (string-prefix? "/dev/" source))
64 (string-prefix? "/dev" target)
65 (string-prefix? "/sys" target))))
66 (operating-system-file-systems os)))
67
68 (define (mapping->fs fs)
69 (file-system (inherit (mapping->file-system fs))
70 (needed-for-boot? #t)))
71
72 (operating-system (inherit os)
73 (swap-devices '()) ; disable swap
74 (file-systems (append (map mapping->fs (cons %store-mapping mappings))
75 %container-file-systems
76 user-file-systems))))
77
78 (define* (container-script os #:key (mappings '()))
79 "Return a derivation of a script that runs OS as a Linux container.
80 MAPPINGS is a list of <file-system> objects that specify the files/directories
81 that will be shared with the host system."
82 (let* ((os (containerized-operating-system os mappings))
83 (file-systems (filter file-system-needed-for-boot?
84 (operating-system-file-systems os)))
85 (specs (map file-system->spec file-systems)))
86
87 (mlet* %store-monad ((os-drv (operating-system-derivation
88 os
89 #:container? #t)))
90
91 (define script
92 (with-imported-modules (source-module-closure
93 '((guix build utils)
94 (gnu build linux-container)))
95 #~(begin
96 (use-modules (gnu build linux-container)
97 (guix build utils))
98
99 (call-with-container '#$specs
100 (lambda ()
101 (setenv "HOME" "/root")
102 (setenv "TMPDIR" "/tmp")
103 (setenv "GUIX_NEW_SYSTEM" #$os-drv)
104 (for-each mkdir-p '("/run" "/bin" "/etc" "/home" "/var"))
105 (primitive-load (string-append #$os-drv "/boot")))
106 ;; A range of 65536 uid/gids is used to cover 16 bits worth of
107 ;; users and groups, which is sufficient for most cases.
108 ;;
109 ;; See: http://www.freedesktop.org/software/systemd/man/systemd-nspawn.html#--private-users=
110 #:host-uids 65536))))
111
112 (gexp->script "run-container" script))))