diff --git a/.gitignore b/.gitignore old mode 100755 new mode 100644 diff --git a/hosts/lolcathost/default.nix b/hosts/lolcathost/default.nix index a90cfbd..599e295 100755 --- a/hosts/lolcathost/default.nix +++ b/hosts/lolcathost/default.nix @@ -6,7 +6,7 @@ }: let home-manager = builtins.fetchTarball { url = "https://github.com/nix-community/home-manager/archive/release-25.05.tar.gz"; - sha256 = "0z94i2ig7wcm63fp1wkpp6r4458g2bj3r7ijlfapxihqybpgvng5"; + sha256 = "1kk5qzfb87mkgy6vzm7x8z8akxr3k8k7839yjdy48z034pvidhsr"; }; in { imports = [ diff --git a/hosts/myputer/default.nix b/hosts/myputer/default.nix index 1143a0d..bd85eb5 100755 --- a/hosts/myputer/default.nix +++ b/hosts/myputer/default.nix @@ -7,7 +7,7 @@ }: let home-manager = builtins.fetchTarball { url = "https://github.com/nix-community/home-manager/archive/release-24.11.tar.gz"; - sha256 = "0z94i2ig7wcm63fp1wkpp6r4458g2bj3r7ijlfapxihqybpgvng5"; + sha256 = "1kk5qzfb87mkgy6vzm7x8z8akxr3k8k7839yjdy48z034pvidhsr"; }; in { imports = [ diff --git a/scripts/nix-list-installed b/scripts/nix-list-installed new file mode 100755 index 0000000..5a253b9 --- /dev/null +++ b/scripts/nix-list-installed @@ -0,0 +1,3 @@ +#!/usr/bin/env sh +nix-store -q --references /var/run/current-system/sw \ + | cut -d'-' -f2- diff --git a/scripts/openport b/scripts/openport new file mode 100755 index 0000000..dd6222f --- /dev/null +++ b/scripts/openport @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +# Credit: u/boxofrox https://discourse.nixos.org/u/boxofrox +# Ref: https://discourse.nixos.org/t/how-to-temporarily-open-a-tcp-port-in-nixos/12306/3 +USAGE="[Usage] sudo withport " + +set -ueo pipefail + +open-port() { + local port=$1 + iptables -A INPUT -p tcp --dport $port -j ACCEPT +} + +close-port() { + local port=${1:-0} + iptables -D INPUT -p tcp --dport $port -j ACCEPT +} + +PORT=$1 +if [[ -z "$PORT" ]]; then + echo -e "[!] Port not given\n$USAGE" >&2 + exit 1 +fi +shift; # Drop port argument + +if [[ 0 -eq $# ]]; then + echo -e "[!] Command not given\n$USAGE" >&2 + exit 1 +fi + +open-port $PORT +# Ensure port closes if error occurs. +trap "close-port $PORT" EXIT +# Run the command as user, not root. +runuser -u $SUDO_USER -- "$@" +# Trap will close port. diff --git a/scripts/testvps b/scripts/testvps new file mode 100755 index 0000000..4382b7d --- /dev/null +++ b/scripts/testvps @@ -0,0 +1,82 @@ +#!/usr/bin/env bash +# Requires: systemctl + +set -u + +function match { + [[ "$1" =~ $2 ]] +} + +function padlen { + local MAX=0 + for KEY in "$@"; do + local LEN=${#KEY} + ((LEN > MAX)) && MAX=$LEN + done + echo $MAX +} + +function pad { + local PAD="$1" + if [ -z "$PAD" ]; then + exit 1 + fi + + for ((i=0; i/dev/null) + if [ -z "$STAT" ]; then + fmt_print "$SERVICE" "$LPAD" "NOTFOUND" + exit 1 + fi + + local STAT_LOADED=$(head -n2 <<< "$STAT" | tail -n1) + # NOTE: "active " intentionally contains right padding + local ENABLED=$(match "$STAT_LOADED" "^\s*Loaded: loaded \(.*; enabled; .*\)" && echo "enabled" || echo "disabled") + local ACTIVE=$(match "$STAT" "\s*Active: active \(running\)" && echo "active " || echo "inactive") + fmt_print "$SERVICE" "$LPAD" "$ACTIVE" "$ENABLED" +) + +function test_services { + local PAD_ALIGN=$(padlen $@) + # fmt_test_service + for SERVICE in "$@"; do + local LPAD=$((PAD_ALIGN - ${#SERVICE})) + fmt_test_service "$SERVICE" $LPAD + done +} + +test_services nginx forgejo vaultwarden