testing
This commit is contained in:
@@ -15,32 +15,6 @@
|
||||
}))
|
||||
(lib.filter (s: s.unit != null))
|
||||
];
|
||||
|
||||
# Build script body
|
||||
motdScript = lib.concatStringsSep "\n" (
|
||||
[
|
||||
"#!/usr/bin/env bash"
|
||||
"blue='\\033[34m'"
|
||||
"red='\\033[31m'"
|
||||
"reset='\\033[0m'"
|
||||
""
|
||||
"echo \"Hostname: $(hostname)\""
|
||||
"echo \"Uptime: $(uptime -p)\""
|
||||
"echo \"Load: $(awk '{print $1, $2, $3}' /proc/loadavg)\""
|
||||
"echo"
|
||||
"echo \"Service status:\""
|
||||
]
|
||||
++ (map (s: ''
|
||||
status=$(systemctl is-active ${lib.escapeShellArg s.unit} 2>/dev/null)
|
||||
if [ "$status" = "active" ]; then
|
||||
printf "%b%-25s%b %s\\n" "$blue" "${s.name}" "$reset" "running"
|
||||
else
|
||||
printf "%b%-25s%b %s\\n" "$red" "${s.name}" "$reset" "FAILED ($status)"
|
||||
fi
|
||||
'')
|
||||
motdServices)
|
||||
++ [""]
|
||||
);
|
||||
in {
|
||||
options.homelab.motd = {
|
||||
enable = lib.mkEnableOption "enable custom homelab motd";
|
||||
@@ -48,7 +22,70 @@ in {
|
||||
|
||||
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 = motdScript;
|
||||
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";
|
||||
|
||||
Reference in New Issue
Block a user