87 lines
2.6 KiB
Nix
87 lines
2.6 KiB
Nix
{
|
|
config,
|
|
lib,
|
|
pkgs,
|
|
...
|
|
}: let
|
|
inherit (lib) mkIf mkOption types filterAttrs mapAttrsToList;
|
|
cfg = config.homelab.motd;
|
|
|
|
# Collect all homelab services that define a motd string
|
|
motdServices =
|
|
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 the homelab MOTD.";
|
|
};
|
|
};
|
|
|
|
config = mkIf cfg.enable {
|
|
environment.etc."motd".text = ''
|
|
#!/usr/bin/env bash
|
|
|
|
BLUE="\033[1;34m"
|
|
RED="\033[1;31m"
|
|
YELLOW="\033[1;33m"
|
|
RESET="\033[0m"
|
|
|
|
# --- 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)
|
|
|
|
echo -e "''${YELLOW}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
|
|
|
|
# --- 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 "''${YELLOW}Resources''${RESET}"
|
|
echo -e " CPU Usage: ''${cpu_usage}"
|
|
echo -e " Memory: ''${mem_percent}% used"
|
|
echo -e " Uptime: ''${uptime_fmt}"
|
|
echo
|
|
|
|
# --- Homelab Services ---
|
|
echo -e "''${YELLOW}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 " ''${BLUE}''${service}''${RESET} - running"
|
|
else
|
|
echo -e " ''${RED}''${service}''${RESET} - not running"
|
|
fi
|
|
else
|
|
echo -e " ''${RED}''${service}''${RESET} - not found"
|
|
fi
|
|
'')
|
|
motdServices)}
|
|
|
|
echo
|
|
'';
|
|
|
|
environment.etc."motd".mode = "0755";
|
|
|
|
programs.bash.interactiveShellInit = ''
|
|
/etc/motd
|
|
'';
|
|
};
|
|
}
|