-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun
More file actions
executable file
·164 lines (143 loc) · 3.56 KB
/
Copy pathrun
File metadata and controls
executable file
·164 lines (143 loc) · 3.56 KB
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
#!/usr/bin/env sh
set -eu
# set -x
set -o pipefail
PROG=$(basename $0)
usage()
{
printf ""
printf "Usage: %s <option>\n" "$PROG"
printf "options:\n"
printf "\t home-switch <machine> Rebuild and switch user profile\n"
printf "\t rebuild-switch <machine> Rebuild and switch machine\n"
printf "\t rebuild-boot <machine> Rebuild and boot machine\n"
printf "\t collect-garbage Collect garbage\n"
printf "\t delete-generations Delete generations older than 7 days\n"
printf "\t fmt Run formatter\n"
printf "\t lint Run linter\n"
printf "\t deadnix Run dead code analyzer\n"
printf "\n"
printf "machines:\n"
printf "\t thorfinn Personal laptop\n"
printf "\t kuujo Remote server\n"
printf "\n"
}
total_args=$#
# validate min args
if [ $total_args -lt 1 ]; then
echo "Expected atleast 1 argument(s), got $total_args"
usage
exit 1
fi
# validate exact args count
validate_exact_args()
{
if [ $total_args -ne "$1" ]; then
echo "Expected exactly $1 argument(s), got $total_args"
usage
exit 1
fi
}
run()
{
printf "Running: \033[32m%s\033[0m" "$1"
printf "\n"
eval "$1"
}
# switch-switch
# first you will need nix run
home_switch()
{
run "rm -f /home/$1/.gtkrc-2.0"
run "rm -f /home/$1/.config/fontconfig/conf.d/10-hm-fonts.conf"
run "home-manager switch --flake .#$1 --impure"
exit 0
}
# rebuild-switch
rebuild_switch()
{
run "sudo nixos-rebuild switch --flake ./hosts#$1"
exit 0
}
rebuild_boot()
{
run "sudo nixos-rebuild boot --flake ./hosts#$1 --show-trace -L -v"
exit 0
}
option="$1"
case $option in
"home-switch")
# validate user
validate_exact_args 2
user=$2
case $user in
"prashant")
home_switch "$user"
;;
*)
echo "Invalid user: $user"
usage
exit 1
esac
;;
"rebuild-switch")
# validate machine
validate_exact_args 2
machine=$2
case $machine in
"thorfinn" | "kuujo")
rebuild_switch "$machine"
;;
*)
echo "Invalid machine: $machine"
usage
exit 1
esac
;;
"rebuild-boot")
# validate machine
validate_exact_args 2
machine=$2
case $machine in
"thorfinn" | "kuujo")
rebuild_boot "$machine"
;;
*)
echo "Invalid machine: $machine"
usage
exit 1
esac
;;
"collect-garbage")
validate_exact_args 1
run "nix-store --gc"
run "nix-collect-garbage -d"
run "sudo nix-collect-garbage -d"
exit 0
;;
"delete-generations")
validate_exact_args 1
run "sudo nix profile wipe-history --older-than 7d --profile /nix/var/nix/profiles/system"
exit 0
;;
"fmt")
validate_exact_args 1
run "nix fmt ."
exit 0
;;
"deadnix")
validate_exact_args 1
run "NO_COLOR=1 nix run nixpkgs#deadnix -- . | less"
exit 0
;;
"lint")
validate_exact_args 1
run "nix run nixpkgs#statix -- check"
exit 0
;;
*)
echo "Invalid option: $option"
usage
exit 1
;;
esac