diff --git a/modules/nixos/homelab/services.nix b/modules/nixos/homelab/services.nix index 1bbdff3..934ea12 100644 --- a/modules/nixos/homelab/services.nix +++ b/modules/nixos/homelab/services.nix @@ -2,16 +2,14 @@ { config, pkgs, - ... + ... }: { arrSuite.enable = true; services.samba.shares.Media.path = "/media/storage/media"; - virtualisation.incus.enable = true; - - + # virtualisation.incus.enable = true; users.users.audiobookshelf = { isSystemUser = true; @@ -20,8 +18,6 @@ users.groups.media_services = { }; - - services.audiobookshelf = { enable = true; user = "audiobookshelf"; @@ -30,8 +26,6 @@ port = 13378; }; - - users.users.qbittorrent.extraGroups = [ "media_services" ]; users.groups.qbittorrent = { }; @@ -69,7 +63,10 @@ image = "sigoden/dufs:latest"; ports = [ "5000:5000" ]; volumes = [ "/media/storage/media/:/data" ]; - cmd = [ "/data" "-A" ]; + cmd = [ + "/data" + "-A" + ]; }; c2c-scraper = { image = "death916/c2cscrape:latest"; @@ -77,7 +74,9 @@ "/media/storage/media/books/audio/podcasts/C2C:/downloads" "/media/storage/media/docker/volumes/c2cscrape:/app/data" ]; - environment = { TZ = "America/Los_Angeles"; }; + environment = { + TZ = "America/Los_Angeles"; + }; autoStart = true; extraOptions = [ "--dns=8.8.8.8" ]; }; @@ -101,7 +100,10 @@ Type = "oneshot"; User = "root"; ExecStart = "/usr/local/bin/nixos-kopia-backup.sh"; - path = with pkgs; [ coreutils kopia ]; + path = with pkgs; [ + coreutils + kopia + ]; }; }; @@ -126,6 +128,21 @@ security.sudo.wheelNeedsPassword = true; + virtualisation.libvirtd = { + enable = true; + qemu = { + ovmf = true; # UEFI support for HAOS + }; + }; + + services.homeassistant-vm = { + enable = false; + imagePath = "/var/lib/libvirt/images/haos.qcow2"; + memory = 6096; + vcpus = 4; + bridge = "br0"; + }; + environment.systemPackages = with pkgs; [ git vim @@ -145,5 +162,8 @@ manix nh qemu + virt-install + virt-manager + usbutils ]; } diff --git a/modules/nixos/homelab/user.nix b/modules/nixos/homelab/user.nix index c28c2b5..cdba043 100644 --- a/modules/nixos/homelab/user.nix +++ b/modules/nixos/homelab/user.nix @@ -3,7 +3,7 @@ config, pkgs, primaryUser, - ... + ... }: { @@ -11,7 +11,15 @@ isNormalUser = true; home = "/home/${primaryUser}"; description = "${primaryUser}"; - extraGroups = [ "wheel" "media_services" "nextcloud" "docker" "qbittorent" "incus-admin" ]; + extraGroups = [ + "wheel" + "media_services" + "nextcloud" + "docker" + "qbittorent" + "incus-admin" + "libvirtd" + ]; openssh.authorizedKeys.keys = [ "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCte9KjJUSn4xBPEKCk9QER6+jF+C0uBatVl27zIamYsryyHdFrmqK2DAg7OhqTHqzjxID6sp6d57MsJqOUAtwXbUDMLffqBSerUxfTm+1SPDrhL0GSvo0QVjMLVseOEq8d2qXgW1R7dIk412VbO5e9SAump5aJOHI/SzT6VLoUczalmqrjnDacWQMeLu/TSOZHcfrhjYSg+b1xbc1vHp6C4obOb8JIj/anAieT/1P36MhlNW79ow6PWenLemBYeeezFrKtESF1oMc8jmcxogzgLamlqhKYAHlKhOuBF6u0nRneI5IPDbbMF5zwEv5szCEKj8XZJVYUk8uUg7ARyppjcA7yAXuaNKBNxa7tfjqWrDWOACn97ufE5FFJt0XH5JzkXcDh96K8ZSZaWxMRu2s+GlIu/1F415xtVfe1d79HYkWke/ewaQ4NqgOt8f7wRvyzabpQZDzkaXO0UoK65O2HyUur33XWCEmV+1pB6BrS8pD+1I4Tvbnc+rOgtHTTRfKqezKqZmaErEOxClBwvWjvn0PzhGSoClTGXPjhl239/sH0JGY09dTBh8GtAVbfv+jFO6nm6aR7O/OwSaohY3uOdRo8XyxJr4XyGAaBNRdm6BUJRnB4W51J49IQBZzIe2NUkNMHeUT4jkxFpfhkujnSFw2ZnOLkERpwkltAlbwuLw== tavn1992@gmail.com" ]; diff --git a/modules/vms/ha-control-script.nix b/modules/vms/ha-control-script.nix new file mode 100644 index 0000000..6d6f3ca --- /dev/null +++ b/modules/vms/ha-control-script.nix @@ -0,0 +1,31 @@ +{ pkgs, ... }: + +{ + environment.systemPackages = [ + (pkgs.writeShellScriptBin "haos" '' + VM_NAME="homeassistant" + case "$1" in + start) virsh start "$VM_NAME" ;; + stop) virsh shutdown "$VM_NAME" ;; + status) virsh list --all | grep "$VM_NAME" ;; + ip) virsh domifaddr "$VM_NAME" | awk '/ipv4/ {print $4}' | cut -d/ -f1 ;; + console) virsh console "$VM_NAME" ;; + destroy) + echo "This will permanently delete the VM. Are you sure? (y/N)" + read -r confirmation + if [[ "$confirmation" =~ ^[Yy]$ ]]; then + virsh destroy "$VM_NAME" || true + virsh undefine "$VM_NAME" --remove-all-storage || true + echo "VM destroyed." + else + echo "Destruction cancelled." + fi + ;; + *) + echo "Usage: haos {start|stop|status|ip|console|destroy}" + ;; + esac + '') + ]; +} + diff --git a/modules/vms/ha-deploy-script.nix b/modules/vms/ha-deploy-script.nix new file mode 100644 index 0000000..66c5884 --- /dev/null +++ b/modules/vms/ha-deploy-script.nix @@ -0,0 +1,43 @@ +{ config, pkgs, ... }: + +let + cfg = config.services.homeassistant-vm; +in +{ + environment.systemPackages = [ + (pkgs.writeShellScriptBin "deploy-haos" '' + set -e + IMAGE="${cfg.imagePath}" + VM_NAME="homeassistant" + BRIDGE="${cfg.bridge}" + MEM_MB="${toString cfg.memory}" + VCPUS="${toString cfg.vcpus}" + + if [ ! -f "$IMAGE" ]; then + echo "Error: HAOS image not found at $IMAGE" + exit 1 + fi + + if virsh list --all | grep -q " $VM_NAME "; then + echo "VM $VM_NAME already exists" + exit 0 + fi + + virt-install \ + --name "$VM_NAME" \ + --memory "$MEM_MB" \ + --vcpus "$VCPUS" \ + --import \ + --disk path="$IMAGE",format=qcow2,bus=virtio \ + --network bridge="$BRIDGE",model=virtio \ + --os-variant generic \ + --graphics none \ + --noautoconsole \ + --boot uefi + + echo "Home Assistant VM deployed!" + echo "Get IP with: haos ip" + '') + ]; +} + diff --git a/modules/vms/home-assistant.nix b/modules/vms/home-assistant.nix deleted file mode 100644 index b812140..0000000 --- a/modules/vms/home-assistant.nix +++ /dev/null @@ -1,67 +0,0 @@ -# /home/death916/nixconfig/modules/vms/home-assistant.nix -{ config, pkgs, ... }: - -let - # The incus-migrate tool is a separate package that we need to reference. - incus-migrate = pkgs.incus-migrate; - - # The path to your HAOS image, as requested. - haos-image-path = "/home/death916/incus/haos_ova-15.2.qcow2"; - - # This script will run on boot to ensure the VM exists. - # It uses incus-migrate with a configuration file. - setupScript = pkgs.writeShellScript "ha-vm-setup.sh" '' - set -e - INCUS_CMD="${pkgs.incus}/bin/incus" - VM_NAME="home-assistant" - - # 1. Check if the VM already exists. If so, do nothing. - if $INCUS_CMD info "''${VM_NAME}" >/dev/null 2>&1; then - echo "VM ''${VM_NAME} already exists. Ensuring it is running." - $INCUS_CMD start "''${VM_NAME}" - exit 0 - fi - - # 2. If the VM does not exist, create it using incus-migrate. - echo "Creating HAOS VM (''${VM_NAME}) with incus-migrate..." - - # The configuration for incus-migrate, passed via stdin. - # This tells the tool what to do non-interactively. - cat <