Utility for git repository absolute paths
Hey there,
I wonder if there's a way to reference a path within the git repository in an absolute way so I don't have to do ../../../. when importing from higher directory levels.
Something like Makes's projectPath that allows us to reference files using absolute paths from the repo root:
e.g
# /some/path/within/repo/devenv.nix
{ pkgs, lib, ... }:
let
my-custom-utils = import (lib.projectPath "/some/other/path/within/repo") { inherit pkgs };
in {
packages = [ my-custom-utils ];
}
Thanks!
Nice! The issue with adding it now is that it'll be broken out of the box, unless you use --impure evaluation.
If you're happy with that trade-off, you can add lib.projectPath = path: config.devenv.root + path; to your config and use it as config.lib.projectPath "/some/nested/path".
Marking this as a good addition for https://github.com/cachix/devenv/issues/1548.
@sandydoo I just tried the config.devenv.root approach but it did not work because it returns the path where the devenv environment resides (devenv.nix, devenv.yaml, etc.) rather than the git repository root.
If the devenv environment resides in a path that is not the repository root, we end up with a wrong path.
e.g:
For a repository with this structure:
.
├── app
│ └── devenv.nix
└── utils
└── default.nix
And a /app/devenv.nix like this:
# /app/devenv.nix
{ config, pkgs, ... }:
let
projectPath = path: config.devenv.root + path;
utils = import (projectPath "/utils/default.nix") { inherit pkgs };
in {
packages = [ utils ];
}
We get the following error:
Error: cannot operate on non-existent file "/app/utils/default.nix"
It looks like referencing relative paths within devenv.yaml does the trick :
# /app/devenv.yaml
inputs:
utils:path:./utils # A devenv bug where ./. is actually the root of the repository and not the current directory?
# /app/devenv.nix
{ config, inputs, pkgs, ... }:
let
utils = import inputs.utils { inherit pkgs; };
in {
packages = [ utils ];
}
@sandydoo Should we add documentation for this? I'm still not sure if this should be the official way to do this.
This is now config.git.root as part of devenv 1.10