Copy formatter config from another flake error

I want to copy the nixfmt formatter config from my common flake into my desktop flake and server flake.
The common flake contains the setup recommended in the documentation for nixfmt:

{
  inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-24.05";
  inputs.flake-utils.url = "github:numtide/flake-utils";

  outputs = { self, nixpkgs, flake-utils }: flake-utils.lib.eachDefaultSystem (system:
    let
      pkgs = import nixpkgs { inherit system; };
    in {
      formatter = pkgs.nixfmt-rfc-style;
    }
  );

When running nix fmt it formats the flake as expected.

The other repos contain the following:

  inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-24.05";
  inputs.flake-utils.url = "github:numtide/flake-utils";
  common.url = "path:/path/to/common/flake";

  outputs = { self, nixpkgs, flake-utils, common }: flake-utils.lib.eachDefaultSystem (system:
    let
      pkgs = import nixpkgs { inherit system; };
    in {
      formatter = common.formatter;
    }
  );

When running nix fmt it here fails with:

error: attribute 'formatter.x86_64-linux.type' does not exist

However, when checking if that attribute exists using builtins.trace, like this:

formatter = builtins.trace common.formatter.x86_64-linux.type common.formatter;

it very much exists and prints: trace: derivation.
When I try to set it manually using formatter.x86_64-linux.type = "derivation" it complains that the attribute already exists.

What is going wrong here and how can I make it work?

Simple, stop using flake-utils, as you basically wrote something like formatter.x86_64-linux = common.formatter; which would become formatter.x86_64-linux = { x86_64-linux = pkgs.nixfmt-rfc-style; };

i.e. you’re doubling the system in this case

Also, you’re using two different nixpkgs instances (one for your current flake and one for the “common” flake), so be wary of that.

1 Like

oof, that is quite the oversight. Thanks a lot for your reply.
I do wanna keep using flake utils since I am targeting amd64 and arm linux, and arm darwin, so changing it to formatter = common.formatter.${system} did the trick.

Also thanks for making me aware of the potential nixpkgs mismatch.

In that case you can utilise the technique shown here:

Nevertheless I think this makes it clearer when something is “system-dependent” vs not.

I think a simpler solution would be

  outputs = { common, ... }: {
    inherit (common) formatter;
  };

Which obviates the need for flake-utils or even the genAttrs approach entirely on the “child” flakes. (Ignoring any other outputs for the moment.)