Files
nix/modules/homelab/system/motd/default.nix
T

119 lines
3.8 KiB
Nix

{
config,
lib,
pkgs,
...
}: let
inherit (lib) mkIf mkOption types filterAttrs mapAttrsToList;
cfg = config.homelab.motd;
homelab = lib.attrByPath ["homelab"] {} config;
gameservers = lib.attrByPath ["gameservers"] {} config;
# collect services to display
motd_list =
mapAttrsToList (_: v: v.motd)
(filterAttrs (
_: v:
(v.enable or false)
&& v ? motd
&& v.motd != null
)
homelab);
motd_gs_list = lib.concatLists (
mapAttrsToList (_: v: v.motd_gs)
(filterAttrs (
_: v:
(v.enable or false)
&& v ? motd_gs
&& v.motd_gs != null
)
gameservers)
);
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[1;35m'
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))
nixos_version=$(nixos-version | sed -E 's/^([0-9]{2}\.[0-9]{2}).*\(([^)]+)\)$/\1 (\2)/')
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"
echo
# --- 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
printf "%-32s%s\n" " ''${active}[${service}]''${reset}" "running"
else
printf "%-32s%s\n" " ''${active}[${service}]''${reset}" "not running"
fi
else
printf "%-32s%s\n" " ''${active}[${service}]''${reset}" "not found"
fi
'')
motd_list)}
echo
${lib.optionalString (motd_gs_list != []) ''
# --- gameservers ---
echo -e "''${headings}gameservers:''${reset}"
${lib.concatStringsSep "\n" (map (service: ''
status=$(systemctl is-active ${service} 2>/dev/null)
if [ "$status" = "active" ]; then
printf "%-32s%s\n" " ''${active}[${service}]''${reset}" "running"
else
printf "%-32s%s\n" " ''${active}[${service}]''${reset}" "not running"
fi
'')
motd_gs_list)}
''}
'';
environment.etc."motd".mode = "0755";
programs.zsh.interactiveShellInit = ''
/etc/motd
'';
};
}