Help with mkDerivation and devShell in flake -

I am creating a custom derivation that downloads an artifact (this artifact is not statically linked).

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };
  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        name = "simple";
        src = ./.;
        pkgs = nixpkgs.legacyPackages.${system};
      in
      {
        packages = {
        } // (if system == "x86_64-linux" then {
          spanner-emulator =
            let
              version = "1.5.2";
              inherit (pkgs) stdenv lib;
            in
            stdenv.mkDerivation
              {
                name = "spanner-emulator";
                src = pkgs.fetchurl {
                  url =
                    "https://storage.googleapis.com/cloud-spanner-emulator/releases/${version}/cloud-spanner-emulator_linux_amd64-${version}.tar.gz";
                  sha256 = "e02e53776f36865dd581234c0c21a54add77d88fb956023aa47f99d96c0af788";
                };
                buildInputs = [ pkgs.stdenv.cc.cc.lib ];
                nativeBuildInputs = [ pkgs.stdenv.cc.cc.lib ];
                phases = [ "unpackPhase" "installPhase" ];
                unpackPhase = ''
                  mkdir -p $out/bin
                  tar -xzf $src -C $out/bin
                '';

                # this phase is not necessary, but it's here to show how to install
                installPhase = ''
                '';

                meta = with nixpkgs.lib; {
                  homepage = "https://github.com/GoogleCloudPlatform/cloud-spanner-emulator";
                  description =
                    "Cloud Spanner Emulator is a local emulator for the Google Cloud Spanner database service.";
                  platforms = platforms.linux;
                };
              };
        } else { });
        defaultPackage = self.packages.spanner-emulator;
      }
    );
}

How do I make a devShell from this with this default package in the path?

I also tried using devbox tool to create, but I get this error

/nix/store/pz5gz7b6hnrkyilakr4sbzclmhrlv1cf-spanner-emulator/bin/emulator_main: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.33' not found (required by /home/user/Projects/spanner-emulator/.devbox/nix/profile/default/lib/libstdc++.so.6)

I changed it to add

          default = pkgs.mkShell {
            buildInputs = with pkgs; [ stdenv.cc.cc.lib self.packages.${system}.spanner-emulator ];
          };

it works but libstdc++ is not found here

ldd /nix/store/pz5gz7b6hnrkyilakr4sbzclmhrlv1cf-spanner-emulator/bin/emulator_main
        linux-vdso.so.1 (0x00007ffff7fc8000)
        libpthread.so.0 => /nix/store/x33pcmpsiimxhip52mwxbb5y77dhmb21-glibc-2.37-8/lib/libpthread.so.0 (0x00007ffff4d31000)
        libm.so.6 => /nix/store/x33pcmpsiimxhip52mwxbb5y77dhmb21-glibc-2.37-8/lib/libm.so.6 (0x00007ffff4c51000)
        libstdc++.so.6 => not found
        libgcc_s.so.1 => /nix/store/05cd846a6pbhdijq28ga5piyk6d5mzvb-xgcc-12.2.0-libgcc/lib/libgcc_s.so.1 (0x00007ffff4c30000)
        libc.so.6 => /nix/store/x33pcmpsiimxhip52mwxbb5y77dhmb21-glibc-2.37-8/lib/libc.so.6 (0x00007ffff4a4a000)
        /lib64/ld-linux-x86-64.so.2 => /nix/store/x33pcmpsiimxhip52mwxbb5y77dhmb21-glibc-2.37-8/lib64/ld-linux-x86-64.so.2 (0x00007ffff7fca000)

Your’re very close! I think you’re missing libstdc++5 in your build inputs, the c++ standard library might not be part of the stdenv.cc.lib. See nixpkgs search

1 Like

I actually changed to

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };
  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        name = "simple";
        src = ./.;
        pkgs = nixpkgs.legacyPackages.${system};
      in
      rec {
        packages = { } // (if system == "x86_64-linux" then {
          spanner-emulator =
            let
              version = "1.5.2";
              inherit (pkgs) stdenv lib;
            in
            stdenv.mkDerivation rec
            {
              name = "spanner-emulator";
              src = pkgs.fetchurl {
                url =
                  "https://storage.googleapis.com/cloud-spanner-emulator/releases/${version}/cloud-spanner-emulator_linux_amd64-${version}.tar.gz";
                sha256 = "e02e53776f36865dd581234c0c21a54add77d88fb956023aa47f99d96c0af788";
              };
              sourceRoot = ".";
              unpackPhase = ''
                mkdir -p $out/bin
                tar -xzf $src -C $out/bin
              '';
              buildPhase = ":";
              libPath = with pkgs; lib.makeLibraryPath [
                pkgs.stdenv.cc.cc.lib # libstdc++.so.6
              ];

              postFixUp = ''
                patchelf \
                  --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" \
                  --set-rpath "${libPath}" \
                  $out/bin/emulator_main
              '';
              meta = with nixpkgs.lib; {
                homepage = "https://github.com/GoogleCloudPlatform/cloud-spanner-emulator";
                description =
                  "Cloud Spanner Emulator is a local emulator for the Google Cloud Spanner database service.";
                platforms = platforms.linux;
              };
            };
        } else { });
        devShells = { } // (if system == "x86_64-linux" then {
          default = pkgs.mkShell rec {
            libPath = with pkgs; lib.makeLibraryPath [
              pkgs.stdenv.cc.cc.lib # libstdc++.so.6
            ];
            LD_LIBRARY_PATH = "${libPath}";
            buildInputs = [
              self.packages.${system}.spanner-emulator
            ];
          };
        } else { });

      }
    );
}

I used postFixUp command, but now I get different error

emulator_main: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.33' not found (required by /nix/store/7ls5xhx6kqpjgpg67kdd4pmbkhna4b6c-gcc-12.2.0-lib/lib/libstdc++.so.6)
emulator_main: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found (required by /nix/store/7ls5xhx6kqpjgpg67kdd4pmbkhna4b6c-gcc-12.2.0-lib/lib/libstdc++.so.6)
emulator_main: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.36' not found (required by /nix/store/7ls5xhx6kqpjgpg67kdd4pmbkhna4b6c-gcc-12.2.0-lib/lib/libstdc++.so.6)
emulator_main: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found (required by /nix/store/7ls5xhx6kqpjgpg67kdd4pmbkhna4b6c-gcc-12.2.0-lib/lib/libstdc++.so.6)
emulator_main: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.35' not found (required by /nix/store/7ls5xhx6kqpjgpg67kdd4pmbkhna4b6c-gcc-12.2.0-lib/lib/libgcc_s.so.1)
emulator_main: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found (required by /nix/store/7ls5xhx6kqpjgpg67kdd4pmbkhna4b6c-gcc-12.2.0-lib/lib/libgcc_s.so.1)

Hmm that /lib/x86_64-linux-gnu/libc.so.6 is not good, that should be /nix/store/x33pcmpsiimxhip52mwxbb5y77dhmb21-glibc-2.37-8/lib/libc.so.6 like it was before.
The error message basically means that the glibc of your distro is not new enough to run the binary, which is not surprising.

Can you check what ldd returns for your new binary?

You could also try running the newly patched binary with LD_PRELOAD:

export LD_PRELOAD=/nix/store/x33pcmpsiimxhip52mwxbb5y77dhmb21-glibc-2.37-8/lib/libc.so.6

Please use the autoPatchelfHook or manual alternatives to properly patch the prebuild binary or even better, build it from source if the sources are available.