Cmake custom command shell script not found

Nix package build is not able to call shell scripts defined in cmake as custom command.

When i build it (with nix-build -A) i get a cannot execute: required file not found error.

Running phase: buildPhase
build flags: -j8 SHELL=/nix/store/717iy55ncqs0wmhdkwc5fg2vci5wbmq8-bash-5.2p32/bin/bash
[100%] Generating foo
current: /build/source absolut: /build/source
-rwxr-xr-x 1 nixbld nixbld 20 Jan  1  1970 /build/source/foo.sh
/nix/store/717iy55ncqs0wmhdkwc5fg2vci5wbmq8-bash-5.2p32/bin/bash: line 1: /build/source/foo.sh: cannot execute: required file not found

As far as i can tell with ls the file exists and has permissions to be executed. One workaround i found was to patch the cmake to include bash in front of the script.

Why does this fail? I would rather not have to patch the cmake.

Steps to reproduce:

Summary

To reproduce create a directory with these 2 files:

cmake_minimum_required(VERSION 3.6 FATAL_ERROR)

project(shelltest)

add_custom_command(
    OUTPUT foo
    COMMAND echo "current: ${CMAKE_CURRENT_SOURCE_DIR} absolut: ${CMAKE_SOURCE_DIR}"
    COMMAND ls -la ${CMAKE_CURRENT_SOURCE_DIR}/foo.sh
    COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/foo.sh
    DEPENDS foo.sh
)

add_custom_target(run ALL
    DEPENDS foo)
#!/usr/bin/env bash

echo "Hoii"

And use a nix package expression like this:

{
  lib,
  stdenv,
  cmake,
}:

stdenv.mkDerivation (finalAttrs: {
  pname = "test";
  version = "1.0.0";

  src = lib.cleanSource /path/to/folder/with/files/from/above;

  nativeBuildInputs = [
    cmake
  ];
})

You probably just need to use patchShebangs on it. (I suspect the error is because /usr/bin/env isn’t in the sandbox.)

1 Like

Looking at the examples a well places patchShebangs will do the trick.

postPatch = ''
    patchShebangs ./
  '';
2 Likes