{fi,}{sh,{ni,}x{es,}}
# Edit this configuration file to define what should be installed on
# your system.  Help is available in the configuration.nix(5) man page
# and in the NixOS manual (accessible by running ‘nixos-help’).

{ config, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
      ./modules/profiles/all-hardware.nix
      ./modules/installer/scan/detected.nix
      ./modules/installer/scan/not-detected.nix
      # ./modules/installer/cd-dvd/installation-cd-graphical-plasma5-new-kernel.nix
    ];

  # Use the systemd-boot EFI boot loader.
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  # boot.kernelPackages = pkgs.linuxPackages_latest;

  boot.kernelPackages = pkgs.linuxPackagesFor (pkgs.linux_5_11.override {
    argsOverride = rec {
      src = pkgs.fetchurl {
            url = "mirror://kernel/linux/kernel/v5.x/linux-${version}.tar.xz";
            sha256 = "0yvgkc1fmmd4g06sydn51q4l3g5785q9yaaq04lv3kgj4hyijqgs";
      };
      version = "5.11.13";
      modDirVersion = "5.11.13";
      };
  });

  # boot.initrd.availableKernelModules = [ "ahci" "ata_piix" "ehci_pci" "mptspi" "nvme" "sd_mod" "sr_mod" "uhci_hcd" "usbhid" "usb_storage" "xhci_pci" ];
  # boot.initrd.kernelModules = [ "amdgpu" ];
  boot.kernelModules = [ "kvm-amd" ];
  boot.initrd.availableKernelModules = [ "amdgpu" "vfio-pci" ];
  boot.initrd.preDeviceCommands = ''
    DEVS="0000:0d:00.0 0000:0d:00.1"
    for DEV in $DEVS; do
      echo "vfio-pci" > /sys/bus/pci/devices/$DEV/driver_override
    done
    modprobe -i vfio-pci
  '';
  boot.kernelParams = [ "amd_iommu=on" "pcie_aspm=off" ];

  # 0000:0a:00.0
  # 0000:0a:00.1
  #
  # 0000:0d:00.0
  # 0000:0d:00.1

  # networking.hostName = "nixos"; # Define your hostname.
  # networking.wireless.enable = true;  # Enables wireless support via wpa_supplicant.

  # Set your time zone.
  time.timeZone = "Atlantic/Reykjavik";

  # The global useDHCP flag is deprecated, therefore explicitly set to false here.
  # Per-interface useDHCP will be mandatory in the future, so this generated config
  # replicates the default behaviour.
  networking.useDHCP = true;
  # networking.interfaces.ens33.useDHCP = true;
  # networking.interfaces.enp6s0.useDHCP = true;

  systemd.network.links."10-macfix" = {
    matchConfig.MACAddress = "04:d4:c4:57:dd:a3";
    linkConfig.MACAddress = "00:0c:29:12:9b:37";
  };

  systemd.services.minecraftserver = {
    description = "Minecraft Server";
    serviceConfig = {
      User = "xlefr";
      ExecStart = "/run/current-system/sw/bin/env DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 /run/current-system/sw/bin/docker-compose -f docker-compose.yml up";
      ExecStartPre="/run/current-system/sw/bin/env DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 /run/current-system/sw/bin/docker-compose -f docker-compose.yml down";
      ExecStop="/run/current-system-sw/bin/env DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 /run/current-system/sw/bin/docker-compose -f docker-compose.yml down";
      WorkingDirectory = "/leikir/minecraftserver";
      Restart = "always";
    };
    wantedBy = [ "multi-user.target" ];
    requires = [ "docker" ];
  };
  systemd.services.minecraftserver.enable = true;


  # Select internationalisation properties.
  # i18n.defaultLocale = "en_US.UTF-8";
  console = {
    font = "uni-vga";
    keyMap = "us";
  };

  # Enable the Plasma 5 Desktop Environment.
  services.xserver.enable = true;
  services.xserver.videoDrivers = [ "amdgpu" "vmware" ];
  services.xserver.displayManager.sddm.enable = true;
  services.xserver.displayManager.autoLogin.enable = true;
  services.xserver.displayManager.autoLogin.user = "xlefr";
  # services.xserver.displayManager.wdm.enable = true;
  services.xserver.desktopManager.plasma5.enable = true;


  virtualisation.vmware.guest.enable = false;


  # virtualisation.vmware.guest.enable = true;
  virtualisation.docker.enable = true;

