# 1) partition + fs
nix run nixpkgs#parted -- -s /dev/vda mklabel msdos mkpart primary ext4 1MiB 100% set 1 boot on

#fdisk /dev/vda <<'EOF'
#o
#n
#p
#1
#
# 
#a
#w
#EOF

mkfs.ext4 -L nixos /dev/vda1

# 2) mount target
mkdir /mnt
mount /dev/vda1 /mnt

# 3) generate config
nixos-generate-config --root /mnt

# 4) edit config to enable GRUB on the disk (manual step)
# open an editor and ensure these lines exist:
#   boot.loader.grub.enable = true;
#   boot.loader.grub.devices = [ "/dev/vda" ];

nvim /mnt/etc/nixos/configuration.nix

# 5) install (will prompt for root password)
export NIX_BUILD_CORES=2
export NIX_MAX_JOBS=2
nixos-install --root /mnt --flake /mnt/vproxy#master_x86_64-linux --impure --option sandbox false --option cores 2

#nixos-install
#nixos-install --root /mnt --flake /mnt/vproxy#master_x86_64-linux --impure

# 6) reboot into the installed system
reboot


###### otheway

echo '{pkgs, ...}: let
  vmIp      = "147.45.116.17";
  vmGateway = "147.45.116.1";
in {
  nix.settings.experimental-features = "nix-command flakes";
  networking.firewall.allowedTCPPorts = [ 80 443 ];
  environment.systemPackages = with pkgs; [ neovim ];
  networking.useDHCP = false;
  networking.interfaces.ens3 = {
    ipv4.addresses = [
      { address = vmIp; prefixLength = 24; }
    ];
  };
  networking.defaultGateway = vmGateway;
  networking.nameservers = [ "1.1.1.1" "8.8.8.8" ];
  users.users.root.openssh.authorizedKeys.keys = [
    "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBsYIQVwnVi/zoqHvuXNXb9qcGsv9q3rY7z7gHmLPfI4"
  ];
}' > /tmp/settings.nix
curl -O https://raw.githubusercontent.com/elitak/nixos-infect/69819871dcced600959ab1c875702acbe6dccc29/nixos-infect && env NIXOS_IMPORT=/tmp/settings.nix NIX_CHANNEL=nixos-25.05 bash nixos-infect 2>&1 | tee /tmp/infect.log
