From 6e2981cf221ea89e39b3b2b1529eaa113cc368a9 Mon Sep 17 00:00:00 2001 From: Myned Date: Sun, 15 Sep 2024 18:25:10 -0500 Subject: [PATCH] nixos: refactor build scripts into monolithic wrapper Signed-off-by: Myned --- options/custom/scripts/default.nix | 36 ++++----- options/custom/scripts/flakegen.sh | 16 ---- options/custom/scripts/nixos.sh | 113 +++++++++++++++++++++++++++++ options/custom/scripts/rebuild.sh | 10 --- options/custom/scripts/repl.sh | 10 --- options/custom/scripts/target.sh | 10 --- options/custom/scripts/upgrade.sh | 11 --- 7 files changed, 127 insertions(+), 79 deletions(-) delete mode 100644 options/custom/scripts/flakegen.sh create mode 100644 options/custom/scripts/nixos.sh delete mode 100644 options/custom/scripts/rebuild.sh delete mode 100644 options/custom/scripts/repl.sh delete mode 100644 options/custom/scripts/target.sh delete mode 100644 options/custom/scripts/upgrade.sh diff --git a/options/custom/scripts/default.nix b/options/custom/scripts/default.nix index b81f5b7..adf2321 100644 --- a/options/custom/scripts/default.nix +++ b/options/custom/scripts/default.nix @@ -20,6 +20,10 @@ in { ".local/bin/${name}".source = pkgs.writeShellApplication { inherit name; + + # https://github.com/NixOS/nixpkgs/pull/261115 + #// excludeShellChecks = ["SC2154"]; # argc evaluates variables at runtime + runtimeInputs = dependencies; text = builtins.readFile ./${name}.sh; } @@ -82,11 +86,6 @@ in { fprintd libnotify ]) - (bash "flakegen" [ - git - libnotify - nix - ]) (bash "inhibit" [ coreutils libnotify @@ -111,18 +110,20 @@ in { libnotify networkmanager ]) + (bash "nixos" [ + argc + coreutils + git + nh + nix + nixos-rebuild + nvd + systemd + ]) (bash "power" [ libnotify power-profiles-daemon ]) - (bash "rebuild" [ - libnotify - nixos-rebuild - ]) - (bash "repl" [ - libnotify - nixos-rebuild - ]) (bash "screenshot" [ coreutils grimblast @@ -130,21 +131,12 @@ in { libnotify swappy ]) - (bash "target" [ - libnotify - nixos-rebuild - ]) (bash "toggle" [ gnugrep hyprland jq libnotify ]) - (bash "upgrade" [ - libnotify - nix - nixos-rebuild - ]) (bash "vm" [ coreutils freerdp3 diff --git a/options/custom/scripts/flakegen.sh b/options/custom/scripts/flakegen.sh deleted file mode 100644 index b11058e..0000000 --- a/options/custom/scripts/flakegen.sh +++ /dev/null @@ -1,16 +0,0 @@ -#! /usr/bin/env bash - -# Generate flake.nix via flakegen -# https://github.com/jorsn/flakegen - -cd /etc/nixos || exit 1 - -if [[ "${1-}" == '-r' ]]; then - # Nuke and reinitialize - rm flake.nix - nix flake init --template github:jorsn/flakegen -else - # Generate and track all files - nix run .#genflake flake.nix - git add . -fi diff --git a/options/custom/scripts/nixos.sh b/options/custom/scripts/nixos.sh new file mode 100644 index 0000000..7371940 --- /dev/null +++ b/options/custom/scripts/nixos.sh @@ -0,0 +1,113 @@ +#! /usr/bin/env bash + +cd /etc/nixos || exit 1 + +# @describe Wrapper for NixOS tools +# Assumes flakes and configuration in /etc/nixos/ +# +# https://github.com/sigoden/argc +# https://github.com/jorsn/flakegen +# https://github.com/viperML/nh + +# @meta combine-shorts +# @meta inherit-flag-options + +# @cmd Build NixOS configuration +# @alias b,bu,bui,buil +# @option -b --builder[=nh|nixos] Use nh os (default) or nixos-rebuild to build +# @option -t --target Remote machine to build with root, only nixos-rebuild is supported +# @flag -n --no-generate Do not regenerate flake.nix before building +# @flag -p --poweroff Gracefully poweroff system after a successful build +# @flag -r --reboot Gracefully reboot system after a successful build +# @flag -u --update Update flake.lock before building +# @arg extra~ Pass extra arguments to builder +build() { :; } + +# Internal wrapper for subcommands +_build() { + # Regenerate flake.nix and stage git files by default + if [[ ! "${argc_no_generate:-}" ]]; then + nix run .#genflake flake.nix + git add . + fi + + # Update flake.lock + if [[ "${argc_update:-}" ]]; then + nix flake update + fi + + # Build and send closures to remote machine + if [[ "${argc_target:-}" ]]; then + nixos-rebuild --flake ".#${argc_target}" --target-host "root@${argc_target}" "$1" ${argc_extra:+"${argc_extra[@]}"} + else + # Build current system + if [[ "${argc_builder:-}" == nh ]]; then + nh os "$1" ${argc_extra:+"${argc_extra[@]}"} + elif [[ "${argc_builder:-}" == nixos ]]; then + sudo nixos-rebuild "$1" ${argc_extra:+"${argc_extra[@]}"} + fi + fi + + # Invoke systemd to shutdown system + # Assumes errexit shell option is set + if [[ "${argc_poweroff:-}" ]]; then + sudo systemctl poweroff + elif [[ "${argc_reboot:-}" ]]; then + sudo systemctl reboot + fi +} + +# @cmd Build and boot NixOS configuration +# @alias b,bo,boo +build::boot() { _build boot; } + +# @cmd Build and switch NixOS configuration +# @alias s,sw,swi,swit,switc +build::switch() { _build switch; } + +# @cmd Build and test NixOS configuration +# @alias t,te,tes +build::test() { _build test; } + +# @cmd Compare NixOS system generations +# @alias d,di,dif +# @arg path1=/run/current-system Store path to compare with (current system by default) +# @arg path2=/nix/var/nix/profiles/system Store path to compare against (built system by default) +diff() { + nvd diff "${argc_path1:-}" "${argc_path2:-}" +} + +# @cmd Generate flake.nix from flake.in.nix with flakegen +# @alias g,ge,gen,gene,gener,genera,generat +# @flag -n --nuke Delete flake.nix and reinitialize +generate() { + if [[ "${argc_nuke:-}" ]]; then + rm --force flake.nix + nix flake init --template github:jorsn/flakegen + else + nix run .#genflake flake.nix + git add . + fi +} + +# @cmd List NixOS generations +# @alias l,li,lis +# @arg extra~ Pass extra arguments to nixos-rebuild +list() { + nixos-rebuild list-generations ${argc_extra:+"${argc_extra[@]}"} +} + +# @cmd Enter an interactive NixOS read-eval-print loop with the current configuration +# @alias r,re,rep +# @flag -n --no-generate Do not regenerate flake.nix before entering loop +# @arg extra~ Pass extra arguments to nixos-rebuild +repl() { + if [[ ! "${argc_no_generate:-}" ]]; then + nix run .#genflake flake.nix + git add . + fi + + nixos-rebuild repl ${argc_extra:+"${argc_extra[@]}"} +} + +eval "$(argc --argc-eval "$0" "$@")" diff --git a/options/custom/scripts/rebuild.sh b/options/custom/scripts/rebuild.sh deleted file mode 100644 index cbb1b28..0000000 --- a/options/custom/scripts/rebuild.sh +++ /dev/null @@ -1,10 +0,0 @@ -#! /usr/bin/env bash - -# Rebuild local flake configuration - -cd /etc/nixos || exit 1 - -nix run .#genflake flake.nix && - sleep 0.1 && - git add . && - sudo nixos-rebuild --show-trace "$@" diff --git a/options/custom/scripts/repl.sh b/options/custom/scripts/repl.sh deleted file mode 100644 index aff0f49..0000000 --- a/options/custom/scripts/repl.sh +++ /dev/null @@ -1,10 +0,0 @@ -#! /usr/bin/env bash - -# Nix repl with loaded configuration - -cd /etc/nixos || exit 1 - -nix run .#genflake flake.nix && - sleep 0.1 && - git add . && - nixos-rebuild repl "$@" diff --git a/options/custom/scripts/target.sh b/options/custom/scripts/target.sh deleted file mode 100644 index 0bc5285..0000000 --- a/options/custom/scripts/target.sh +++ /dev/null @@ -1,10 +0,0 @@ -#! /usr/bin/env bash - -# Rebuild and send closures to remote machine - -cd /etc/nixos || exit 1 - -nix run .#genflake flake.nix && - sleep 0.1 && - git add . && - nixos-rebuild --flake .#"$1" --target-host root@"$1" "$2" --show-trace "${@:3}" diff --git a/options/custom/scripts/upgrade.sh b/options/custom/scripts/upgrade.sh deleted file mode 100644 index b95ca5f..0000000 --- a/options/custom/scripts/upgrade.sh +++ /dev/null @@ -1,11 +0,0 @@ -#! /usr/bin/env bash - -# Update flake.lock and rebuild - -cd /etc/nixos || exit 1 - -nix run .#genflake flake.nix && - sleep 0.1 && - git add . && - sudo nix flake update && - sudo nixos-rebuild --show-trace "$@"