Merge pull request #5720 from tomberek/flake_search
flakes: search up to git or filesystem boundary
This commit is contained in:
commit
dc4730ee94
6 changed files with 110 additions and 12 deletions
|
@ -1,5 +1,6 @@
|
|||
# Release X.Y (202?-??-??)
|
||||
|
||||
* The Nix cli now searches for a flake.nix up until the root of the current git repository or a filesystem boundary rather than just in the current directory
|
||||
* The TOML parser used by `builtins.fromTOML` has been replaced by [a
|
||||
more compliant one](https://github.com/ToruNiina/toml11).
|
||||
* Added `:st`/`:show-trace` commands to nix repl, which are used to
|
||||
|
|
|
@ -122,6 +122,28 @@ std::pair<FlakeRef, std::string> parseFlakeRefWithFragment(
|
|||
|
||||
if (isFlake) {
|
||||
|
||||
if (!allowMissing && !pathExists(path + "/flake.nix")){
|
||||
notice("path '%s' does not contain a 'flake.nix', searching up",path);
|
||||
|
||||
// Save device to detect filesystem boundary
|
||||
dev_t device = lstat(path).st_dev;
|
||||
bool found = false;
|
||||
while (path != "/") {
|
||||
if (pathExists(path + "/flake.nix")) {
|
||||
found = true;
|
||||
break;
|
||||
} else if (pathExists(path + "/.git"))
|
||||
throw Error("path '%s' is not part of a flake (neither it nor its parent directories contain a 'flake.nix' file)", path);
|
||||
else {
|
||||
if (lstat(path).st_dev != device)
|
||||
throw Error("unable to find a flake before encountering filesystem boundary at '%s'", path);
|
||||
}
|
||||
path = dirOf(path);
|
||||
}
|
||||
if (!found)
|
||||
throw BadURL("could not find a flake.nix file");
|
||||
}
|
||||
|
||||
if (!S_ISDIR(lstat(path).st_mode))
|
||||
throw BadURL("path '%s' is not a flake (because it's not a directory)", path);
|
||||
|
||||
|
|
|
@ -137,15 +137,6 @@ Currently the `type` attribute can be one of the following:
|
|||
*path* must be a directory in the file system containing a file
|
||||
named `flake.nix`.
|
||||
|
||||
If the directory or any of its parents is a Git repository, then
|
||||
this is essentially equivalent to `git+file://<path>` (see below),
|
||||
except that the `dir` parameter is derived automatically. For
|
||||
example, if `/foo/bar` is a Git repository, then the flake reference
|
||||
`/foo/bar/flake` is equivalent to `/foo/bar?dir=flake`.
|
||||
|
||||
If the directory is not inside a Git repository, then the flake
|
||||
contents is the entire contents of *path*.
|
||||
|
||||
*path* generally must be an absolute path. However, on the command
|
||||
line, it can be a relative path (e.g. `.` or `./foo`) which is
|
||||
interpreted as relative to the current directory. In this case, it
|
||||
|
|
|
@ -57,9 +57,47 @@ the Nix store. Here are the recognised types of installables:
|
|||
These have the form *flakeref*[`#`*attrpath*], where *flakeref* is a
|
||||
flake reference and *attrpath* is an optional attribute path. For
|
||||
more information on flakes, see [the `nix flake` manual
|
||||
page](./nix3-flake.md). Flake references are most commonly a flake
|
||||
identifier in the flake registry (e.g. `nixpkgs`) or a path
|
||||
(e.g. `/path/to/my-flake` or `.`).
|
||||
page](./nix3-flake.md). Flake references are most commonly a flake
|
||||
identifier in the flake registry (e.g. `nixpkgs`), or a raw path
|
||||
(e.g. `/path/to/my-flake` or `.` or `../foo`), or a full URL
|
||||
(e.g. `github:nixos/nixpkgs` or `path:.`)
|
||||
|
||||
When the flake reference is a raw path (a path without any URL
|
||||
scheme), it is interpreted as a `path:` or `git+file:` url in the following
|
||||
way:
|
||||
|
||||
- If the path is within a Git repository, then the url will be of the form
|
||||
`git+file://[GIT_REPO_ROOT]?dir=[RELATIVE_FLAKE_DIR_PATH]`
|
||||
where `GIT_REPO_ROOT` is the path to the root of the git repository,
|
||||
and `RELATIVE_FLAKE_DIR_PATH` is the path (relative to the directory
|
||||
root) of the closest parent of the given path that contains a `flake.nix` within
|
||||
the git repository.
|
||||
If no such directory exists, then Nix will error-out.
|
||||
|
||||
Note that the search will only include files indexed by git. In particular, files
|
||||
which are matched by `.gitignore` or have never been `git add`-ed will not be
|
||||
available in the flake. If this is undesireable, specify `path:<directory>` explicitly;
|
||||
|
||||
For example, if `/foo/bar` is a git repository with the following structure:
|
||||
```
|
||||
.
|
||||
└── baz
|
||||
├── blah
|
||||
│ └── file.txt
|
||||
└── flake.nix
|
||||
```
|
||||
|
||||
Then `/foo/bar/baz/blah` will resolve to `git+file:///foo/bar?dir=baz`
|
||||
|
||||
- If the supplied path is not a git repository, then the url will have the form
|
||||
`path:FLAKE_DIR_PATH` where `FLAKE_DIR_PATH` is the closest parent
|
||||
of the supplied path that contains a `flake.nix` file (within the same file-system).
|
||||
If no such directory exists, then Nix will error-out.
|
||||
|
||||
For example, if `/foo/bar/flake.nix` exists, then `/foo/bar/baz/` will resolve to
|
||||
`path:/foo/bar`
|
||||
|
||||
|
||||
|
||||
If *attrpath* is omitted, Nix tries some default values; for most
|
||||
subcommands, the default is `defaultPackage.`*system*
|
||||
|
|
45
tests/flake-searching.sh
Normal file
45
tests/flake-searching.sh
Normal file
|
@ -0,0 +1,45 @@
|
|||
source common.sh
|
||||
|
||||
clearStore
|
||||
|
||||
cp ./simple.nix ./simple.builder.sh ./config.nix $TEST_HOME
|
||||
cd $TEST_HOME
|
||||
mkdir -p foo/subdir
|
||||
echo '{ outputs = _: {}; }' > foo/flake.nix
|
||||
cat <<EOF > flake.nix
|
||||
{
|
||||
inputs.foo.url = "$PWD/foo";
|
||||
outputs = a: {
|
||||
defaultPackage.$system = import ./simple.nix;
|
||||
packages.$system.test = import ./simple.nix;
|
||||
};
|
||||
}
|
||||
EOF
|
||||
mkdir subdir
|
||||
pushd subdir
|
||||
|
||||
success=("" . .# .#test ../subdir ../subdir#test "$PWD")
|
||||
failure=("path:$PWD")
|
||||
|
||||
for i in "${success[@]}"; do
|
||||
nix build $i || fail "flake should be found by searching up directories"
|
||||
done
|
||||
|
||||
for i in "${failure[@]}"; do
|
||||
! nix build $i || fail "flake should not search up directories when using 'path:'"
|
||||
done
|
||||
|
||||
popd
|
||||
|
||||
nix build --override-input foo . || fail "flake should search up directories when not an installable"
|
||||
|
||||
sed "s,$PWD/foo,$PWD/foo/subdir,g" -i flake.nix
|
||||
! nix build || fail "flake should not search upwards when part of inputs"
|
||||
|
||||
pushd subdir
|
||||
git init
|
||||
for i in "${success[@]}" "${failure[@]}"; do
|
||||
! nix build $i || fail "flake should not search past a git repository"
|
||||
done
|
||||
rm -rf .git
|
||||
popd
|
|
@ -47,6 +47,7 @@ nix_tests = \
|
|||
describe-stores.sh \
|
||||
flakes.sh \
|
||||
flake-local-settings.sh \
|
||||
flake-searching.sh \
|
||||
build.sh \
|
||||
repl.sh ca/repl.sh \
|
||||
ca/build.sh \
|
||||
|
|
Loading…
Reference in a new issue