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

in older Nix versions, nix fmt using treefmt-nix has different results depending on cwd. #303

Open
arilotter opened this issue Jan 22, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@arilotter
Copy link

arilotter commented Jan 22, 2025

note

it seems this issue happens with Lix 2.91, but doesn't happen with nix 2.24.
Maybe the fix is to note a minimum supported Nix version in the README?

Describe the bug

NOTE: I'm using lix

using treefmt-nix, i have my treefmt.nix file in a ./nix folder, while my flake is in the root.

if i set projectRootFile = "./flake.nix";
then formatting doesn't work if i run nix fmt in the cwd of the ./nix folder, just outputting

traversed 0 files
emitted 0 files for processing
formatted 0 files (0 changed) in 1ms

even though the nix fmt command logs path '/home/ari/src/project_here/nix' does not contain a 'flake.nix', searching up and runs successfully.

but if i set it to projectRootFile = "../flake.nix";, then it doesn't work when i run nix fmt from the root, with
treefmt: error: could not find [../flake.nix] in /home/ari/src/project_here.

To Reproduce

Steps to reproduce the behavior:

  1. Create the following folder structure:
flake.nix
nix/
  treefmt.nix

and write to treefmt.nix

{ pkgs, ... }:
{
  projectRootFile = "../flake.nix";
  programs.nixfmt.enable = true;
}

and flake.nix

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    treefmt-nix.url = "github:numtide/treefmt-nix";
  };
  outputs = { self, nixpkgs, systems, treefmt-nix }:
    let
      eachSystem = f: nixpkgs.lib.genAttrs (import systems) (system: f nixpkgs.legacyPackages.${system});
      treefmtEval = eachSystem (pkgs: treefmt-nix.lib.evalModule pkgs ./nix/treefmt.nix);
    in
    {
      formatter = eachSystem (pkgs: treefmtEval.${pkgs.system}.config.build.wrapper);
    };
}

Expected behavior
In treefmt.nix,
either projectRootFile = "../flake.nix"; should work when i run nix fmt from the root,
or projectRootFile = "./flake.nix"; should work when i run nix fmt from the nix folder.
System information

Commit f2cc121df15418d028a59c9737d38e3a90fbaf8f on NixOS 25.05.20250115.0bd024d (Warbler) x86_64

nix (Lix, like Nix) 2.91.1

Reproduction is available here: https://github.com/arilotter/fmt-tree-bug

@arilotter arilotter added the bug Something isn't working label Jan 22, 2025
@arilotter arilotter changed the title nix fmt using treefmt-nix has different results depending on cwd. in older Nix versions, nix fmt using treefmt-nix has different results depending on cwd. Jan 22, 2025
@zimbatm
Copy link
Member

zimbatm commented Jan 23, 2025

This is because nix fmt on older versions is passing the "." argument to the underlying formatter if no other arguments are passed to it. Unfortunately there isn't a good way from treefmt's perspective to find out if the "." argument was passed by default, or if it comes from the user's intent to only format the current folder.

NixOS/nix#11438 is fixing the issue with Nix. I submitted the same patch to Lix as well.

@arilotter
Copy link
Author

Latest Lix includes that patch. have you considered a minimum supporter Nix version listed somewhere?

@zimbatm
Copy link
Member

zimbatm commented Jan 24, 2025

Sounds good, we could add a "works best with Nix 2.25 or Lix 2.92 or later.

zimbatm added a commit that referenced this issue Jan 24, 2025
Document what versions of Nix work best with the project.

Fixes #303
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

2 participants