Is it possible to define imports relative to the root of the flake directory (while still keeping pure evaluation mode)?
I would like to avoid having to refactor file paths every time I move a module around.
Suppose I have a flake that defines my nixos system and a bunch of modules:
This specific version does not work since inputs.self contains the path of the file that is being evaluated, so the path is duplicated â/nix/store/<hash>-source/configurations/foo/configurations/foo/nginx.nix' does not exist.
Your instance sounds like it should not be happening. Are you using any type of submodules or flakes as inputs with self references in them?
I hate to wax frameworks, but I went dendritic to avoid the exact thing youâre talking about. Pathing was by far the most annoying thing and not an ancient relic of the past in my configs. However, that severely breaks the âno refactorâ request.
Assuming I understand what youâre trying to do, and supposing this is in configurations/foo.nix, you could define a local variable with the relative path to the flake root:
{ ... }:
let flake_root = ../;
in {
imports = [
./${flake_root}/configurations/foo/nginx.nix
./${flake_root}/modules/services/postgres.nix
./${flake_root}/modules/services/forgejo.nix
];
}
You would have to change the let flake_root = line when you move the module, but not anything else. The leading ./ on each of the imports is unfortunately necessary due to the syntax of path literals.