testing new motd

This commit is contained in:
2025-10-14 21:27:07 -05:00
parent 0057e3f757
commit 86d3855734

View File

@@ -4,90 +4,79 @@
pkgs, pkgs,
... ...
}: let }: let
homelab = config.homelab or {}; inherit (lib) mkIf mkOption types filterAttrs mapAttrsToList;
cfg = config.homelab.motd;
# Build a list of { name, unit } for all non-null motd entries # Collect services that have a motd value set
motdServices = lib.pipe homelab [ motdServices =
lib.attrNames mapAttrsToList (name: value: value.motd)
(map (name: { (filterAttrs (_: v: v ? motd && v.motd != null) config.homelab);
inherit name;
unit = homelab.${name}.motd or null;
}))
(lib.filter (s: s.unit != null))
];
in { in {
options.homelab.motd = { options.homelab.motd = {
enable = lib.mkEnableOption "enable custom homelab motd"; enable = mkOption {
type = types.bool;
default = true;
description = "Enable the homelab MOTD.";
};
}; };
config = lib.mkIf config.homelab.motd.enable { config = mkIf cfg.enable {
# write into /etc/profile.d so shells source it at login environment.etc."motd".text = ''
environment.etc."profile.d/homelab-motd.sh".text = ''
#!/usr/bin/env bash #!/usr/bin/env bash
# Homelab MOTD pretty status display
# Generated by NixOS BLUE="\033[1;34m"
RED="\033[1;31m"
# GREEN="\033[1;32m"
# Colors YELLOW="\033[1;33m"
blue="\033[34m" RESET="\033[0m"
red="\033[31m"
cyan="\033[36m" # --- System Info ---
magenta="\033[35m" default_iface=$(ip route | awk '/default/ {print $5; exit}')
bold="\033[1m" ip_addr=$(ip -4 addr show "''${default_iface}" | awk '/inet / {print $2}' | cut -d/ -f1)
reset="\033[0m" nixos_version=$(grep VERSION= /etc/os-release | cut -d'"' -f2)
kernel_version=$(uname -r)
#
# System info echo -e "''${GREEN}System Info''${RESET}"
hostname=$(hostname) echo -e " Hostname: $(hostname)"
nixos_ver=$(nixos-version 2>/dev/null || echo "unknown") echo -e " Interface: ''${default_iface}"
kernel_ver=$(uname -r) echo -e " IPv4: ''${ip_addr}"
echo -e " NixOS: ''${nixos_version}"
read iface ipv4 <<< "$(ip route get 1.1.1.1 2>/dev/null | awk '/dev/ {for (i=1;i<=NF;i++) if ($i=="dev") iface=$(i+1); if ($1=="src") ip=$2;} END {print iface, ip}')" echo -e " Kernel: ''${kernel_version}"
echo
# CPU usage (avg over 1s)
read cpu_a idle_a < <(awk '/^cpu /{print $2+$4,$5}' /proc/stat) # --- Resource Stats ---
sleep 0.5 cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print 100 - $8 "%"}')
read cpu_b idle_b < <(awk '/^cpu /{print $2+$4,$5}' /proc/stat) mem_total=$(grep MemTotal /proc/meminfo | awk '{print $2}')
cpu_usage=$(( (100 * (cpu_b - cpu_a - (idle_b - idle_a))) / (cpu_b - cpu_a) )) mem_avail=$(grep MemAvailable /proc/meminfo | awk '{print $2}')
mem_used=$((mem_total - mem_avail))
# Memory usage mem_percent=$((100 * mem_used / mem_total))
read total used <<< "$(free -m | awk '/Mem:/ {print $2, $3}')" uptime_fmt=$(uptime -p 2>/dev/null || cat /proc/uptime | awk '{print int($1/3600)"h "int(($1%3600)/60)"m"}')
mem_pct=$((100 * used / total))
echo -e "''${GREEN}Resources''${RESET}"
# Uptime echo -e " CPU Usage: ''${cpu_usage}"
uptime_fmt=$(awk '{d=int($1/86400);h=int(($1%86400)/3600);m=int(($1%3600)/60);printf("%dd %dh %dm",d,h,m)}' /proc/uptime) echo -e " Memory: ''${mem_percent}% used"
echo -e " Uptime: ''${uptime_fmt}"
# echo
# Display system info
printf "\\n${bold}${cyan}System Info${reset}\\n" # --- Service Status ---
printf " %-15s %s\\n" "Hostname:" "$hostname" echo -e "''${GREEN}Homelab Services''${RESET}"
printf " %-15s %s\\n" "Interface:" "${iface:-N/A}" ${lib.concatStringsSep "\n" (map (service: ''
printf " %-15s %s\\n" "IPv4 Address:" "${ipv4:-N/A}" status=$(systemctl is-active ''${service} 2>/dev/null)
printf " %-15s %s\\n" "NixOS Version:" "$nixos_ver" if [ "''${status}" = "active" ]; then
printf " %-15s %s\\n" "Kernel:" "$kernel_ver" echo -e " ''${BLUE}''${service}''${RESET} - running"
else
printf "\\n${bold}${magenta}Performance${reset}\\n" echo -e " ''${RED}''${service}''${RESET} - not running"
printf " %-15s %s%%\\n" "CPU Usage:" "$cpu_usage" fi
printf " %-15s %s%% of %s MB\\n" "Memory Usage:" "$mem_pct" "$total" '')
printf " %-15s %s\\n" "Uptime:" "$uptime_fmt" motdServices)}
printf "\\n${bold}${blue}Service Status${reset}\\n" echo
#
# Loop through Nix-provided list of services
${lib.concatStringsSep "\n" (map (s: ''
status=$(systemctl is-active ${lib.escapeShellArg s.unit} 2>/dev/null)
if [ "$status" = "active" ]; then
printf " ${blue}%-25s${reset} %s\\n" "${s.name}" "running"
else
printf " ${red}%-25s${reset} %s\\n" "${s.name}" "FAILED ($status)"
fi
'') motdServices)}
echo ""
''; '';
# make sure it is executable environment.etc."motd".mode = "0755";
environment.etc."profile.d/homelab-motd.sh".mode = "0755";
programs.bash.interactiveShellInit = ''
/etc/motd
'';
}; };
} }