oizys/styx

136 lines
2.4 KiB
Plaintext
Raw Normal View History

2024-01-23 11:51:13 -06:00
#!/usr/bin/env bash
set -e
2024-01-24 15:01:47 -06:00
TERM=${TERM:-dumb}
2024-01-24 13:33:48 -06:00
HOSTNAME=${HOSTNAME:-$(hostname)}
2024-01-24 14:12:52 -06:00
FLAKE_PATH=${FLAKE_PATH:-$HOME/nixcfg}
2024-01-23 11:51:13 -06:00
DIM="$(tput dim)"
BOLD="$(tput bold)"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
CYAN="$(tput setaf 4)"
RESET="$(tput sgr0)"
PREFIX="${CYAN}styx${RESET}"
log() {
printf "%s | %s\n" "$PREFIX" "$*"
}
error() {
printf "%s | %s | %s\n" "$PREFIX" "${RED}error${RESET}" "$*"
}
help() {
cat <<EOF
styx <cmd> [-h]
${DIM}sister moon to nix on pluto
sister software to nix in this repo${RESET}
${BOLD}commands${RESET}:
EOF
printf "${GREEN}%8s${RESET} | ${YELLOW}%s${RESET}\n" \
fmt "format *.nix" \
build "build and monitor with nom" \
boot "evaluate flake for next boot" \
switch "perform nixos rebuild" \
2024-01-24 14:12:52 -06:00
store "run some store cleanup" \
cache "nix build and push to daylin.cachix.org"
2024-01-23 11:51:13 -06:00
exit
}
fmt() {
alejandra . "$@"
}
boot() {
sudo nixos-rebuild boot --flake "$FLAKE_PATH" "$@"
}
switch() {
sudo nixos-rebuild switch --flake "$FLAKE_PATH" "$@"
}
store() {
nix store optimise "$@"
}
build() {
2024-01-25 10:47:36 -06:00
nom build "$FLAKE_PATH#nixosConfigurations.${HOSTNAME}.config.system.build.toplevel"
2024-01-23 11:51:13 -06:00
case "$1" in
switch | boot | test ) sudo ./result/bin/switch-to-configuration "$1";;
esac
}
2024-01-23 15:36:31 -06:00
dry() {
# poor mans nix flake check
2024-01-25 10:47:36 -06:00
nix build "$FLAKE_PATH#nixosConfigurations.${HOSTNAME}.config.system.build.toplevel" --dry-run
2024-01-23 15:36:31 -06:00
}
2024-01-24 17:37:15 -06:00
2024-01-24 13:33:48 -06:00
cache() {
2024-01-24 17:37:15 -06:00
start=$(date +%s)
2024-01-24 13:33:48 -06:00
cachix watch-exec daylin \
-- \
nix build "$FLAKE_PATH#nixosConfigurations.${HOSTNAME}.config.system.build.toplevel" \
--print-build-logs
2024-01-24 17:37:15 -06:00
end=$(date +%s)
runtime=$(date -d@$((end-start)) +'%M minutes, %S seconds')
2024-01-25 10:47:36 -06:00
echo "Built host: ${HOSTNAME} in ${runtime} seconds" >> "$GITHUB_STEP_SUMMARY"
2024-01-24 13:33:48 -06:00
}
2024-01-23 11:51:13 -06:00
if [[ $# -eq 0 ]]; then
log no command specified see below for help
help
fi
while [[ $# -gt 0 ]]; do
case $1 in
2024-01-24 13:33:48 -06:00
fmt | boot | switch | store | build | dry | cache)
2024-01-23 11:51:13 -06:00
cmd=$1
shift
;;
2024-01-24 13:33:48 -06:00
-f | --flake)
FLAKE_PATH="$2"
shift; shift;
2024-01-23 11:51:13 -06:00
;;
2024-01-24 13:33:48 -06:00
-h | --host)
2024-01-25 11:02:00 -06:00
shift
HOSTNAME="$1"
2024-01-25 10:53:10 -06:00
shift;
2024-01-24 13:33:48 -06:00
;;
--help)
help
2024-01-24 14:12:52 -06:00
;;
--)
shift
break
2024-01-23 11:51:13 -06:00
;;
-*,--*)
error "unknown flag: ${BOLD}$1${RESET}"
exit 1
;;
*)
error "unknown command: ${BOLD}$1${RESET}"
exit 1
;;
esac
done
if [[ $# -gt 0 ]]; then
echo "forwarding args: ${BOLD}$*${RESET}"
fi
2024-01-25 10:53:10 -06:00
if [[ -z ${cmd+x} ]]; then
error "please specify a command"
help
fi
2024-01-24 13:33:48 -06:00
2024-01-23 11:51:13 -06:00
$cmd "$@"