Is there a method to have absolute filepaths in a flake's repo, perhaps treating the base of the repo as a "root"?

To try to explain what im saying here:

Suppose I have the following repo

.git
flake.nix
flake.lock
utils.nix
dir1/
  file1.foo
  file2.bar
dir2/
  default.nix

Suppose, for whatever reason, I was trying to reference the contents of file1.foo in default.nix. I could do that via a relative path i.e. ../dir1/file1.foo. I am curious if there is a way to use something resembling an absolute path, so, perhaps something like /project-root/dir1/file1.foo.

This example is a bit trivial, but this would be useful to me in a flake-managed home-manager, where I would have a lot of relative-path references and a propensity to moving things around.

You can use self argument of outputs function:

{
  inputs = {
    nixpkgs.url = "nixpkgs/nixos-21.05";
  };
  outputs = { self, nixpkgs }: {
    devShell.x86_64-linux =
      let
        pkgs = import nixpkgs { system = "x86_64-linux"; };
      in
      pkgs.mkShell {
        shellHook =''
          echo "project root: ${self}"
        '';
      };
  };
}

self is an attribute set that refers to the current flake. Since the attribute set includes an attribute named outPath, its value will be used when the set is coerced to a string.

2 Likes

Ah great, had no idea self could be used like that, thank you thank you