-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnswitch
executable file
·116 lines (88 loc) · 2.75 KB
/
nswitch
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
#!/usr/bin/env bash
# from https://github.com/0atman/noboilerplate/blob/main/scripts/38-nixos.md#dont-use-nix-env
# docstring=quick edit (or ling xD) for nixos config
set -E -o pipefail
shopt -s failglob
export LC_ALL=C.UTF8
INITIAL_PWD="$(pwd)"
PROJECT="$HOME/Projects/NixOS"
GIT_PATTERN="." # "*.nix"
SELF_EDITOR="$EDITOR" # here you can overwrite which editor should be used
# switch into the project for the whole script
cd "$PROJECT" || exit 1
exit_with_new_shell() {
# check if already in directory
[[ "$INITIAL_PWD" = "$PROJECT" ]] && printf '>>> already in directory\n' && exit 0
cd "$PROJECT" || exit 1
printf '\n'
printf '>>> opening a new \"%s\" shell\n' "$(basename "$SHELL")"
printf ' cwd: %s\n' "$(pwd)"
"$SHELL"
exit 0
}
ask_for_rebuild() {
printf ">>> Do you want to rebuild NixOS? [y/N]\n"
printf "<<< " && read -r YESNO
if ! [[ "${YESNO,,}" == "y" ]]; then
exit_with_new_shell
fi
}
rebuild() {
printf ">>> Rebuilding NixOS\n"
sudo nixos-rebuild switch | tee nixos-switch.log || (
grep --color error nixos-switch.log && false)
}
autoformat() {
printf ">>> auto formating ..."
nix fmt . &> /dev/null
printf " DONE!\n"
}
quick_edit() {
"$SELF_EDITOR"
# check if there are changes at all
[[ -z "$(git diff -U0 "$GIT_PATTERN")" ]] && echo ">>> no changes, exiting ..." && exit_with_new_shell
# only then do auto formating
autoformat
# and then show the user a diff
git diff -U0 "$GIT_PATTERN" # as a seperate command so we get nice colors and pager
ask_for_rebuild
printf ">>> Add everything to git staging so nix can find it\n"
git add "$GIT_PATTERN"
rebuild
gen=$(nixos-rebuild list-generations | grep current)
printf ">>> commiting to git\n"
git commit -am "$gen"
exit_with_new_shell
}
update() {
nix flake update
ask_for_rebuild
rebuild
}
help_msg() {
printf -- '# only one option can be selected\n'
printf -- ' without arguments -> quick edit mode\n'
printf -- '--edit | -e -> only edit\n'
printf -- '--update | -u -> update the system\n'
printf -- '--autoformat | -f -> only autoformat\n'
printf -- '--new-shell-in-project | -s -> start new shell, with cwd in the project\n'
printf -- '--help | -h -> show this message\n'
}
if [[ -z "$1" ]]; then
quick_edit
elif [[ "$1" = "--edit" || "$1" = "-e" ]]; then
"$SELF_EDITOR"
elif [[ "$1" = "--update" || "$1" = "-u" ]]; then
update
elif [[ "$1" = "--autoformat" || "$1" = "-f" ]]; then
autoformat
elif [[ "$1" = "--new-shell-in-project" || "$1" = "-s" ]]; then
exit_with_new_shell
elif [[ "$1" = "--help" || "$1" = "-h" ]]; then
help_msg
else
help_msg
printf "=================\n"
printf "didn't recognise: %s\n" "$1"
exit 1
fi