{ config, lib, pkgs, ... }: let homelab = config.homelab or {}; # Build a list of { name, unit } for all non-null motd entries motdServices = lib.pipe homelab [ lib.attrNames (map (name: { inherit name; unit = homelab.${name}.motd or null; })) (lib.filter (s: s.unit != null)) ]; in { options.homelab.motd = { enable = lib.mkEnableOption "enable custom homelab motd"; }; config = lib.mkIf config.homelab.motd.enable { # write into /etc/profile.d so shells source it at login environment.etc."profile.d/homelab-motd.sh".text = '' #!/usr/bin/env bash # Homelab MOTD — pretty status display # Generated by NixOS # ──────────────────────────────────────────────────────────────────────── # Colors blue="\033[34m" red="\033[31m" cyan="\033[36m" magenta="\033[35m" bold="\033[1m" reset="\033[0m" # ──────────────────────────────────────────────────────────────────────── # System info hostname=$(hostname) nixos_ver=$(nixos-version 2>/dev/null || echo "unknown") kernel_ver=$(uname -r) 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}')" # CPU usage (avg over 1s) read cpu_a idle_a < <(awk '/^cpu /{print $2+$4,$5}' /proc/stat) sleep 0.5 read cpu_b idle_b < <(awk '/^cpu /{print $2+$4,$5}' /proc/stat) cpu_usage=$(( (100 * (cpu_b - cpu_a - (idle_b - idle_a))) / (cpu_b - cpu_a) )) # Memory usage read total used <<< "$(free -m | awk '/Mem:/ {print $2, $3}')" mem_pct=$((100 * used / total)) # Uptime 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) # ──────────────────────────────────────────────────────────────────────── # Display system info printf "\\n${bold}${cyan}System Info${reset}\\n" printf " %-15s %s\\n" "Hostname:" "$hostname" printf " %-15s %s\\n" "Interface:" "${iface:-N/A}" printf " %-15s %s\\n" "IPv4 Address:" "${ipv4:-N/A}" printf " %-15s %s\\n" "NixOS Version:" "$nixos_ver" printf " %-15s %s\\n" "Kernel:" "$kernel_ver" printf "\\n${bold}${magenta}Performance${reset}\\n" printf " %-15s %s%%\\n" "CPU Usage:" "$cpu_usage" printf " %-15s %s%% of %s MB\\n" "Memory Usage:" "$mem_pct" "$total" printf " %-15s %s\\n" "Uptime:" "$uptime_fmt" printf "\\n${bold}${blue}Service Status${reset}\\n" # ──────────────────────────────────────────────────────────────────────── # 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."profile.d/homelab-motd.sh".mode = "0755"; }; }