# # nix build '.#base-image'
# raspi-image =
#   let
#     system = "aarch64-linux";
#   in
#   import "${nixpkgs}/nixos/lib/make-disk-image.nix" {
#     pkgs = nixpkgs.legacyPackages."${system}";
#     lib = nixpkgs.lib;
#     config =
#       (nixpkgs.lib.nixosSystem {
#         inherit system;
#         modules = [ ./images/raspi.nix ];
#       }).config;
#     format = "qcow2";
#     diskSize = 4096;
#     name = "raspi-image";
#   };
#
# base-image =
#   let
#     system = "x86_64-linux";
#   in
#   import "${nixpkgs}/nixos/lib/make-disk-image.nix" {
#     pkgs = nixpkgs.legacyPackages."${system}";
#     lib = nixpkgs.lib;
#     config =
#       (nixpkgs.lib.nixosSystem {
#         inherit system;
#         modules = [ ./images/configuration.nix ];
#       }).config;
#     format = "qcow2";
#     diskSize = 2048;
#     name = "base-image";
#   };
#

{
  config,
  pkgs,
  lib,
  ...
}:

{

  # Filesystems
  fileSystems."/" = {
    device = "/dev/disk/by-label/nixos";
    fsType = "ext4";
    autoResize = true;
  };
  # This configuration worked on 09-03-2021 nixos-unstable @ commit 102eb68ceec
  # The image used https://hydra.nixos.org/build/134720986

  boot = {
    kernelPackages = pkgs.linuxPackages_rpi4;
    tmpOnTmpfs = true;
    initrd.availableKernelModules = [
      "usbhid"
      "usb_storage"
    ];
    # ttyAMA0 is the serial console broken out to the GPIO
    kernelParams = [
      "8250.nr_uarts=1"
      "console=ttyAMA0,115200"
      "console=tty1"
      # A lot GUI programs need this, nearly all wayland applications
      "cma=128M"
    ];
  };

  # Openssh
  programs.ssh.startAgent = false;
  services.openssh = {
    enable = true;
    passwordAuthentication = false;
    startWhenNeeded = true;
    kbdInteractiveAuthentication = false;
    permitRootLogin = "yes";
  };

  boot.growPartition = true;

  boot.loader.raspberryPi = {
    enable = true;
    version = 4;
  };
  boot.loader.grub.enable = false;

  # Required for the Wireless firmware
  hardware.enableRedistributableFirmware = true;

  networking = {
    hostName = "nixos-raspi-4"; # Define your hostname.
    networkmanager = {
      enable = true;
    };
  };

  # Locale settings
  i18n.defaultLocale = "en_US.UTF-8";
  console = {
    font = "Lat2-Terminus16";
    keyMap = "colemak";
  };

  users = {
    users.root = {
      openssh.authorizedKeys.keyFiles = [
        (pkgs.fetchurl {
          url = "https://github.com/pinpox.keys";
          sha256 = "sha256-V0ek+L0axLt8v1sdyPXHfZgkbOxqwE3Zw8vOT2aNDcE=";
        })
      ];
    };
  };

  nix = {
    autoOptimiseStore = true;
    gc = {
      automatic = true;
      dates = "weekly";
      options = "--delete-older-than 30d";
    };
    # Free up to 1GiB whenever there is less than 100MiB left.
    extraOptions = ''
      min-free = ${toString (100 * 1024 * 1024)}
      max-free = ${toString (1024 * 1024 * 1024)}
    '';
  };
  system.stateVersion = "20.09";
}
