-
Notifications
You must be signed in to change notification settings - Fork 57
/
stage2
executable file
·96 lines (80 loc) · 2.86 KB
/
stage2
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
#!/bin/sh
set -eu
root_mount=$1
root_type=$2
grub_device=$3
digitalocean=$4
digitalocean_configs=""
if [ "$digitalocean" = "true" ];
then
digitalocean_configs="
## Digital Ocean networking setup; manage interfaces manually
networking.useDHCP = false;
systemd.services.setup-network = {
wantedBy = [ \"multi-user.target\" ];
after = [ \"network-pre.target\" ];
path = [ pkgs.gawk pkgs.iproute pkgs.openresolv ];
serviceConfig = {
ExecStart = \"\${pkgs.bash}/bin/bash /etc/nixos-in-place/setup-network\";
};
};
"
mkdir -p /nixos/etc/nixos-in-place
cp /nixos-in-place/setup-network /nixos/etc/nixos-in-place/
## Not a valid link now, but this will represent the file which DO will
## update each boot.
mkdir -p /nixos/etc/network
ln -sf /old-root/etc/network/interfaces /nixos/etc/network/
## Use the original authorized_keys that DO added
mkdir -pm 711 /root/
mkdir -pm 700 /root/.ssh/
ln -sf /old-root/root/.ssh/authorized_keys /root/.ssh/authorized_keys
fi
## Enable host resolution
cp /etc/external-resolv.conf /etc/resolv.conf
## Install dependencies; it's easier to do this here, in the Nix chroot,
## since we don't rely on the host system.
nix-env -i pcre
## Generate a base config
nixos-generate-config --root /nixos
cat <<EOF > /nixos/etc/nixos/nixos-in-place.nix
{ config, pkgs, ... }:
{
## You probably want to change or remove these settings.
$(cat /nixos/extra-config)
## You probably do NOT want to change these settings.
boot.kernelParams = ["boot.shell_on_fail"];
boot.loader.grub.device = "$grub_device";
boot.loader.grub.storePath = "/nixos/nix/store";
boot.initrd.supportedFilesystems = [ "$root_type" ];
boot.initrd.postDeviceCommands = ''
mkdir -p /mnt-root/old-root ;
mount -t $root_type $root_mount /mnt-root/old-root ;
'';
fileSystems = {
"/" = {
device = "/old-root/nixos";
fsType = "none";
options = [ "bind" ];
};
"/old-root" = {
device = "$root_mount";
fsType = "$root_type";
};
};
## These settings are specified to DigitalOcean.
$digitalocean_configs
}
EOF
## Add in our configuration additions
nixos_dir=/nixos/etc/nixos
cp $nixos_dir/configuration.nix $nixos_dir/backup-configuration.nix
sed -i 's|\(\s*\)\(./hardware-configuration.nix\)|\1\2\n\1./nixos-in-place.nix|' $nixos_dir/configuration.nix
## Enable SSH by default
sed -i 's/# \(services\.openssh\)/\1/' $nixos_dir/configuration.nix
## Remove the automatically-generated fileSystems configuration
mv $nixos_dir/hardware-configuration.nix $nixos_dir/backup-hardware-configuration.nix
pcregrep -Mv "fileSystems.\"/\"[\s\S]*};" $nixos_dir/backup-hardware-configuration.nix > $nixos_dir/hardware-configuration.nix
## Installs grub and a base NixOS system; after a reboot, we're golden
NIXOS_INSTALL_REEXEC=1 nixos-install --root /nixos --show-trace --no-root-passwd
exit