{
  description = "Flake for installing XP-Pen PenTablet driver on NixOS";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";  # You can pin this to a specific commit if needed
  };

  outputs = { self, nixpkgs }:
  let
    pkgs = import nixpkgs { system = "x86_64-linux"; config.allowUnfree = true; };
  in
  {
    packages.x86_64-linux.pentablet = pkgs.stdenv.mkDerivation rec {
      pname = "pentablet";
      version = "3.2.1.210426";  # Replace with the actual version

      src = pkgs.fetchurl {
        # Replace the URL with the actual download link of the driver
        url = "https://www.xp-pen.com/download/file.html?id=2901&pid=977&ext=gz";

        sha256 = "1wyl96lpw8wvzfq3q5q0njp2ca8j5h89psi8j0lmrhymxhp94ml7";  # Replace with the actual sha256
      };

      nativeBuildInputs = [ pkgs.makeWrapper ];

      buildInputs = with pkgs; [
        glib
        gtk3
        xorg.libX11
        xorg.libXext
        xorg.libXrender
        xorg.libXfixes
        # Add any other dependencies required by the driver
      ];

      unpackPhase = ''
        mkdir -p tmpdir
        cd tmpdir
        tar xzf $src
        echo $PWD
      '';

      installPhase = ''
        # Create necessary directories
        mkdir -p $out/bin
        mkdir -p $out/lib/pentablet
        mkdir -p $out/share/applications
        mkdir -p $out/share/icons/hicolor/256x256/apps
        mkdir -p $out/lib/udev/rules.d

        # Copy application files
        echo "Current directory is: $PWD"
        echo "Listing files in $PWD:"
        ls -al $PWD
        cp -r XPPenLinux3.4.9-240607/App/usr/lib/pentablet $out/lib

        # Copy the executable to bin
        ln -s $out/lib/pentablet/PenTablet $out/bin/PenTablet

        # Install desktop file
        cp XPPenLinux3.4.9-240607/App/usr/share/applications/xppentablet.desktop $out/share/applications/

        # Install icon
        cp XPPenLinux3.4.9-240607/App/usr/share/icons/hicolor/256x256/apps/xppentablet.png $out/share/icons/hicolor/256x256/apps/

        # Install udev rules
        cp XPPenLinux3.4.9-240607/App/lib/udev/rules.d/10-xp-pen.rules $out/lib/udev/rules.d/

        # Create a wrapper script
        makeWrapper $out/lib/pentablet/PenTablet $out/bin/PenTablet \
          --set XPPEN_CONFIG_DIR "$HOME/.config/xpen" \
          --prefix LD_LIBRARY_PATH : "$out/lib/pentablet"

        chmod +x $out/bin/PenTablet

        # Adjust permissions
        chmod +x $out/lib/pentablet/PenTablet
      '';

      # Since the application might expect to write to certain directories, we'll need to address this
      # in the wrapper script or by setting up writable directories (discussed later)

      meta = with pkgs.lib; {
        description = "XP-Pen PenTablet driver";
        homepage = "https://www.xp-pen.com/";
        license = licenses.unfreeRedistributable;  # Since it's proprietary software
        platforms = platforms.linux;
      };
    };

    # Optionally, you can define a NixOS module to handle udev rules and other configurations
    nixosModules.pentablet = { config, lib, pkgs, ... }: {
      options = {};

      config = {
        # Install the pentablet package
        environment.systemPackages = [ self.packages.${pkgs.system}.pentablet ];

        # Add udev rules
        services.udev.packages = [ self.packages.${pkgs.system}.pentablet ];

        # If the driver requires any system services, you can define them here
        # For example, if it needs to run a daemon:
        # systemd.services.pentablet = {
        #   description = "XP-Pen PenTablet Service";
        #   after = [ "network.target" ];
        #   wantedBy = [ "multi-user.target" ];
        #   serviceConfig = {
        #     ExecStart = "${self.packages.${pkgs.system}.pentablet}/bin/PenTablet";
        #   };
        # };

        # Set up auto-start if needed
        # programs.autoStart.enable = true;
        # programs.autoStart.startupCommands = ''
          # ${self.packages.${pkgs.system}.pentablet}/bin/PenTablet &
        # '';
      };
    };
  };
}