Skip to content

Commit 6b4626c

Browse files
committed
flake: add treefmt
1 parent b97657f commit 6b4626c

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

flake.lock

Lines changed: 22 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,22 @@
88
androidPkgs.url = "github:tadfisher/android-nixpkgs/stable";
99

1010
flake-compat.url = "github:nix-community/flake-compat";
11+
12+
treefmt-nix.url = "github:numtide/treefmt-nix";
13+
treefmt-nix.inputs.nixpkgs.follows = "nixpkgs-unstable";
1114
};
1215

13-
outputs = { self, nixpkgs, nixpkgs-unstable, androidPkgs, flake-compat, ... }@inputs: let
16+
outputs = { self, nixpkgs, nixpkgs-unstable, androidPkgs, flake-compat, treefmt-nix, ... }@inputs: let
1417
pkgs = import ./pkgs/default.nix { inherit inputs; };
18+
19+
treeFmt = treefmt-nix.lib.evalModule pkgs ./treefmt.nix;
20+
pythonForUpdaterScripts = pkgs.python3.withPackages (
21+
p: with p; [
22+
mypy
23+
flake8
24+
pytest
25+
]
26+
);
1527
in {
1628
# robotnixSystem evaluates a robotnix configuration
1729
lib.robotnixSystem = configuration: import ./default.nix {
@@ -33,9 +45,9 @@
3345

3446
devShell.x86_64-linux = pkgs.mkShell {
3547
name = "robotnix-scripts";
48+
inputsFrom = [ treeFmt.config.build.devShell ];
3649
nativeBuildInputs = with pkgs; [
37-
# For android updater scripts
38-
(python3.withPackages (p: with p; [ mypy flake8 pytest ]))
50+
pythonForUpdaterScripts
3951
gitRepo nix-prefetch-git
4052
curl pup jq
4153
shellcheck
@@ -49,5 +61,11 @@
4961
];
5062
PYTHONPATH=./scripts;
5163
};
64+
65+
formatter.x86_64-linux = treeFmt.config.build.wrapper;
66+
67+
checks.x86_64-linux = {
68+
formatting = treeFmt.config.build.check self;
69+
};
5270
};
5371
}

pkgs/overlay.nix

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{ inputs }:
2+
let
3+
inherit (inputs) nixpkgs-unstable;
4+
5+
unstablePkgs = nixpkgs-unstable.legacyPackages.x86_64-linux;
6+
in
27
self: super: {
38
android-emulator = super.callPackage ./android-emulator {};
49

@@ -36,6 +41,8 @@ self: super: {
3641

3742
gitRepo = super.callPackage ./gitRepo { inherit inputs; };
3843

44+
nixfmt-rfc-style = unstablePkgs.nixfmt-rfc-style;
45+
3946
###
4047

4148
# Robotnix helper derivations

treefmt.nix

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{ pkgs, lib, ... }:
2+
{
3+
# Adding this options is a workaround for `config.build.programs` not evaluating properly
4+
# to get all formatter binaries into a devShell
5+
options.programs.mypy.package = lib.mkPackageOption pkgs [ "python3Packages" "mypy" ] { };
6+
config = {
7+
projectRootFile = "flake.nix";
8+
9+
programs = {
10+
nixpkgs-fmt.enable = true;
11+
nixpkgs-fmt.package = pkgs.nixfmt-rfc-style;
12+
13+
mypy.enable = true;
14+
mypy.package = pkgs.python3.withPackages(ps: with ps; [ mypy pytest ]);
15+
mypy.directories = {
16+
"." = {
17+
# Fot whatever reason using `settings.formatter.mypy.excludes` does not work properly
18+
options = [ "--exclude" "apks/chromium" "--exclude" "result" ];
19+
# Has to be set again, because treefmt clears PYTHONPATH and running via `treefmt` fails, see:
20+
# https://github.com/numtide/treefmt-nix/blob/main/programs/mypy.nix#L67
21+
extraPythonPackages = [ pkgs.python3Packages.pytest ];
22+
};
23+
};
24+
25+
# Drop–in flake8 replacement
26+
ruff.format = true;
27+
ruff.check = true;
28+
29+
shfmt.enable = true;
30+
31+
shellcheck.enable = true;
32+
};
33+
34+
settings.formatter = {
35+
ruff-check.excludes = [ "apks/chromium/*" ];
36+
ruff-format.excludes = [ "apks/chromium/*" ];
37+
shellcheck.includes = lib.mkForce [
38+
"flavors/**/*.sh"
39+
"modules/pixel/update.sh"
40+
"scripts/patchelf-prefix.sh"
41+
"pkgs/robotnix/unpack-images.sh"
42+
];
43+
};
44+
};
45+
}

0 commit comments

Comments
 (0)