From 56f2abc737aa58b92ba24e8c16f46650b5460903 Mon Sep 17 00:00:00 2001 From: Ryan Yin Date: Fri, 8 Dec 2023 14:22:33 +0800 Subject: [PATCH] fix: terminfo error on macOS --- Makefile | 3 ++- flake.nix | 7 ++++--- home/base/desktop/terminal/README.md | 18 +++++++++++++++++- modules/darwin/apps.nix | 9 +++++++-- modules/nixos/user-group.nix | 3 +++ 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 2d0f6c9e..3cbd10a1 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,8 @@ gc: ############################################################################ darwin-set-proxy: - sudo python3 scripts/darwin_set_proxy.py + echo "skip setting proxy, use global proxy instead" + # sudo python3 scripts/darwin_set_proxy.py ha: darwin-set-proxy nix build .#darwinConfigurations.harmonica.system diff --git a/flake.nix b/flake.nix index 909197b5..8701b034 100644 --- a/flake.nix +++ b/flake.nix @@ -416,12 +416,13 @@ # status: https://mirrors.ustc.edu.cn/status/ # "https://mirrors.ustc.edu.cn/nix-channels/store" - # my own cache server + # fallback to official cache server if the above mirrors are not available + "https://cache.nixos.org" + + # fallback to other non-official cache server "https://ryan4yin.cachix.org" "https://anyrun.cachix.org" "https://hyprland.cachix.org" - - "https://cache.nixos.org" ]; # nix community's cache server diff --git a/home/base/desktop/terminal/README.md b/home/base/desktop/terminal/README.md index d6423903..80600d27 100644 --- a/home/base/desktop/terminal/README.md +++ b/home/base/desktop/terminal/README.md @@ -5,7 +5,7 @@ 3. alacritty: Standby terminal. -## 'xterm-kitty': unknown terminal type when `ssh` into a remote host +## 'xterm-kitty': unknown terminal type when `ssh` into a remote host or `sudo xxx` > https://sw.kovidgoyal.net/kitty/faq/#i-get-errors-about-the-terminal-being-unknown-or-opening-the-terminal-failing-or-functional-keys-like-arrow-keys-don-t-work @@ -19,6 +19,22 @@ But when you `ssh` into a remote host, the remote host is very likely to not hav 'xterm-kitty': unknown terminal type ``` +Or when you `sudo xxx`, `sudo` won't preserve the `TERM` variable, it will be reset to root's default `TERM` value, which is `xterm` or `xterm-256color` in most linux distributions, so you will get this error: + +``` +'xterm-256color': unknown terminal type +``` + +or + +``` +Error opening terminal: xterm-kitty. +``` + +NixOS preserve the `TERMINFO` and `TERMINFO_DIRS` environment variables, for `root` and the `wheel` group: [nixpkgs/nixos/modules/config/terminfo.nix](https://github.com/NixOS/nixpkgs/blob/nixos-23.11/nixos/modules/config/terminfo.nix#L18) + +For nix-darwin, take a look at + ### Solutions Simplest solution, it will automatically copy over the terminfo files and also magically enable shell integration on the remote machine: diff --git a/modules/darwin/apps.nix b/modules/darwin/apps.nix index 39d26f95..0a63ec96 100644 --- a/modules/darwin/apps.nix +++ b/modules/darwin/apps.nix @@ -1,4 +1,4 @@ -{pkgs, ...}: { +{pkgs, config, ...}: { ########################################################################## # # Install all apps and packages here. @@ -23,7 +23,12 @@ gnugrep # replacee macos's grep gnutar # replacee macos's tar ]; - environment.variables.EDITOR = "nvim"; + environment.variables = { + # Fix https://github.com/LnL7/nix-darwin/wiki/Terminfo-issues + # TERMINFO_DIRS = map (path: path + "/share/terminfo") config.environment.profiles ++ [ "/usr/share/terminfo" ]; + + EDITOR = "nvim"; + }; # Create /etc/zshrc that loads the nix-darwin environment. # this is required if you want to use darwin's default shell - zsh diff --git a/modules/nixos/user-group.nix b/modules/nixos/user-group.nix index d7360c8e..4a777a5a 100644 --- a/modules/nixos/user-group.nix +++ b/modules/nixos/user-group.nix @@ -43,6 +43,9 @@ }; users.users.root.initialHashedPassword = "$7$CU..../....X6uvZYnFD.i1CqqFFNl4./$4vgqzIPyw5XBr0aCDFbY/UIRRJr7h5SMGoQ/ZvX3FP2"; + # fix for `sudo xxx` in kitty/wezterm and other modern terminal emulators + security.sudo.keepTerminfo = true; + # DO NOT promote the specified user to input password for `nix-store` and `nix-copy-closure` security.sudo.extraRules = [ {