Stdlib.h not found when building schroot on NixOS

Hi. I’m trying to package sbuild using Nix.

{ stdenv
, boost
, cmake
, cppunit
, doxygen
, fetchgit
, gettext
, groff
, lib
, libuuid
, linux-pam
, perlPackages
, pkgconfig
, btrfs-progs ? null
, lvm2 ? null
, zfs ? null
}:
let
  src' = fetchgit {
    url = "https://salsa.debian.org/debian/schroot.git";
    rev = "779a0285f6ce323064cdbe1c9844559c18ddd139";
    sha256 = "0wrsnfhjifqrfzqp6nc1ghgm07wa6blkb1ig8dsh7j7xcx42vihs";
  };
in
stdenv.mkDerivation {
  name = "schroot";
  src = src';
  #src = ../tmp/schroot;

  nativeBuildInputs = [ cmake doxygen pkgconfig ];

  buildInputs = [
    boost
    cppunit
    gettext
    groff
    libuuid
    linux-pam
    perlPackages.Po4a
  ];

  patches = [
    ./cross.patch
  ];

  cmakeFlags = [
    "--debug-output"
    "-DSCHROOT_LIBEXEC_DIR=/$(LIBDIR)/schroot"
    "-Dbash_completion_dir=/usr/share/bash-completion/completions"
  ] ++ (if (lvm2 == null) then [
      "-Dlvm-snapshot=OFF"
    ] else [
      "-Dlvm-snapshot=ON"
      "-DLVCREATE_EXECUTABLE=${lvm2}/bin/lvcreate"
      "-DLVREMOVE_EXECUTABLE=${lvm2}/bin/lvremove"
    ]
  ) ++ (if (btrfs-progs == null) then [
      "-Dbtrfs-snapshot=OFF"
    ] else [
      "-Dbtrfs-snapshot=ON"
      "-DBTRFS_EXECUTABLE=${btrfs-progs}/bin/btrfs"
    ]
  ) ++ (if (zfs == null) then [
      "-Dzfs-snapshot=OFF"
    ] else [
      "-Dzfs-snapshot=ON"
      "-DZFS_EXECUTABLE=${zfs}/bin/zfs"
    ]
  );
}

cross.patch:

--- schroot-1.6.10.orig/CMakeLists.txt
+++ schroot-1.6.10/CMakeLists.txt
@@ -96,7 +96,7 @@
 # Configure testing
 SET(CMAKE_REQUIRED_LIBRARIES_SAVE ${CMAKE_REQUIRED_LIBRARIES})
 SET(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} cppunit)
