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

with lib;

let
  # Extract the set-environment path that nix-darwin generates
  setEnvironmentPath = "${config.system.build.setEnvironment}";
in
{
  config = {
    # Salt manages /etc/bashrc, /etc/zshrc, /etc/zshenv
    # nix-darwin writes to .local variants for nix-specific configuration

    # Disable nix-darwin from managing the main shell files
    environment.etc."bashrc".enable = false;
    environment.etc."zshrc".enable = false;
    environment.etc."zshenv".enable = false;
    environment.etc."zprofile".enable = false;

    # Create .local files with nix environment setup
    environment.etc."bash.local".text = ''
      # Nix environment setup
      if [ -z "$__NIX_DARWIN_SET_ENVIRONMENT_DONE" ]; then
        . ${setEnvironmentPath}
      fi
    '';

    environment.etc."zshrc.local".text = ''
      # Nix environment setup (already done in zshenv.local)
    '';

    environment.etc."zshenv.local".text = ''
      # Nix environment setup
      if [[ -o rcs ]]; then
        if [ -z "''${__NIX_DARWIN_SET_ENVIRONMENT_DONE-}" ]; then
          . ${setEnvironmentPath}
        fi

        # Tell zsh how to find installed completions
        for p in ''${(z)NIX_PROFILES}; do
          fpath=($p/share/zsh/site-functions $p/share/zsh/$ZSH_VERSION/functions $p/share/zsh/vendor-completions $fpath)
        done
      fi
    '';

    time.timeZone = "America/Los_Angeles";

    # System preferences
    system.defaults = {
      # Custom keyboard shortcuts
      CustomUserPreferences = {
        "com.apple.symbolichotkeys" = {
          AppleSymbolicHotKeys = {
            # Screenshot - Capture entire screen (Cmd+Ctrl+3)
            "28" = {
              enabled = true;
              value = {
                parameters = [ 51 20 1310720 ];
                type = "standard";
              };
            };
            # Screenshot - Capture selected portion (Cmd+Ctrl+4)
            "30" = {
              enabled = true;
              value = {
                parameters = [ 52 21 1310720 ];
                type = "standard";
              };
            };
            # Screenshot - Show screenshot toolbar (Cmd+Ctrl+5)
            "184" = {
              enabled = true;
              value = {
                parameters = [ 53 23 1310720 ];
                type = "standard";
              };
            };
          };
        };
      };
    };

    environment.systemPackages = with pkgs; [
      git
      glances
      pciutils
      tree
      usbutils
      vim
    ];

    nix = {
      package = pkgs.nix;
      # distributedBuilds = true;
      # buildMachines = [{
      #   hostName = "z790prors.oglehome";
      #   system = "x86_64-linux";
      #   protocol = "ssh-ng";
      #   sshUser = "johno";
      #   sshKey = "/root/.ssh/id_ed25519";
      #   maxJobs = 3;
      #   speedFactor = 2;
      # }];
      settings = {
        experimental-features = [ "nix-command" "flakes" ];
        max-jobs = "auto";
        trusted-users = [ "johno" ];
        substituters = [
        ];
      };

      gc = {
        automatic = true;
        options = "--delete-older-than 10d";
      };
    };

    nixpkgs.config.allowUnfree = true;
  };
}