From bcb7065174f014567157c6e87531d0f3e426f182 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sat, 17 Dec 2022 18:36:15 +0100 Subject: [PATCH] Fix infinite recursion in inputs --- ChangeLog.md | 18 +++++++++++ README.md | 4 +-- dev/default.nix | 7 +++-- lib.nix | 53 ++++++++++++++++++++++++++++----- template/default/flake.nix | 4 +-- template/multi-module/flake.nix | 4 +-- 6 files changed, 75 insertions(+), 15 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index c9d894e..79f5b95 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -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 - The `darwinModules` option has been removed. This was added in the early days diff --git a/README.md b/README.md index 8283840..900ed60 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,8 @@ Otherwise, add the input, then slide `mkFlake` between your outputs function head and body, ``` - outputs = { self, flake-parts, ... }: - flake-parts.lib.mkFlake { inherit self; } { + outputs = inputs@{ flake-parts, ... }: + flake-parts.lib.mkFlake { inherit inputs; } { flake = { # Put your original flake attributes here. }; diff --git a/dev/default.nix b/dev/default.nix index 6fa49dd..df712a4 100644 --- a/dev/default.nix +++ b/dev/default.nix @@ -1,13 +1,16 @@ let flake = builtins.getFlake (toString ./.); fmc-lib = (builtins.getFlake (toString ../.)).lib; + args = { + inherit self; + } // flake.inputs; self = { inherit (flake) inputs; outPath = ../.; # used by pre-commit module, etc outputs = self.config.flake; } // - fmc-lib.evalFlakeModule - { inherit self; } + fmc-lib.mkFlake + { inputs = args; } ./flake-module.nix; in self.config.flake // { inherit (flake) inputs; } diff --git a/lib.nix b/lib.nix index a57d687..4a5bf63 100644 --- a/lib.nix +++ b/lib.nix @@ -7,7 +7,9 @@ let isAttrs isFunction showOption + throwIf types + warnIf ; inherit (lib.types) 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 = { evalFlakeModule = - { self + args@ + { inputs ? self.inputs , specialArgs ? { } - }: - module: - lib.evalModules { - specialArgs = { inherit self flake-parts-lib; inherit (self) inputs; } // specialArgs; - modules = [ ./all-modules.nix module ]; - }; + # legacy + , self ? inputs.self or (throw '' + 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: (flake-parts-lib.evalFlakeModule args module).config.flake; diff --git a/template/default/flake.nix b/template/default/flake.nix index 8ecef23..ab4bc55 100644 --- a/template/default/flake.nix +++ b/template/default/flake.nix @@ -5,8 +5,8 @@ nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; }; - outputs = { self, flake-parts, ... }: - flake-parts.lib.mkFlake { inherit self; } { + outputs = inputs@{ flake-parts, ... }: + flake-parts.lib.mkFlake { inherit inputs; } { imports = [ # To import a flake module # 1. Add foo to inputs diff --git a/template/multi-module/flake.nix b/template/multi-module/flake.nix index 1f27514..82cd3bd 100644 --- a/template/multi-module/flake.nix +++ b/template/multi-module/flake.nix @@ -5,8 +5,8 @@ nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; }; - outputs = { self, flake-parts, ... }: - flake-parts.lib.mkFlake { inherit self; } { + outputs = inputs@{ flake-parts, ... }: + flake-parts.lib.mkFlake { inherit inputs; } { imports = [ ./hello/flake-module.nix ];