# SPDX-FileCopyrightText: 2020 Luke Granger-Brown <depot@lukegb.com>
#
# SPDX-License-Identifier: Apache-2.0

{ depot, lib, pkgs, config, ... }:
let
  inherit (depot.ops) secrets;
in {
  imports = [
    ../../../third_party/nixpkgs/nixos/modules/installer/netboot/netboot-minimal.nix
  ];

  environment.noXlibs = false;
  services.getty.autologinUser = lib.mkForce "root";
  systemd.services.sshd.wantedBy = lib.mkOverride 0 [ "multi-user.target" ];
  boot.kernelPackages = pkgs.linuxPackages_latest;

  environment.systemPackages = with depot.pkgs; [
    mprime
    hp-rom
  ];

  system.build.netbootWrap = pkgs.symlinkJoin {
    name = "netboot";
    paths = with config.system.build; [
      netbootRamdisk
      kernel
      netbootIpxeScript
    ];
  };
  system.build.pixiecore = pkgs.writeScript "netboot-pixiecore.sh" ''
    n="$(realpath ${config.system.build.netbootWrap})"
    init="$(grep -ohP 'init=\S+' "$n/netboot.ipxe")"
    pixiecore="${pkgs.pixiecore}"

    exec "$pixiecore/bin/pixiecore" \
      boot "$n/bzImage" "$n/initrd" \
      --cmdline "$init loglevel=4" \
      --debug --dhcp-no-bind --port 64172 --status-port 64172
  '';

  system.stateVersion = "21.05";
}