# Hardware settings for Teclast F5 10" Laptop
# NixOS @ sda2
#
# flake.nix
#  └─ ./hosts
#      └─ ./laptop
#          └─ hardware-configuration.nix *
#
# Do not modify this file!  It was generated by ‘nixos-generate-config’
# and may be overwritten by future invocations.  Please make changes
# to /etc/nixos/configuration.nix instead.
{ config, lib, pkgs, modulesPath, ... }:

{
  imports =
    [ (modulesPath + "/profiles/qemu-guest.nix")
    ];

  boot.initrd.availableKernelModules = [ "xhci_pci" "ahci" "nvme" "usb_storage" "usbhid" "sd_mod" ];
  boot.initrd.kernelModules = [ "vfio_pci" "vfio" "vfio_iommu_type1" ];
  boot.initrd.secrets = {
    "/root/NASKeyfile" =
        /root/NASKeyfile;
  };
  boot.initrd.luks.devices = {
    NAS-RAID1 = {
      device = "/dev/disk/by-uuid/57e6446d-faca-4b67-9063-e8d9afb80088";
      keyFile = "/root/NASKeyfile";
      bypassWorkqueues = true;
    };
    NAS-RAID2 = {
      device = "/dev/disk/by-uuid/b9edc489-ac37-4b28-981d-442722df7ae2";
      keyFile = "/root/NASKeyfile";
      bypassWorkqueues = true;
    };
  };
  boot.kernelModules = [ "kvm-intel" ];
  boot.extraModulePackages = [ ];
  boot.tmp.useTmpfs = false;
  boot.tmp.cleanOnBoot = true;
  zramSwap.enable = true;

  services.btrfs.autoScrub = {
    enable = true;
    interval = "monthly";
    fileSystems = [
      "/"
      "/mnt/nas"
    ];
  };

  services.btrbk = {
      extraPackages = [ pkgs.lz4 pkgs.mbuffer ];
      instances = {
          hf = {
              onCalendar = "hourly";
              settings = {
                  incremental = "yes";
                  snapshot_create = "ondemand";
                  snapshot_dir = "@snapshots";
                  timestamp_format = "long";

                  snapshot_preserve = "2m 2w 5d 5h";
                  snapshot_preserve_min = "latest";
                
                  volume =  {
                      "/mnt/snapshots/root" = {
                          snapshot_create = "always";
                          subvolume = {
                              "@" = {};
                              "@home" = {};
                          };
                      };
                  };
              };
          };
          bak = {
              onCalendar = "weekly";
              settings = {
	          stream_buffer = "265m";
                  stream_compress = "lz4";
                  incremental = "yes";
                  snapshot_create = "no";
                  snapshot_dir = "@snapshots";
                  timestamp_format = "long";

                  snapshot_preserve_min = "all";
                  target_preserve_min = "no";
                  target_preserve = "4w 2m";
                  archive_preserve_min = "no";
                  archive_preserve = "4w 2m";

                  ssh_identity = "/etc/btrbk/ssh/id_ed25519_btrbk";
                  ssh_user = "btrbk";
                
                  volume =  {
                      "ssh://jupiter.home.opel-online.de:2220/mnt/snapshots/Mars" = {
                          subvolume = {
                              "@nas" = {
                                  target = "/mnt/nas/Backups/Mars";
			      };
                              "@hades/@home" = {
				  target = "/mnt/nas/Backups/Hades";
				  snapshot_dir = "@snapshots/@hades";
			      };
                              "@lifebook/@home" = {
				  target = "/mnt/nas/Backups/Lifebook";
				  snapshot_dir = "@snapshots/@lifebook";
			      };
#                              "@steamdeck/@home" = {
#				  target = "/mnt/nas/Backups/Steamdeck";
#				  snapshot_dir = "@snapshots/@steamdeck";
#			      };
                          };
                      };
                  };
                  volume =  {
                      "ssh://jupiter.home.opel-online.de:2220/mnt/snapshots/Pluto" = {
                          target = "/mnt/nas/Backups/Pluto";
                          subvolume = {
                              "@/Games" = {};
                              "@/IT" = {};
                              "@/Media" = {};
                              "@/Pictures" = {};
                              "@/Rest" = {};
                          };
                      };
                  };
              };
          };
      };
  };

  systemd.services = {
      btrbk-bak = {
          after = [ "network-online.target" ];
          requires = [ "network-online.target" ];
      };
  };


  fileSystems."/" =
    { device = "/dev/disk/by-label/NIXROOT";
      fsType = "btrfs";
      options = [ "compress=zstd,space_cache=v2,ssd,noatime,subvol=@,discard=async" ];
    };

  fileSystems."/home" =
    { device = "/dev/disk/by-label/NIXROOT";
      fsType = "btrfs";
      options = [ "compress=zstd,space_cache=v2,ssd,noatime,subvol=@home,discard=async" ];
    };

  fileSystems."/srv" =
    { device = "/dev/disk/by-label/NIXROOT";
      fsType = "btrfs";
      options = [ "compress=zstd,space_cache=v2,ssd,noatime,subvol=@srv,discard=async" ];
    };

  fileSystems."/nix" =
    { device = "/dev/disk/by-label/NIXROOT";
      fsType = "btrfs";
      options = [ "compress=zstd,space_cache=v2,ssd,noatime,subvol=@nix,discard=async" ];
    };

  fileSystems."/swap" =
    { device = "/dev/disk/by-label/NIXROOT";
      fsType = "btrfs";
      options = [ "compress=zstd,space_cache=v2,ssd,noatime,subvol=@swap,discard=async" ];
    };

  fileSystems."/mnt/snapshots/root" =
    { device = "/dev/disk/by-label/NIXROOT";
      fsType = "btrfs";
      options = [ "compress=zstd,space_cache=v2,ssd,noatime,subvolid=5,discard=async" ];
  };

#  fileSystems."/mnt/snapshots/Pluto" =
#    { device = "/dev/disk/by-label/NAS-RAID";
#      fsType = "btrfs";
#      options = [ "compress=zstd,space_cache=v2,noatime,subvolid=5" ];
#    };
#
  fileSystems."/mnt/nas" =
    { device = "/dev/disk/by-uuid/70523c79-ef5c-40f2-8782-60fc86bb445b";
      fsType = "btrfs";
      options = [ "compress=zstd:9,space_cache=v2,noatime,subvol=@nasbak" ];
    };

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

  swapDevices = [ { device = "/swap/swapfile"; } ];
  
  systemd.network = {
      enable = true;
      networks = {
          "10-lan" = {
              matchConfig.Name = "enp6s18";
              ntp = [ "192.168.2.1" ];
              #domains = [ "home.opel-online.de" ];
              networkConfig = {
                DHCP = "yes";
                IPv6AcceptRA = true;
              };
          };
      };
  };
  networking = {
    hostName = "nasbak";
    domain = "home.opel-online.de";
    useDHCP = false;                     # For versatility sake, manually edit IP on nm-applet.
    #firewall = {
    #  enable = false;
    #  #allowedUDPPorts = [ 53 67 ];
    #  #allowedTCPPorts = [ 53 80 443 9443 ];
    #};
  };

  hardware.cpu.intel.updateMicrocode = lib.mkDefault config.hardware.enableRedistributableFirmware;
  powerManagement = {
    cpuFreqGovernor = lib.mkDefault "powersave";
    powertop.enable = true;
#    powerUpCommands = ''
#      ${pkgs.hdparm}/sbin/hdparm -S 150 /dev/disk/by-uuid/57e6446d-faca-4b67-9063-e8d9afb80088
#      ${pkgs.hdparm}/sbin/hdparm -S 150 /dev/disk/by-uuid/b9edc489-ac37-4b28-981d-442722df7ae2
#    '';
  };
  
}