1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-08 19:23:54 +02:00

Improve support for subflakes

Subflakes are flakes in the same tree, accessed in flake inputs via
relative paths (e.g. `inputs.foo.url = "path:./subdir"`). Previously
these didn't work very well because they would be separately copied to
the store, which is inefficient and makes references to parent
directories tricky or impossible. Furthermore, they had their own NAR
hash in the lock file, which is superfluous since the parent is
already locked.

Now subflakes are accessed via the accessor of the calling flake. This
avoids the unnecessary copy and makes it possible for subflakes to
depend on flakes in a parent directory (so long as they're in the same
tree).

Lock file nodes for relative flake inputs now have a new `parent` field:

  {
    "locked": {
      "path": "./subdir",
      "type": "path"
    },
    "original": {
      "path": "./subdir",
      "type": "path"
    },
    "parent": [
      "foo",
      "bar"
    ]
  }

which denotes that `./subdir` is to be interpreted relative to the
directory of the `bar` input of the `foo` input of the root flake.

Extracted from the lazy-trees branch.
This commit is contained in:
Eelco Dolstra 2024-04-23 16:00:52 +02:00
parent bbe780b137
commit b2be6fed86
14 changed files with 332 additions and 120 deletions

View file

@ -115,7 +115,7 @@ nix flake lock $flakeFollowsA
[[ $(jq -c .nodes.B.inputs.foobar $flakeFollowsA/flake.lock) = '"foobar"' ]]
jq -r -c '.nodes | keys | .[]' $flakeFollowsA/flake.lock | grep "^foobar$"
# Ensure a relative path is not allowed to go outside the store path
# Check that path: inputs cannot escape from their root.
cat > $flakeFollowsA/flake.nix <<EOF
{
description = "Flake A";
@ -128,7 +128,28 @@ EOF
git -C $flakeFollowsA add flake.nix
expect 1 nix flake lock $flakeFollowsA 2>&1 | grep 'points outside'
expect 1 nix flake lock $flakeFollowsA 2>&1 | grep '/flakeB.*is forbidden in pure evaluation mode'
expect 1 nix flake lock --impure $flakeFollowsA 2>&1 | grep '/flakeB.*does not exist'
# Test relative non-flake inputs.
cat > $flakeFollowsA/flake.nix <<EOF
{
description = "Flake A";
inputs = {
E.flake = false;
E.url = "./foo.nix"; # test relative paths without 'path:'
};
outputs = { E, ... }: { e = import E; };
}
EOF
echo 123 > $flakeFollowsA/foo.nix
git -C $flakeFollowsA add flake.nix foo.nix
nix flake lock $flakeFollowsA
[[ $(nix eval --json $flakeFollowsA#e) = 123 ]]
# Non-existant follows should print a warning.
cat >$flakeFollowsA/flake.nix <<EOF

View file

@ -0,0 +1,89 @@
source ./common.sh
requireGit
rootFlake=$TEST_ROOT/flake1
subflake0=$rootFlake/sub0
subflake1=$rootFlake/sub1
subflake2=$rootFlake/sub2
rm -rf $rootFlake
mkdir -p $rootFlake $subflake0 $subflake1 $subflake2
cat > $rootFlake/flake.nix <<EOF
{
inputs.sub0.url = "./sub0";
outputs = { self, sub0 }: {
x = 2;
y = self.x * sub0.x;
};
}
EOF
cat > $subflake0/flake.nix <<EOF
{
outputs = { self }: {
x = 7;
};
}
EOF
[[ $(nix eval $rootFlake#x) = 2 ]]
[[ $(nix eval $rootFlake#y) = 14 ]]
cat > $subflake1/flake.nix <<EOF
{
inputs.root.url = "../";
outputs = { self, root }: {
x = 3;
y = self.x * root.x;
};
}
EOF
[[ $(nix eval $rootFlake?dir=sub1#y) = 6 ]]
git init $rootFlake
git -C $rootFlake add flake.nix sub0/flake.nix sub1/flake.nix
[[ $(nix eval $subflake1#y) = 6 ]]
cat > $subflake2/flake.nix <<EOF
{
inputs.root.url = "../";
inputs.sub1.url = "../sub1";
outputs = { self, root, sub1 }: {
x = 5;
y = self.x * sub1.x;
};
}
EOF
git -C $rootFlake add flake.nix sub2/flake.nix
[[ $(nix eval $subflake2#y) = 15 ]]
# Make sure there are no content locks for relative path flakes.
(! grep "$TEST_ROOT" $subflake2/flake.lock)
(! grep "$NIX_STORE_DIR" $subflake2/flake.lock)
(! grep narHash $subflake2/flake.lock)
# Test circular relative path flakes. FIXME: doesn't work at the moment.
if false; then
cat > $rootFlake/flake.nix <<EOF
{
inputs.sub1.url = "./sub1";
inputs.sub2.url = "./sub1";
outputs = { self, sub1, sub2 }: {
x = 2;
y = self.x * sub1.x * sub2.x;
z = sub1.y * sub2.y;
};
}
EOF
[[ $(nix eval $rootFlake#x) = 30 ]]
[[ $(nix eval $rootFlake#z) = 90 ]]
fi

View file

@ -7,6 +7,7 @@ nix_tests = \
flakes/circular.sh \
flakes/init.sh \
flakes/inputs.sh \
flakes/relative-paths.sh \
flakes/follow-paths.sh \
flakes/bundle.sh \
flakes/check.sh \