-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·96 lines (88 loc) · 2.58 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
#!/usr/bin/env bash
set -xe
NIXADDR=172.16.55.128
NIXPORT=22
NIXBLOCKDEVICE=sda
NIXUSER=uniqueding
NIXHOST=uniqueding-nas
_init()
{
ssh -p$NIXPORT root@$NIXADDR << EOF
parted /dev/$NIXBLOCKDEVICE -- mklabel gpt
parted /dev/$NIXBLOCKDEVICE -- mkpart primary 512MiB -8GiB
parted /dev/$NIXBLOCKDEVICE -- mkpart primary linux-swap -8GiB 100\%
parted /dev/$NIXBLOCKDEVICE -- mkpart ESP fat32 1MiB 512MiB
parted /dev/$NIXBLOCKDEVICE -- set 3 esp on
mkfs.ext4 -L nixos /dev/${NIXBLOCKDEVICE}1
mkswap -L swap /dev/${NIXBLOCKDEVICE}2
mkfs.fat -F 32 -n boot /dev/${NIXBLOCKDEVICE}3
mount /dev/disk/by-label/nixos /mnt
mkdir -p /mnt/boot
mkdir -p /mnt/opt
mount /dev/disk/by-label/boot /mnt/boot
nixos-generate-config --root /mnt
sed --in-place '/system\.stateVersion = .*/a \
nix.package = pkgs.nixUnstable;\n \
nix.extraOptions = \"experimental-features = nix-command flakes\";\n \
services.openssh.enable = true;\n \
services.openssh.passwordAuthentication = true;\n \
services.openssh.permitRootLogin = \"yes\";\n \
users.users.root.initialPassword = \"root\";\n \
' /mnt/etc/nixos/configuration.nix
nixos-install --no-root-passwd
reboot
EOF
}
_start()
{
NIXUSER=root
echo $NIXUSER
_cp
_switch
}
_cp()
{
ssh -p$NIXPORT $NIXUSER@$NIXADDR << EOF
sudo mkdir -p /opt/dotfiles
EOF
scp -r -P$NIXPORT * $NIXUSER@$NIXADDR:/opt/dotfiles
ssh -p$NIXPORT $NIXUSER@$NIXADDR << EOF
sudo mkdir -p /opt/dotfiles/hosts/$NIXHOST
sudo cp /etc/nixos/hardware-configuration.nix /opt/dotfiles/hosts/$NIXHOST/
EOF
}
_switch()
{
ssh -p$NIXPORT $NIXUSER@$NIXADDR << EOF
sudo NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM=1 nixos-rebuild switch --flake "/opt/dotfiles#$NIXHOST"
sudo chown -R uniqueding:users /opt/dotfiles
EOF
}
_install()
{
nixos-generate-config --root /mnt
sed --in-place '/system\.stateVersion = .*/a \
nix.package = pkgs.nixUnstable;\n \
nix.extraOptions = \"experimental-features = nix-command flakes\";\n \
services.openssh.enable = true;\n \
services.openssh.passwordAuthentication = true;\n \
services.openssh.permitRootLogin = \"yes\";\n \
users.users.root.initialPassword = \"root\";\n \
' /mnt/etc/nixos/configuration.nix
nixos-install --no-root-passwd
mkdir -p /mnt/opt/dotfiles
cp -r * /mnt/opt/dotfiles
mkdir -p /mnt/opt/dotfiles/hosts/$NIXHOST
cp /mnt/etc/nixos/hardware-configuration.nix /mnt/opt/dotfiles/hosts/$NIXHOST/
}
if [[ $1 == "init" ]];then
$(_init)
elif [[ $1 == "start" ]];then
$(_start)
elif [[ $1 == "cp" ]];then
$(_cp)
elif [[ $1 == "switch" ]];then
$(_switch)
elif [[ $1 == "install" ]];then
$(_install)
fi