Skip to content

Commit

Permalink
fix: pass inputs arguments to modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ratson committed Nov 2, 2024
1 parent 12152af commit 56cec61
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 5 deletions.
3 changes: 2 additions & 1 deletion examples/nixos/nix/homeModules/_default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{ config, lib, pkgs, ... }:
{ config, lib, pkgs, inputs, inputs', ... }:

{
home.packages = [
pkgs.hello
inputs'.greet.packages.greet
];
}
3 changes: 2 additions & 1 deletion examples/nixos/nix/nixosModules/_default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{ config, lib, pkgs, ... }:
{ config, lib, pkgs, inputs, ... }:

{
environment.systemPackages = [
pkgs.hello
inputs.greet.packages.${pkgs.system}.greet
];
}
37 changes: 37 additions & 0 deletions modules/config/argsModule.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{ config, lib, conflake, ... }:

let
inherit (builtins) mapAttrs;
inherit (lib) mkDefault mkOption types;

argsModule = { inputs, pkgs, ... }:
let
inherit (pkgs.stdenv.hostPlatform) system;
inputs' = mapAttrs (_: conflake.selectAttr system) inputs;
in
{
_file = ./argsModule.nix;

config = {
_module.args = mapAttrs (_: v: mkDefault v) {
inherit conflake inputs';
inherit (config) inputs;

self = inputs.self;
self' = inputs'.self;
};
};
};
in
{
options = {
argsModule = mkOption {
type = types.deferredModule;
default = argsModule;
internal = true;
description = ''
Module to provide extra args.
'';
};
};
}
7 changes: 7 additions & 0 deletions modules/homeModules.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{ config, lib, conflake, moduleArgs, ... }:

let
inherit (builtins) mapAttrs;
inherit (lib) mkOption mkIf mkMerge;
inherit (lib.types) lazyAttrsOf;
inherit (conflake.types) module nullable optCallWith;
Expand All @@ -14,6 +15,12 @@ in

homeModules = mkOption {
type = optCallWith moduleArgs (lazyAttrsOf module);
apply = mapAttrs (_: module: {
imports = [
config.argsModule
module
];
});
default = { };
};
};
Expand Down
1 change: 1 addition & 0 deletions modules/module-list.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
./config/argsModule.nix
./apps.nix
./autoInputs.nix
./builtinFormatters.nix
Expand Down
7 changes: 7 additions & 0 deletions modules/nixosModules.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{ config, lib, conflake, moduleArgs, ... }:

let
inherit (builtins) mapAttrs;
inherit (lib) mkOption mkIf mkMerge;
inherit (lib.types) lazyAttrsOf;
inherit (conflake.types) module nullable optCallWith;
Expand All @@ -14,6 +15,12 @@ in

nixosModules = mkOption {
type = optCallWith moduleArgs (lazyAttrsOf module);
apply = mapAttrs (_: module: {
imports = [
config.argsModule
module
];
});
default = { };
};
};
Expand Down
18 changes: 15 additions & 3 deletions tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,18 @@ in
description = "test template";
});

template-fn = test
(conflake' {
template = { inputs, ... }: {
path = ./test;
description = "test template";
};
})
(f: f.templates.default == {
path = ./test;
description = "test template";
});

templates = test
(conflake' {
templates.test-template = {
Expand Down Expand Up @@ -707,7 +719,7 @@ in
}))
(f: f ? nixosConfigurations.test.config.system.build.toplevel);

nixosConfigurationsManual = test
nixosConfigurations-manual = test
(conflake' ({ lib, ... }: {
nixosConfigurations.test = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
Expand All @@ -716,13 +728,13 @@ in
}))
(f: f ? nixosConfigurations.test.config.system.build.toplevel);

nixosConfigurationsManualWithProp = test
nixosConfigurations-manualWithProp = test
(conflake' ({ lib, config, ... }: {
nixosConfigurations.test = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
config.propagationModule
({ flake, ... }: {
({ flake, inputs', ... }: {
system.stateVersion = "24.05";
environment.variables = {
TEST1 = flake.inputs.nixpkgs.legacyPackages.x86_64-linux.hello;
Expand Down

0 comments on commit 56cec61

Please sign in to comment.