aboutsummaryrefslogtreecommitdiffstats
path: root/layers/console.nix
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-21 15:45:58 +0000
committertv <tv@krebsco.de>2026-11-21 15:45:58 +0000
commit4148b4543c7d776d3428d01497cfc9de3ae7c9f7 (patch)
tree2a82a076cffdd3904ecd293d122ab788b2b2289c /layers/console.nix
parent4dd6ea85895430c1ef231c0f5ec1437bcb7ff0e9 (diff)
layers/console.nix: the console's size follows the terminal's
A serial line, unlike a pty, doesn't get told when the terminal's size changes, so the console kept the size the run script saw at boot (systemd.tty.rows/columns.ttyS0), e.g. 6 rows, from a tmux pane that small for a moment, and a tmux attached there drew for that, whatever the terminal's size. Now there's resize, which asks the terminal (ESC[6n, after moving the cursor as far as it goes, then back) and sets what it answers (stty rows, cols), and the shell on the serial console runs it before each prompt, until the terminal doesn't answer (within a second). A tmux attached there still keeps its size until attached again.
Diffstat (limited to 'layers/console.nix')
-rw-r--r--layers/console.nix42
1 files changed, 41 insertions, 1 deletions
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
+ '';
}