From 0c8d9e30cb8fb9b42d1093540a2b73901c770fc5 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 21 Nov 2026 05:19:09 +0000 Subject: layers/console.nix: a shell on the serial console, to import into a layer Since the base system turned off the console (and with it kbd), it had no getty at all: NixOS's gettys come with the console (services/ttys/getty.nix), so there was no shell on ttyS0 any more, only the kernel's and systemd's messages, and ssh. That stays the default, and the base system's console.enable is only that now, a default, for layers/console.nix: the console, with root logged in on ttyS0 (a default, too: services.getty.autologinUser can name another user, or be null for a login prompt), but no getty on tty1, and no systemd-vconsole-setup, which moved there from the base system, like the autologin. It's small, so the README has it imported into another layer, rather than as a layer, with an image, of its own, which works, too. --- README.md | 21 ++++++++++++++++++--- layers/console.nix | 23 +++++++++++++++++++++++ pkgs/run-firecracker-vm/default.nix | 8 +++----- 3 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 layers/console.nix diff --git a/README.md b/README.md index 5b81928..874cc45 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ # firecracker NixOS guests in [Firecracker](https://firecracker-microvm.github.io/), -started from a NixOS host, dropping into a root shell on the serial -console. Boots in about a second and a quarter. Stable Nix, no flakes. +started from a NixOS host, reached by ssh, or with `layers/console.nix`, +dropping into a root shell on the serial console. Boots in about a +second and a quarter. Stable Nix, no flakes. The guest gets: @@ -75,7 +76,20 @@ nix-build pkgs/run-firecracker-vm ./result/bin/run-firecracker-vm ``` -Leave the guest with `reboot`. +The guest's console (the run script's terminal) shows the kernel's and +systemd's messages, but has no shell, unless the guest has +`layers/console.nix`: root logged in on the console (or another user, +with `services.getty.autologinUser`; `null` for a login prompt, which +needs a password). It's small, so rather than a layer, with an image, +of its own, import it into another layer: + +```nix +{ imports = [ /path/to/firecracker/layers/console.nix ]; } +``` + +(As a layer of its own, it works, too: `--arg layers '[ layers/console.nix ]'`.) + +Leave the guest with `reboot` (there, or via ssh, see below). The guest's store is read-only, and there's no nix, unless it has the nix layer, preferably first, so its image is the same for all guests @@ -225,6 +239,7 @@ For `nix-shell` and the like to download, allow `cache.nixos.org`. the Firecracker configuration, and the `run-firecracker-vm` script. - `pkgs/run-firecracker-vm/kernel.nix`: the cut-down kernel. - `layers/nix.nix`: nix, as a layer. +- `layers/console.nix`: a shell on the serial console, to import into a layer. - `modules/host/default.nix`: the host's side: network, DHCP/DNS, egress proxy, `fc-egress`. - `modules/host/egress.lua`: the egress policy, for the proxy (OpenResty). diff --git a/layers/console.nix b/layers/console.nix new file mode 100644 index 0000000..378e3fa --- /dev/null +++ b/layers/console.nix @@ -0,0 +1,23 @@ +# A shell on the serial console, i.e. in the run script's terminal: root, +# logged in, unless services.getty.autologinUser says otherwise (another +# user, or null for a login prompt, which needs a password). Small, so +# better imported by another layer than a layer, with an image, of its +# own: +# imports = [ /path/to/firecracker/layers/console.nix ]; +# though that works, too: +# --arg layers '[ layers/console.nix ./my-layer.nix ]' +# Without it, the guest has no console (no getty, no kbd), and the way in +# is ssh (see users.users..openssh.authorizedKeys in a layer). +{ lib, ... }: +{ + # NixOS's gettys come with the console (services/ttys/getty.nix), and + # with them kbd and systemd-vconsole-setup, which has nothing to do + # here, but would still get started at boot. + console.enable = true; + systemd.units."systemd-vconsole-setup.service".enable = lib.mkForce false; + + # Only on ttyS0 (console= on the kernel command line, which systemd's + # getty generator goes by), not on tty1. + services.getty.autologinUser = lib.mkDefault "root"; + systemd.targets.getty.wants = lib.mkForce [ ]; +} diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index a8b3a92..c8a77e5 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -134,8 +134,6 @@ let (pkgs.runCommand "mount.vsock9p" { } "install -D ${mountVsock9p} $out/bin/mount.vsock9p") ]; - services.getty.autologinUser = "root"; - # sshd, started on demand (a layer may turn that off), with the host # key the run script serves via 9P on vsock port 567 (--ssh-host-key; # not in the store), copied to /run/ssh before sshd (or its socket) @@ -167,7 +165,6 @@ let rmdir "$share" ''; }; - systemd.targets.getty.wants = lib.mkForce [ ]; # no getty on tty1 # A fixed machine-id, so systemd doesn't consider every boot the first. environment.etc.machine-id.text = builtins.hashString "md5" "firecracker" + "\n"; @@ -220,8 +217,9 @@ let # No screen, no X, and no hardware to tell apart, but virtio: # neither kbd and systemd-vconsole-setup, nor fontconfig, nor su's # xauth (shadow.nix sets it, not as a default), nor udev's hardware - # database. - console.enable = false; + # database. No console means no getty either, not even on ttyS0: + # the way in is ssh, or layers/console.nix. + console.enable = lib.mkDefault false; fonts.fontconfig.enable = false; security.pam.services.su.forwardXAuth = lib.mkForce false; environment.etc."udev/hwdb.bin".enable = false; -- cgit v1.3.1