;; 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)))))