KTX-Software derivation build failure

NOTE: Full flake is in a spoiler at the bottom of this post

Hello! I’ve been working on getting a flake for my project for the upcoming bevy game jam 2. As part of my preparations I’d like to automate my asset processing so my artists don’t have to spend as much time fussing with tools like toktx and psd exporting which have proven to be error prone in the past.

Part of this requires building github:Khronos/KTX-Software which I have successfully done on my local machine in accordance to their BUILDING documentation which works without a hitch. The issue comes when trying to translate this into a derivation:

# Build Khronos Texture tools
# NOTE: This is not fully open and can't be shipped with final packages
ktx-tools = (with pkgs; stdenv.mkDerivation {
  pname = "ktx-tools";
  version = "4.0.0";
  src = fetchFromGitHub {
    owner = "KhronosGroup";
     repo = "KTX-Software";
     rev = "d1a25c8";
     sha256 = "1y03717ix63cdkiisnixkzbqjsk6l52a5izsc3z2vrqxrjnpwanx";
   };
   buildInputs = [ cmake ninja gcc perl tree ];
});

which produces (NOTE: .#assets depends on ktx-tools):

$ nix build .#assets

error: builder for '/nix/store/p3nxagkb7rvak10vn8ckjwyfiilz3izw-ktx-tools-4.0.0.drv' failed with exit code 1;
       last 10 log lines:
       > /bin/sh: ./mkversion: not found
       > [7/114] Generating VkFormat-related source files
       > FAILED: ../lib/vkformat_enum.h ../lib/vkformat_check.c ../lib/vkformat_str.c /build/source/lib/vkformat_enum.h /build/source/lib/vkformat_check.c /build/source/lib/vkformat_str.c
       > cd /build/source && /nix/store/s42gmjjg1gyg7f8s7nnjv0s3363f7gmx-cmake-3.23.2/bin/cmake -E make_directory lib && VULKAN_SDK= lib/mkvkformatfiles lib
       > /bin/sh: lib/mkvkformatfiles: not found
       > [8/114] Building CXX object CMakeFiles/objUtil.dir/utils/argparser.cpp.o
       > [9/114] Building CXX object interface/basisu_c_binding/CMakeFiles/obj_basisu_cbind.dir/src/basisu_c_binding.cpp.o
       > [10/114] Building CXX object tests/gtest/CMakeFiles/gtest.dir/src/gtest_main.cc.o
       > [11/114] Building CXX object tests/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
       > ninja: build stopped: subcommand failed.
       For full logs, run 'nix log /nix/store/p3nxagkb7rvak10vn8ckjwyfiilz3izw-ktx-tools-4.0.0.drv'.
error: 1 dependencies of derivation '/nix/store/c5z2f21jpp5skkm8bkns7wjvda43awy7-errata-assets.drv' failed to build
Full Log
warning: Git tree '/home/gen/Projects/errata' is dirty
error: builder for '/nix/store/p3nxagkb7rvak10vn8ckjwyfiilz3izw-ktx-tools-4.0.0.drv' failed with exit code 1;
       last 10 log lines:
       > /bin/sh: ./mkversion: not found
       > [7/114] Generating VkFormat-related source files
       > FAILED: ../lib/vkformat_enum.h ../lib/vkformat_check.c ../lib/vkformat_str.c /build/source/lib/vkformat_enum.h /build/source/lib/vkformat_check.c /build/source/lib/vkformat_str.c
       > cd /build/source && /nix/store/s42gmjjg1gyg7f8s7nnjv0s3363f7gmx-cmake-3.23.2/bin/cmake -E make_directory lib && VULKAN_SDK= lib/mkvkformatfiles lib
       > /bin/sh: lib/mkvkformatfiles: not found
       > [8/114] Building CXX object CMakeFiles/objUtil.dir/utils/argparser.cpp.o
       > [9/114] Building CXX object interface/basisu_c_binding/CMakeFiles/obj_basisu_cbind.dir/src/basisu_c_binding.cpp.o
       > [10/114] Building CXX object tests/gtest/CMakeFiles/gtest.dir/src/gtest_main.cc.o
       > [11/114] Building CXX object tests/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
       > ninja: build stopped: subcommand failed.
       For full logs, run 'nix log /nix/store/p3nxagkb7rvak10vn8ckjwyfiilz3izw-ktx-tools-4.0.0.drv'.
error: 1 dependencies of derivation '/nix/store/c5z2f21jpp5skkm8bkns7wjvda43awy7-errata-assets.drv' failed to build
[gen@gdw errata]$ nix log /nix/store/p3nxagkb7rvak10vn8ckjwyfiilz3izw-ktx-tools-4.0.0.drv
@nix { "action": "setPhase", "phase": "unpackPhase" }
unpacking sources
unpacking source archive /nix/store/gvyqa56a2fb902g3cq3qkjdikgl7ihpi-source
source root is source
@nix { "action": "setPhase", "phase": "patchPhase" }
patching sources
@nix { "action": "setPhase", "phase": "configurePhase" }
configuring
fixing cmake files...
cmake flags: -GNinja -DCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY=OFF -DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF -DCMAKE_EXPORT_NO_PACKAGE_RE>
-- Found Bash: /nix/store/2r9n7fz1rxq088j6mi5s7izxdria6d5f-bash-5.1-p16/bin/bash  
CMake Warning at cmake/version.cmake:151 (message):
  Error retrieving version from GIT tag.  Falling back to 0.0.0-noversion
Call Stack (most recent call first):
  CMakeLists.txt:10 (include)

I suppose I’m really looking for three things:

  • What is the process or the documentation for fixing problems like this?
  • Given that the CMake build works fine when done manually, what is nixos doing that’s breaking this CMake script?
  • What is / How do i fix this specific issue?

My first attempt at fixing this was to manually specify the build stage so I could be certain it matched the KTX-Software docs:

# Build Khronos Texture tools
# NOTE: This is not fully open and can't be shipped with final packages
ktx-tools = (with pkgs; stdenv.mkDerivation {
  pname = "ktx-tools";
  version = "4.0.0";
  src = fetchFromGitHub {
    owner = "KhronosGroup";
    repo = "KTX-Software";
    rev = "d1a25c8";
    sha256 = "1y03717ix63cdkiisnixkzbqjsk6l52a5izsc3z2vrqxrjnpwanx";
  };
  buildInputs = [ cmake ninja gcc perl tree ];

  buildPhase = ''
    cmake . -G Ninja -B build -DCMAKE_BUILD_TYPE=Release
    cmake --build build
  '';

  installPhase = ''
    # TODO add the rest of the tools
    cp build/tools/toktx/toktx $out/bin/
  '';
});

This produces the same ouput as above.

Thanks in advance for any help!

Full `flake.nix`

{
inputs = {
fenix.url = “github:nix-community/fenix”;
nixpkgs.url = “github:nixos/nixpkgs/nixpkgs-unstable”;
flake-utils.url = “github:numtide/flake-utils”;
};
outputs = { self, fenix, nixpkgs, flake-utils }:
with flake-utils.lib; eachSystem (with system; [ x86_64-linux x86_64-windows ])
(system:
let
pkgs = (import nixpkgs) {
inherit system;
};

      rustPlatform = pkgs.makeRustPlatform {
        inherit (fenix.packages.x86_64-linux.latest) cargo rustc;
      };

      # Build Khronos Texture tools
      # NOTE: This is not fully open and can't be shipped with final packages
      ktx-tools = (with pkgs; stdenv.mkDerivation {
        pname = "ktx-tools";
        version = "4.0.0";
        src = fetchFromGitHub {
          owner = "KhronosGroup";
          repo = "KTX-Software";
          rev = "d1a25c8";
          sha256 = "1y03717ix63cdkiisnixkzbqjsk6l52a5izsc3z2vrqxrjnpwanx";
        };
        buildInputs = [ cmake ninja gcc perl tree ];

        buildPhase = ''
          cmake . -G Ninja -B build -DCMAKE_BUILD_TYPE=Release
          cmake --build build
        '';

        installPhase = ''
          # TODO add the rest of the tools
          cp build/tools/toktx/toktx $out/bin/
        '';
      });

      nativeBuildInputs = with pkgs; [
        pkgconfig
        mold
        cargo
        rustc
      ];
      buildInputs.x86_64-linux = with pkgs; [
        # System
        udev
        # Audio
        alsaLib
        # Video
        vulkan-loader
        # X11
        xlibsWrapper
        xorg.libXcursor
        xorg.libXrandr
        xorg.libXi
        # Wayland
        libxkbcommon
        wayland
      ];
      assetInputs = with pkgs; [
        # Model Processing
        blender
        # Image Processing
        inkscape
        imagemagick
        ktx-tools
      ];

      LD_PATH-hook = ''
        export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${pkgs.lib.makeLibraryPath buildInputs."${system}"}"
      '';
    in
    rec {
      # Setup checks for each platform
      #checks = packages."${system}";
      packages.default = packages.app;

      devShells = {
        default = pkgs.mkShell {
          packages = nativeBuildInputs ++ buildInputs."${system}" ++ (with pkgs;[ nixpkgs-fmt ]);
          shellInputs = buildInputs."${system}";
          shellHook = ''
            export RUST_BACKTRACE=1
            alias cargo="mold -run cargo"
          '' + LD_PATH-hook;
        };
      };
      packages = {
        assets = pkgs.stdenvNoCC.mkDerivation {
          name = "errata-assets";
          src = builtins.path {
            name = "resources";
            path = ./res;
          };
          buildInputs = assetInputs;

          buildPhase = ''
            mkdir -p out/assets/
            find assets -type f -name "*.svg" -exec sh -c scripts/build-svg.sh {} out \;
            find assets -type f -name "*.blend" -exec echo {} \;
          '';

          installPhase = ''
            cp -r out $out
          '';
        };
        app = pkgs.symlinkJoin {
          name = "errata" + "-${system}";
          paths = [ packages.assets packages.client.release ];
        };
        client = rec {
          release = rustPlatform.buildRustPackage {
            name = "errata-client-${system}";
            src = ./.;
            cargoLock.lockFile = ./Cargo.lock;
            nativeBuildInputs = nativeBuildInputs;
            buildInputs = buildInputs.x86_64-linux;
            shellHook = LD_PATH-hook;
            allowParallelBuild = true;

            postInstall = ''
              mv $out/bin/errata $out/bin/errata-client-${system}
            '';
          };
          debug = pkgs.overrideDerivation release {
            buildType = "debug";
          };
        };
      };
    }
  );

}

I’m not an expert in this, but I believe cmake needs to be in nativeBuildInputs in order for its hooks to fire and properly modify stdenv to do cmake builds.

Moving cmake to nativeBuildInputs here

# Build Khronos Texture tools
          # NOTE: This is not fully open and can't be shipped with final packages
          ktx-tools = (with pkgs; stdenv.mkDerivation {
            pname = "ktx-tools";
            version = "4.0.0";
            src = fetchFromGitHub {
              owner = "KhronosGroup";
              repo = "KTX-Software";
              rev = "d1a25c8";
              sha256 = "1y03717ix63cdkiisnixkzbqjsk6l52a5izsc3z2vrqxrjnpwanx";
            };
            nativeBuildInputs = [ cmake ];
            buildInputs = [ ninja gcc perl tree ];

            buildPhase = ''
              cmake . -G Ninja -B build -DCMAKE_BUILD_TYPE=Release
              cmake --build build
            '';

            installPhase = ''
              # TODO add the rest of the tools
              cp build/tools/toktx/toktx $out/bin/
            '';
          });

doesn’t seem to change build output here, but I think this is the right direction.

Build output error: builder for '/nix/store/n1x0bmraqsgv30fy491jyq6xrr97nby3-ktx-tools-4.0.0.drv' failed with exit code 1; last 10 log lines: > [6/114] Generate tools/toktx/version.h > FAILED: tools/toktx/tools/toktx/version.h /build/source/build/build/tools/toktx/tools/toktx/version.h > cd /build/source && ./mkversion -o version.h tools/toktx > /bin/sh: ./mkversion: not found > [7/114] Generating VkFormat-related source files > FAILED: ../../lib/vkformat_enum.h ../../lib/vkformat_check.c ../../lib/vkformat_str.c /build/source/lib/vkformat_enum.h /build/source/lib/vkformat_check.c /build/source/lib/vkformat_str.c > cd /build/source && /nix/store/s42gmjjg1gyg7f8s7nnjv0s3363f7gmx-cmake-3.23.2/bin/cmake -E make_directory lib && VULKAN_SDK= lib/mkvkformatfiles lib > /bin/sh: lib/mkvkformatfiles: not found > [11/114] Building CXX object tests/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.oKir/src/basisu_c_binding.cpp.o > ninja: build stopped: subcommand failed. For full logs, run 'nix log /nix/store/n1x0bmraqsgv30fy491jyq6xrr97nby3-ktx-tools-4.0.0.drv'. error: 1 dependencies of derivation '/nix/store/z1hv1nx29dvydkmjhdqi83sch5mpa4qm-errata-assets.drv' failed to build error: 1 dependencies of derivation '/nix/store/5z63pb0qnwj2l4xf1kfv1hfhzljjckyq-errata-x86_64-linux.drv' failed to build

Special thanks to @k900:0upti.me on matrix for providing the solution.

# Build Khronos Texture tools
          # NOTE: This is not fully open and can't be shipped with final packages
          ktx-tools = (with pkgs; stdenv.mkDerivation rec {
            pname = "ktx-tools";
            version = "4.0.0";

            src = fetchFromGitHub {
              owner = "KhronosGroup";
              repo = "KTX-Software";
              rev = "v${version}";
              sha256 = "sha256-3Sp+rcwd5y3+YPrHokShZmqJ1589Wh3jbGyYHk84A/g=";
            };

            patches = [
              # backport patch fixing builds with C++17 compilers
              (fetchpatch {
                url = "https://github.com/KhronosGroup/KTX-Software/commit/877bca30f3c6eaca28628033180406f07cc34663.patch";
                sha256 = "sha256-dl4fImAq0E5d0Xm6V3PNm0G/CpdvxHvef45Wxiri1nE=";
              })
            ];

            postPatch = "patchShebangs .";

            nativeBuildInputs = [ cmake ninja getopt perl ];
          });

It required a a couple patches and additional native dependencies.