testing new motd
This commit is contained in:
@@ -4,90 +4,79 @@
|
|||||||
pkgs,
|
pkgs,
|
||||||
...
|
...
|
||||||
}: let
|
}: 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
|
# Collect services that have a motd value set
|
||||||
motdServices = lib.pipe homelab [
|
motdServices =
|
||||||
lib.attrNames
|
mapAttrsToList (name: value: value.motd)
|
||||||
(map (name: {
|
(filterAttrs (_: v: v ? motd && v.motd != null) config.homelab);
|
||||||
inherit name;
|
|
||||||
unit = homelab.${name}.motd or null;
|
|
||||||
}))
|
|
||||||
(lib.filter (s: s.unit != null))
|
|
||||||
];
|
|
||||||
in {
|
in {
|
||||||
options.homelab.motd = {
|
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 {
|
config = mkIf cfg.enable {
|
||||||
# write into /etc/profile.d so shells source it at login
|
environment.etc."motd".text = ''
|
||||||
environment.etc."profile.d/homelab-motd.sh".text = ''
|
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# Homelab MOTD — pretty status display
|
|
||||||
# Generated by NixOS
|
|
||||||
|
|
||||||
# ────────────────────────────────────────────────────────────────────────
|
BLUE="\033[1;34m"
|
||||||
# Colors
|
RED="\033[1;31m"
|
||||||
blue="\033[34m"
|
GREEN="\033[1;32m"
|
||||||
red="\033[31m"
|
YELLOW="\033[1;33m"
|
||||||
cyan="\033[36m"
|
RESET="\033[0m"
|
||||||
magenta="\033[35m"
|
|
||||||
bold="\033[1m"
|
|
||||||
reset="\033[0m"
|
|
||||||
|
|
||||||
# ────────────────────────────────────────────────────────────────────────
|
# --- System Info ---
|
||||||
# System info
|
default_iface=$(ip route | awk '/default/ {print $5; exit}')
|
||||||
hostname=$(hostname)
|
ip_addr=$(ip -4 addr show "''${default_iface}" | awk '/inet / {print $2}' | cut -d/ -f1)
|
||||||
nixos_ver=$(nixos-version 2>/dev/null || echo "unknown")
|
nixos_version=$(grep VERSION= /etc/os-release | cut -d'"' -f2)
|
||||||
kernel_ver=$(uname -r)
|
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)
|
# --- Resource Stats ---
|
||||||
read cpu_a idle_a < <(awk '/^cpu /{print $2+$4,$5}' /proc/stat)
|
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print 100 - $8 "%"}')
|
||||||
sleep 0.5
|
mem_total=$(grep MemTotal /proc/meminfo | awk '{print $2}')
|
||||||
read cpu_b idle_b < <(awk '/^cpu /{print $2+$4,$5}' /proc/stat)
|
mem_avail=$(grep MemAvailable /proc/meminfo | awk '{print $2}')
|
||||||
cpu_usage=$(( (100 * (cpu_b - cpu_a - (idle_b - idle_a))) / (cpu_b - cpu_a) ))
|
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
|
echo -e "''${GREEN}Resources''${RESET}"
|
||||||
read total used <<< "$(free -m | awk '/Mem:/ {print $2, $3}')"
|
echo -e " CPU Usage: ''${cpu_usage}"
|
||||||
mem_pct=$((100 * used / total))
|
echo -e " Memory: ''${mem_percent}% used"
|
||||||
|
echo -e " Uptime: ''${uptime_fmt}"
|
||||||
|
echo
|
||||||
|
|
||||||
# Uptime
|
# --- Service Status ---
|
||||||
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)
|
echo -e "''${GREEN}Homelab Services''${RESET}"
|
||||||
|
${lib.concatStringsSep "\n" (map (service: ''
|
||||||
# ────────────────────────────────────────────────────────────────────────
|
status=$(systemctl is-active ''${service} 2>/dev/null)
|
||||||
# Display system info
|
if [ "''${status}" = "active" ]; then
|
||||||
printf "\\n${bold}${cyan}System Info${reset}\\n"
|
echo -e " ''${BLUE}''${service}''${RESET} - running"
|
||||||
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
|
else
|
||||||
printf " ${red}%-25s${reset} %s\\n" "${s.name}" "FAILED ($status)"
|
echo -e " ''${RED}''${service}''${RESET} - not running"
|
||||||
fi
|
fi
|
||||||
'') motdServices)}
|
'')
|
||||||
|
motdServices)}
|
||||||
|
|
||||||
echo ""
|
echo
|
||||||
'';
|
'';
|
||||||
|
|
||||||
# make sure it is executable
|
environment.etc."motd".mode = "0755";
|
||||||
environment.etc."profile.d/homelab-motd.sh".mode = "0755";
|
|
||||||
|
programs.bash.interactiveShellInit = ''
|
||||||
|
/etc/motd
|
||||||
|
'';
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user