Merge pull request #8852 from flox/tomberek.absolute.attrpath.notation

Absolute attrPath notation ("flakeref#.attrPath")
This commit is contained in:
Robert Hensing 2023-09-28 15:52:39 +01:00 committed by GitHub
commit 62ddb6851f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 2 deletions

View file

@ -5,3 +5,5 @@
- [Path-like flake references](@docroot@/command-ref/new-cli/nix3-flake.md#path-like-syntax) now accept arbitrary unicode characters (except `#` and `?`). - [Path-like flake references](@docroot@/command-ref/new-cli/nix3-flake.md#path-like-syntax) now accept arbitrary unicode characters (except `#` and `?`).
- The experimental feature `repl-flake` is no longer needed, as its functionality is now part of the `flakes` experimental feature. To get the previous behavior, use the `--file/--expr` flags accordingly. - The experimental feature `repl-flake` is no longer needed, as its functionality is now part of the `flakes` experimental feature. To get the previous behavior, use the `--file/--expr` flags accordingly.
- Introduce new flake installable syntax `flakeref#.attrPath` where the "." prefix denotes no searching of default attribute prefixes like `packages.<SYSTEM>` or `legacyPackages.<SYSTEM>`.

View file

@ -28,6 +28,11 @@ namespace nix {
std::vector<std::string> InstallableFlake::getActualAttrPaths() std::vector<std::string> InstallableFlake::getActualAttrPaths()
{ {
std::vector<std::string> res; std::vector<std::string> res;
if (attrPaths.size() == 1 && attrPaths.front().starts_with(".")){
attrPaths.front().erase(0,1);
res.push_back(attrPaths.front());
return res;
}
for (auto & prefix : prefixes) for (auto & prefix : prefixes)
res.push_back(prefix + *attrPaths.begin()); res.push_back(prefix + *attrPaths.begin());

View file

@ -301,6 +301,11 @@ void completeFlakeRefWithFragment(
completionType = ctAttrs; completionType = ctAttrs;
auto fragment = prefix.substr(hash + 1); auto fragment = prefix.substr(hash + 1);
std::string prefixRoot = "";
if (fragment.starts_with(".")){
fragment = fragment.substr(1);
prefixRoot = ".";
}
auto flakeRefS = std::string(prefix.substr(0, hash)); auto flakeRefS = std::string(prefix.substr(0, hash));
auto flakeRef = parseFlakeRef(expandTilde(flakeRefS), absPath(".")); auto flakeRef = parseFlakeRef(expandTilde(flakeRefS), absPath("."));
@ -309,6 +314,9 @@ void completeFlakeRefWithFragment(
auto root = evalCache->getRoot(); auto root = evalCache->getRoot();
if (prefixRoot == "."){
attrPathPrefixes.clear();
}
/* Complete 'fragment' relative to all the /* Complete 'fragment' relative to all the
attrpath prefixes as well as the root of the attrpath prefixes as well as the root of the
flake. */ flake. */
@ -333,7 +341,7 @@ void completeFlakeRefWithFragment(
auto attrPath2 = (*attr)->getAttrPath(attr2); auto attrPath2 = (*attr)->getAttrPath(attr2);
/* Strip the attrpath prefix. */ /* Strip the attrpath prefix. */
attrPath2.erase(attrPath2.begin(), attrPath2.begin() + attrPathPrefix.size()); attrPath2.erase(attrPath2.begin(), attrPath2.begin() + attrPathPrefix.size());
completions->add(flakeRefS + "#" + concatStringsSep(".", evalState->symbols.resolve(attrPath2))); completions->add(flakeRefS + "#" + prefixRoot + concatStringsSep(".", evalState->symbols.resolve(attrPath2)));
} }
} }
} }
@ -344,7 +352,7 @@ void completeFlakeRefWithFragment(
for (auto & attrPath : defaultFlakeAttrPaths) { for (auto & attrPath : defaultFlakeAttrPaths) {
auto attr = root->findAlongAttrPath(parseAttrPath(*evalState, attrPath)); auto attr = root->findAlongAttrPath(parseAttrPath(*evalState, attrPath));
if (!attr) continue; if (!attr) continue;
completions->add(flakeRefS + "#"); completions->add(flakeRefS + "#" + prefixRoot);
} }
} }
} }

View file

@ -132,6 +132,8 @@ subcommands, these are `packages.`*system*,
attributes `packages.x86_64-linux.hello`, attributes `packages.x86_64-linux.hello`,
`legacyPackages.x86_64-linux.hello` and `hello`. `legacyPackages.x86_64-linux.hello` and `hello`.
If *attrpath* begins with `.` then no prefixes or defaults are attempted. This allows the form *flakeref*[`#.`*attrpath*], such as `github:NixOS/nixpkgs#.lib.fakeSha256` to avoid a search of `packages.*system*.lib.fakeSha256`
### Store path ### Store path
Example: `/nix/store/v5sv61sszx301i0x6xysaqzla09nksnd-hello-2.10` Example: `/nix/store/v5sv61sszx301i0x6xysaqzla09nksnd-hello-2.10`

View file

@ -0,0 +1,17 @@
source ./common.sh
flake1Dir=$TEST_ROOT/flake1
mkdir -p $flake1Dir
cat > $flake1Dir/flake.nix <<EOF
{
outputs = { self }: {
x = 1;
packages.$system.x = 2;
};
}
EOF
[ "$(nix eval --impure --json $flake1Dir#.x)" -eq 1 ]
[ "$(nix eval --impure --json $flake1Dir#x)" -eq 2 ]
[ "$(nix eval --impure --json $flake1Dir#.packages.$system.x)" -eq 2 ]

View file

@ -12,6 +12,7 @@ nix_tests = \
flakes/check.sh \ flakes/check.sh \
flakes/unlocked-override.sh \ flakes/unlocked-override.sh \
flakes/absolute-paths.sh \ flakes/absolute-paths.sh \
flakes/absolute-attr-paths.sh \
flakes/build-paths.sh \ flakes/build-paths.sh \
flakes/flake-in-submodule.sh \ flakes/flake-in-submodule.sh \
gc.sh \ gc.sh \