Search code examples
nixnixpkgs

selecting a list of files as build input


I would like to restrict the number of files to include in my build src to a select few. Directly passing the list of files to src or srcs isn't allowed as an archive or a directory is expected. I couldn't find a function to do this and builtins.filterSource does not seem to work for me (no idea why -- the intermediate derivation containing the filtered files ends up empty):

    files = [
        ./Cargo.toml
        ./Cargo.lock
        ./cpu.rs
    ];
    src = builtins.filterSource (p: t: builtins.elem p files) ./.;

Note: I'm using the rustPlatform builder but I don't think it matters.


Solution

  • filterSource passes the full path as a string. in order to compare the paths of your list with it you need to convert the string to a path:

    $ cd /Users/fghibellini/code/nix
    $ nix-instantiate --eval -E './a == "/Users/fghibellini/code/nix/a"'
    false
    $ nix-instantiate --eval -E './a == (/. + "/Users/fghibellini/code/nix/a")'
    true
    

    i.e. the following code should work fine:

    files = [
        ./Cargo.toml
        ./Cargo.lock
        ./cpu.rs
    ];
    src = builtins.filterSource (p: t: builtins.elem (/. + p) files) ./.;
    

    You can use builtins.typeOf and builtins.trace to debug such issues.