-check_cxx_source_runs(
+check_cxx_source_compiles(
 "#include <cppunit/ui/text/TestRunner.h>
 
 int main() {
--- schroot-1.6.10.orig/cmake/regex-checks.cmake
+++ schroot-1.6.10/cmake/regex-checks.cmake
@@ -3,7 +3,23 @@
 function(regex_test namespace header library outvar outlib)
   set(CMAKE_REQUIRED_LIBRARIES_SAVE ${CMAKE_REQUIRED_LIBRARIES})
   set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${library})
-  check_cxx_source_runs(
+
+  if (CMAKE_CROSSCOMPILING)
+    message(WARNING "Using simplified regex check for cross compilation. Assuming that regex implementations work correctly.")
+    check_cxx_source_compiles(
+"#include <${header}>
+
+int main() {
+  ${namespace} foo(\"x\");
+  ${namespace} bar(\"x\", ${namespace}::extended);
+  std::string test(\"x\");
+  ${namespace}_search(test, foo);
+  ${namespace}_match(test, foo);
+  return 0;
+}"
+${outvar})
+  else(CMAKE_CROSSCOMPILING)
+    check_cxx_source_runs(
 "#include <${header}>
 #include <iostream>
 
@@ -43,6 +59,7 @@
   return 0;
 }"
 ${outvar})
+  endif(CMAKE_CROSSCOMPILING)
 
   set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES_SAVE})
 

I am taking the debian package for sid as reference.

When I build the derivation above, I get some errors on the libboost checks:

-- Found Boost: /nix/store/fb2a7qqm209wzc9fb3wx8jj413rcw4yy-boost-1.67.0-dev/include (found version "1.67.0")
-- Found Boost: /nix/store/fb2a7qqm209wzc9fb3wx8jj413rcw4yy-boost-1.67.0-dev/include (found version "1.67.0") found components:  filesystem system iostreams program_options regex
-- Looking for C++ include boost/format.hpp
-- Looking for C++ include boost/format.hpp - not found
-- Looking for C++ include boost/iostreams/device/file_descriptor.hpp
-- Looking for C++ include boost/iostreams/device/file_descriptor.hpp - not found
-- Looking for C++ include boost/program_options.hpp
-- Looking for C++ include boost/program_options.hpp - not found
-- Looking for C++ include boost/type_traits.hpp
-- Looking for C++ include boost/type_traits.hpp - not found
-- Performing Test BOOST_PROGRAM_OPTIONS_LINK
-- Performing Test BOOST_PROGRAM_OPTIONS_LINK - Failed
-- Performing Test BOOST_PROGRAM_OPTIONS_DESCRIPTION_CURRENT_LINK
-- Performing Test BOOST_PROGRAM_OPTIONS_DESCRIPTION_CURRENT_LINK - Failed
-- Performing Test BOOST_IOSTREAMS_LINK
-- Performing Test BOOST_IOSTREAMS_LINK - Failed
-- Performing Test BOOST_IOSTREAMS_CLOSE_HANDLE_CURRENT_LINK
-- Performing Test BOOST_IOSTREAMS_CLOSE_HANDLE_CURRENT_LINK - Failed
-- Performing Test BOOST_FILESYSTEM_LINK
-- Performing Test BOOST_FILESYSTEM_LINK - Failed
-- Performing Test HAVE_REGEX_REGEX
-- Performing Test HAVE_REGEX_REGEX - Failed
-- Performing Test HAVE_TR1_REGEX
-- Performing Test HAVE_TR1_REGEX - Failed
-- Performing Test HAVE_BOOST_REGEX
-- Performing Test HAVE_BOOST_REGEX - Failed
CMake Error at cmake/regex-checks.cmake:51 (message):
  No working regular expression implementation found
Call Stack (most recent call first):
  CMakeLists.txt:72 (include)


-- Configuring incomplete, errors occurred!
See also "/build/schroot-779a028/build/CMakeFiles/CMakeOutput.log".
See also "/build/schroot-779a028/build/CMakeFiles/CMakeError.log".
builder for '/nix/store/ab21q6vrrmpxf4d4mv6j8m7l1yadxmjn-schroot.drv' failed with exit code 1
error: build of '/nix/store/ab21q6vrrmpxf4d4mv6j8m7l1yadxmjn-schroot.drv' failed

However, since cmake does find boost, I tried just disabling those tests in the project’s CMakeLists.txt:71-72.

When I do so, I get the following errors during compilation:

[ 43%] Building CXX object sbuild/CMakeFiles/sbuild.dir/sbuild-log.cc.o
cd /build/schroot/build/sbuild && /nix/store/291ldi6fqsbmkbvbs8is4mcg3jb64ld4-gcc-wrapper-8.3.0/bin/g++   -I/build/schroot/build/lib -I/build/schroot/lib -I/build/schroot/build -I/build/schroot -I/build/schroot/sbuild -I/build/schroot/build/sbuild  -std=c++11 -pedantic -Wall -Wcast-align -Wwrite-strings -Wswitch-default -Wcast-qual -Wunused-variable -Wredundant-decls -Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder -Wold-style-cast -Woverloaded-virtual -fstrict-aliasing -O3 -DNDEBUG   -o CMakeFiles/sbuild.dir/sbuild-log.cc.o -c /build/schroot/sbuild/sbuild-log.cc
In file included from /nix/store/62x7m20m7lm8y8s17cbgha0sf3cmma19-gcc-8.3.0/include/c++/8.3.0/ext/string_conversions.h:41,
                 from /nix/store/62x7m20m7lm8y8s17cbgha0sf3cmma19-gcc-8.3.0/include/c++/8.3.0/bits/basic_string.h:6400,
                 from /nix/store/62x7m20m7lm8y8s17cbgha0sf3cmma19-gcc-8.3.0/include/c++/8.3.0/string:52,
                 from /nix/store/62x7m20m7lm8y8s17cbgha0sf3cmma19-gcc-8.3.0/include/c++/8.3.0/bits/locale_classes.h:40,
                 from /nix/store/62x7m20m7lm8y8s17cbgha0sf3cmma19-gcc-8.3.0/include/c++/8.3.0/bits/ios_base.h:41,
                 from /nix/store/62x7m20m7lm8y8s17cbgha0sf3cmma19-gcc-8.3.0/include/c++/8.3.0/ios:42,
                 from /nix/store/62x7m20m7lm8y8s17cbgha0sf3cmma19-gcc-8.3.0/include/c++/8.3.0/ostream:38,
                 from /build/schroot/sbuild/sbuild-log.h:22,
                 from /build/schroot/sbuild/sbuild-environment.h:22,
                 from /build/schroot/sbuild/sbuild-environment.cc:21:
/nix/store/62x7m20m7lm8y8s17cbgha0sf3cmma19-gcc-8.3.0/include/c++/8.3.0/cstdlib:75:15: fatal error: stdlib.h: No such file or directory
 #include_next <stdlib.h>
               ^~~~~~~~~~
compilation terminated.
make[2]: *** [sbuild/CMakeFiles/sbuild.dir/build.make:79: sbuild/CMakeFiles/sbuild.dir/sbuild-environment.cc.o] Error 1

I tried looking in the internet, but all I could find remotely related to this is a bug from webkit due to the misuse of -isystem include flag from 4 years ago. As can be seen in the output above, such flag is not used.

Another occurrence of this message is in Nixpkgs Swift derivation, but this doesn’t apply either since sbuild does not bootstrap its own compiler.

I tried asking around on IRC with no luck, maybe somebody has seen such a bug or has any suggestions. Any ideas welcome !

My end goal is to be able to make a dpkg builder. My current setup involves chrooting into an ubuntu, calling sbuild (which calls schroot on a debian chroot) and fai to produce debian distributions. I would like to have a Nix builder do this for me.

Bests,
Roos

Update:

I had misread the comment in the swift derivation. Turns out that actually solves the issue.

I’m surprised that I have to use clang.default_cxx_stdlib_compile though, using stdenv.cc.default_cxx_stdlib_compile (or gcc...) does not work.

Any hints on why this is the case?