Include relative directories in stdenv srcs

Is it possible to include relative paths in build process without having to include entire root directory?

Given build config

with (import <nixpkgs> {});

pkgs.stdenv.mkDerivation {
    pname = "mypackage";
    version = "2.0.0";
    srcs = [../hardcoded ./.];
    setSourceRoot = ./.;
    buildInputs = [tree];
    buildPhase = ''
    tree .
    make
    '';
}

I get

$ nix-build example.nix 
this derivation will be built:
  /nix/store/2j1vspqvy2r5i8kbdpp12ak7vl9rrc49-mypackage-2.0.0.drv
building '/nix/store/2j1vspqvy2r5i8kbdpp12ak7vl9rrc49-mypackage-2.0.0.drv'...
unpacking sources
unpacking source archive /nix/store/csh3kng8wrpa1lkqffkxni7wmcnq3iks-hardcoded
unpacking source archive /nix/store/ygx43wbhcy30mbv7b94sjr9knsvl3dyf-src
/nix/store/bj5n3k01mq8bysw0rcdm7jxvhc620pd3-stdenv-linux/setup: line 86: /nix/store/ygx43wbhcy30mbv7b94sjr9knsvl3dyf-src: Is a directory
error: builder for '/nix/store/2j1vspqvy2r5i8kbdpp12ak7vl9rrc49-mypackage-2.0.0.drv' failed with exit code 1;
       last 4 log lines:
       > unpacking sources
       > unpacking source archive /nix/store/csh3kng8wrpa1lkqffkxni7wmcnq3iks-hardcoded
       > unpacking source archive /nix/store/ygx43wbhcy30mbv7b94sjr9knsvl3dyf-src
       > /nix/store/bj5n3k01mq8bysw0rcdm7jxvhc620pd3-stdenv-linux/setup: line 86: /nix/store/ygx43wbhcy30mbv7b94sjr9knsvl3dyf-src: Is a directory
       For full logs, run 'nix log /nix/store/2j1vspqvy2r5i8kbdpp12ak7vl9rrc49-mypackage-2.0.0.drv'.

You’re probably looking for Nix - Nixpkgs 22.05 manual or GitHub - numtide/nix-filter: a small self-container source filtering lib.

Unless I misunderstand what you’re trying to achieve here? What should the output of your commands be?

I have a makefile that references executable hardcoded into source control.

I actually set src to project root directory, but cannot execute my binary.

with (import <nixpkgs> {});

pkgs.stdenv.mkDerivation {
    pname = "mypackage";
    version = "2.0.0";
    src = ./.;
    buildInputs = [tree];
    buildPhase = ''
    tree .
    ls -l ./hardcoded/executable
    ./hardcoded/executable
    '';
}
$ nix-build example.nix 
this derivation will be built:
  /nix/store/3v5n2j0a10jcfmav3as92553hn8znnvh-mypackage-2.0.0.drv
building '/nix/store/3v5n2j0a10jcfmav3as92553hn8znnvh-mypackage-2.0.0.drv'...
unpacking sources
unpacking source archive /nix/store/v0pdlg2am067j8aj6gbkf3im9c10y3xi-hello3
source root is hello3
patching sources
configuring
no configure script, doing nothing
building
.
|-- example.nix
|-- hardcoded
|   `-- executable
`-- src

2 directories, 2 files
-rwxr-xr-x 1 nixbld nixbld 133792 Jan  1  1970 ./hardcoded/executable
/nix/store/bj5n3k01mq8bysw0rcdm7jxvhc620pd3-stdenv-linux/setup: line 1399: ./hardcoded/executable: No such file or directory
error: builder for '/nix/store/3v5n2j0a10jcfmav3as92553hn8znnvh-mypackage-2.0.0.drv' failed with exit code 127;
       last 10 log lines:
       > building
       > .
       > |-- example.nix
       > |-- hardcoded
       > |   `-- executable
       > `-- src
       >
       > 2 directories, 2 files
       > -rwxr-xr-x 1 nixbld nixbld 133792 Jan  1  1970 ./hardcoded/executable
       > /nix/store/bj5n3k01mq8bysw0rcdm7jxvhc620pd3-stdenv-linux/setup: line 1399: ./hardcoded/executable: No such file or directory
       For full logs, run 'nix log /nix/store/3v5n2j0a10jcfmav3as92553hn8znnvh-mypackage-2.0.0.drv'.

Even though ls and tree show that file is there, the error No such file or directory

Let me guess, if you run ldd on it it tells you the interpreter is some variant of /lib/ld-linux.so? That file doesn’t exist on NixOS, the error is super unreadable, but that’s basically a Linux kernel problem.

NixOS cannot use precompiled, dynamically linked libraries without some additional work.

You’ll either need to patchelf the binary before you use it, or just package it. See this wiki entry for more details: Packaging/Binaries - NixOS Wiki

1 Like