Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utility for git repository absolute paths #1697

Open
dsalaza4 opened this issue Jan 31, 2025 · 3 comments
Open

Utility for git repository absolute paths #1697

dsalaza4 opened this issue Jan 31, 2025 · 3 comments
Labels
enhancement New feature or request

Comments

@dsalaza4
Copy link

dsalaza4 commented Jan 31, 2025

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 dsalaza4 added the enhancement New feature or request label Jan 31, 2025
@dsalaza4 dsalaza4 changed the title Utility for repository absolute paths Utility for git repository absolute paths Jan 31, 2025
@sandydoo
Copy link
Member

sandydoo commented Feb 5, 2025

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 #1548.

@dsalaza4
Copy link
Author

dsalaza4 commented Feb 5, 2025

@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
Copy link
Author

dsalaza4 commented Feb 10, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants