{
  lib,
  stdenv,
  buildGo126Module,
  makeBinaryWrapper,
  xdg-dbus-proxy,
  pkg-config,
  libffi,
  libseccomp,
  acl,
  wayland,
  wayland-protocols,
  wayland-scanner,

  libxcb,
  libxau,
  libxdmcp,

  # for sharefs
  fuse3,

  # for passthru.buildInputs
  go_1_26,
  clang,
  xorgproto,

  # for check
  util-linux,
  nettools,

  glibc, # for ldd
  withStatic ? stdenv.hostPlatform.isStatic,
}:

buildGo126Module rec {
  pname = "hakurei";
  version = with lib.strings; removePrefix "v" (trim (builtins.readFile ../cmd/dist/VERSION));

  srcFiltered = builtins.path {
    name = "${pname}-src";
    path = lib.cleanSource ../.;
    filter = path: type: !(type == "regular" && (lib.hasSuffix ".nix" path || lib.hasSuffix ".py" path)) && !(type == "directory" && lib.hasSuffix "/test" path) && !(type == "directory" && lib.hasSuffix "/cmd/hsu" path);
  };
  vendorHash = null;

  src = stdenv.mkDerivation {
    name = "${pname}-src-full";
    inherit version;
    enableParallelBuilding = true;
    src = srcFiltered;

    buildInputs = [
      wayland
      wayland-protocols
    ];

    nativeBuildInputs = [
      go_1_26
      pkg-config
      wayland-scanner
    ];

    buildPhase = "GOCACHE=$(mktemp -d) go generate ./...";
    installPhase = "cp -r . $out";
  };

  ldflags =
    lib.attrsets.foldlAttrs
      (
        ldflags: name: value:
        ldflags ++ [ "-X hakurei.app/internal/info.${name}=${value}" ]
      )
      (
        [ "-s -w" ]
        ++ lib.optionals withStatic [
          "-linkmode external"
          "-extldflags \"-static\""
        ]
      )
      {
        buildVersion = "v${version}";
        hakureiPath = "${placeholder "out"}/libexec/hakurei";
        hsuPath = "/run/wrappers/bin/hsu";
      };

  env = {
    # use clang instead of gcc
    CC = "clang -O3 -Werror";
  };

  buildInputs = [
    libffi
    libseccomp
    fuse3
    acl
    wayland

    libxcb
    libxau
    libxdmcp
  ];

  nativeBuildInputs = [
    pkg-config
    makeBinaryWrapper

    # for container example
    nettools
  ];

  postInstall =
    let
      appPackages = [
        glibc
        xdg-dbus-proxy
      ];
    in
    ''
      install -D --target-directory=$out/share/zsh/site-functions cmd/dist/comp/*

      mkdir "$out/libexec"
      mv "$out"/bin/* "$out/libexec/"

      makeBinaryWrapper "$out/libexec/hakurei" "$out/bin/hakurei" \
        --inherit-argv0 --prefix PATH : ${lib.makeBinPath appPackages}
    '';

  passthru = {
    go = go_1_26;

    targetPkgs = [
      go_1_26
      clang
      xorgproto
      util-linux

      # for go generate
      wayland-protocols
      wayland-scanner
    ]
    ++ buildInputs
    ++ nativeBuildInputs;
  };
}
