forked from numtide/treefmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.nix
121 lines (101 loc) · 3.31 KB
/
default.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
{ system ? builtins.currentSystem
, inputs ? import ./flake.lock.nix { }
, overlays ? [ (import inputs.rust-overlay) ]
, nixpkgs ? import inputs.nixpkgs {
inherit system overlays;
# Makes the config pure as well. See <nixpkgs>/top-level/impure.nix:
config = { };
}
, rustVersion ? nixpkgs.rust-bin.stable."1.65.0".default
}:
let
lib = nixpkgs.lib;
rustVersionExtended = rustVersion.override {
# include source for IDE's and other tools that resolve the source automatically via
# $(rustc --print sysroot)/lib/rustlib/src/rust
extensions = [ "rust-src" "rustfmt" ];
};
rustPlatform = nixpkgs.makeRustPlatform {
cargo = rustVersionExtended;
rustc = rustVersionExtended;
};
cargoToml = with builtins; (fromTOML (readFile ./Cargo.toml));
# Use the Nix module system to validate the treefmt config file format.
evalModule = config:
throw "treefmt.evalModule has been moved to https://github.com/numtide/treefmt-nix";
# What is used when invoking `nix run github:numtide/treefmt`
treefmt = rustPlatform.buildRustPackage {
inherit (cargoToml.package) name version;
src = builtins.path {
path = ./.;
filter = name: type:
name == toString ./Cargo.toml
|| name == toString ./Cargo.lock
|| lib.hasPrefix (toString ./src) name
|| lib.hasPrefix (toString ./benches) name
;
};
buildInputs = with nixpkgs; lib.optionals stdenv.isDarwin [ darwin.apple_sdk.frameworks.Security libiconv ];
doCheck = true;
cargoLock.lockFile = ./Cargo.lock;
meta.description = "one CLI to format your repo";
passthru.withConfig = config:
throw "treefmt.withConfig has been moved to https://github.com/numtide/treefmt-nix";
};
# Add all the dependencies of treefmt, plus more build tools
devShell = treefmt.overrideAttrs (prev: {
shellHook = ''
# Put the treefmt binary on the PATH when it's built
export PATH=$PWD/target/debug:$PATH
# Export location of llvm tools for use in code coverage
# see https://discourse.nixos.org/t/llvm-profdata-grcov-and-rust-code-coverage/19849/2 for background
export LLVM_PATH=$(dirname "$(type -p llvm-profdata)")
'';
nativeBuildInputs = prev.nativeBuildInputs ++ (with nixpkgs; [
# Build tools
grcov
rust-analyzer
rustc.llvmPackages.llvm
just
# Code formatters
elmPackages.elm-format
go
haskellPackages.cabal-fmt
haskellPackages.ormolu
mdsh
nixpkgs-fmt
nodePackages.prettier
python3.pkgs.black
rufo
shellcheck
shfmt
terraform
mdbook
]);
});
in
{
inherit treefmt devShell evalModule;
# reduce a bit of repetition
inherit (treefmt.passthru) withConfig;
# A collection of packages for the project
docs = nixpkgs.callPackage ./docs { };
# Flake attributes
default = treefmt;
# Test that no HOME is needed when --no-cache is passed
treefmt-no-cache-no-home = nixpkgs.runCommandLocal "format"
{
buildInputs = [ treefmt ];
}
''
cat <<CONFIG > treefmt.toml
[formatter.nix]
command = "${nixpkgs.nixpkgs-fmt}/bin/nixpkgs-fmt"
includes = ["*.nix"]
CONFIG
# uncommenting this makes it work fine
# export HOME=$TMP
treefmt --no-cache --fail-on-change -C ./.
touch $out
'';
}