-
Notifications
You must be signed in to change notification settings - Fork 0
/
nixos-rebuild.sh
76 lines (59 loc) · 1.39 KB
/
nixos-rebuild.sh
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
#!/usr/bin/env bash
set -euo pipefail
# Function to print usage
usage() {
echo "Usage: $0 <hostname>"
exit 1
}
# Function to autoformat Nix files
autoformat_nix_files() {
alejandra . &>/dev/null || {
alejandra .
echo "Formatting failed!"
exit 1
}
}
# Function to rebuild NixOS
rebuild_nixos() {
local hostname=$1
sudo nixos-rebuild switch --flake .#$hostname &>nixos-rebuild.log || {
cat nixos-rebuild.log | grep --color error
exit 1
}
}
# Function to commit changes
commit_changes() {
local current
current=$(nixos-rebuild list-generations | grep current | cut -d ' ' -f 1)
# Check if there are any changes to commit
if git diff-index --quiet HEAD --; then
echo "No changes to commit."
else
git commit -am "build: generation $current"
fi
}
# Check if hostname is provided
if [ $# -ne 1 ]; then
usage
fi
HOSTNAME=$1
# Navigate to the NixOS configuration directory
pushd /etc/nixos &>/dev/null
# Pull the latest changes from the git repository
echo "Pulling the latest changes..."
git pull
# Autoformat all Nix files
echo "Formatting Nix files..."
autoformat_nix_files
# Show the changes
git add .
git diff -U0 '*.nix'
echo "Rebuilding NixOS..."
# Rebuild NixOS
rebuild_nixos "$HOSTNAME"
# Commit all changes with the generation metadata
commit_changes
# Back to where you were
popd &>/dev/null
# Return success message
echo "Rebuild successfull!"