How to reference another flake in the same repo?

I have an example repository with two flakes:

  • a/flake.nix exporting some package:
    {
      description = "Flake A";
     
      inputs = {
        nixpkgs.url = "github:NixOS/nixpkgs";
        flake-utils.url = "github:numtide/flake-utils";
      };
     
      outputs = { self, nixpkgs, flake-utils }:
        flake-utils.lib.eachDefaultSystem (system:
          let
            pkgs = import nixpkgs { inherit system; };
          in
          {
            packages.default = pkgs.hello;
          }
        );
    }
    
  • b/flake.nix which gets a as input:
    {
      description = "Flake B";
     
      inputs = {
        a.url = "path:./../a";
        nixpkgs.follows = "a/nixpkgs";
        flake-utils.follows = "a/flake-utils";
      };
     
      outputs = { self, a, nixpkgs, flake-utils }:
        flake-utils.lib.eachDefaultSystem (system: {
          packages.default = a.packages."${system}".default;
        });
    }
    

This works well on darwin but on linux you run into the fact that path inputs must be absolute:

$ cd b
$ nix build
...
error: cannot fetch input 
  'path:./../a?lastModified=1&narHash=sha256-xlRoGtT72GTwflhQ8QNxtx3Ws51mf0b90aUfh2uW5Bg%3D' 
  because it uses a relative path

One attempt to fix this is to remove the path: type in front of the input Here’s that “fix” inlined:

diff --git a/b/flake.nix b/b/flake.nix
index 6408279..cfc118f 100644
--- a/b/flake.nix
+++ b/b/flake.nix
@@ -2,7 +2,7 @@
   description = "Flake B";
 
   inputs = {
-    a.url = "path:./../a";
+    a.url = "./../a";
     nixpkgs.follows = "a/nixpkgs";
     flake-utils.follows = "a/flake-utils";
   };

However, after running cd b; nix flake update this changes b/flake.lock to:

diff --git a/b/flake.lock b/b/flake.lock
index 1464e8e..47b22e6 100644
--- a/b/flake.lock
+++ b/b/flake.lock
@@ -8,11 +8,11 @@
       "locked": {
         "lastModified": 1,
         "narHash": "sha256-xlRoGtT72GTwflhQ8QNxtx3Ws51mf0b90aUfh2uW5Bg=",
-        "path": "./../a",
+        "path": "/nix/store/y15jkqn1bgs4cskgczz0b11szz673hhw-source/a",
         "type": "path"
       },
       "original": {
-        "path": "./../a",
+        "path": "/nix/store/y15jkqn1bgs4cskgczz0b11szz673hhw-source/a",
         "type": "path"
       }
     },

The problem with this is that the whole repository is copied into the /nix/store before b/flake.lock gets updated with the /nix/store path it will get copied to. This means that running nix flake update again will now result in a different store path since b/flake.lock got changed. This will happen ad infinitum.

At this point I’m stuck. What’s the right way of referencing flakes from the same repository?

1 Like

This topic might be helpful to you. There’s also this comment that says dropping ./ in the relative path to Flake A could work.

1 Like

Thank you! I can confirm that this fixes the issue for me. That’s totally unexpected behaviour.

1 Like