# 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’).

{ lib, config, pkgs, inputs, ... }: {
    nix.settings.auto-optimise-store = true;
    nix.registry.nixpkgs.flake = inputs.nixpkgs;

    # Allow user-mode qemu for running riscv64 binaries
    # boot.binfmt.emulatedSystems = [ "riscv64-linux" ];

    imports =
      [ # Include the results of the hardware scan.
        ./hardware-configuration.nix

        # Desktop/Graphical Things defined:
        ./desktop/default.nix
        ./fonts.nix
        
        # Secrets specified via:
        ./secrets.nix

        ./programs/art.nix
        ./programs/audio.nix
        ./programs/cad.nix
        ./programs/desktop.nix
        ./programs/games.nix
        ./programs/office.nix
        ./programs/programming.nix
        ./programs/terminal.nix
        ./programs/video.nix
        ./programs/remote.nix
        ./printing.nix

        # ccache, y'know for blenders and whatnots
        ./ccache.nix

        # So we can run riscv 'natively'
        ./riscv.nix
      ];

    fileSystems."/home/universe/tmp" = {
      fsType = "tmpfs";
    };
    fileSystems."/home/universe/Downloads" = {
      fsType = "tmpfs";
    };

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

    networking.hostName = "universedesktop";
 
    networking.useDHCP = false;
    networking.interfaces.wlp6s0.useDHCP = true;
    # Fixes DNS issue with tailscale: https://github.com/tailscale/tailscale/issues/4254
    #services.resolved.enable = true;
    networking.resolvconf.enable = true;

    sops.secrets.wireless = { 
        owner = "wpa_supplicant"; # TODO Use systemd service variable
    };
    networking.wireless.secretsFile = config.sops.secrets.wireless.path;

    networking.wireless.enable = true;
    networking.wireless.networks = {
      "BELL422 5G".pskRaw = "ext:PSK_HOME";
      "BELL422".pskRaw = "ext:PSK_HOME";
    };

    hardware.bluetooth.enable = true;

    # Set your time zone.
    time.timeZone = "America/Toronto";
    # Configure network proxy if necessary
    # networking.proxy.default = "http://user:password@proxy:port/";
    # networking.proxy.noProxy = "127.0.0.1,localhost,internal.domain";

    # Select internationalisation properties.
    i18n.defaultLocale = "en_US.UTF-8";
    #console = {
    #  font = "Lat2-Terminus16";
    #  keyMap = "us";
    #  useXkbConfig = true; # use xkbOptions in tty.
    #};

    services.brltty = {
      enable = true;
    };

    users.users.brltty.isSystemUser = true;
    
    # Enable touchpad support (enabled default in most desktopManager).
    # services.xserver.libinput.enable = true;
    users.groups.net = {};
    users.users.universe = {
      isNormalUser = true;
      extraGroups = [ "wheel" "adbusers" "net" "brltty" "brlapi" "dialout" "input" ];
      packages = with pkgs; [
      ];
    };

    nix = {
      package = pkgs.nixVersions.stable; # or versioned attributes like nixVersions.nix_2_8
      extraOptions = ''
      keep-outputs = true
      keep-derivations = true
      experimental-features = nix-command flakes
    '';
    };  

    environment.pathsToLink = [
      "/share/nix-direnv"
    ];
    
    nixpkgs.config.allowUnfree = true;
    services.xserver.videoDrivers = [ "nvidia" ];
        
    services.fwupd.enable = true;
        
    # GTK Support
    programs.dconf.enable = true;

    # Legacy Python2 Support
    nixpkgs.config.permittedInsecurePackages = [
      "python-2.7.18.6"

      # BitWarden uses this
      "nodejs-16.20.0"
    ];
    
    # List packages installed in system profile. To search, run:
    # $ nix search wget
    environment.systemPackages = with pkgs; [
      direnv
      nix-direnv
      android-tools
    ];

    documentation.dev.enable = true;
    documentation.man.generateCaches = false;
    
    # 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.openssh.ports = [7000];
    services.openssh.settings.X11Forwarding = true;
    # Tailscale
    services.tailscale = {
      enable = true;
    };
    
    # Open ports in the firewall.
    # networking.firewall.allowedTCPPorts = [ ... ];
    # networking.firewall.allowedUDPPorts = [ ... ];
    # Or disable the firewall altogether.
    networking.firewall.enable = false;

    # Copy the NixOS configuration file and link it from the resulting system
    # (/run/current-system/configuration.nix). This is useful in case you
    # accidentally delete configuration.nix.
    # system.copySystemConfiguration = true;

    # 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 = "22.05"; # Did you read the comment?
  }
