aboutsummaryrefslogtreecommitdiff
path: root/common.sh
blob: 9b6f48ce22d073931b408083bccb862aa4bfe34d (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Common Bash functions for helper scripts in this repository
set -eu

## Logging #####################################################################
_print() {
	local color="\e[$1m"
	local clrcolor="\e[0m"
	shift
	if [ ! -t 1 ]; then
		color=""
		clrcolor=""
	fi
	printf "${color}%s${clrcolor}\n" "$*" >&2
}

stage() {
	_print '1;32' "$@"
}

info() {
	_print '1;35' "$@"
}

error() {
	_print '1;31' "$@"
}
warning() {
	_print '1;33' "$@"
}

## SSH access helper ###########################################################

# Convert hostname to the SSH destination
sshdest() {
	if [ "$1" = "lipwig" ]; then
		echo "newlipwig"
	else
		awk -F- 'NF > 1 { print $2"."$1; exit } { print $1 }' <<<"$1"
	fi
}

# Reverse opeartion for sshdest
sshhost() {
	awk -F. 'NF > 1 { print $2"-"$1; exit } { print $1 }' <<<"$1"
}

_ssh() {
	local device="$1"
	shift
	if [ "$device" != "$(hostname)" ]; then
		ssh "$(sshdest "$device")" -- "$@"
	else
		if [ $# -gt 1 ]; then
			"$@"
		else
			sh -c "$1"
		fi
	fi
}

_rootssh() {
	local device="$1"
	local cmd="$2"
	if [ "$device" != "$(hostname)" ]; then
		ssh -t "$(sshdest "$device")" sudo "sh -c '${cmd}'"
	else
		sudo sh -c "$cmd"
	fi
}

## Evalutions and queries ######################################################

# The path where build result is linked to
result() {
	echo ".result-$1"
}

# Get system of the device
device_system() {
	nix eval --raw ".#nixosConfigurations.$1.config.nixpkgs.system"
}

# Validates if link is valid.
build_validate() {
	local device="$1"
	[ -L "$(result "$device")" ] && [ -e "$(result "$device")" ]
}

## Build NixOS system ##########################################################
# $1: device name
# All other arguments are passed to the nix build command
build() {
	local device="$1"
	shift

	local toplevel=".config.system.build.toplevel"
	if [ "$(device_system "$device")" = "armv7l-linux" ]; then
		toplevel=".config.system.build.cross.x86_64-linux${toplevel}"
	fi
	if [ "$(device_system "$device")" = "aarch64-linux" ]; then
		toplevel=".config.system.build.cross.x86_64-linux${toplevel}"
	fi

	stage "Building system for device: $device"
	nix build \
		-o "$(result "${device}")" \
		--keep-going \
		"$@" \
		"${0%/*}#nixosConfigurations.${device}${toplevel}"
}

## Copy NixOS system ###########################################################
# $1: device name
copy() {
	local device="$1"
	if ! build_validate "$device"; then
		warning "System for device '$device' seems to be not build." >&2
		return 1
	fi
	local store
	store="$(readlink -f "$(result "$device")")"

	local freespace required;
	freespace="$(_ssh "$device" df -B 1 /nix | awk 'NR == 2 { print $4 }')"
	required="$(nix path-info -S "$store" | awk '{ print $2 }')"
	info "Free space on device: $(numfmt --to=iec "$freespace")"
	info "Required space: $(numfmt --to=iec "$required")"
	if [ "$required" -ge "$freespace" ]; then
		error "There is not enough space to copy clousure to: $device" >&2
		return 1
	fi

	stage "Copy closure to: $device"
	nix copy -s --to "ssh://$(sshdest "$device")" "$store"
}

## Switch Nix encironment ######################################################
# $1: switch operation to be performed
# $2: device name
# TODO possibly really query if switch is or is not required
setenv() {
	local switchop="$1"
	local device="$2"
	if ! build_validate "$device"; then
		warning "System '$device' seems to be not build." >&2
		return 1
	fi
	local store
	store="$(readlink -f "$(result "$device")")"

	stage "${switchop^} system: $device"
	local cursystem
	cursystem="$(_ssh "$device" readlink -f /nix/var/nix/profiles/system)"
	if [ "$cursystem" != "$store" ]; then
		info "-----------------------------------------------------------------"
		_ssh "$device" \
			nix store diff-closures "$cursystem" "$store"
		info "-----------------------------------------------------------------"
		local _store _switchop
		printf -v _store '%q' "$store"
		printf -v _switchop '%q' "$switchop"
		_rootssh "$device" "$_store/bin/nixos-system -s $_switchop"
	else
		warning "The latest system might have been already set."
	fi
}

boot() {
	setenv boot "$1"
}

switch() {
	setenv switch "$1"
}

switch_test() {
	setenv test "$1"
}