# Cmake custom command shell script not found

**URL:** https://discourse.nixos.org/t/cmake-custom-command-shell-script-not-found/54937
**Category:** Help
**Created:** [October 25, 2024, 3:57pm UTC](https://discourse.nixos.org/t/cmake-custom-command-shell-script-not-found/54937 "2024-10-25T15:57:17Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![l33tname](https://discourse.nixos.org/user_avatar/discourse.nixos.org/l33tname/32/16660_2.png) [@l33tname](https://discourse.nixos.org/u/l33tname)
#### Post date: [October 25, 2024, 3:57pm UTC](https://discourse.nixos.org/t/cmake-custom-command-shell-script-not-found/54937/1 "2024-10-25T15:57:17Z")

</div>

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.

```auto
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:
> 
> ```plaintext
> 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)
> 
> ```
> 
> ```bash
> #!/usr/bin/env bash
> 
> echo "Hoii"
> 
> ```
> 
> And use a nix package expression like this:
> 
> ```nix
> {
> lib,
> stdenv,
> cmake,
> }:
> 
> stdenv.mkDerivation (finalAttrs: {
> pname = "test";
> version = "1.0.0";
> 
> src = lib.cleanSource /path/to/folder/with/files/from/above;
> 
> nativeBuildInputs = [
> cmake
> ];
> })
> 
> ```

---

<div class="post-metadata">

### Author: ![abathur](https://discourse.nixos.org/user_avatar/discourse.nixos.org/abathur/32/2719_2.png) [@abathur](https://discourse.nixos.org/u/abathur)
#### Post date: [October 25, 2024, 4:15pm UTC](https://discourse.nixos.org/t/cmake-custom-command-shell-script-not-found/54937/2 "2024-10-25T16:15:01Z")

</div>

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

---

<div class="post-metadata">

### Author: ![l33tname](https://discourse.nixos.org/user_avatar/discourse.nixos.org/l33tname/32/16660_2.png) [@l33tname](https://discourse.nixos.org/u/l33tname)
#### Post date: [October 27, 2024, 10:13am UTC](https://discourse.nixos.org/t/cmake-custom-command-shell-script-not-found/54937/3 "2024-10-27T10:13:03Z")

</div>

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

```auto
postPatch = ''
    patchShebangs ./
  '';

```
