用户用 Gemini 辅助将 Home Assistant 从 HAOS 迁移至 NixOS
来源标题:Migrating My Home Assistant Instance from HAOS to NixOS (On Proxmox)
一位用户借助 Gemini 生成配置,把 Home Assistant 实例从 HAOS 迁移到运行在 Proxmox 上的 NixOS。他将生成的方案交给新的 LLM 会话迭代实现,并利用 NixOS 全系统代码化定义的特点,在本地构建配置、完成确定性验证后再部署到 Proxmox。最终产出包含主机配置、disko、Home Assistant、Nginx 反代和 Zigbee 等模块的 Nix 文件。
以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。
在确认报告没问题之后,我把生成的计划交给了一个全新的 LLM 会话,让 Gemini 据此迭代出实际实现。
这正是 NixOS 在 AI 辅助基础设施工作中的优势所在:整个系统都用代码定义,因此 agent 可以在本地构建配置,并在任何东西接触到 Proxmox 之前就获得即时、确定性的验证。基于这份计划,Gemini 产出了三个产物:
hosts/sfax/default.nix
{ lib, pkgs, modulesPath, ... }: imports = [ (modulesPath + "/profiles/qemu-guest.nix") ../../shared/default.nix # config shared across all of my machines ./disko.nix ./home-assistant ./proxy.nix # Nginx reverse proxy + LetsEncrypt via Cloudflare DNS ./zigbee.nix ];
Setup my user (NixOS + Home Manager config)
ksiksi.users.aksiksi.enable = true;
Bootloader
boot.loader.systemd-boot = { enable = true; configurationLimit = 50; }; boot.loader.efi.canTouchEfiVariables = true; boot.growPartition = true;
Networking
systemd.network.networks = { "10-lan" = { matchConfig.MACAddress = "..."; # Trusted VLAN NIC address = [ "192.168.10.30/24" ]; gateway = [ "192.168.10.1" ]; dns = [ "192.168.10.2" ]; # Pi-hole }; "11-iot" = { matchConfig.MACAddress = "..."; # Untrusted VLAN NIC address = [ "192.168.30.10/24" ]; };
Fallback DHCP when booted pre-cutover.
"20-dhcp-fallback" = { matchConfig.Type = "ether"; networkConfig.DHCP = "yes"; }; };
NFS client and automount
boot.supportedFilesystems = [ "nfs" ]; environment.systemPackages = [ pkgs.nfs-utils ]; services.rpcbind.enable = true; systemd.mounts = [ type = "nfs"; what = "192.168.10.3:/pool/home"; where = "/mnt/home"; mountConfig.Options = "nfsvers=4,async,noatime,nodiratime,defaults,nofail"; wantedBy = [ "multi-user.target" ]; ];
Proxmox integration
services.qemuGuest.enable = true; nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux";
hosts/sfax/home-assistant/default.nix
{ ... }: imports = [ ./tasks.nix # systemd timers to replace HA scripts/automations ]; services.home-assistant = { enable = true; extraComponents = [ "default_config" "dhcp" "mobile_app" "mqtt" "ssdp" "usb" "zeroconf"
... other device/service integrations
]; config = { default_config = {}; homeassistant = { allowlist_external_dirs = [ "/mnt/home" ]; }; logger.default = "critical";
Migrated static config
automation = "!include automations.yaml"; script = "!include scripts.yaml"; scene = "!include scenes.yaml";
Declarative config
"automation nix" = "!include ${./automations.yaml}"; "script nix" = "!include ${./scripts.yaml}"; "scene nix" = "!include ${./scenes.yaml}";
Sidebar tab for Z2M
lovelace.dashboards.zigbee2mqtt-ui = { mode = "yaml"; title = "Zigbee2MQTT"; icon = "mdi:zigbee"; show_in_sidebar = true; require_admin = true; filename = "${./dashboards/z2m.yaml}"; }; }; };
hosts/sfax/disko.nix
Disko partitioning config used to generate a "baked" VM image ready for Proxmox
{ lib, ... }: disko = { memSize = 4096; imageBuilder.imageFormat = "qcow2"; devices.disk.sda = { type = "disk"; device = lib.mkDefault "/dev/sda"; imageName = "sfax"; imageSize = "32G"; content = { type = "gpt"; partitions = { esp = { name = "ESP"; size = "500M"; type = "EF00"; content = { type = "filesystem"; format = "vfat"; mountpoint = "/boot"; mountOptions = [ "umask=0077" ]; }; }; root = { size = "100%"; content = { type = "filesystem"; format = "ext4"; mountpoint = "/"; mountOptions = [ "x-systemd.growfs" ]; }; }; }; }; }; };
第 3 步:用本地 VM 测试
在把磁盘镜像交给 Proxmox 之前,NixOS 让"比干净的求值/构建再进一步"变得很简单:在本地 VM 中启动这套配置。这样你就能验证系统能否干净启动、服务是否按预期启动、有没有明显的运行时或配置问题,而完全不用碰正在运行的 HAOS 实例。
1. Build disko qcow2 image
This will boot the VM in QEMU, partition disks via disko, and finally run the NixOS installer.
nix build .#nixosConfigurations.sfax.config.system.build.diskoImages
2. Create a writable CoW overlay backed by the Nix store image
nix shell nixpkgs#qemu_kvm -c qemu-img create -f qcow2 -b "$(readlink -f result/sfax.qcow2)" -F qcow2 /tmp/sfax-local.qcow2
3. Boot locally in QEMU with KVM + UEFI + port forwards (SSH :2222, HA :18123, Z2M :18080)
nix run nixpkgs#qemu_kvm --
-machine q35 -enable-kvm -cpu host -smp 4 -m 4096
-display none
-bios "$(nix build nixpkgs#OVMF.fd --no-link --print-out-paths)/FV/OVMF.fd"
-device virtio-scsi-pci,id=scsi0
-drive file=/tmp/sfax-local.qcow2,if=none,id=hd0,format=qcow2
-device scsi-hd,drive=hd0
-netdev user,id=net0,hostfwd=tcp::2222-:22,hostfwd=tcp::18123-:8123,hostfwd=tcp::18080-:8080
-device virtio-net-pci,netdev=net0
4. Sanity checks (in another terminal)
ssh -p 2222 aksiksi@localhost -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "hostname && systemctl is-active mosquitto home-assistant systemd-networkd && ip -br a" curl -sSI http://localhost:18123/ | head -n 5
第 4 步:在 Proxmox 上做实时切换
下面是切换的实际过程:
1. Copy qcow2 image to Proxmox
rsync -avh --progress --copy-links result/sfax.qcow2 root@192.168.0.100:/tmp/sfax.qcow2
2. Create VM 111 for staging (temporary MAC on VLAN 10 -> DHCP fallback)
ssh root@192.168.0.100 "qm create 111 --name sfax --ostype l26 --machine q35 --bios ovmf --efidisk0 vmpool:1,efitype=4m,pre-enrolled-keys=0 --scsihw virtio-scsi-single --cores 4 --sockets 1 --memory 4096 --agent 1 --net0 virtio,bridge=vmbr0,firewall=1,tag=10"
3. Import qcow2 onto vmpool (lands at vm-111-disk-1 since efidisk0 took disk-0)
ssh root@192.168.0.100 "qm importdisk 111 /tmp/sfax.qcow2 vmpool && rm -f /tmp/sfax.qcow2"
4. Attach disk and boot for staging
ssh root@192.168.0.100 "qm set 111 --scsi0 vmpool:vm-111-disk-1,iothread=1 --boot order=scsi0 && qm start 111"
(原文较长,此处译出前 6000 字,完整内容请点「打开原文」。)
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。
