{ config, lib, pkgs, ... }: let inherit (lib) mkIf mkOption types filterAttrs mapAttrsToList; cfg = config.homelab.motd; # collect services to display motd_list = mapAttrsToList (_: v: v.motd) (filterAttrs (_: v: v ? motd && v.motd != null) config.homelab); in { options.homelab.motd = { enable = mkOption { type = types.bool; default = true; description = "enable motd script"; }; }; config = mkIf cfg.enable { environment.etc."motd".text = '' #!/usr/bin/env bash active="\033[1;34m" inactive="\033[1;31m" headings="\033[0;36m" bold="\e[1m" reset="\033[0m" memory=`free -m | awk 'NR==2{printf "%s/%sMB (%.2f%%)\n", $3,$2,$3*100 / $2 }'` uptime=`cat /proc/uptime | cut -f1 -d.` up_days=$((uptime/60/60/24)) up_hours=$((uptime/60/60%24)) up_mins=$((uptime/60%60)) up_secs=$((uptime%60)) printf "$bold Welcome to $(hostname)!$reset\n" printf "\n" printf "$bold * %-20s$reset %s\n" "Release" "$PRETTY_NAME" printf "$bold * %-20s$reset %s\n" "Kernel" "$(uname -rs)" printf "\n" printf "$bold * %-20s$reset %s\n" "CPU usage" "$LOAD1, $LOAD5, $LOAD15 (1, 5, 15 min)" printf "$bold * %-20s$reset %s\n" "Memory" "$memory" printf "$bold * %-20s$reset %s\n" "System uptime" "$up_days days $up_hours hours $up_mins minutes $up_secs seconds" printf "\n" printf "$bold Service status$reset\n" # --- System Info --- default_iface=$(ip route | awk '/default/ {print $5; exit}') ip_addr=$(ip -4 addr show "''${default_iface}" | awk '/inet / {print $2}' | cut -d/ -f1) nixos_version=$(grep '^VERSION=' /etc/os-release | cut -d'"' -f2 | tr -d '\n') kernel_version=$(uname -r) # --- Resource Stats --- cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print 100 - $8 "%"}') mem_total=$(grep MemTotal /proc/meminfo | awk '{print $2}') mem_avail=$(grep MemAvailable /proc/meminfo | awk '{print $2}') mem_used=$((mem_total - mem_avail)) mem_percent=$((100 * mem_used / mem_total)) uptime_fmt=$(uptime -p 2>/dev/null || cat /proc/uptime | awk '{print int($1/3600)"h "int(($1%3600)/60)"m"}') echo -e "''${headings}Resources''${reset}" echo -e " CPU Usage: ''${cpu_usage}" echo -e " Memory: ''${mem_percent}% used" echo -e " Uptime: ''${uptime_fmt}" echo # --- services --- echo -e "''${headings}Homelab Services''${reset}" ${lib.concatStringsSep "\n" (map (service: '' if systemctl list-units --type=service --all | grep -q "${service}"; then status=$(systemctl is-active ${service} 2>/dev/null) if [ "''${status}" = "active" ]; then echo -e " ''${active}${service}''${reset} - running" else echo -e " ''${inactive}${service}''${reset} - not running" fi else echo -e " ''${inactive}${service}''${reset} - not found" fi '') motd_list)} echo ''; environment.etc."motd".mode = "0755"; programs.bash.interactiveShellInit = '' /etc/motd ''; }; }