-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·107 lines (92 loc) · 3.28 KB
/
build.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env bash
cd $(dirname "$0")
if [[ "$1" == "-h" || "$1" == "--help" ]]
then
exec echo "
Usage:
$0 [OPTION]
Options:
-h, --help: Open help page.
-d, --default: Use default settings.
-c, --custom: After that, type the value of each question, separate them with <SPC>, and press <RET>.
-r <hostname>, --remote <hostname>: Build configuration for <hostname> in your device and copy to it.
"
fi
# ~~ Interaction ~~
if [[ "$1" != "-d" && "$1" != "--default" ]]
then
if [[ "$1" == "-r" || "$1" == "--remote" ]]
then
hname="$2"
nix build path:.\#nixosConfigurations.$hname.config.system.build.toplevel
exec nix copy ./result --to ssh://$hname
else
if [[ "$1" == "-c" || "$1" == "--custom" ]]
then
flag="$2"
subcommand="$3"
hname="$4"
other=""
while [[ $# -ne 4 ]]
do
other+=" $5"
shift
done
else
echo -e "\nWhich \033[1moperation\033[0m do you like to perform?
\033[31m- build a LiveCD \033[0m(b)
\033[33m- install from a LiveCD \033[0m(i)
\033[1;32m- update your configuration \033[0m(\033[1;4mu\033[0m)"
read -p ">> " flag
if [[ "$flag" != "b" && "$flag" != "B" ]]
then
echo -e "\nWhich rebuild \033[1;34msubcommand\033[0m do you like to use? (\033[1;4mswitch\033[0m/boot/test/build/dry-build/...)"
read -p ">> " subcommand
echo -e "\nWhat will be your \033[1;35mideal hostname\033[0m? (\033[1;4m$(hostname)\033[0m/...)"
read -p ">> " hname
echo -e "\nIf you have other option, such as '--no-build-nix', '--show-trace', '--verbose', please indicate here; or simply press <RET>."
read -p ">> " other
echo
fi
fi
fi
fi
# ~~ Auto Completion ~~
if [[ -z "$flag" ]] ; then flag="u"; fi
if [[ -z "$subcommand" ]] ; then subcommand="switch"; fi
if [[ -z "$hname" ]] ; then hname=$(hostname); fi
if [[ "$other" == " " ]]; then other=""; fi
if [[ -z "$1" ]]
then
echo -e "\033[1;31mHint:\033[0m Next time you can type '\033[1;31m$0 -c $flag $subcommand $hname $other\033[0m' to execute the same operation.\n"
fi
if [[ "$other" == "" ]]; then other="--keep-going --verbose"; fi
# ~~ Action ~~
# 1. LiveCD
if [[ "$flag" == "b" || "$flag" == "B" ]]
then
rsync -rL --delete ~/clash-configuration ./clash-configuration-temp
nix build path:.\#nixosConfigurations.livecd.config.system.build.isoImage
exec rm -rf ./clash-configuration-temp
fi
# 2. normal build
if [[ "$flag" == "i" || "$flag" == "I" ]]
then
# TODO: Use `nixos-generate-config` or some other things to automatically update hardware-configuration.nix
# nixos-generate-config --root /mnt
# cp /mnt/etc/hardware-configuration.nix ...towhere?
echo -e "Oops! It requires you to \033[1;31mcheck your privilege\033[0m..."
exec sudo nixos-install --flake "path:.#$hname" $other
elif [[ "$flag" == "u" || "$flag" == "U" ]]
then
if [[ "$subcommand" != *"build" ]]
then
echo -e "Oops! It requires you to \033[1;31mcheck your privilege\033[0m..."
exec sudo nixos-rebuild $subcommand --flake "path:.#$hname" $other
else
exec nixos-rebuild $subcommand --flake "path:.#$hname" $other
fi
fi
# ~~ Error ~~
echo -e "\n\033[1;31mInvalid input!\033[0m Exiting the build script..."
exit 1