This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flake-module.nix
83 lines (76 loc) · 2.55 KB
/
flake-module.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
# A flake-parts module for using treefmt (provides a check, mainly)
#
# NOTE: This module may be improved using https://github.com/numtide/treefmt/pull/169
{ self, lib, flake-parts-lib, ... }:
let
inherit (flake-parts-lib)
mkPerSystemOption;
inherit (lib)
mkOption
types;
in
{
options = {
perSystem = mkPerSystemOption
({ config, self', inputs', pkgs, system, ... }: {
options.treefmt = mkOption {
description = "treefmt: source code tree autoformatter";
type = types.submodule {
options = {
formatters = mkOption {
type = types.attrsOf (types.nullOr types.package);
default = { };
description = ''Formatter packages in use by treefmt.toml'';
};
# Library option (not to be set by the user)
buildInputs = mkOption {
type = types.listOf types.package;
default = [ pkgs.treefmt ] ++ lib.attrValues config.treefmt.formatters;
};
};
};
};
});
};
config = {
perSystem = { config, self', inputs', pkgs, ... }: {
apps.format = {
type = "app";
program = pkgs.writeShellApplication {
name = "format";
runtimeInputs = config.treefmt.buildInputs;
text = "treefmt";
};
};
checks.treefmt = pkgs.runCommandLocal "treefmt-check"
{
buildInputs = [ pkgs.git ] ++ config.treefmt.buildInputs;
}
''
set -e
# treefmt uses a cache at $HOME. But we can use --no-cache
# to make treefmt not use a cache. We still seem to need
# to export a writable $HOME though.
# TODO: https://github.com/numtide/treefmt/pull/174 fixes this issue
# but we need to wait until a release is made and that release gets
# into the nixpkgs we use.
export HOME="$TMP"
# `treefmt --fail-on-change` is broken for purs-tidy; So we must rely
# on git to detect changes. An unintended advantage of this approach
# is that when the check fails, it will print a helpful diff at the end.
cp -r ${self} $HOME/project
chmod -R a+w $HOME/project
cd $HOME/project
git init
git config user.email "nix@localhost"
git config user.name Nix
git add .
git commit -m init
treefmt --no-cache
git status
git --no-pager diff --exit-code
touch $out
'';
};
};
}