numtide / treefmt-nix

treefmt nix configuration
https://numtide.github.io/treefmt/
MIT License
270 stars 83 forks source link

eval error using flake-parts: attribute 'flake-parts-lib' missing #271

Closed aidalgol closed 4 hours ago

aidalgol commented 1 day ago

Describe the bug

Trying to use treefmt via flake-parts produces an eval error.

To Reproduce

Steps to reproduce the behavior:

  1. Create an empty directory
  2. Write the following flake.nix file.

    {
     inputs = {
       flake-parts.url = "github:hercules-ci/flake-parts";
       treefmt-nix.url = "github:numtide/treefmt-nix";
       nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
     };
     outputs =
       {
         flake-parts,
         nixpkgs,
         ...
       }@inputs:
       flake-parts.lib.mkFlake { inherit inputs; } {
         systems = [ "x86_64-linux" ];
         perSystem =
           {
             config,
             self',
             inputs',
             pkgs,
             system,
             ...
           }:
           {
             imports = [
               inputs.treefmt-nix.flakeModule
             ];
    
             treefmt = {
               projectRootFile = "flake.nix";
               programs.nixfmt-rfc-style.enable = true;
             };
           };
         flake = { };
       };
    }
  3. Run nix flake lock
  4. Run nix fmt
error:
… while calling the 'head' builtin
at /nix/store/f9zgiyh2mhgqrrbbmbn3cwcjhbn2fzf6-source/attrsets.nix:1575:11:
1574|         || pred here (elemAt values 1) (head values) then
1575|           head values
|           ^
1576|         else

… while evaluating the attribute 'value'
at /nix/store/f9zgiyh2mhgqrrbbmbn3cwcjhbn2fzf6-source/modules.nix:816:9:
815|     in warnDeprecation opt //
816|       { value = addErrorContext "while evaluating the option `${showOption loc}':" value;
|         ^
817|         inherit (res.defsFinal') highestPrio;

(stack trace truncated; use '--show-trace' to show the full trace)

error: attribute 'flake-parts-lib' missing
at /nix/store/f9zgiyh2mhgqrrbbmbn3cwcjhbn2fzf6-source/modules.nix:515:28:
514|         addErrorContext (context name)
515|           (args.${name} or config._module.args.${name})
|                            ^
516|       ) (functionArgs f);

(The full trace does not include any lines in flake.nix, so I have omitted it.)

Expected behavior

nix fmt to run treefmt.

System information

Additional context

There is also a documentation bug here, #270, so I had to try working out how to use this from just the minimal example in the README.

zimbatm commented 22 hours ago

Try moving the imports one level up. The issue is that the module is designed to be imported on the top-level instead of per system.

diff --git a/repro/flake.nix b/repro/flake.nix
index 3cc165a..114aceb 100644
--- a/repro/flake.nix
+++ b/repro/flake.nix
@@ -12,6 +12,11 @@
     }@inputs:
     flake-parts.lib.mkFlake { inherit inputs; } {
       systems = [ "x86_64-linux" ];
+
+      imports = [
+        inputs.treefmt-nix.flakeModule
+      ];
+
       perSystem =
         {
           config,
@@ -22,10 +27,6 @@
           ...
         }:
         {
-          imports = [
-            inputs.treefmt-nix.flakeModule
-          ];
-
           treefmt = {
             projectRootFile = "flake.nix";
             programs.nixfmt-rfc-style.enable = true;
aidalgol commented 4 hours ago

Yep, that was the problem. Thanks!