#   hardware.enableRedistributableFirmware = true;
#   hardware.opengl.enable = true;
#   hardware.opengl.driSupport = true;

  # Configure keymap in X11
  # services.xserver.layout = "us";
  # services.xserver.xkbOptions = "eurosign:e";

  # Enable CUPS to print documents.
  # services.printing.enable = true;

  # Enable sound.
  # sound.enable = true;
  # hardware.pulseaudio.enable = true;

  # Enable touchpad support (enabled default in most desktopManager).
  # services.xserver.libinput.enable = true;

  # Define a user account. Don't forget to set a password with ‘passwd’.
  users.users.xlefr = {
    isNormalUser = true;
    shell=pkgs.fish;
    extraGroups = [ "wheel" "docker" "libvirtd" ]; # Enable ‘sudo’ for the user.
  };
  users.users.root = { shell = pkgs.fish; };

  nixpkgs.config.allowUnfree = true;

  # List packages installed in system profile. To search, run:
  # $ nix search wget
  environment.systemPackages = with pkgs; [
    wget vim
    firefox
    fish
    lsof neovim emacs gpm killall tree ag file vim uni-vga git
    ultimate-oldschool-pc-font-pack
    ansifilter
    open-vm-tools
    dstat gparted dosfstools mtools
    lshw
    pciutils
    gksu
    vimpager
    python38Packages.pygments
    cataclysm-dda-git ncurses SDL2 SDL2_image SDL2_ttf SDL2_mixer
    mesa-demos
    glmark2
    gnumake ccache clang_11 colormake cmake
    lldb_11
    jetbrains.clion
    jetbrains.idea-ultimate
    jetbrains.jdk
    atool unzip
    nixFlakes
    colordiff wdiff
    direnv
    docker docker-compose ngrok
    yarn
    nodejs_latest
    minecraft
    keybase keybase-gui kbfs
    jq yq yq-go go-pup
    tree-sitter
    bat bat-extras.batdiff bat-extras.batgrep bat-extras.batman bat-extras.batwatch bat-extras.prettybat
    cargo rustup # rustc
    kdeApplications.kmix
    llvmPackages.bintools
    avahi
    zerotierone
    steam
    zstd lz4
    groff mandoc # for fish help formatting
    atop htop iftop iotop glances # latencytop
    # jetbrains.jdk
    # openjdk14 # tries to build java 12? - and fails
    # jdk14 # this one too
    adoptopenjdk-hotspot-bin-13
    virt-manager
    ruby jruby
    go
    mosh
    pijul
    xmrig monero monero-gui
    ethash # ethminer
    bc
    squashfsTools
    kate xfce.mousepad gnome3.gedit
    # beep
    openssl
  ];


  hardware.opengl.extraPackages = with pkgs; [
    rocm-opencl-icd
    rocm-opencl-runtime
    amdvlk
  ];
  hardware.opengl.driSupport = true;


  virtualisation.libvirtd = {
    enable = true;
    qemuOvmf = true;
    qemuRunAsRoot = false;
    onBoot = "ignore";
    onShutdown = "shutdown";
  };
  programs.dconf.enable = true;

  nixpkgs.config.permittedInsecurePackages = [
    "adoptopenjdk-hotspot-bin-13.0.2"
  ];

  programs.steam.enable = true;

  sound.enable = true;

  services.keybase.enable = true;
  services.kbfs.enable = true;

  fonts.fonts = with pkgs; [
    ultimate-oldschool-pc-font-pack
    barlow
    bakoma_ttf
    cm_unicode
    cherry
    dosemu_fonts
    jost
    league-of-moveable-type
    libre-caslon
    medio
    mno16
    monoid
    meslo-lg
    overpass
    proggyfonts
    route159
    siji
    spleen
    theano
    unscii
    vegur
    aileron
    source-code-pro
  ];

  # Some programs need SUID wrappers, can be configured further or are
  # started in user sessions.
  # programs.mtr.enable = true;
  # programs.gnupg.agent = {
  #   enable = true;
  #   enableSSHSupport = true;
  # };

  # List services that you want to enable:

  # Enable the OpenSSH daemon.
  services.openssh.enable = true;

  services.zerotierone.enable = true;
  services.zerotierone.joinNetworks = [];
  services.zerotierone.port = 9993;

  # Open ports in the firewall.
  networking.firewall.allowedTCPPorts = [ 8080 ];
  # networking.firewall.allowedUDPPorts = [ ... ];
  # Or disable the firewall altogether.
  # networking.firewall.enable = false;

  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It‘s perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  # Before changing this value read the documentation for this option
  # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
  system.stateVersion = "20.09"; # Did you read the comment?


  # some hardware config that lives in in configuration.nix which is probably fine?

  fileSystems."/" =
    { device = "/dev/md/nixos:root";
      fsType = "ext4";
    };

  fileSystems."/boot" =
    { device = "/dev/disk/by-label/boot";
      fsType = "vfat";
    };

  swapDevices = [ {device = "/swapfile"; } ];

}