summary refs log tree commit diff
path: root/scripts/nx.sh
blob: 245531c37b05dbd3598cc2cb5c6adb927d8bb694 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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