Skip to content

Commit 0da3b86

Browse files
chore: nix package (#56)
* add nix flake export * refactor flake custom grammar packages
1 parent 37a5e03 commit 0da3b86

2 files changed

Lines changed: 160 additions & 0 deletions

File tree

flake.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
{
2+
description = "Trailmark source-code graph analysis toolkit";
3+
4+
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-26.05";
5+
6+
outputs =
7+
{ self, nixpkgs, ... }:
8+
let
9+
systems = [
10+
"aarch64-darwin"
11+
# in theory others are supported, not tested so
12+
];
13+
forAllSystems = nixpkgs.lib.genAttrs systems;
14+
in
15+
{
16+
packages = forAllSystems (
17+
system:
18+
let
19+
pkgs = import nixpkgs { inherit system; };
20+
python = pkgs.python312;
21+
pythonPackages = python.pkgs;
22+
pyproject = builtins.fromTOML (builtins.readFile ./pyproject.toml);
23+
24+
treeSitterCustomGrammar =
25+
grammar:
26+
pkgs.stdenv.mkDerivation {
27+
pname = "trailmark-tree-sitter-${grammar}";
28+
version = pyproject.project.version;
29+
30+
src = ./src/trailmark/tree_sitter_custom/${grammar};
31+
32+
dontConfigure = true;
33+
34+
nativeBuildInputs = [
35+
pkgs.stdenv.cc
36+
];
37+
38+
buildPhase = ''
39+
runHook preBuild
40+
41+
ext_suffix="$(${python.interpreter} -c 'import sysconfig; print(sysconfig.get_config_var("EXT_SUFFIX") or ".so")')"
42+
darwin_flags=(${pkgs.lib.optionalString pkgs.stdenv.hostPlatform.isDarwin "-undefined dynamic_lookup"})
43+
44+
cc -shared -fPIC -O2 -std=c11 "''${darwin_flags[@]}" \
45+
-I"${python}/include/${python.libPrefix}" \
46+
-I"$src/src" \
47+
"$src/binding.c" \
48+
"$src/src/parser.c" \
49+
-o "_binding$ext_suffix"
50+
51+
runHook postBuild
52+
'';
53+
54+
installPhase = ''
55+
runHook preInstall
56+
57+
mkdir -p "$out"
58+
cp _binding.* "$out/"
59+
60+
runHook postInstall
61+
'';
62+
};
63+
64+
tree-sitter-circom = treeSitterCustomGrammar "circom";
65+
tree-sitter-masm = treeSitterCustomGrammar "masm";
66+
67+
trailmark = pythonPackages.buildPythonApplication {
68+
pname = "trailmark";
69+
version = pyproject.project.version;
70+
pyproject = true;
71+
72+
src = ./.;
73+
74+
build-system = [
75+
pythonPackages.hatchling
76+
];
77+
78+
dependencies = with pythonPackages; [
79+
rustworkx
80+
tree-sitter
81+
tree-sitter-language-pack
82+
];
83+
84+
nativeBuildInputs = [
85+
pythonPackages.pythonRelaxDepsHook
86+
];
87+
88+
pythonRelaxDeps = [
89+
"tree-sitter-language-pack"
90+
];
91+
92+
postInstall = ''
93+
cp ${tree-sitter-circom}/_binding.* "$out/${python.sitePackages}/trailmark/tree_sitter_custom/circom/"
94+
cp ${tree-sitter-masm}/_binding.* "$out/${python.sitePackages}/trailmark/tree_sitter_custom/masm/"
95+
'';
96+
97+
pythonImportsCheck = [
98+
"trailmark"
99+
"trailmark.cli"
100+
];
101+
102+
meta = {
103+
description = "Parse source code into queryable graphs for security analysis";
104+
homepage = "https://github.com/trailofbits/trailmark";
105+
license = pkgs.lib.licenses.asl20;
106+
mainProgram = "trailmark";
107+
};
108+
};
109+
in
110+
{
111+
inherit trailmark tree-sitter-circom tree-sitter-masm;
112+
default = trailmark;
113+
}
114+
);
115+
116+
apps = forAllSystems (
117+
system:
118+
let
119+
trailmark = nixpkgs.lib.getExe self.packages.${system}.trailmark;
120+
in
121+
{
122+
trailmark = {
123+
type = "app";
124+
program = trailmark;
125+
};
126+
default = {
127+
type = "app";
128+
program = trailmark;
129+
};
130+
}
131+
);
132+
};
133+
}

0 commit comments

Comments
 (0)