1
0
Fork 0
mirror of https://github.com/hercules-ci/flake-parts.git synced 2025-03-16 21:38:24 +00:00

Fix infinite recursion in inputs

This commit is contained in:
Robert Hensing 2022-12-17 18:36:15 +01:00
parent 8d0e2444ab
commit bcb7065174
6 changed files with 75 additions and 15 deletions

View file

@ -1,4 +1,22 @@
# 2022-12-17
- The old syntax `mkFlake { inherit self; }` is now strongly discouraged in
favor of:
```nix
outputs = inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } { /* module */ }
```
This fixes an infinite recursion that occurs with the old syntax when
using the `inputs` module argument in `imports`.
If you're under the impression that this already worked, that's probably
because you were using `inputs` from the lexical scope (ie directly from
the flake outputs function arguments), rather than in a separate module file.
# 2022-12-07 # 2022-12-07
- The `darwinModules` option has been removed. This was added in the early days - The `darwinModules` option has been removed. This was added in the early days

View file

@ -56,8 +56,8 @@ Otherwise, add the input,
then slide `mkFlake` between your outputs function head and body, then slide `mkFlake` between your outputs function head and body,
``` ```
outputs = { self, flake-parts, ... }: outputs = inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit self; } { flake-parts.lib.mkFlake { inherit inputs; } {
flake = { flake = {
# Put your original flake attributes here. # Put your original flake attributes here.
}; };

View file

@ -1,13 +1,16 @@
let let
flake = builtins.getFlake (toString ./.); flake = builtins.getFlake (toString ./.);
fmc-lib = (builtins.getFlake (toString ../.)).lib; fmc-lib = (builtins.getFlake (toString ../.)).lib;
args = {
inherit self;
} // flake.inputs;
self = { self = {
inherit (flake) inputs; inherit (flake) inputs;
outPath = ../.; # used by pre-commit module, etc outPath = ../.; # used by pre-commit module, etc
outputs = self.config.flake; outputs = self.config.flake;
} // } //
fmc-lib.evalFlakeModule fmc-lib.mkFlake
{ inherit self; } { inputs = args; }
./flake-module.nix; ./flake-module.nix;
in in
self.config.flake // { inherit (flake) inputs; } self.config.flake // { inherit (flake) inputs; }

53
lib.nix
View file

@ -7,7 +7,9 @@ let
isAttrs isAttrs
isFunction isFunction
showOption showOption
throwIf
types types
warnIf
; ;
inherit (lib.types) inherit (lib.types)
path path
@ -60,18 +62,55 @@ let
} }
); );
errorExample = ''
For example:
outputs = inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } { /* module */ };
To avoid an infinite recursion, *DO NOT* pass `self.inputs` and
*DO NOT* pass `inherit (self) inputs`, but pass the output function
arguments as `inputs` like above.
'';
flake-parts-lib = { flake-parts-lib = {
evalFlakeModule = evalFlakeModule =
{ self args@
{ inputs ? self.inputs
, specialArgs ? { } , specialArgs ? { }
}:
module:
lib.evalModules { # legacy
specialArgs = { inherit self flake-parts-lib; inherit (self) inputs; } // specialArgs; , self ? inputs.self or (throw ''
modules = [ ./all-modules.nix module ]; When invoking flake-parts, you must pass all the flake output arguments,
}; and not just `self.inputs`.
${errorExample}
'')
}:
throwIf
(!args?self && !args?inputs) ''
When invoking flake-parts, you must pass in the flake output arguments.
${errorExample}
''
warnIf
(!args?inputs) ''
When invoking flake-parts, it is recommended to pass all the flake output
arguments in the `inputs` parameter. If you only pass `self`, it's not
possible to use the `inputs` module argument in the module `imports`.
Please pass the output function arguments. ${errorExample}
''
(module:
lib.evalModules {
specialArgs = {
inherit self flake-parts-lib;
inputs = args.inputs or /* legacy, warned above */ self.inputs;
} // specialArgs;
modules = [ ./all-modules.nix module ];
}
);
mkFlake = args: module: mkFlake = args: module:
(flake-parts-lib.evalFlakeModule args module).config.flake; (flake-parts-lib.evalFlakeModule args module).config.flake;

View file

@ -5,8 +5,8 @@
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
}; };
outputs = { self, flake-parts, ... }: outputs = inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit self; } { flake-parts.lib.mkFlake { inherit inputs; } {
imports = [ imports = [
# To import a flake module # To import a flake module
# 1. Add foo to inputs # 1. Add foo to inputs

View file

@ -5,8 +5,8 @@
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
}; };
outputs = { self, flake-parts, ... }: outputs = inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit self; } { flake-parts.lib.mkFlake { inherit inputs; } {
imports = [ imports = [
./hello/flake-module.nix ./hello/flake-module.nix
]; ];