How to get path of the flake being rebuilt from within the flake at runtime

At the moment I have tried setting an environment variable called FLAKE_DIR in a custom rebuild script using export and pwd. Then in Nix I have used bulletins.getEnv ”FLAKE_DIR" under config, however, it doesn’t seem to return the correct value just an empty string. I tested under nix repl and it works. I wonder if it’s because rebuild opens a new shell in the rebuild process?

Are there any alternative ways to do this such as a builtin variable or function with this information?


Flake evaluation is pure by default. You need to include --impure in your build command because reading the environment is impure.

1 Like

Yes thanks that was it,
Surprised it didn’t throw an error.

It’s actually used in a module for an application. I added some bash script to the home-manager process via DAG in-order to copy some files in the same repo which need to be read/write for this app. unfortunately, this app doesn’t offer any options to get around this. I suppose I should move all this out of nix into that mentioned rebuild script ideally. I get the odd preferences file and format , I need to copy over I am thinking, longterm, of using a backup and restore script to be optionally run after a rebuild…