Files
nix/modules/homelab/motd/default.nix
2025-10-14 23:16:08 -05:00

98 lines
3.1 KiB
Nix

{
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 }'`
load1=`cat /proc/loadavg | awk {'print $1'}`
load5=`cat /proc/loadavg | awk {'print $2'}`
load15=`cat /proc/loadavg | awk {'print $3'}`
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))
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"}')
printf "$bold Welcome to $(hostname)!$reset\n"
printf "\n"
printf "$bold * %-20s$reset %s\n" "Release" "$nixos_version"
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"
# --- 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
echo
# --- gameservers ---
echo -e "''${headings}gameservers''${reset}"
for service in velocity smp superflat bento; do
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
echo
'';
environment.etc."motd".mode = "0755";
programs.bash.interactiveShellInit = ''
/etc/motd
'';
};
}