{ config, pkgs, inputs, ... }: let
in
  {
    imports = [
      # Install Apps Detactable by Spotlight
      # ./app-fix.nix
      # Tiling Window Manager
      ./yabai.nix
      # Shortcuts babe, not Automator
      ./skhd.nix
    ];
    nix.registry.nixpkgs.flake = inputs.nixpkgs;
    nixpkgs.config.allowUnfree = true;
    # nix.package = pkgs.nixUnstable;
    nix.settings.auto-optimise-store = false;
    nix.settings.trusted-users = [
      "root"
      "universelaptop"
    ];

    users.users.universelaptop = {
      name = "universelaptop";
      home = "/Users/universelaptop";
    };
    home-manager = {
      useGlobalPkgs = true;
      useUserPackages = true;
      users.universelaptop = { pkgs, ... }: {
        programs.direnv.enable = true;
        programs.direnv.nix-direnv.enable = true;
        programs.bash.enable = true;
        programs.zsh = {
          enable = true;
          initExtra = ''
            if [[ $(uname -m) == 'arm64' ]]; then
                 eval "$(/opt/homebrew/bin/brew shellenv)"
            fi
          '';
          shellAliases = {
            ssh = "kitty +kitten ssh";
            sshu = "ssh universe";
          };
        };
        home.stateVersion = "22.05";
        # TODO: https://nohzafk.github.io/posts/2023-12-18-nix-emacs-treesit-grammars/
        # home.file = {
        #   # tree-sitter subdirectory of the directory specified by user-emacs-directory
        #   ".config/emacs/.local/cache/tree-sitter".source =
        #     "${emacs29.treesitGrammars}/lib";
        # };
      };
    };

    fonts.packages = with pkgs; [
      julia-mono
      bqn386
    ];

    services.emacs = {
      # Emacsclient behaving weirdly
      # enable = true;
      package = pkgs.me-emacs;
    };

    environment.variables = {
      EDITOR = "emacs -nw";
      VISUAL = "emacs";
    };

    nixpkgs.overlays = [
      (final: prev: {
        skia-aseprite = prev.skia-aseprite.overrideAttrs (old: {
          postPatch = (old.postPatch or "") + ''
            sed -i -e 's/"-march=armv7-a",//g' gn/skia/BUILD.gn
          '';
          buildInputs = old.buildInputs ++ [
            final.xcbuild
            final.darwin.apple_sdk.frameworks.ApplicationServices
          ];
          meta.maintainers = with pkgs.lib.maintainers; [
            davidcromp
          ];
        });
        aseprite = prev.aseprite.overrideAttrs (old: {
          cmakeFlags = [
            "-DENABLE_DESKTOP_INTEGRATION=OFF"
            "-DENABLE_UPDATER=OFF"
            "-DUSE_SHARED_CMARK=ON"
            "-DUSE_SHARED_CURL=ON"
            "-DUSE_SHARED_FMT=ON"
            "-DUSE_SHARED_FREETYPE=ON"
            "-DUSE_SHARED_GIFLIB=ON"
            "-DUSE_SHARED_HARFBUZZ=ON"
            "-DUSE_SHARED_JPEGLIB=ON"
            "-DUSE_SHARED_LIBPNG=ON"
            "-DPNG_ARM_NEON:STRING=on"
            "-DUSE_SHARED_LIBWEBP=ON"
            "-DUSE_SHARED_PIXMAN=ON"
            "-DUSE_SHARED_TINYXML=ON"
            "-DUSE_SHARED_WEBP=ON"
            "-DUSE_SHARED_ZLIB=ON"
            # Disable libarchive programs.
            "-DENABLE_CAT=OFF"
            "-DENABLE_CPIO=OFF"
            "-DENABLE_TAR=OFF"
            # UI backend.
            "-DLAF_OS_BACKEND=skia"
            "-DLAF_WITH_EXAMPLES=OFF"
            "-DSKIA_DIR=${final.skia-aseprite}"
            "-DSKIA_LIBRARY_DIR=${final.skia-aseprite}/lib"
            "-DSKIA_LIBRARYR=${final.skia-aseprite}/lib/libskia.a"
          ];
          buildInputs = with pkgs; [
            cmark
            curl
            fmt
            fontconfig
            freetype
            giflib
            glib
            harfbuzzFull
            libjpeg
            libpng
            libwebp
            pcre2
            pixman
            skia-aseprite
            tinyxml-2
            zlib
            
            # MacOS:
          ] ++ (with darwin.apple_sdk.frameworks; [
            Cocoa
            QuickLook
            Quartz
          ]);
          
          hardeningDisable = [
            "format" # Darwin Specific
          ];
          meta.broken = false;
        });
      })
    ];
    
    environment.systemPackages = (with pkgs; [
      me-emacs
      svgbob
      (aspellWithDicts (p: with p;[
        en
        en-computers
        en-science
      ]))
      pandoc
      texliveFull
      wget

      #xquartz
      kitty
      
      btop

      remmina
      
      cbqn-replxx

      # Graphical Apps:
      rectangle
      tailscale
      discord
      slack
      gimp
      # aseprite
      # TODO: Consider using Yabai??
      
      # Games
      prismlauncher
    ]);

    homebrew = {
      # It is not installed by nix, for now, so manually install:
      # https://brew.sh/
      enable = true;
      onActivation = {
        cleanup = "uninstall";
      };
      # Graphical apps to install through homebrew instead of nixpkgs
      casks = [
        "firefox"
        "nextcloud"
        "tidal"
        "bitwarden"
        "zoom-for-it-admins"
        "libreoffice"
        "tailscale"
        "godot"
        "pixelorama"
        "blender"
        "inkscape"
      ];
      # Apps to install outside of nix for when wanting to test/run
      # things that... aren't used by other people who use nix, like
      # python apps (which is why poetry is there)
      brews = [
        "poetry"
        "pdm"
        "python@3.11"
        "python@3.10"
        "pygments"
        # sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk \
	# /Library/Java/JavaVirtualMachines/openjdk.jdk
        "openjdk"
      ];
    };

    # Create /etc/zshrc that loads the nix-darwin environment.
    programs.bash.enable = true;  # default shell on catalina
    programs.zsh.enable = true;  # default shell on catalina

    # Used for backwards compatibility, please read the changelog before changing.
    # $ darwin-rebuild changelog
    system.stateVersion = 4;
    system.primaryUser = "universelaptop";
  }
