testing new motd
This commit is contained in:
@@ -4,90 +4,79 @@
|
||||
pkgs,
|
||||
...
|
||||
}: 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
|
||||
motdServices = lib.pipe homelab [
|
||||
lib.attrNames
|
||||
(map (name: {
|
||||
inherit name;
|
||||
unit = homelab.${name}.motd or null;
|
||||
}))
|
||||
(lib.filter (s: s.unit != null))
|
||||
];
|
||||
# Collect services that have a motd value set
|
||||
motdServices =
|
||||
mapAttrsToList (name: value: value.motd)
|
||||
(filterAttrs (_: v: v ? motd && v.motd != null) config.homelab);
|
||||
in {
|
||||
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 {
|
||||
# write into /etc/profile.d so shells source it at login
|
||||
environment.etc."profile.d/homelab-motd.sh".text = ''
|
||||
config = mkIf cfg.enable {
|
||||
environment.etc."motd".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"
|
||||
BLUE="\033[1;34m"
|
||||
RED="\033[1;31m"
|
||||
GREEN="\033[1;32m"
|
||||
YELLOW="\033[1;33m"
|
||||
RESET="\033[0m"
|
||||
|
||||
# ────────────────────────────────────────────────────────────────────────
|
||||
# System info
|
||||
hostname=$(hostname)
|
||||
nixos_ver=$(nixos-version 2>/dev/null || echo "unknown")
|
||||
kernel_ver=$(uname -r)
|
||||
# --- 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)
|
||||
kernel_version=$(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}')"
|
||||
echo -e "''${GREEN}System Info''${RESET}"
|
||||
echo -e " Hostname: $(hostname)"
|
||||
echo -e " Interface: ''${default_iface}"
|
||||
echo -e " IPv4: ''${ip_addr}"
|
||||
echo -e " NixOS: ''${nixos_version}"
|
||||
echo -e " Kernel: ''${kernel_version}"
|
||||
echo
|
||||
|
||||
# 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) ))
|
||||
# --- 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"}')
|
||||
|
||||
# Memory usage
|
||||
read total used <<< "$(free -m | awk '/Mem:/ {print $2, $3}')"
|
||||
mem_pct=$((100 * used / total))
|
||||
echo -e "''${GREEN}Resources''${RESET}"
|
||||
echo -e " CPU Usage: ''${cpu_usage}"
|
||||
echo -e " Memory: ''${mem_percent}% used"
|
||||
echo -e " Uptime: ''${uptime_fmt}"
|
||||
echo
|
||||
|
||||
# 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)
|
||||
# --- Service Status ---
|
||||
echo -e "''${GREEN}Homelab Services''${RESET}"
|
||||
${lib.concatStringsSep "\n" (map (service: ''
|
||||
status=$(systemctl is-active ''${service} 2>/dev/null)
|
||||
if [ "''${status}" = "active" ]; then
|
||||
echo -e " ''${BLUE}''${service}''${RESET} - running"
|
||||
else
|
||||
echo -e " ''${RED}''${service}''${RESET} - not running"
|
||||
fi
|
||||
'')
|
||||
motdServices)}
|
||||
|
||||
# ────────────────────────────────────────────────────────────────────────
|
||||
# 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 ""
|
||||
echo
|
||||
'';
|
||||
|
||||
# make sure it is executable
|
||||
environment.etc."profile.d/homelab-motd.sh".mode = "0755";
|
||||
environment.etc."motd".mode = "0755";
|
||||
|
||||
programs.bash.interactiveShellInit = ''
|
||||
/etc/motd
|
||||
'';
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user