NixOS / nixfmt

The official (but not yet stable) formatter for Nix code
https://nixfmt.serokell.io
Mozilla Public License 2.0
907 stars 39 forks source link

cabal2nix prevents `nix flake show` (IFD) #140

Open spikespaz opened 11 months ago

spikespaz commented 11 months ago

The formatter is specified like this:

{
  inputs = {
    nixpkgs-lib.url = "github:nix-community/nixpkgs.lib";
    systems.url = "github:nix-systems/default";
    nixfmt.url = "github:serokell/nixfmt";
  };
  outputs = { self, nixpkgs-lib, systems, nixfmt }:
    let
      inherit (self.lib) lib;
      eachSystem = lib.genAttrs (import systems);
    in {
      # ...
      formatter = eachSystem (system: nixfmt.packages.${system}.default);
    };
}

When you run nix flake show:

warning: formatter.aarch64-darwin omitted (use '--all-systems' to show)
warning: formatter.aarch64-linux omitted (use '--all-systems' to show)
warning: formatter.x86_64-darwin omitted (use '--all-systems' to show)
error:
       … while evaluating the attribute 'packages.x86_64-linux.default'

         at /nix/store/wfjzvlbfj7ccczbphcbd16ncgxs2bj24-source/flake.nix:73:11:

           72|         packages = rec {
           73|           default = nixfmt;
             |           ^
           74|           nixfmt = pkgs.haskellPackages.nixfmt;

       … while evaluating the attribute 'haskellPackages.nixfmt'

         at /nix/store/wfjzvlbfj7ccczbphcbd16ncgxs2bj24-source/flake.nix:31:15:

           30|             packageOverrides = self: super: {
           31|               nixfmt = self.callCabal2nix "nixfmt" src { };
             |               ^
           32|             };

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

       error: cannot build '/nix/store/770ly489hm8i079paqjq2153c0izx7k6-cabal2nix-nixfmt.drv' during evaluation because the option 'allow-import-from-derivation' is disabled
infinisil commented 11 months ago

Unfortunately not really fixable nicely here, this is a Nix bug: https://github.com/NixOS/nix/issues/4265

yorickvP commented 11 months ago

We could commit the cabal2nix output..

infinisil commented 11 months ago

Yeah that would work, it's just not nice, since it requires manual updates (or setting up automated ones). I don't think that's worth it for a bug that should really be fixed in Nix.

spikespaz commented 11 months ago

For the sake of having this formatter be adopted by anyone, I wouldn't be waiting for Nix to solve this particular issue for us. It's been an issue for quite some time now, and I can't say "run nix flake show to see what this provides" and instead I have to say "use this complicated Nix eval piped to sed".

spikespaz commented 11 months ago

Also prevents nix flake check do that you have to come up with something other than checking the status code in an integration script.

SuperSandro2000 commented 11 months ago

I would also like to see IFD not being fully required as it prevents usage in hydra.

infinisil commented 11 months ago

Fair enough, let's reconsider!

yorickvP commented 11 months ago

Note that this package is already in nixpkgs without IFD via hackage

remi-gelinas commented 5 months ago

This is actually blocking my ability to use nix fmt as well - specifying the nixfmt package here: https://github.com/remi-gelinas/rosetta/blob/trunk/flake.nix#L45

Results in nix fmt failing. Targeting the aarch64-darwin package on an M1 machine, it still attempts to evaluate the formatters for other systems, and encounters IFD as aarch64-darwin can't build the cabal2nix package for x86_64-linux.

Not sure if this specifically is a Nix issue or a nixfmt issue, or one that I can work around, but it's really blocking my attempt to adopt nixfmt :/

infinisil commented 5 months ago

@remi-gelinas Hmm I just tried it on an aarch64-darwin machine, but couldn't reproduce the problem. I also don't see why it would need to build anything for x86_64-linux. Can you share exactly how you're getting the problem along with the error message?

remi-gelinas commented 5 months ago

I just did an isolated reproduction attempt with a barebones flake like so:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs?rev=c4ce542c774aa505fd80d86d9d5de51349328e43";
    nixfmt.url = "github:NixOS/nixfmt?rev=3bcb63c13e7aaf0b8e14081cf0c14c44f62e840a";
  };
  outputs = {nixpkgs, nixfmt, ...}: let
        supportedSystems = [
        "aarch64-darwin"
        "x86_64-linux"
      ];

      forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
  in {
    formatter = forAllSystems (system: nixfmt.packages.${system}.nixfmt);
  };
}

and it works correctly, totally as expected. I am assuming the problem lies somewhere in my usage of flake-parts to generate the flake options then. Seems to not be an actual issue! That being said, I do still have to explicitly allow IFD for flake operations to work as expected, so this issue is still a problem