Skip to content

Expose flake directory to nix fmt as NIX_FLAKE_DIR env var #13143

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jfly
Copy link
Contributor

@jfly jfly commented May 7, 2025

This was discussed in #8034. I personally like NIX_FLAKE_DIR, which hopefully avoids some ambiguity around with subflakes.

I only implemented this for nix fmt because it doesn't let you point at a flake not on your filesystem.

Motivation

Context


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@github-actions github-actions bot added documentation new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority labels May 7, 2025
@jfly jfly force-pushed the issue-8034-nix-fmt branch from b5bd4cf to 1858f55 Compare May 7, 2025 02:45
@@ -72,6 +94,12 @@ struct CmdFormatterRun : MixFormatter, MixJSON
auto evalState = getEvalState();
auto evalStore = getEvalStore();

auto flakeRef = parseFlakeRef(fetchSettings, ".", std::filesystem::current_path().string());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flakeref should be obtained from the installable returned by parseInstallable() below. flakeRef.input has a method getSourcePath() that returns the physical path of a flake, if any.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flakeRef.input has a method getSourcePath() that returns the physical path of a flake, if any.

This worked, thanks!

The flakeref should be obtained from the installable returned by parseInstallable() below

I haven't been able to get this working. The issue is that parseInstallable() returns a Installable, but I need it to be a FlakeInstallable (which is a descendant class of Installable). I suppose I know that my installable is actually a FlakeInstallable, should I be casting/asserting this? If yes, how?

@jfly jfly force-pushed the issue-8034-nix-fmt branch 2 times, most recently from b4a86f6 to 5caf343 Compare May 7, 2025 18:21
This type was defined in `DerivationBuilderImpl`, but it could live
happily in `environment-variables.h`, so I put it there.
@jfly jfly force-pushed the issue-8034-nix-fmt branch from 5caf343 to 262938d Compare May 7, 2025 18:25
@jfly jfly changed the title WIP/RFC: expose flake directory to nix fmt as FLAKE_DIR env var WIP/RFC: expose flake directory to nix fmt as NIX_FLAKE_DIR env var May 7, 2025
This was discussed in NixOS#8034. I
personally like `NIX_FLAKE_DIR`, which hopefully avoids some ambiguity
around with subflakes.

I only implemented this for `nix fmt` because it doesn't let you point
at a flake not on your filesystem.
@jfly jfly force-pushed the issue-8034-nix-fmt branch from 262938d to 4996d89 Compare May 7, 2025 18:27
@jfly jfly changed the title WIP/RFC: expose flake directory to nix fmt as NIX_FLAKE_DIR env var Expose flake directory to nix fmt as NIX_FLAKE_DIR env var May 7, 2025
@jfly jfly marked this pull request as ready for review May 7, 2025 18:27
@jfly jfly requested a review from Ericson2314 as a code owner May 7, 2025 18:27
Comment on lines +31 to +40
/* Convert `env` to a list of strings suitable for `execve`'s `envp` argument. */
Strings toEnvp(Environment env)
{
Strings envStrs;
for (auto & i : env) {
envStrs.push_back(i.first + "=" + i.second);
}

return envStrs;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@edolstra, should I also move this to src/libutil/environment-variables.cc?

You need an `extern` dance to access `environ`, and apparently `execvpe`
doesn't exist on macOS :(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants