summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMel <mel@rnrd.eu>2026-01-16 15:03:49 +0100
committerMel <mel@rnrd.eu>2026-01-16 15:03:49 +0100
commit8e6c980c9548ec0d5e7c200a760121a824b6d47d (patch)
tree26c159459a5f6d041eff25096a92262bb757089c
parentb637a518646ae2147403f01bd9ee250381bd8de0 (diff)
downloadnetwork-8e6c980c9548ec0d5e7c200a760121a824b6d47d.tar.zst
network-8e6c980c9548ec0d5e7c200a760121a824b6d47d.zip
Nix/NixOS shortcut script `nx`
Signed-off-by: Mel <mel@rnrd.eu>
-rw-r--r--modules/home/common.nix8
-rwxr-xr-xscripts/nx.sh148
2 files changed, 156 insertions, 0 deletions
diff --git a/modules/home/common.nix b/modules/home/common.nix
index e2f5c94..4eb0d56 100644
--- a/modules/home/common.nix
+++ b/modules/home/common.nix
@@ -1,5 +1,9 @@
{ pkgs, ... }:
+let
+ # a small useful script with common nix+nixos commands, invoked with `nx`
+ nx-script = pkgs.writeShellScriptBin "nx" (builtins.readFile ../../scripts/nx.sh);
+in
{
programs = {
git = {
@@ -38,4 +42,8 @@
zoxide = { enable = true; enableFishIntegration = true; };
};
+
+ home.packages = [
+ nx-script
+ ];
}
diff --git a/scripts/nx.sh b/scripts/nx.sh
new file mode 100755
index 0000000..245531c
--- /dev/null
+++ b/scripts/nx.sh
@@ -0,0 +1,148 @@
+#!/usr/bin/env bash
+
+# a small useful script for quickly accessing different nix and nixos commands
+# through a single nice interface.
+# run `nx help` for command list!
+
+set -euo pipefail
+
+config="/etc/configuration"
+current_hostname="$(hostname)"
+
+run() {
+ echo -ne "* \033[1;33m"
+ echo "$@"
+ echo -ne "\033[0m"
+ $@
+}
+
+error() {
+ echo -ne "\033[1;31m"
+ echo -n "error: "
+ echo -ne "\033[0m"
+ echo "$@"
+}
+
+rebuild() {
+ local target="$1"
+ local tool="$2"
+
+ run nixos-rebuild --sudo --builders "\"\"" --flake "$config#$target" "$tool"
+}
+
+flag() {
+ local f="$1"
+
+ case "$f" in
+ -h | --here)
+ config="."
+ ;;
+ *)
+ error "unknown flag '$f'"
+ exit 1
+ ;;
+ esac
+}
+
+apply_command() {
+ rebuild "$current_hostname" "switch"
+}
+
+apply_boot_command() {
+ rebuild "$current_hostname" "boot"
+}
+
+test_command() {
+ local given_host="${1:-}"
+
+ local target
+
+ if [ -z "$given_host" ]; then
+ target="$current_hostname"
+ else
+ target="$given_host"
+ fi
+
+ run nix build "$config#nixosConfigurations.$target.config.system.build.toplevel" --dry-run
+}
+
+repl_command() {
+ local given_host="${1:-}"
+
+ local target
+
+ if [ -z "$given_host" ]; then
+ target="$current_hostname"
+ else
+ target="$given_host"
+ fi
+
+ rebuild "$target" "repl"
+}
+
+garbage_command() {
+ local given_period="${1:-}"
+
+ if [ -z "$given_period" ]; then
+ run sudo nix-collect-garbage -d
+ else
+ run sudo nix-collect-garbage --delete-older-than "$given_period"
+ fi
+}
+
+help_command() {
+ echo "usage: nx <command> [args] [flags]"
+ echo ""
+ echo "commands:"
+ echo " apply apply nixos configuration and switch to it"
+ echo " apply-boot apply nixos configuration on next boot"
+ echo " test [host] try building configuration of a host (default: current host)"
+ echo " repl [host] start repl with host configuration (default: current host)"
+ echo " garbage [age] collect garbage (default: all)"
+ echo ""
+ echo "flags:"
+ echo " --here, -h evaluate configuration in current directory"
+}
+
+args=()
+for arg in "$@"; do
+ if [[ "$arg" == -* ]]; then
+ flag "$arg"
+ else
+ args+=("$arg")
+ fi
+done
+
+command="${args[0]:-}"
+
+if [ -z "$command" ]; then
+ error "no command given!"
+ echo ""
+ help_command
+ exit 1
+fi
+
+case "$command" in
+help)
+ help_command
+ ;;
+apply)
+ apply_command
+ ;;
+apply-boot)
+ apply_boot_command
+ ;;
+test)
+ test_command "${args[1]:-}"
+ ;;
+repl)
+ repl_command "${args[1]:-}"
+ ;;
+garbage)
+ garbage_command "${args[1]:-}"
+ ;;
+*)
+ error "unknown command '$command'"
+ exit 1
+ ;;
+esac