# Edit this configuration file to define what should be installed on
# your system. Help is available in the configuration.nix(5) man page, on
# https://search.nixos.org/options and in the NixOS manual (`nixos-help`).

{
  config,
  lib,
  pkgs,
  namespace,
  ...
}:
let
  user = "matt";
  # password = config.sops.secrets."pi5/matt-password".path;
  kernelBundle = pkgs.linuxAndFirmware.latest;
in
{
  imports = [
    ./boot.nix
    ./networking.nix
    ./services.nix
    ./sops.nix
  ];

  ${namespace} = {
    hardware.disko.enable = true;
    desktop.hyprland.enable = false;
    network = {
      hostName = "pi5";
    };
  };

  # Configure nixpkgs
  nixpkgs = {
    overlays = lib.mkAfter [
      (_self: _super: {
        # This is used in (modulesPath + "/hardware/all-firmware.nix") when at least
        # enableRedistributableFirmware is enabled
        # I know no easier way to override this package
        inherit (kernelBundle) raspberrypiWirelessFirmware;
        # Some derivations want to use it as an input,
        # e.g. raspberrypi-dtbs, omxplayer, sd-image-* modules
        inherit (kernelBundle) raspberrypifw;
      })
    ];
  };

  system.nixos.tags =
    let
      cfg = config.boot.loader.raspberry-pi;
    in
    [
      "raspberry-pi-${cfg.variant}"
      cfg.bootloader
      config.boot.kernelPackages.kernel.version
    ];

  systemd.services.btattach = {
    before = [ "bluetooth.service" ];
    after = [ "dev-ttyAMA0.device" ];
    wantedBy = [ "multi-user.target" ];
    serviceConfig = {
      ExecStart = "${pkgs.bluez}/bin/btattach -B /dev/ttyAMA0 -P bcm -S 3000000";
    };
  };

  environment = {
    systemPackages = with pkgs; [
      erofs-utils
      fex
      libraspberrypi
      raspberrypi-eeprom
      raspberrypifw
      raspberrypiWirelessFirmware
      raspberrypi-armstubs
      squashfuse
      squashfsTools
    ];
  };

  hardware.graphics.enable32Bit = lib.mkForce false;

  users = {
    mutableUsers = false;
    users."${user}" = {
      isNormalUser = true;
      #      hashedPasswordFile = password;
      password = lib.mkForce "BogieDudie1";
      extraGroups = [
        "wheel"
        "docker"
      ];
      openssh.authorizedKeys.keys = [
        # macBook
        "ssh-rsa AAAAB3NzaC1yc2EAAAADAQ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JcKw+WmizvTrxBmaMjWALlkYl+s= mattl@Jallen-PC"
        # desktop nixos
        "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPTBMydhOc6SnOdB5WrEd7X07DrboAtagCUgXiOJjLov matt@matt-nixos"
        "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOTha0FbV1tkpnJr7xVH78S5MetJH+0o2YrEcuvhL692 root@jallen-nas"
        "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIwoHWOLSTGVif9hAhaMLl0qDA4roIzCNuyR6kyIXDOj admin@jallen-nas"
      ];
      shell = pkgs.zsh;
    };
    users.root.shell = pkgs.zsh;
  };

  programs = {
    kdeconnect.enable = false;
  };

  zramSwap.enable = true;
}
