Skip to content

Flake integration in README.md fails with error: attribute 'config' missing #273

Open
@tomaskala

Description

@tomaskala

Describe the bug

When I copy over the example Flake configuration from README.md and attempt to run either nix fmt or nix flake check, I get an error. Specifically, running nix fmt produces

error: attribute 'config' missing
       at /nix/store/7awqj4fc64wnv8y81l5cdk1h5n4wv66p-source/flake.nix:15:37:
           14|       # for `nix fmt`
           15|       formatter = eachSystem (pkgs: treefmtEval.config.build.wrapper);
             |                                     ^
           16|       # for `nix flake check`

while running nix flake check produces

error:
       … while checking flake output 'checks'
         at /nix/store/7awqj4fc64wnv8y81l5cdk1h5n4wv66p-source/flake.nix:17:7:
           16|       # for `nix flake check`
           17|       checks = eachSystem (pkgs: {
             |       ^
           18|         formatting = treefmtEval.config.build.check self;

       … while checking the derivation 'checks.aarch64-darwin.formatting'
         at /nix/store/7awqj4fc64wnv8y81l5cdk1h5n4wv66p-source/flake.nix:18:9:
           17|       checks = eachSystem (pkgs: {
           18|         formatting = treefmtEval.config.build.check self;
             |         ^
           19|       });

       error: attribute 'config' missing
       at /nix/store/7awqj4fc64wnv8y81l5cdk1h5n4wv66p-source/flake.nix:18:22:
           17|       checks = eachSystem (pkgs: {
           18|         formatting = treefmtEval.config.build.check self;
             |                      ^
           19|       });

To Reproduce

Steps to reproduce the behavior:

  1. Create an empty directory
  2. cd into it
  3. Run git init
  4. Copy the flake.nix and treefmt.nix from the Flakes readme section
  5. Run git add -A
  6. Run nix fmt or nix flake check

Expected behavior

Running nix fmt should not fail. The example configuration enables the terraform formatter but an empty repository contains no Terraform files, so this should be a no-op.

Likewise, running nix flake check should not fail but be a no-op in the example empty repository.

System information

Running MacOS Sonoma 14.7.1, my Nix version is 2.24.10.

Additional context

I tracked the error to eachDefaultSystem being called in this section of flake.nix:

# Eval the treefmt modules from ./treefmt.nix
treefmtEval = eachSystem (pkgs: treefmt-nix.lib.evalModule pkgs ./treefmt.nix);

When I write my flake.nix like this instead, everything is working fine:

# flake.nix
{
  inputs.treefmt-nix.url = "github:numtide/treefmt-nix";

  outputs = { self, nixpkgs, systems, treefmt-nix }:
    let
      # Small tool to iterate over each systems
      eachSystem = f: nixpkgs.lib.genAttrs (import systems) (system: f nixpkgs.legacyPackages.${system});

      # Import the treefmt modules from ./treefmt.nix
      treefmtConfig = import ./treefmt.nix;
    in
    {
      # for `nix fmt`
      formatter = eachSystem (pkgs: treefmt-nix.lib.mkWrapper pkgs treefmtConfig);
      # for `nix flake check`
      checks = eachSystem (pkgs: {
        formatting = (treefmt-nix.lib.evalModule pkgs treefmtConfig).config.build.check self;
      });
    };
}

However, I'm not sure if this is how the configuration should be written, so I wanted to discuss this first before opening a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions