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

No access to flake inputs within a perSystem module #101

Open
DavHau opened this issue Jan 6, 2023 · 4 comments
Open

No access to flake inputs within a perSystem module #101

DavHau opened this issue Jan 6, 2023 · 4 comments

Comments

@DavHau
Copy link
Contributor

DavHau commented Jan 6, 2023

Similar to #81.

Trying to add inputs to the perSystem args, triggers this error:

error: `inputs` (without `'`) is not a `perSystem` module argument, but a
       module argument of the top level config.

       The following is an example usage of `inputs`. Note that its binding
       is in the `top` parameter list, which is declared by the top level module
       rather than the `perSystem` module.

         top@{ config, lib, inputs, ... }: {
           perSystem = { config, inputs', ... }: {
             # in scope here:
             #  - inputs
             #  - inputs'
             #  - config (of perSystem)
             #  - top.config (note the `top@` pattern)
           };
         }

The statements about the scope only hold true as long as all code is defined inside a single flake.nix file.
The lexical scope breaks as soon as the code is split into modules.

For example given the following flake.nix:

{
  inputs = {
    flake-parts.url = "github:hercules-ci/flake-parts";
    poetry2nix.url = "poetry2nix";
  };

  outputs = inputs@{ flake-parts, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      systems = [
        "x86_64-linux"
      ];
      perSystem = { config, pkgs, ... }: {
        imports = [./my-package.nix];
      };
    };
}

and my-package.nix:

# flake-parts module for the tesh package
{config, pkgs, inputs, self, ...}: let
  poetry2nix = import inputs.poetry2nix {inherit pkgs;};
in {
  packages.tesh = poetry2nix.mkPythonPplication {
    projectDir = self;
  };
}

This breaks because inputs is not allowed as a function argument. This means it is not possible to access some inputs lib or similar things inside a perSystem module.

@DavHau DavHau changed the title No access to flake inputs within a preSystem module No access to flake inputs within a perSystem module Jan 6, 2023
@roberth
Copy link
Member

roberth commented Jan 6, 2023

The idea behind this restriction is to make sure that all flake-parts modules are in one, compatible form.
This convention avoids weird errors that would arise when importing a perSystem-level module into the top level or vice versa.

The statements about the scope only hold true as long as all code is defined inside a single flake.nix file.
The lexical scope breaks as soon as the code is split into modules.

This should work if you define the module as a top-level module. The top-level module provides the lexical scope with these variables, as suggested in the error message.

@DavHau DavHau closed this as completed Jan 9, 2023
@roberth
Copy link
Member

roberth commented Mar 31, 2023

This decision could be reverted when we have

@Atry
Copy link
Contributor

Atry commented Nov 16, 2023

I think the top-level module should always be a flake module, then it can define nested nixosModules/flakeModules. For example:

When defining a nested flakeModules, the top level inputs are from the flake that defines the module, and the nested inputs are from the flake that use the module.

@TECHNOFAB11
Copy link

I just had the same issue, I wanted to access my util flake from a perSystem module. A workaround is adding this to the imports where the module gets imported:

({...}: {
  _module.args.origInputs = inputs;
})

I could then just use origInputs.<flake>

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

4 participants