aboutsummaryrefslogtreecommitdiff
path: root/modules/scripts
diff options
context:
space:
mode:
authortdback <tyler@tdback.net>2024-12-26 20:21:47 -0500
committertdback <tyler@tdback.net>2024-12-26 20:21:47 -0500
commit397aee1840f8a55e53595ca0f55e1df4ad245bf5 (patch)
treea859de6aca2983b6dbedd84a7bd745ca08224504 /modules/scripts
parent274a1e84f12c39e2f0a67b1fc85511381c8b798d (diff)
modularize motd script
Diffstat (limited to 'modules/scripts')
-rw-r--r--modules/scripts/motd/default.nix155
1 files changed, 78 insertions, 77 deletions
diff --git a/modules/scripts/motd/default.nix b/modules/scripts/motd/default.nix
index d59f787..2b60ca3 100644
--- a/modules/scripts/motd/default.nix
+++ b/modules/scripts/motd/default.nix
@@ -1,96 +1,97 @@
{ config, lib, pkgs, ... }:
+with lib;
let
- motd =
- pkgs.writeShellScriptBin "motd" ''
- #!/usr/bin/env bash
- RED="\e[31m"
- GREEN="\e[32m"
- YELLOW="\e[33m"
- BOLD="\e[1m"
- ENDCOLOR="\e[0m"
+ cfg = config.programs.motd;
- case "$(date +'%H')" in
- [0-9]|1[0-1])
- TIME="morning"
- ;;
- 1[2-7])
- TIME="afternoon"
- ;;
- *)
- TIME="evening"
- ;;
- esac
+ motd = pkgs.writeShellScriptBin "motd" ''
+ #!/usr/bin/env bash
- UPTIME=$(cat /proc/uptime | cut -f1 -d.)
- UPDAYS=$((UPTIME/60/60/24))
- UPHOURS=$((UPTIME/60/60%24))
- UPMINS=$((UPTIME/60%60))
- UPSECS=$((UPTIME%60))
+ RED="\e[31m"
+ GREEN="\e[32m"
+ YELLOW="\e[33m"
+ BOLD="\e[1m"
+ ENDCOLOR="\e[0m"
- MEMORY=$(free -m | awk 'NR == 2 { printf "%s/%sMB (%.2f%%)\n", $3, $2, ($3 * 100) / $2 }')
+ case "$(date +'%H')" in
+ [0-9]|1[0-1])
+ TIME="morning"
+ ;;
+ 1[2-7])
+ TIME="afternoon"
+ ;;
+ *)
+ TIME="evening"
+ ;;
+ esac
- SERVICES=$(systemctl list-units | grep -P 'podman-|${lib.strings.concatStringsSep "|" config.motd.servicesToCheck}')
+ UPTIME=$(cat /proc/uptime | cut -f1 -d.)
+ UPDAYS=$((UPTIME/60/60/24))
+ UPHOURS=$((UPTIME/60/60%24))
+ UPMINS=$((UPTIME/60%60))
+ UPSECS=$((UPTIME%60))
- printf "\n"
- printf "''${BOLD}Good $TIME $(whoami), welcome to $(hostname)!$ENDCOLOR\n"
- printf "\n"
- ${lib.strings.concatStrings (lib.lists.forEach config.motd.networkInterfaces (x:
- "printf \"$BOLD * %-20s$ENDCOLOR %s\\n\" \"IPv4 ${x}\" \"$(ip -4 addr show ${x} | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}')\"\n"
- ))}
- printf "$BOLD * %-20s$ENDCOLOR %s\n" "Release" "$(awk -F= '/PRETTY_NAME/ { print $2 }' /etc/os-release | tr -d '"')"
- printf "$BOLD * %-20s$ENDCOLOR %s\n" "Kernel" "$(uname -rs)"
- printf "\n"
- printf "$BOLD * %-20s$ENDCOLOR %s\n" "CPU Usage" "$(awk '{ print $1 ", " $2 ", " $3 }' /proc/loadavg) (1, 5, 15 min)"
- printf "$BOLD * %-20s$ENDCOLOR %s\n" "Memory" "$MEMORY"
- printf "$BOLD * %-20s$ENDCOLOR %s\n" "System Uptime" "$UPDAYS days $UPHOURS hours $UPMINS minutes $UPSECS seconds"
- printf "\n"
+ MEMORY=$(free -m | awk 'NR == 2 { printf "%s/%sMB (%.2f%%)\n", $3, $2, ($3 * 100) / $2 }')
- [ -z "$SERVICES" ] && exit
+ SERVICES=$(systemctl list-units | grep -P 'podman-|${strings.concatStringsSep "|" cfg.servicesToCheck}')
- printf "''${BOLD}Service status:$ENDCOLOR\n"
- while IFS= read -r line; do
- if [[ ! $line =~ ".service" ]] || [[ $line =~ ".mount" ]]; then
- continue
- fi
- if echo "$line" | grep -q 'failed'; then
- name=$(echo "$line" | awk '{ print $1 }' | sed 's/podman-//g')
- printf "$RED• $ENDCOLOR%-50s $RED[failed]$ENDCOLOR\n" "$name"
- elif echo "$line" | grep -q 'running'; then
- name=$(echo "$line" | awk '{ print $1 }' | sed 's/podman-//g')
- printf "$GREEN• $ENDCOLOR%-50s $GREEN[active]$ENDCOLOR\n" "$name"
- elif echo "$line" | grep -q 'exited'; then
- name=$(echo "$line" | awk '{ print $1 }' | sed 's/podman-//g')
- printf "$YELLOW• $ENDCOLOR%-50s $YELLOW[exited]$ENDCOLOR\n" "$name"
- else
- echo "service status unknown"
- fi
- done <<< "$SERVICES"
- printf "\n"
- '';
+ printf "\n"
+ printf "''${BOLD}Good $TIME $(whoami), welcome to $(hostname)!$ENDCOLOR\n"
+ printf "\n"
+ ${strings.concatStrings (lists.forEach cfg.networkInterfaces (
+ int: "printf \"$BOLD * %-20s$ENDCOLOR %s\\n\" \"IPv4 ${int}\" \"$(ip -4 addr show ${int} | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}')\"\n"
+ ))}
+ printf "$BOLD * %-20s$ENDCOLOR %s\n" "Release" "$(awk -F= '/PRETTY_NAME/ { print $2 }' /etc/os-release | tr -d '"')"
+ printf "$BOLD * %-20s$ENDCOLOR %s\n" "Kernel" "$(uname -rs)"
+ printf "\n"
+ printf "$BOLD * %-20s$ENDCOLOR %s\n" "CPU Usage" "$(awk '{ print $1 ", " $2 ", " $3 }' /proc/loadavg) (1, 5, 15 min)"
+ printf "$BOLD * %-20s$ENDCOLOR %s\n" "Memory" "$MEMORY"
+ printf "$BOLD * %-20s$ENDCOLOR %s\n" "System Uptime" "$UPDAYS days $UPHOURS hours $UPMINS minutes $UPSECS seconds"
+ printf "\n"
+
+ [ -z "$SERVICES" ] && exit
+
+ printf "''${BOLD}Service status:$ENDCOLOR\n"
+ while IFS= read -r line; do
+ if [[ ! $line =~ ".service" ]] || [[ $line =~ ".mount" ]]; then
+ continue
+ fi
+ if echo "$line" | grep -q 'failed'; then
+ name=$(echo "$line" | awk '{ print $1 }' | sed 's/podman-//g')
+ printf "$RED• $ENDCOLOR%-50s $RED[failed]$ENDCOLOR\n" "$name"
+ elif echo "$line" | grep -q 'running'; then
+ name=$(echo "$line" | awk '{ print $1 }' | sed 's/podman-//g')
+ printf "$GREEN• $ENDCOLOR%-50s $GREEN[active]$ENDCOLOR\n" "$name"
+ elif echo "$line" | grep -q 'exited'; then
+ name=$(echo "$line" | awk '{ print $1 }' | sed 's/podman-//g')
+ printf "$YELLOW• $ENDCOLOR%-50s $YELLOW[exited]$ENDCOLOR\n" "$name"
+ else
+ echo "service status unknown"
+ fi
+ done <<< "$SERVICES"
+ printf "\n"
+ '';
in
{
- options.motd = {
- networkInterfaces = lib.mkOption {
- description = "Network interfaces to monitor.";
- type = lib.types.listOf lib.types.str;
- default = [ ];
- };
-
- servicesToCheck = lib.mkOption {
- description = "Services to validate alongside podman containers.";
- type = lib.types.listOf lib.types.str;
- default = [ ];
+ options = {
+ programs.motd = {
+ enable = mkEnableOption "motd";
+ networkInterfaces = mkOption {
+ default = [ ];
+ type = types.listOf types.str;
+ description = "Network interfaces to monitor.";
+ };
+ servicesToCheck = mkOption {
+ default = [ ];
+ type = types.listOf types.str;
+ description = "Services to validate alongside podman containers.";
+ };
};
};
- config = {
- environment.systemPackages = [
- motd
- ];
-
+ config = mkIf cfg.enable {
+ environment.systemPackages = [ motd ];
programs.bash.loginShellInit = ''
[ -z "$PS1" ] && return
-
if command -v motd &> /dev/null; then
motd
fi