nixos: zsh for every account, security summary on login, XMPP over IPv6
All checks were successful
nixos / nixos (push) Successful in 1m3s
All checks were successful
nixos / nixos (push) Successful in 1m3s
Every account now logs in with zsh, including ones added later, except deploy and git: the app CI and Forgejo run commands through their login shells. root gets the same zsh with a red prompt ending in #. Interactive SSH logins show, under fastfetch, the host's security state as the Grafana alerts see it, read from the local metrics files: anything unexpected (peers, devices, accounts, ports, containers), who is logged in, the last good backup per repository, the bucket replica, failed units and fail2ban bans. XMPP gets a Podman network of its own with IPv6. xmpp.asxp.io has an AAAA record, and Podman's default network forwards published ports over IPv4 only, where Docker on Debian did both: since the conversion, XMPP over IPv6 was refused. Tried on the rehearsal host with a published port on the new network, reached over IPv6 and IPv4. Assisted-by: Claude:opus-5-5
This commit is contained in:
parent
8c7e725463
commit
72a14a1b1b
4 changed files with 129 additions and 12 deletions
|
|
@ -28,7 +28,8 @@ in
|
||||||
isSystemUser = true;
|
isSystemUser = true;
|
||||||
group = "git";
|
group = "git";
|
||||||
home = cfg.stateDir;
|
home = cfg.stateDir;
|
||||||
useDefaultShell = true;
|
# Forgejo's SSH commands run through it; not the zsh default
|
||||||
|
shell = pkgs.bashInteractive;
|
||||||
};
|
};
|
||||||
users.groups.git = { };
|
users.groups.git = { };
|
||||||
# Anyone may try a key against git; Forgejo decides what it may do
|
# Anyone may try a key against git; Forgejo decides what it may do
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,28 @@ in
|
||||||
options.asxpio.publicIPv4 = lib.mkOption { type = lib.types.str; };
|
options.asxpio.publicIPv4 = lib.mkOption { type = lib.types.str; };
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
|
# A network of its own with IPv6: xmpp.asxp.io has an AAAA record,
|
||||||
|
# and Podman's default network forwards published ports over IPv4
|
||||||
|
# only (Docker on Debian did both).
|
||||||
|
systemd.services.podman-network-xmpp = {
|
||||||
|
description = "Podman network xmpp (IPv4 and IPv6)";
|
||||||
|
wantedBy = [ "multi-user.target" ];
|
||||||
|
before = [ "podman-xmpp-ejabberd.service" ];
|
||||||
|
path = [ config.virtualisation.podman.package ];
|
||||||
|
serviceConfig = {
|
||||||
|
Type = "oneshot";
|
||||||
|
RemainAfterExit = true;
|
||||||
|
};
|
||||||
|
script = ''
|
||||||
|
podman network exists xmpp \
|
||||||
|
|| podman network create --ipv6 --subnet 10.90.0.0/24 --subnet fd69:90::/64 xmpp
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
systemd.services.podman-xmpp-ejabberd = {
|
||||||
|
requires = [ "podman-network-xmpp.service" ];
|
||||||
|
after = [ "podman-network-xmpp.service" ];
|
||||||
|
};
|
||||||
|
|
||||||
virtualisation.oci-containers.containers = {
|
virtualisation.oci-containers.containers = {
|
||||||
xmpp-ejabberd = {
|
xmpp-ejabberd = {
|
||||||
image = ejabberd;
|
image = ejabberd;
|
||||||
|
|
@ -37,6 +59,7 @@ in
|
||||||
EJABBERD_MACRO_ACME_URL = "https://acme-v02.api.letsencrypt.org/directory";
|
EJABBERD_MACRO_ACME_URL = "https://acme-v02.api.letsencrypt.org/directory";
|
||||||
};
|
};
|
||||||
environmentFiles = [ "${state}/secrets.env" ];
|
environmentFiles = [ "${state}/secrets.env" ];
|
||||||
|
extraOptions = [ "--network=xmpp" ];
|
||||||
ports = [
|
ports = [
|
||||||
"5222:5222" # clients, STARTTLS
|
"5222:5222" # clients, STARTTLS
|
||||||
"5223:5223" # clients, direct TLS
|
"5223:5223" # clients, direct TLS
|
||||||
|
|
|
||||||
62
nixos/modules/login-summary.sh
Normal file
62
nixos/modules/login-summary.sh
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
# Printed on interactive SSH logins, under fastfetch: the host's security
|
||||||
|
# state as the Grafana alerts see it (from the node_exporter textfiles
|
||||||
|
# written by security-metrics, restic and s3-replicate), plus failed units.
|
||||||
|
# Reads local files only, so it is instant and works when ops is down.
|
||||||
|
|
||||||
|
tf=/var/lib/node_exporter/textfile
|
||||||
|
now=$(date +%s)
|
||||||
|
red=$'\e[1;31m' green=$'\e[1;32m' yellow=$'\e[1;33m' dim=$'\e[2m' off=$'\e[0m'
|
||||||
|
|
||||||
|
ago() { # seconds -> "3h 12m" / "2d 4h"
|
||||||
|
local s=$1
|
||||||
|
if [ "$s" -ge 86400 ]; then echo "$((s / 86400))d $((s % 86400 / 3600))h"
|
||||||
|
elif [ "$s" -ge 3600 ]; then echo "$((s / 3600))h $((s % 3600 / 60))m"
|
||||||
|
else echo "$((s / 60))m"; fi
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "${dim}── security ──────────────────────────────────────────${off}"
|
||||||
|
|
||||||
|
if [ -r "$tf/security.prom" ]; then
|
||||||
|
age=$((now - $(awk '/^security_audit_last_run_timestamp_seconds/ {print int($2)}' "$tf/security.prom")))
|
||||||
|
unknown=$(grep 'known="false"' "$tf/security.prom" \
|
||||||
|
| sed -E 's/^security_([a-z_]+)_info\{.*/\1/; s/^security_([a-z_]+)\{.*/\1/' \
|
||||||
|
| sort | uniq -c | awk '{printf "%s%s %s", sep, $1, $2; sep=", "}')
|
||||||
|
failed_checks=$(awk -F'"' '/^security_audit_check_ok/ && $NF ~ / 0$/ {printf "%s%s", sep, $2; sep=", "}' "$tf/security.prom")
|
||||||
|
if [ -n "$unknown" ]; then
|
||||||
|
echo " unexpected : ${red}${unknown}${off} ${dim}(see Grafana → Security)${off}"
|
||||||
|
else
|
||||||
|
echo " unexpected : ${green}nothing${off} ${dim}(peers, devices, accounts, ports, containers)${off}"
|
||||||
|
fi
|
||||||
|
[ -z "$failed_checks" ] || echo " checks : ${yellow}not run: ${failed_checks}${off}"
|
||||||
|
[ "$age" -lt 300 ] || echo " baseline : ${yellow}last written $(ago "$age") ago${off}"
|
||||||
|
sessions=$(awk -F'"' '/^security_sessions/ {printf "%s%s from %s", sep, $2, $4; sep=", "}' "$tf/security.prom")
|
||||||
|
echo " logged in : ${sessions:-nobody}"
|
||||||
|
else
|
||||||
|
echo " baseline : ${yellow}no data yet${off}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
backups=""
|
||||||
|
for f in "$tf"/restic_*.prom; do
|
||||||
|
[ -r "$f" ] || continue
|
||||||
|
repo=$(basename "$f" .prom); repo=${repo#restic_}
|
||||||
|
t=$(awk '/^restic_backup_last_success/ {print int($2)}' "$f")
|
||||||
|
a=$((now - t)); c=$green; [ "$a" -lt 93600 ] || c=$red
|
||||||
|
backups="$backups${backups:+, }$repo ${c}$(ago "$a") ago${off}"
|
||||||
|
done
|
||||||
|
echo " backups : ${backups:-${yellow}none recorded yet${off}}"
|
||||||
|
|
||||||
|
if [ -r "$tf/s3_replicate.prom" ]; then
|
||||||
|
a=$((now - $(awk '/^s3_replicate_last_success/ {print int($2)}' "$tf/s3_replicate.prom")))
|
||||||
|
c=$green; [ "$a" -lt 93600 ] || c=$red
|
||||||
|
echo " replica : ${c}$(ago "$a") ago${off}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
failed=$(systemctl --failed --no-legend --plain 2>/dev/null | awk '{printf "%s%s", sep, $1; sep=", "}')
|
||||||
|
if [ -n "$failed" ]; then echo " failed : ${red}${failed}${off}"
|
||||||
|
else echo " failed : ${green}no units${off}"; fi
|
||||||
|
|
||||||
|
if [ "$(id -u)" = 0 ] && command -v fail2ban-client >/dev/null; then
|
||||||
|
banned=$(fail2ban-client status sshd 2>/dev/null | awk -F: '/Currently banned/ {gsub(/[ \t]/, "", $2); print $2}')
|
||||||
|
echo " fail2ban : ${banned:-0} banned"
|
||||||
|
fi
|
||||||
|
echo
|
||||||
|
|
@ -1,10 +1,34 @@
|
||||||
# Interactive shell for the admins: zsh as on the admin's workstation
|
# Interactive shell: zsh as on the admin's workstation (asxpi/nixOS,
|
||||||
# (asxpi/nixOS, modules/shell.nix), minus what only makes sense there.
|
# modules/shell.nix), minus what only makes sense there.
|
||||||
#
|
#
|
||||||
# deploy keeps bash: the app CI runs its SSH scripts through deploy's
|
# Every account logs in with zsh, including ones added later, except
|
||||||
# login shell, and they are bash. root keeps bash too, for scripts and
|
# those that run other programs' commands through their login shell:
|
||||||
# `ssh root@host '<command>'`.
|
# deploy (the app CI's SSH scripts are bash) and git (Forgejo).
|
||||||
{ lib, pkgs, ... }:
|
# root gets the same zsh with a red prompt ending in #.
|
||||||
|
#
|
||||||
|
# Interactive SSH logins show fastfetch and a short security summary;
|
||||||
|
# scp, CI and `ssh host <command>` stay quiet.
|
||||||
|
{
|
||||||
|
config,
|
||||||
|
lib,
|
||||||
|
pkgs,
|
||||||
|
...
|
||||||
|
}:
|
||||||
|
let
|
||||||
|
summary = pkgs.writeShellApplication {
|
||||||
|
name = "login-summary";
|
||||||
|
# Read-only reporting: an empty grep or a missing file must not end it
|
||||||
|
bashOptions = [ "nounset" ];
|
||||||
|
runtimeInputs = with pkgs; [
|
||||||
|
coreutils
|
||||||
|
gawk
|
||||||
|
gnugrep
|
||||||
|
gnused
|
||||||
|
config.systemd.package
|
||||||
|
];
|
||||||
|
text = builtins.readFile ./login-summary.sh;
|
||||||
|
};
|
||||||
|
in
|
||||||
{
|
{
|
||||||
programs.zsh = {
|
programs.zsh = {
|
||||||
enable = true;
|
enable = true;
|
||||||
|
|
@ -20,9 +44,14 @@
|
||||||
grep = "grep --color=auto";
|
grep = "grep --color=auto";
|
||||||
};
|
};
|
||||||
|
|
||||||
# Blue: [HH:MM:SS] [user@host path] $
|
# Blue for everyone: [HH:MM:SS] [user@host path] $
|
||||||
|
# Red for root: [HH:MM:SS] [root@host path] #
|
||||||
promptInit = ''
|
promptInit = ''
|
||||||
PROMPT='%B%F{#0055ff}[%b%f%D{%H:%M:%S}%B%F{#0055ff}]%b%f %B%F{#0055ff}[%b%f%n@%m %B%F{#0055ff}%~]%b%f %B%F{#0055ff}$%b%f '
|
if [[ $EUID -eq 0 ]]; then
|
||||||
|
PROMPT='%B%F{#ff2020}[%b%f%D{%H:%M:%S}%B%F{#ff2020}]%b%f %B%F{#ff2020}[%b%F{#ff2020}%n%f@%m %B%F{#ff2020}%~]%b%f %B%F{#ff2020}#%b%f '
|
||||||
|
else
|
||||||
|
PROMPT='%B%F{#0055ff}[%b%f%D{%H:%M:%S}%B%F{#0055ff}]%b%f %B%F{#0055ff}[%b%f%n@%m %B%F{#0055ff}%~]%b%f %B%F{#0055ff}$%b%f '
|
||||||
|
fi
|
||||||
'';
|
'';
|
||||||
|
|
||||||
# Fresh homes have no ~/.zshrc: never offer the new-user wizard
|
# Fresh homes have no ~/.zshrc: never offer the new-user wizard
|
||||||
|
|
@ -52,19 +81,21 @@
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
users.users.asxpi.shell = pkgs.zsh;
|
users.defaultUserShell = pkgs.zsh;
|
||||||
|
users.users.root.shell = pkgs.zsh;
|
||||||
|
users.users.deploy.shell = pkgs.bashInteractive;
|
||||||
|
|
||||||
environment.systemPackages = with pkgs; [
|
environment.systemPackages = with pkgs; [
|
||||||
fastfetch
|
fastfetch
|
||||||
bat
|
bat
|
||||||
eza
|
eza
|
||||||
|
summary
|
||||||
];
|
];
|
||||||
|
|
||||||
# System summary on interactive SSH logins only: scp, CI and
|
|
||||||
# `ssh host <command>` stay quiet
|
|
||||||
environment.loginShellInit = ''
|
environment.loginShellInit = ''
|
||||||
if [ -n "''${SSH_TTY:-}" ] && [ -t 1 ]; then
|
if [ -n "''${SSH_TTY:-}" ] && [ -t 1 ]; then
|
||||||
${lib.getExe pkgs.fastfetch} 2>/dev/null
|
${lib.getExe pkgs.fastfetch} 2>/dev/null
|
||||||
|
${lib.getExe summary} 2>/dev/null
|
||||||
fi
|
fi
|
||||||
'';
|
'';
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue