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 non-system output of flakeModule in submodule #81

Open
DavHau opened this issue Nov 30, 2022 · 5 comments
Open

Cannot access non-system output of flakeModule in submodule #81

DavHau opened this issue Nov 30, 2022 · 5 comments
Labels
undecided Depends on postponed design decision

Comments

@DavHau
Copy link
Contributor

DavHau commented Nov 30, 2022

To reproduce the issue:

$ git clone https://github.com/DavHau/drv-parts --branch problem
$ cd drv-parts
$ nix flake show ./examples/htop --override-input drv-parts ./.

error:

error: attribute 'modules' missing

       at /nix/store/ni13vsgnf6hv42gpnlsv42yvv73w43m0-source/examples/htop/htop.nix:6:47:

            5|   # select mkDerivation as a backend for this package
            6|   imports = builtins.trace inputs'.drv-parts [inputs'.drv-parts.modules.mkDerivation];
             |                                               ^
            7|

The problem is, that I want to access an output of my flakeModule drv-parts, but the inputs' variable only contains system prefixed outputs, like devShells and packages. Other outputs like nixosModules for example are missing.

Am I using this wrong? If not, could we also add non-system specific outputs to inputs'?

@roberth
Copy link
Member

roberth commented Nov 30, 2022 via email

@roberth
Copy link
Member

roberth commented Dec 1, 2022

Hmm, this seems to be in a (novel) "package" module. I don't think those should have direct access to such global variables as inputs', inputs, or even pkgs. Once you rely directly on those, overridability in the sense of the (classic) pkg.override goes out of the window.
For this purpose, you could create a package option called deps that is the only option to be defined in terms of pkgs and/or inputs, etc.

deps = mkOption {
  description = ''
    All dependencies of the package. This is the only option that gets to access the "outer world", such as `pkgs` or the flake `inputs`.

    By separating the task of retrieving things from the outside world, you ensure that the package is overridable.
    Nothing will stop you from adding `pkgs` itself as a dependencies, but this will make it very hard for the user of the package to override any dependencies, because they'd have to figure out a way to insert their changes into the Nixpkgs fixpoint. By adding specific attributes to `deps` instead, the user has a realistic chance of overriding those dependencies.
    
    So deps should be specific, but not overly specific. For instance, the caller shouldn't have to know the version of a dependency in order to override it. The name should suffice. (e.g. `nix = nixVersions.nix_2_12` instead of `inherit (nixVersions) nix_2_12`.
  '';
  type = types.coercedTo (types.functionTo (types.lazyAttrsOf types.raw)) kindOfLikeCallPackageButActuallyWeOnlyPassPkgsAndInputsAndInputs' (types.lazyAttrsOf types.raw);
  default = {};
  example = lib.literalExpression ''
    { pkgs, inputs', ... }: {
      inherit (pkgs) stdenv;
      inherit (pkgs.haskellPackages) pandoc;
      nix = inputs'.nix.packages.default;
    }
  '';
}

@DavHau
Copy link
Contributor Author

DavHau commented Dec 1, 2022

Thanks, that's amazing advice. I'll try to incorporate that. If you have more ideas like that, please let me know ;)

DavHau added a commit to DavHau/drv-parts that referenced this issue Dec 1, 2022
@montchr
Copy link

montchr commented Dec 2, 2022

the inputs' variable only contains system prefixed outputs, like devShells and packages. Other outputs like nixosModules for example are missing.

FWIW I’ve run into this behavior when trying to access inputs'.nixpkgs.lib -- lib was not accessible in that case, so I had to reach up to the top-level inputs.nixpkgs to get lib.

@roberth roberth added the undecided Depends on postponed design decision label Feb 1, 2023
@roberth
Copy link
Member

roberth commented Mar 31, 2023

Leaning into the difference between top level and perSystem until we can blur the distinction thanks to improved error messages; see #101 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
undecided Depends on postponed design decision
Projects
None yet
Development

No branches or pull requests

3 participants