diff options
| -rw-r--r-- | README.md | 5 | ||||
| -rw-r--r-- | layers/console.nix | 42 |
2 files changed, 46 insertions, 1 deletions
@@ -92,6 +92,11 @@ of its own, import it into another layer: (As a layer of its own, it works, too: `--arg layers '[ layers/console.nix ]'`.) +A serial console doesn't get told when the terminal's size changes, so +the shell there asks the terminal before each prompt (`resize`, which +works in tmux, too). A tmux attached on the console keeps the size it +started with: detach and attach again after resizing. + 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 diff --git a/layers/console.nix b/layers/console.nix index 378e3fa..417b761 100644 --- a/layers/console.nix +++ b/layers/console.nix @@ -8,7 +8,32 @@ # --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.<name>.openssh.authorizedKeys in a layer). -{ lib, ... }: +{ lib, pkgs, ... }: +let + # A serial line, unlike a pty, doesn't get told when the terminal's size + # changes: the guest only knows the size the run script saw at boot + # (systemd.tty.rows/columns.ttyS0). So ask the terminal (ESC[6n, the + # cursor's position, after moving it as far as it goes, then back), and + # set what it answers. + resize = pkgs.writeShellScriptBin "resize" '' + # usage: resize — sets the terminal's size (stty rows and cols) to + # what the terminal says it is; for a serial console, whose size + # doesn't follow the terminal's. + set -eu + export PATH=${lib.makeBinPath [ pkgs.coreutils ]} + tty=$(tty) || { echo "resize: not a terminal" >&2; exit 1; } + old=$(stty -g) + trap 'stty "$old"' EXIT + stty raw -echo + printf '\0337\033[999;999H\033[6n\0338' > "$tty" + if ! IFS='[;' read -r -s -t 1 -d R _ rows cols || + ! test "''${rows:-x}" -gt 0 2>/dev/null || ! test "''${cols:-x}" -gt 0 2>/dev/null; then + echo "resize: no answer from the terminal" >&2 + exit 1 + fi + stty rows "$rows" cols "$cols" + ''; +in { # NixOS's gettys come with the console (services/ttys/getty.nix), and # with them kbd and systemd-vconsole-setup, which has nothing to do @@ -20,4 +45,19 @@ # getty generator goes by), not on tty1. services.getty.autologinUser = lib.mkDefault "root"; systemd.targets.getty.wants = lib.mkForce [ ]; + + # On the serial console, the size follows the terminal's before each + # prompt (so a tmux attached there gets the right one), until the + # terminal doesn't answer. A tmux attached there doesn't learn of later + # changes: detach (C-b d) and attach again, or run resize. + environment.systemPackages = [ resize ]; + programs.bash.interactiveShellInit = '' + case $(tty 2>/dev/null) in + /dev/ttyS*) + __fc_resize() { + test -n "''${__fc_noresize-}" || resize 2>/dev/null || __fc_noresize=1 + } + PROMPT_COMMAND=(__fc_resize "''${PROMPT_COMMAND[@]}") + esac + ''; } |
