Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot access inputs.self.outPath in top level imports #148

Open
Atry opened this issue May 7, 2023 · 4 comments
Open

Cannot access inputs.self.outPath in top level imports #148

Atry opened this issue May 7, 2023 · 4 comments

Comments

@Atry
Copy link
Contributor

Atry commented May 7, 2023

Given

# flake.nix
{
  inputs.flake-parts.url = "github:hercules-ci/flake-parts";
  outputs = inputs@{ self, flake-parts, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } ({inputs, ...}: {
      imports = [
        "${inputs.self.outPath}/my-module.nix"
      ];
    });
}
# my-module.nix
{
  flake.lib.myFunction = x: x;
}

When running

nix flake show

Got

error: infinite recursion encountered

Root cause

NixOS/nix#8300

Workaround

# flake.nix
{
  inputs.flake-parts.url = "github:hercules-ci/flake-parts";
  outputs = inputs@{ self, flake-parts, ... }:
    {
      inherit (
        flake-parts.lib.mkFlake { inherit inputs; } ({inputs, ...}: {
          imports = [
            "${inputs.self.outPath}/my-module.nix"
          ];
        })
      ) lib;
    };
}
@roberth
Copy link
Member

roberth commented May 7, 2023

Workaround 2

Instead of self.outPath, you could use the equivalent ./. (edit: unless you're writing a reusable flakeModule):

# flake.nix
{
  inputs.flake-parts.url = "github:hercules-ci/flake-parts";
  outputs = inputs@{ flake-parts, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } ({inputs, ...}: {
      imports = [
        ./my-module.nix
      ];
    });
}

@Atry
Copy link
Contributor Author

Atry commented May 7, 2023

It's not equivalent when your flake is used in another flake

@roberth roberth changed the title Cannot access inputs.self.outPath Cannot access inputs.self.outPath in top level imports May 7, 2023
@roberth
Copy link
Member

roberth commented May 7, 2023

So I guess you're writing a framework with a fixed location for a flake module then?

With the Nix we have, you can only imports from a self where you've already decided the set of outputs attrNames.
In other words, there's no workaround for NixOS/nix#8300.
There might be a solution in the form of NixOS/nix#4154, but otherwise, the best we can do is workarounds.

Workaround 3, perSystem only

Maybe this helps you or someone. perSystem generally does not affect the attrNames of the flake option.

# flake.nix
{
  inputs.flake-parts.url = "github:hercules-ci/flake-parts";
  outputs = inputs@{ self, flake-parts, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } ({inputs, ...}: {
      perSystem = {
        imports = [
          "${inputs.self.outPath}/my-module.nix"
        ];
      };
    });
}

Workaround 4, specialArgs

specialArgs was designed for imports.
You might require specialArgs.root = ./. to be passed by users. I don't think it's the right tradeoff between inconveniences, but you could require this for your framework.

Users would call

mkFlake { inherit inputs; specialArgs.root = ./.; } <module>

You would write a module such as

{ root, ... }:
{
  imports = [ (root + "/my-module.nix") ];
}

@bb010g
Copy link

bb010g commented Jul 6, 2023

See also my investigation of this in #137. I should probably extract that commit set out into its own PR.

shymega pushed a commit to shymega/flake-parts that referenced this issue Aug 18, 2024
shymega pushed a commit to shymega/flake-parts that referenced this issue Aug 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants