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

feature/treefmt - integrate treefmt-nix #1679

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Running ``devenv init`` generates ``devenv.nix``:
# https://devenv.sh/git-hooks/
git-hooks.hooks.shellcheck.enable = true;

# https://devenv.sh/treefmt/
treefmt.programs.nixpkgs-fmt.enable = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, not that important feature to be in the README


# https://devenv.sh/processes/
processes.ping.exec = "ping localhost";
}
Expand Down
85 changes: 31 additions & 54 deletions devenv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@
]
},
"locked": {
"lastModified": 1733312601,
"lastModified": 1736143030,
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "205b12d8b7cd4802fbcb8e8ef6a0f1408781a4f9",
"rev": "b905f6fc23a9051a6e1b741e1438dbfc0634c6de",
"type": "github"
},
"original": {
Expand All @@ -63,32 +63,10 @@
"type": "github"
}
},
"git-hooks": {
"inputs": {
"flake-compat": "flake-compat",
"gitignore": "gitignore",
"nixpkgs": [
"nixpkgs"
],
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1734425854,
"owner": "cachix",
"repo": "git-hooks.nix",
"rev": "0ddd26d0925f618c3a5d85a4fa5eb1e23a09491d",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "git-hooks.nix",
"type": "github"
}
},
"gitignore": {
"inputs": {
"nixpkgs": [
"git-hooks",
"pre-commit-hooks",
"nixpkgs"
]
},
Expand All @@ -108,10 +86,10 @@
"libgit2": {
"flake": false,
"locked": {
"lastModified": 1734347458,
"lastModified": 1737414590,
"owner": "libgit2",
"repo": "libgit2",
"rev": "3251d1bb62b8c420988d16c098c48ac463f548b0",
"rev": "346930e2808eaf9d74d895b6efc3abd085086a33",
"type": "github"
},
"original": {
Expand All @@ -122,7 +100,7 @@
},
"nix": {
"inputs": {
"flake-compat": "flake-compat_2",
"flake-compat": "flake-compat",
"flake-parts": "flake-parts",
"libgit2": "libgit2",
"nixpkgs": "nixpkgs",
Expand All @@ -146,10 +124,10 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1734284970,
"lastModified": 1735651292,
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "6903830d1075f13346be0ea9611ae6ef50a472d6",
"rev": "0da3c44a9460a26d2025ec3ed2ec60a895eb1114",
"type": "github"
},
"original": {
Expand Down Expand Up @@ -189,21 +167,6 @@
"type": "github"
}
},
"nixpkgs-stable": {
"locked": {
"lastModified": 1734202038,
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "bcba2fbf6963bf6bed3a749f9f4cf5bff4adb96d",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-24.05",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1733477122,
Expand All @@ -230,17 +193,34 @@
"nixpkgs": [
"nix",
"nixpkgs"
],
"nixpkgs-stable": [
"nix",
]
},
"locked": {
"lastModified": 1737301351,
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "15a87cedeb67e3dbc8d2f7b9831990dffcf4e69f",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"type": "github"
}
},
"pre-commit-hooks_2": {
"inputs": {
"flake-compat": "flake-compat_2",
"gitignore": "gitignore",
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1734425854,
"lastModified": 1737301351,
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "0ddd26d0925f618c3a5d85a4fa5eb1e23a09491d",
"rev": "15a87cedeb67e3dbc8d2f7b9831990dffcf4e69f",
"type": "github"
},
"original": {
Expand All @@ -252,12 +232,9 @@
"root": {
"inputs": {
"devenv": "devenv",
"git-hooks": "git-hooks",
"nix": "nix",
"nixpkgs": "nixpkgs_2",
"pre-commit-hooks": [
"git-hooks"
]
"pre-commit-hooks": "pre-commit-hooks_2"
}
}
},
Expand Down
1 change: 1 addition & 0 deletions devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,4 @@ EOF
};
};
}

