Is there a way to specifiy a file inside a derivation through nix?

I can declare the following variable that results in a fish function:

testFn = "${pkgs.rofi}/bin/rofi";

If for whatever reason the rofi binary has been moved or renamed, this will only fail at runtime.

I would like to know if there is a method to indicate the path to a file inside of a derivation solely using nix and thus catching errors & changes during the build.

Thanks in advance

For an executable, lib.meta.getExe could work. See Nixpkgs Reference Manual

To handle any file within a derivation, take a look at what nixGL is doing here: nixGL/nixGL.nix at 310f8e49a149e4c9ea52f1adf70cdc768ec53f8a · nix-community/nixGL · GitHub

1 Like

You could just add a test, whether around the checkPhase, installCheckPhase, or in passthru.tests, that will fail if it’s missing.

FWIW, this probably a problem resholve can address when the source is plain POSIX shell and Bash, but I assume since you mentioned Fish that it wouldn’t be appropriate here.

1 Like

This is what I was looking for, as I would prefer to not modify/override existing derivations (which is also more expensive I think?) Thanks a lot!

The test solution is great when creating your own derivation I suppose.
Thx for pointing out resholve, that looks like a useful tool! Fish was just an example, I previously had an issue with catppuccin renaming their themes, which resulted in breakage without nix telling me, so I had to write a function that would validate the path at build time.

Actually lib.meta.getExe’ looks more appropriate for my usecase/preference as you indicate the binary name and instead of succeeding it breaks if the given binary is missing.

And allows you to fetch non default binaries, such as libnotify’s notify-send

lib.getExe’ turned out to not throw so i wrote this function:

    checkPath =
        pkg: bin:
          abs = lib.getExe' pkg bin;
        if builtins.pathExists abs then abs else throw "${abs} does not exist.";

In what capacity would it make sense to add this to lib? Not having blocking build time validation of binaries (or files) by default is kind of a bummer