nix-community / flakelight

Framework for simplifying flake setup [maintainer=@accelbread]
MIT License
177 stars 3 forks source link

`inputs` in `nixosModule` is not available for external flake #22

Open ratson opened 1 month ago

ratson commented 1 month ago
nix build "github:ratson/bug-report/flakelight-nixos-module?dir=flake2#nixosConfigurations.vm2.config.system.build.vm"

gives

error: attribute 'inputs'' missing

While the following is working,

nix build "github:ratson/bug-report/flakelight-nixos-module?dir=flake1#nixosConfigurations.vm1.config.system.build.vm"

Source code in https://github.com/ratson/bug-report/tree/flakelight-nixos-module

@accelbread Would you check if usage in flake2/flake.nix is correct?

FYR, here is a copy of it,

{
  inputs = {
    nixpkgs.url = "nixpkgs/nixos-unstable";
    flake1.url = "path:../flake1";
  };
  outputs = { nixpkgs, flake1, ... }@inputs:
    let
      system = "x86_64-linux";
    in
    {
      nixosConfigurations.vm2 = nixpkgs.lib.nixosSystem {
        inherit system;

        modules = [
          flake1.nixosModules.default
        ];
      };

      packages.${system}.hello2 = flake1.packages.${system}.hello1;
    };
}

There is test.sh script in the repo to run locally to verify the bug.

accelbread commented 1 week ago

Sorry for delay; have been busy.

The inputs' arg is provided by flakelight, so is not available in second flake. The args to modules come from the nixosSystem as well, so inputs' in a module refers to the inputs of the flake defining the system, not the one that defined the module.

If you want to use packages from another flake, I'd recommend having the module use the package from the pkgs arg, and having the second flake's system use the first's overlay.

ratson commented 1 week ago

@accelbread Should the inputs' be provided to flake1.nixosModules.default with flake1's inputs? Or is there a way to refer to flake1's inputs in flale1.nixosModules, so that it can used by another flake?

ratson commented 1 week ago

To better illustrate the problem, I updated flake1 to include vm2,

https://github.com/ratson/bug-report/blob/flakelight-nixos-module/flake1/flake.nix#L13-L19

      nixosConfigurations.vm2 = nixpkgs.lib.nixosSystem {
        inherit system;

        modules = [
          self.nixosModules.default
        ];
      };

It is strange that nix build ./flake1#nixosConfigurations.vm1.config.system.build.vm works, while nix build ./flake1#nixosConfigurations.vm2.config.system.build.vm fails.

Here is the vm1 code,

https://github.com/ratson/bug-report/blob/flakelight-nixos-module/flake1/nix/nixos/vm1.nix

{ inputs, ... }:

{
  system = "x86_64-linux";

  modules = [
    inputs.self.nixosModules.default
  ];
}
accelbread commented 4 days ago

if you change vm2 to not call nixosSystem explicitly, it will work:

      nixosConfigurations.vm2 = {
        inherit system;

        modules = [
          self.nixosModules.default
        ];
      };

If you don't call it, flakelight can propagate inputs and etc. to the nixos invocation since it calls it. If you call it yourself, flakelight can't add its stuff. You can call it yourself and add the flakelight stuff by adding config.propogationModule to your modules. Can see here: https://github.com/nix-community/flakelight/blob/2a96b83bf6ffad4b8a15afe19e883ca1a1720d88/API_GUIDE.md?plain=1#L965-L977

ratson commented 14 hours ago

self.nixosModules.default should propagate inputs and moduleArgs for outputs, so non-flakelight users could use it.

I don't know if it can be generically implemented or not, here is a working example,

https://github.com/ratson/bug-report/blob/d0034228e83bb99f30bb60ea905bc2ee262ec364/flake1/flake.nix#L21-L29