[Solved] Nested flakes with Nix 2.26+

For quite some time I have a single Git repo to maintain multiple hosts and virtual servers. Ignoring the multi-host aspect, my setup is like this:

/flake.nix - entry point for nixos config
/servers/flake.nix - configures all servers / adds systemd files…
/servers/vm01/flake.nix - some server as VM
/servers/cont23/flake.nix - some server as container

Each subdirectory / flake can live on its own and is a valid flake. So is the servers-flake (that creates a module-list for nixosConfigurations). So no flake depends on its parent.

This worked fine for quite some time, but since 2.26+ I run into errors with lock files. Here is a MVP-reproducer:

/flake.nix

{
  description = "Main flake: uses servers flake";

  inputs = {
    servers.url = "path:./servers";
  };

  outputs = { self, servers }:
    {
      packages.x86_64-linux.default = servers.server;
    };
}

/servers/flake.nix

{
  description = "Servers flake: aggregates individual server flakes";

  inputs = {
    foo.url = "path:./foo";
  };

  outputs = { self, foo }:
    {
      server = foo.foo;
    };
}

servers/foo/flake.nix

{
  description = "Foo flake";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
  };

  outputs = { self, nixpkgs }:
    let
      pkgs = import nixpkgs { system = "x86_64-linux"; };
    in
    {
      foo = pkgs.hello;
    };
}

An initial nix run works as expected. But then, a flake.lock is created and a further run results in:

$ nix run
error:
       … while evaluating the attribute 'server'
         at /nix/store/4zfn4xmmsp3j4qmi42mbp782rczlpva0-source/servers/flake.nix:10:7:
            9|     {
           10|       server = foo.foo;
             |       ^
           11|     };

       … while evaluating the attribute 'foo.result'
         at «flakes-internal»/call-flake.nix:98:7:
           97|     {
           98|       result =
             |       ^
           99|         if node.flake or true then

       (stack trace truncated; use '--show-trace' to show the full, detailed trace)

       error: path '/nix/store/4zfn4xmmsp3j4qmi42mbp782rczlpva0-source/servers/servers/foo/flake.nix' does not exist

For some reason servers is twice in the path.

I’ve been pulling my hair out over this since weeks, and all super-duper AI to the rescue couldn’t help me either…

I really hope, someone can tell me what I’m doing wrong here. Any help very much appreciated!

1 Like

Nix bugs/regressions should be reported here: GitHub · Where software is built

But it seems like someone’s beat you to it: Nested flake source paths broken · Issue #13164 · NixOS/nix · GitHub
It was fixed in the 2.30.x branch and backported to 2.28 and 2.29. However there hasn’t been a tagged release since it was merged on Jun 27 for those branches.

So for now, you’d have to use nix 2.30 (nixVersions.nix_2_30), or build nix 2.28 yourself (i.e. add it as a flake input, use the package from said input, which will cause nix to get rebuilt locally). Put either value in nix.package in your system config, rebuild, etc.

3 Likes

ahh! You’re a lifesaver! 2.30.2 “just works”. Thanks a lot!

1 Like