about summary refs log tree commit diff
path: root/services/hello-container.scm
blob: 216aff1ef562eb75e561a3ac2896e2e6486935a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
;; Bare-bones Guix system container running 'hello' with an SSH
;; server, so it can be inspected over ssh (nsenter is not always an
;; option).  Build with --network so sshd is reachable on the host:
;;
;;   guix system container --network hello-container.scm
;;
;; then start it via the run-guix-system-container daemon and connect:
;;
;;   ssh -p 2222 -i ../etc/hello-container-key root@127.0.0.1

(use-modules (gnu)
             (gnu packages base)
             (gnu services base)
             (gnu services shepherd)
             (gnu services sysctl)
             (srfi srfi-1))
(use-package-modules base)
(use-service-modules ssh)

;; Pre-generated SSH host key (etc/ in this repository),
;; installed at activation and pinned via (generate-host-keys? #f), so
;; every container build presents the SAME host key and known_hosts
;; does not complain about a changed identification.
(define ssh-host-key (local-file "../etc/ssh-container-key"
                                 "ssh_host_ecdsa_key"))
(define ssh-host-key.pub (local-file "../etc/ssh-container-key.pub"
                                     "ssh_host_ecdsa_key.pub"))

(operating-system
  (host-name "hello-container")
  (timezone "Europe/Amsterdam")
  (locale "en_US.utf8")
  (bootloader (bootloader-configuration
               (bootloader grub-bootloader)
               (targets '("/dev/sdX"))))
  (file-systems (cons (file-system
                        (device (file-system-label "does-not-matter"))
                        (mount-point "/")
                        (type "ext4"))
                      %base-file-systems))

  ;; Users are mandatory in an OS declaration.  Give root a password
  ;; so we can also fall back to password login ("hello") when the
  ;; key does not work.
  (users (cons (user-account
                (name "root")
                (uid 0)
                (group "root")
                (password (crypt "hello" "$6$abc$0123456789abcdef$")))
               %base-user-accounts))

  ;; 'hello' ends up in /run/current-system/profile/bin (bash comes
  ;; with %base-packages).
  (packages (cons hello %base-packages))

  (services
   (append
    (list
     ;; Password-less root login with etc/hello-container-key.  Host
     ;; keys are pinned (see ssh-host-key above), not generated at boot.
     (service openssh-service-type
              (openssh-configuration
               (port-number 2222)
               (permit-root-login #t)
               (password-authentication? #f)
               (generate-host-keys? #f)
               (authorized-keys
                `(("root" ,(local-file "../etc/hello-container-key.pub"
                                       "authorized_keys"))))))

     ;; Install the pinned ECDSA host key into /etc/ssh where sshd
     ;; looks for it by default.
     (simple-service 'container-ssh-host-key
                     activation-service-type
                     (with-imported-modules '((guix build utils))
                       #~(begin
                           (use-modules (guix build utils))
                           (mkdir-p "/etc/ssh")
                           (copy-file #$ssh-host-key
                                      "/etc/ssh/ssh_host_ecdsa_key")
                           (chmod "/etc/ssh/ssh_host_ecdsa_key" #o600)
                           (copy-file #$ssh-host-key.pub
                                      "/etc/ssh/ssh_host_ecdsa_key.pub")
                           (chmod "/etc/ssh/ssh_host_ecdsa_key.pub" #o644))))

     ;; sshd needs the privilege separation directory /var/empty; on
     ;; a full system it is created as the home directory of some
     ;; system account, but a minimal container has none.
     (simple-service 'container-var-empty
                     activation-service-type
                     (with-imported-modules '((guix build utils))
                       #~(begin
                           (use-modules (guix build utils))
                           (mkdir-p "/var/empty"))))

     ;; In a container sysctl (writes /proc/sys) and udev (coldplug on
     ;; the static /dev the container runtime sets up) are useless and
     ;; their start-up hangs the whole boot: urandom-seed requires
     ;; udev, user-processes requires urandom-seed, and ssh-daemon
     ;; requires user-processes.  Provide no-ops instead.
     (simple-service 'container-sysctl
                     shepherd-root-service-type
                     (list (shepherd-service
                            (provision '(sysctl))
                            (one-shot? #t)
                            (start #~(const #t)))))
     (simple-service 'container-udev
                     shepherd-root-service-type
                     (list (shepherd-service
                            (provision '(udev))
                            (one-shot? #t)
                            (start #~(const #t))))))
    (modify-services %base-services
      ;; Drop the guix service: its activation runs 'guix archive
      ;; --generate-key' at every boot, which stalled the container
      ;; boot; a minimal container does not need guix-daemon anyway.
      (delete guix-service-type)
      (delete sysctl-service-type)
      (delete udev-service-type)))))