#  _____    _     _  ___  ___ 
# |_   _| _(_)_ _(_)/ _ \/ __|
#   | || '_| \ V / | (_) \__ \
#   |_||_| |_|\_/|_|\___/|___/
#                             
# Copyright (C) 2023 - Group 8

{ pkgs, modulesPath, ... }: {

  imports = [
    (modulesPath + "/installer/sd-card/sd-image-aarch64.nix")
    (modulesPath + "/profiles/minimal.nix")
    ./web.nix
  ];

  disabledModules = [
    (modulesPath + "/profiles/base.nix")
    (modulesPath + "/profiles/all-hardware.nix")
  ];

  system.stateVersion = "23.05";

  system.nixos = {
    distroId = "trivios";
    distroName = "TriviOS";
    variant_id = "rpi3";
  };
  sdImage.imageBaseName = "trivios-sd-image";

  nix.enable = false;

  hardware.enableRedistributableFirmware = true;
  environment.systemPackages = with pkgs; [
    libraspberrypi
    libgpiod
  ];

  networking = {
    hostName = "tripi";
    firewall.allowedTCPPorts = [ 80 ];

    wireless = {
      enable = true;
      environmentFile = "/var/lib/wlan.conf";
      networks."@WLAN_SSID@".psk = "@WLAN_PSK@";
    };
  };


  services.openssh = {
    enable = true;
    settings.PasswordAuthentication = true;
    settings.PermitRootLogin = "yes";
  };

  users.users."root".initialPassword = "password";


  boot = {
    # Doesn't work per https://github.com/NixOS/nixpkgs/issues/146199
    # loader.raspberryPi = {
    #   enable = true;
    #   version = 3;
    #   firmwareConfig = ''
    #     core_freq=250
    #     gpu_mem=256
    #   '';
    # };

    initrd.includeDefaultModules = false;
    kernelPackages = pkgs.linuxPackagesFor (pkgs.linux_rpi3.overrideDerivation (super: {
      buildFlags = super.buildFlags ++ [ "KBUILD_BUILD_VERSION=1-TriviOS" ];
    }));

    kernelParams = [
      "console=ttyS1,115200n8"
      "cma=256M"
    ];
  };

  services.timesyncd.enable = false;
  systemd.oomd.enable = false;
  systemd.coredump.enable = false;
}
