-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
executable file
·106 lines (96 loc) · 3.21 KB
/
install.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
#!/usr/bin/env bash
set -e
dir="$(dirname "$(readlink -f "$0")")"
flakePath="${flakePath:-"$dir"}"
# if SUDO_ASKPASS is set, use sudo -A
if [ -n "$SUDO_ASKPASS" ]; then
sudo() {
SUDO_ASKPASS="$SUDO_ASKPASS" command sudo -A "$@"
}
fi
# if user is root, sudo should do nothing
if [ "$(id -u)" -eq 0 ]; then
sudo() {
"$@"
}
fi
helpMessage(){
local green='\033[0;32m'
local yellow='\033[0;33m'
local bold='\033[1m'
local NC='\033[0m' # No Color
echo -e "Usage: ${bold}$(basename "$0") ${yellow}[--boot|--switch|--build|--build-vm|--help] ${NC}[HOST]"
echo -e "${green} --boot ${NC}Build and add boot entry for the system configuration"
echo -e "${green} --build ${NC}Build the system configuration"
echo -e "${green} --help ${NC}Show this help message"
echo -e "${green} --switch ${NC}Build and switch to the system configuration (default)"
echo -e "${green} --verbose ${NC}Enable verbose output"
}
getFlakeRef(){
local ref
local flake
pushd "$flakePath" > /dev/null
ref=""
# check if repo is dirty
if git diff --quiet; then
ref="?ref=$(git rev-parse --abbrev-ref HEAD)"
fi
popd > /dev/null
flake="git+file://${flakePath}${ref}"
echo "$flake"
}
main(){
nixCommand=(nix --experimental-features 'pipe-operator nix-command flakes' --accept-flake-config)
logFormat=(--log-format bar-with-logs)
flake="$(getFlakeRef)"
case $1 in
--boot)
sudo nixos-rebuild boot --fast --flake "$flake#$2" --accept-flake-config "${logFormat[@]}" "${@:3}"
;;
--build)
if [ "$#" -lt 2 ]; then
output="$(hostname)"
else
output="$2"
fi
if grep -q "@" <<< "$output"; then
"${nixCommand[@]}" build "${logFormat[@]}" "$flake#homeConfigurations.$output.activationPackage" "${@:3}"
elif grep -q "nixos-vm" <<< "$output"; then
"${nixCommand[@]}" build "${logFormat[@]}" "$flake#nixosConfigurations.$output.config.system.build.vm" "${@:3}"
elif grep -q "nixos-iso" <<< "$output"; then
"${nixCommand[@]}" build "${logFormat[@]}" "$flake#nixosConfigurations.$output.config.system.build.isoImage" "${@:3}"
else
"${nixCommand[@]}" build "${logFormat[@]}" "$flake#nixosConfigurations.$output.config.system.build.toplevel" "${@:3}"
fi
;;
--help|-h)
helpMessage
;;
--list-outputs)
nix eval --impure --raw --expr "
let
flake = builtins.getFlake \"$flake\";
nixosConfigurations = builtins.attrNames flake.nixosConfigurations;
homeConfigurations = builtins.attrNames flake.homeConfigurations;
in
builtins.concatStringsSep \"\\n\" (nixosConfigurations ++ homeConfigurations) + \"\\n\""
;;
--list-systems)
nix eval --impure --raw --expr "
let
flake = builtins.getFlake \"$flake\";
nixosConfigurations = builtins.attrNames flake.nixosConfigurations;
in
builtins.concatStringsSep \"\\n\" nixosConfigurations + \"\\n\""
;;
--verbose|-v)
shift;
set -x
main "$@"
;;
--switch|*)
sudo nixos-rebuild switch --fast --flake "$flake#$2" --accept-flake-config "${logFormat[@]}" "${@:3}"
;;
esac
}
main "$@"