10 changes: 6 additions & 4 deletions devenv/src/flake.tmpl.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
let
__DEVENV_VARS__
in {
treefmt-nix.url = "github:numtide/treefmt-nix";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add it by default, not many will use it so it should be optional.

git-hooks.url = "github:cachix/git-hooks.nix";
git-hooks.inputs.nixpkgs.follows = "nixpkgs";
pre-commit-hooks.follows = "git-hooks";
nixpkgs.url = "github:cachix/devenv-nixpkgs/rolling";
devenv.url = "github:cachix/devenv?dir=src/modules";
git-hooks.inputs.nixpkgs.follows = "nixpkgs";
pre-commit-hooks.follows = "git-hooks";
nixpkgs.url = "github:cachix/devenv-nixpkgs/rolling";
devenv.url = "github:cachix/devenv?dir=src/modules";
} // (if builtins.pathExists (devenv_dotfile + "/flake.json")
then builtins.fromJSON (builtins.readFile (devenv_dotfile + "/flake.json"))
else { });
Expand Down Expand Up @@ -135,3 +136,4 @@
devShell."${system}" = config.shell;
};
}

73 changes: 73 additions & 0 deletions docs/treefmt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# `devenv` Integration for [treefmt](https://treefmt.com/) via [treefmt-nix](https://github.com/numtide/treefmt-nix)

## Set up

Check the available integrations in [the list of all available integrations](reference/options.md#treefmt).

Add your desired integration to your `devenv.nix` file. For example, the following code would enable `treefmt` with the `nixpkgs-fmt` and `rustfmt` integrations:

```nix
{ inputs, ... }:

{
treefmt = {
projectRootFile = "devenv.nix";
programs = {
nixpkgs-fmt.enable = true;
rustfmt.enable = true;
};
};
}
```

### In Action:

```shell-session
$ devenv shell
Building shell ...
Entering shell ...

treefmt # This would run treefmt on all files.
```

## Additional Devenv Integrations

### Pre-commit Hooks

If you would like to enable `treefmt` in your pre-commit hooks, simply add:

```nix
{ inputs, ... }:

{
pre-commit.hooks = {
treefmt.enable = true;
};
}
```

This will enable `treefmt` hooks and automatically change the default package to the one you have defined in your `devenv`.

## Using a Custom Formatter

It is also possible to use custom formatters with `treefmt-nix`. For example, the following custom formatter formats JSON files using `yq-go`:

```nix
{
treefmt.settings.formatter = {
"yq-json" = {
command = "${pkgs.bash}/bin/bash";
options = [
"-euc"
''
for file in "$@"; do
${lib.getExe yq-go} -i --output-format=json $file
done
''
"--" # bash swallows the second argument when using -c
];
includes = [ "*.json" ];
};
};
}
```
47 changes: 34 additions & 13 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
flake-compat.follows = "";
};
};
inputs.treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs = {
nixpkgs.follows = "nixpkgs";
};
};
inputs.flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
Expand All @@ -40,7 +46,7 @@
};
};

outputs = { self, nixpkgs, git-hooks, nix, ... }@inputs:
outputs = { self, nixpkgs, git-hooks, treefmt-nix, nix, ... }@inputs:
let
systems = [ "x86_64-linux" "i686-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
forAllSystems = f: builtins.listToAttrs (map (name: { inherit name; value = f name; }) systems);
Expand All @@ -62,6 +68,7 @@
sources = [
{ name = "${self}"; url = "https://github.com/cachix/devenv/blob/main"; }
{ name = "${git-hooks}"; url = "https://github.com/cachix/git-hooks.nix/blob/master"; }
{ name = "${treefmt-nix}"; url = "https://github.com/numtide/treefmt-nix/blob/main"; }
];
rewriteSource = decl:
let
Expand Down Expand Up @@ -260,3 +267,4 @@
};
};
}

Loading