-
Notifications
You must be signed in to change notification settings - Fork 7
/
flake.nix
106 lines (106 loc) · 3.59 KB
/
flake.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
{
inputs = {
nixpkgs.url = "nixpkgs/nixos-22.11";
};
outputs = { self, nixpkgs }:
let
systems = [
"x86_64-linux"
"aarch64-linux"
"armv7l-linux"
"x86_64-darwin"
"aarch64-darwin"
];
eachSystem = f: with nixpkgs.lib; foldAttrs mergeAttrs { }
(map (s: mapAttrs (_: v: { ${s} = v; }) (f s)) systems);
in
eachSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) lib fd;
runCheck = cmd: pkgs.runCommand "check" { }
"cp --no-preserve=mode -r ${./.} src; cd src\n${cmd}\n touch $out";
format_tools = with pkgs; [
clang-tools
coreutils
cmake-format
nixpkgs-fmt
nodePackages.prettier
];
format_cmd = pkgs.writeShellScript "format" ''
PATH=${lib.makeBinPath format_tools}
case "$1" in
*.c | *.h)
clang-format -i "$1";;
*.cmake | */CMakeLists.txt)
cmake-format -c ${./.cmake-format.yml} -i "$1";;
*.nix)
nixpkgs-fmt "$1";;
*.md | *.json | *.yml)
prettier --write "$1";;
esac &>/dev/null
'';
getFetchContentFlags = file:
let
inherit (builtins) head elemAt match;
parse = match
"(.*)\nFetchContent_Declare\\(\n ([^\n]*)\n([^)]*)\\).*"
file;
name = elemAt parse 1;
content = elemAt parse 2;
getKey = key: elemAt
(match "(.*\n)? ${key} ([^\n]*)(\n.*)?" content) 1;
url = getKey "GIT_REPOSITORY";
pkg = pkgs.fetchFromGitHub {
owner = head (match ".*github.com/([^/]*)/.*" url);
repo = head (match ".*/([^/]*)\\.git" url);
rev = getKey "GIT_TAG";
hash = getKey "# hash:";
};
in
if (parse == null) then [ ] else
([ "-DFETCHCONTENT_SOURCE_DIR_${lib.toUpper name}=${pkg}" ] ++
getFetchContentFlags (head parse));
in
rec {
devShells.default = pkgs.mkShell {
inputsFrom = [ packages.default ];
packages = with pkgs; [ clang-tools jq moreutils ] ++ format_tools;
hardeningDisable = [ "fortify" ];
};
packages.default = pkgs.stdenv.mkDerivation {
name = "coreOTA_Demo";
src = ./.;
nativeBuildInputs = with pkgs; [ cmake ];
buildInputs = with pkgs; [ openssl_1_1 ];
hardeningDisable = [ "fortify" ];
cmakeFlags = getFetchContentFlags
(builtins.readFile ./CMakeLists.txt);
installPhase = "cp coreOTA_Demo $out";
};
checks = packages // {
formatting = runCheck ''
${lib.getExe formatter} .
${pkgs.diffutils}/bin/diff -rq --no-dereference . ${./.} | \
sed -e 's/Files \(.*\) and .* differ/\1 not formatted/'
'';
editorconfig = runCheck ''
${pkgs.editorconfig-checker}/bin/editorconfig-checker
'';
cmake-lint = runCheck ''
${pkgs.cmake-format}/bin/cmake-lint \
$(${fd}/bin/fd '.*\.cmake|CMakeLists.txt' \
-E 'reference/esp32/lib') -c .cmake-format.yml
'';
};
formatter = pkgs.writeShellScriptBin "formatter" ''
for f in "$@"; do
if [ -d "$f" ]; then
(cd "$f"; ${fd}/bin/fd -t f -H -E '**/lib/*/*' -x ${format_cmd})
else
${format_cmd} "$f"
fi
done
'';
});
}