mirror of
https://github.com/bin456789/reinstall.git
synced 2025-01-19 04:49:13 +08:00
nixos: 完善安装 nixos
This commit is contained in:
parent
3ac1a8aef5
commit
36c90784f5
@ -1,4 +1,5 @@
|
|||||||
#!/bin/bash
|
#!/usr/bin/env bash
|
||||||
|
# nixos 默认的配置不会生成 /bin/bash
|
||||||
# shellcheck disable=SC2086
|
# shellcheck disable=SC2086
|
||||||
|
|
||||||
set -eE
|
set -eE
|
||||||
|
56
trans.sh
56
trans.sh
@ -891,9 +891,9 @@ EOF
|
|||||||
prefixLength = $prefix;
|
prefixLength = $prefix;
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
defaultGateway = {
|
defaultGateway6 = {
|
||||||
address = "$ipv6_gateway";
|
address = "$ipv6_gateway";
|
||||||
interface = "$ethx";
|
interface = "$ethx";
|
||||||
};
|
};
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
@ -1133,10 +1133,27 @@ get_build_threads() {
|
|||||||
min $threads_by_ram $threads_by_core
|
min $threads_by_ram $threads_by_core
|
||||||
}
|
}
|
||||||
|
|
||||||
|
add_newline() {
|
||||||
|
# shellcheck disable=SC1003
|
||||||
|
case "$1" in
|
||||||
|
head | start) sed -e '1s/^/\n/' ;;
|
||||||
|
tail | end) sed -e '$a\\' ;;
|
||||||
|
both) sed -e '1s/^/\n/' -e '$a\\' ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
install_nixos() {
|
install_nixos() {
|
||||||
os_dir=/os
|
os_dir=/os
|
||||||
keep_swap=true
|
keep_swap=true
|
||||||
|
|
||||||
|
show_nixos_config() {
|
||||||
|
echo
|
||||||
|
cat -n /os/etc/nixos/configuration.nix
|
||||||
|
echo
|
||||||
|
cat -n /os/etc/nixos/hardware-configuration.nix
|
||||||
|
echo
|
||||||
|
}
|
||||||
|
|
||||||
# 挂载分区,创建 swapfile
|
# 挂载分区,创建 swapfile
|
||||||
mount_part_basic_layout /os /os/efi
|
mount_part_basic_layout /os /os/efi
|
||||||
need_ram=2048
|
need_ram=2048
|
||||||
@ -1191,10 +1208,12 @@ install_nixos() {
|
|||||||
# 添加 nix-env 安装的软件到 PATH
|
# 添加 nix-env 安装的软件到 PATH
|
||||||
PATH="/root/.nix-profile/bin:$PATH"
|
PATH="/root/.nix-profile/bin:$PATH"
|
||||||
|
|
||||||
# 生成配置
|
# 生成配置并显示
|
||||||
nixos-generate-config --root /os
|
nixos-generate-config --root /os
|
||||||
|
echo "Original NixOS Configuration:"
|
||||||
|
show_nixos_config
|
||||||
|
|
||||||
# configuration.nix
|
# 修改 configuration.nix
|
||||||
if is_efi; then
|
if is_efi; then
|
||||||
nix_bootloader="boot.loader.efi.efiSysMountPoint = \"/efi\";"
|
nix_bootloader="boot.loader.efi.efiSysMountPoint = \"/efi\";"
|
||||||
else
|
else
|
||||||
@ -1212,10 +1231,7 @@ install_nixos() {
|
|||||||
# TODO: 准确匹配网卡,添加 udev 或者直接配置 networkd 匹配 mac
|
# TODO: 准确匹配网卡,添加 udev 或者直接配置 networkd 匹配 mac
|
||||||
create_nixos_network_config /tmp/nixos_network_config.nix
|
create_nixos_network_config /tmp/nixos_network_config.nix
|
||||||
|
|
||||||
# sed -e '1s/^/\n/' -e '$a\\' # 也可以在前后添加空行
|
cat <<EOF |
|
||||||
{
|
|
||||||
echo # 前面的空行
|
|
||||||
cat <<EOF | add_space 2 | del_empty_lines
|
|
||||||
############### Add by reinstall.sh ###############
|
############### Add by reinstall.sh ###############
|
||||||
$nix_bootloader
|
$nix_bootloader
|
||||||
$nix_swap
|
$nix_swap
|
||||||
@ -1226,19 +1242,21 @@ services.openssh.settings.PermitRootLogin = "yes";
|
|||||||
$(cat /tmp/nixos_network_config.nix)
|
$(cat /tmp/nixos_network_config.nix)
|
||||||
###################################################
|
###################################################
|
||||||
EOF
|
EOF
|
||||||
echo # 后面的空行
|
add_space 2 | del_empty_lines | add_newline both |
|
||||||
} | insert_into_file /os/etc/nixos/configuration.nix before "networking.hostName" -F
|
insert_into_file /os/etc/nixos/configuration.nix before "networking.hostName" -F
|
||||||
|
|
||||||
# hardware-configuration.nix
|
# 修改 hardware-configuration.nix
|
||||||
# 在 vultr efi 机器上,nixos-generate-config 不会添加 virtio_pci
|
# 在 vultr efi 机器上,nixos-generate-config 不会添加 virtio_pci
|
||||||
# 导致 virtio_blk 用不了,启动时 initrd 找不到系统分区
|
# 导致 virtio_blk 用不了,启动时 initrd 找不到系统分区
|
||||||
# 可能由于 alpine 的 virtio_pci 编译进内核而不是模块,所以不会添加到配置文件
|
# 可能由于 alpine 的 virtio_pci 编译进内核而不是模块
|
||||||
|
# 因此 nixos-generate-config 不会添加 virtio_pci 到配置文件
|
||||||
olds=$(
|
olds=$(
|
||||||
grep -F 'boot.initrd.availableKernelModules' /os/etc/nixos/hardware-configuration.nix |
|
grep -F 'boot.initrd.availableKernelModules' /os/etc/nixos/hardware-configuration.nix |
|
||||||
cut -d= -f2 | tr -d '"[];' | xargs
|
cut -d= -f2 | tr -d '"[];' | xargs
|
||||||
)
|
)
|
||||||
alls="$olds"
|
alls="$olds"
|
||||||
for mod in ata_piix uhci_hcd sr_mod nvme \
|
# https://github.com/search?q=repo%3ANixOS%2Fnixpkgs+availableKernelModules&type=code
|
||||||
|
for mod in ahci ata_piix uhci_hcd sr_mod nvme \
|
||||||
virtio_pci virtio_blk virtio_scsi \
|
virtio_pci virtio_blk virtio_scsi \
|
||||||
xen_blkfront xen_scsifront \
|
xen_blkfront xen_scsifront \
|
||||||
hv_storvsc \
|
hv_storvsc \
|
||||||
@ -1259,6 +1277,10 @@ EOF
|
|||||||
array \
|
array \
|
||||||
/os/etc/nixos/hardware-configuration.nix
|
/os/etc/nixos/hardware-configuration.nix
|
||||||
|
|
||||||
|
# 显示修改后的配置
|
||||||
|
echo "Modified NixOS Configuration:"
|
||||||
|
show_nixos_config
|
||||||
|
|
||||||
# 安装系统
|
# 安装系统
|
||||||
nixos-install --root /os --no-root-passwd -j "$(get_build_threads 2048)"
|
nixos-install --root /os --no-root-passwd -j "$(get_build_threads 2048)"
|
||||||
|
|
||||||
@ -1276,7 +1298,8 @@ EOF
|
|||||||
# 清理
|
# 清理
|
||||||
nix-env -e '*'
|
nix-env -e '*'
|
||||||
# /nix/var/nix/profiles/system/sw/bin/nix-collect-garbage -d
|
# /nix/var/nix/profiles/system/sw/bin/nix-collect-garbage -d
|
||||||
/nix/var/nix/profiles/system/sw/bin/nixos-enter --root /os -- /run/current-system/sw/bin/nix-collect-garbage -d
|
/nix/var/nix/profiles/system/sw/bin/nixos-enter --root /os -- \
|
||||||
|
/run/current-system/sw/bin/nix-collect-garbage -d
|
||||||
|
|
||||||
# 删除 nix
|
# 删除 nix
|
||||||
umount /nix
|
umount /nix
|
||||||
@ -1291,6 +1314,9 @@ EOF
|
|||||||
rm -rf /os/swapfile
|
rm -rf /os/swapfile
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# 重新显示配置,方便查看
|
||||||
|
show_nixos_config
|
||||||
}
|
}
|
||||||
|
|
||||||
install_arch_gentoo() {
|
install_arch_gentoo() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user