Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tomaskala opened this issue Dec 6, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@tomaskala
Copy link

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.

@tomaskala tomaskala added the bug Something isn't working label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant