1
1
Fork 0

nixos: refactor build scripts into monolithic wrapper

Signed-off-by: Myned <dev@bjork.tech>
This commit is contained in:
Myned 2024-09-15 18:25:10 -05:00
parent 23f756a5bd
commit 6e2981cf22
Signed by: myned
GPG key ID: C7224454F7881A34
7 changed files with 127 additions and 79 deletions

View file

@ -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

View file

@ -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

View file

@ -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" "$@")"

View file

@ -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 "$@"

View file

@ -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 "$@"

View file

@ -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}"

View file

@ -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 "$@"