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 #1317

Open
wants to merge 7 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 @@ -42,6 +42,9 @@ Running ``devenv init`` generates ``devenv.nix``:
# https://devenv.sh/pre-commit-hooks/
pre-commit.hooks.shellcheck.enable = true;

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

# https://devenv.sh/processes/
processes.ping.exec = "ping localhost";
}
Expand Down
37 changes: 36 additions & 1 deletion devenv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1718895438,
"owner": "nixos",
"repo": "nixpkgs",
"rev": "d603719ec6e294f034936c0d0dc06f689d91b6c3",
"treeHash": "5431d5da7bc85049065fe5a6ac014ed1d2b9abe4",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"pre-commit-hooks": {
"inputs": {
"flake-compat": "flake-compat_2",
Expand Down Expand Up @@ -184,7 +200,8 @@
"devenv": "devenv",
"nix": "nix",
"nixpkgs": "nixpkgs",
"pre-commit-hooks": "pre-commit-hooks"
"pre-commit-hooks": "pre-commit-hooks",
"treefmt-nix": "treefmt-nix"
}
},
"systems": {
Expand All @@ -201,6 +218,24 @@
"repo": "default",
"type": "github"
}
},
"treefmt-nix": {
"inputs": {
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1718522839,
"owner": "numtide",
"repo": "treefmt-nix",
"rev": "68eb1dc333ce82d0ab0c0357363ea17c31ea1f81",
"treeHash": "3a739f95f5906aab649d47fe4832696cc9f87de5",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "treefmt-nix",
"type": "github"
}
}
},
"root": "root",
Expand Down
14 changes: 12 additions & 2 deletions devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,22 @@
'';
};

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

pre-commit.hooks = {
nixpkgs-fmt.enable = true;
#shellcheck.enable = true;
#clippy.enable = true;
rustfmt.enable = true;
#markdownlint.enable = true;
treefmt = {
enable = true;
};
markdownlint.settings.configuration = {
MD013 = {
line_length = 120;
Expand Down
1 change: 1 addition & 0 deletions devenv/src/flake.tmpl.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
in {
pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
treefmt-nix.url = "github:numtide/treefmt-nix";
nixpkgs.url = "github:cachix/devenv-nixpkgs/rolling";
devenv.url = "github:cachix/devenv?dir=src/modules";
} // (if builtins.pathExists (devenv_dotfile + "/flake.json")
Expand Down
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" ];
};
};
}
```
23 changes: 22 additions & 1 deletion flake.lock

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

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


outputs = { self, nixpkgs, pre-commit-hooks, nix, ... }@inputs:
outputs = { self, nixpkgs, pre-commit-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 @@ -49,11 +55,12 @@
./src/modules/top-level.nix
{ devenv.warnOnNewVersion = false; }
];
specialArgs = { inherit pre-commit-hooks pkgs inputs; };
specialArgs = { inherit pre-commit-hooks treefmt-nix pkgs inputs; };
};
sources = [
{ name = "${self}"; url = "https://github.com/cachix/devenv/blob/main"; }
{ name = "${pre-commit-hooks}"; url = "https://github.com/cachix/pre-commit-hooks.nix/blob/master"; }
{ name = "${treefmt-nix}"; url = "https://github.com/numtide/treefmt-nix/blob/main"; }
];
rewriteSource = decl:
let
Expand Down
21 changes: 21 additions & 0 deletions src/modules/integrations/treefmt.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{ pkgs, self, lib, inputs, config, ... }: {

options.treefmt = lib.mkOption {
type = lib.types.submoduleWith {
modules = inputs.treefmt-nix.lib.submodule-modules;
specialArgs = { inherit pkgs; };
shorthandOnlyDefinesConfig = true;
};
default = { };
description = "Integration of https://github.com/numtide/treefmt-nix";
};

config = lib.mkIf ((lib.filterAttrs (id: value: value.enable) config.treefmt.programs) != { }) {
packages = [
config.treefmt.build.wrapper
];

#automatically add treefmt-nix to pre-commit if the user enables it.
pre-commit.hooks.treefmt.package = lib.mkForce config.treefmt.build.wrapper;
};
}