devenv icon indicating copy to clipboard operation
devenv copied to clipboard

Utility for git repository absolute paths

Open dsalaza4 opened this issue 1 year ago • 3 comments

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!

dsalaza4 avatar Jan 31 '25 01:01 dsalaza4

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 avatar Feb 05 '25 11:02 sandydoo

@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"

dsalaza4 avatar Feb 05 '25 17:02 dsalaza4

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.

dsalaza4 avatar Feb 10 '25 18:02 dsalaza4

This is now config.git.root as part of devenv 1.10

domenkozar avatar Oct 06 '25 14:10 domenkozar