Files
nix/modules/homelab/motd/default.nix
2025-10-14 21:06:17 -05:00

57 lines
1.4 KiB
Nix

{
config,
lib,
pkgs,
...
}: let
homelab = config.homelab or {};
# 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))
];
# 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";
};
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;
# make sure it is executable
environment.etc."profile.d/homelab-motd.sh".mode = "0755";
};
}