-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebuild.sh
executable file
·83 lines (63 loc) · 2.13 KB
/
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
77
78
79
80
81
82
83
#!/usr/bin/env bash
# modified from https://gist.github.com/0atman/1a5133b842f929ba4c1e195ee67599d5
# A rebuild script that commits on a successful build
# useage:
# bash rebuild.sh nix
# or
# bash rebuild.sh home
# where do you keep your *.nix files?
directory_nixconfig=~/.config/nixfiles/system # should contain hardware-configuration.nix
filename_nixconfig=configuration.nix # should be in above folder
directory_hmconfig=~/.config/nixfiles/homemanager
filename_hmconfig=home.nix # should be in above folder
logfile=nixos-switch.log # logfile will be in the above directories
case $1 in
'nix')
directory=$directory_nixconfig
build() {
sudo nixos-rebuild switch -I nixos-config=$filename_nixconfig &>$logfile || (cat $logfile | rg error && exit 1)
current=$(nixos-rebuild list-generations | rg current)
}
;;
'home')
directory=$directory_hmconfig
build() {
home-manager -f $filename_hmconfig switch &>$logfile || (cat $logfile | rg error && exit 1)
current=$(home-manager generations | sed -n 1p)
}
;;
*)
echo "Usage: bash rebuild.sh [nix|home]" && exit 1
;;
esac
# cd to your config dir
pushd $directory
# check for untracked .nix files, exit code 0 if any exist, exist code 1 if none do
{
git status --short --untracked-files=normal "*.nix" | rg "\?\?"
} > /dev/null
if [ $? -eq 0 ]; then
echo "WARNING: The following untracked .nix files exist in "$directory
git status --short --untracked-files=normal "*.nix" | rg "\?\?"
echo "Changes to these files will not be detected."
fi
set -e # needed to move this down because of the git status section
# Early return if no changes were detected (thanks @singiamtel!)
if git diff --quiet '*.nix'; then
echo "No changes detected, exiting."
popd
exit 0
fi
# Autoformat your nix files
alejandra . &>/dev/null \
|| ( alejandra . ; echo "Formatting failed!" && exit 1)
# Shows your changes
git diff -U0 '*.nix'
# call nixos-rebuild or home-manager as appropriate
echo "Building..."
build
# Commit all changes with the generation metadata
git commit -am "$current"
echo "Rebuilt OK!"
# Back to where you were
popd