How to refer to current directory in shell.nix?

I’m developing a package that includes some Lua library files. Supposing my git repository is in /home/dan/src/example/, this means I need to set the environment variable LUA_PATH to /home/dan/src/example/lib/?.lua - and I’d like to do this in shell.nix

My first thought was to use ./. but that expands to a store path not to the “local” directory. I’d rather be able to use an absolute path here than just lib/?.lua which will fail as soon as I cd into a subdirectory

Any ideas?

Maybe it’s easier than I thought it was:

with import <nixpkgs> {};
(callPackage ./default.nix {}).overrideAttrs(o: rec {
  SWARM_BASE_PATH = "/tmp/services";
  PROJECT_ROOT = builtins.getEnv "PWD";
  LUA_PATH = "${PROJECT_ROOT}/lib/?.lua";
  nativeBuildInputs = [ pkgs.foreman ] ;
})

although it would be even better if I could find the path of the shell.nix file itself, then I can run nix-shell from other directories as well as .

...
  PROJECT_ROOT = builtins.toString ./.;
...
1 Like

I was not expecting that to work, but … thank you very much!

Why does toString work there? For anyone skimming the thread:

FOO = builtins.toString ./.;
FOO2 = ./.;

nix-shell:~/src/swarm]$ echo $FOO
/home/dan/src/swarm

[nix-shell:~/src/swarm]$ echo $FOO2
/nix/store/3ll5lrn0cys4rfq0asvxz0jar2z24c21-swarm

Magic!

nix paths work like that:
If you reference something as a path and not a string it gets copied to the nix-store so it doesn’t change it output when you run it multiple times.
when you convert the current directory to a string you skip to copy to nix-store step and get the current directory as string.

:joy: I always end up looking for the same piece of information and almost always end here: