Skip to content

Commit e68fb0e

Browse files
committed
Add minimal working Nix env
Create a shell environment with a CodeQL CLI that includes the Ql extractor to create CodeQL databases of QL files.
1 parent 8b9f677 commit e68fb0e

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

scripts/codeql/codeql-cli/2.16.0.nix

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{ lib, stdenv, fetchzip, withQlExtractor ? null}:
2+
3+
stdenv.mkDerivation rec {
4+
pname = "codeql-cli";
5+
version = "2.16.0";
6+
platform = if stdenv.isDarwin then "osx64" else "linux64";
7+
8+
dontConfigure = true;
9+
dontBuild = true;
10+
dontStrip = true;
11+
12+
src = fetchzip {
13+
url = "https://github.com/github/codeql-cli-binaries/releases/download/v${version}/codeql-${platform}.zip";
14+
hash = "sha256-trWUSMOT7h7J5ejjp9PzhGgBS3DYsJxzcv6aYKuk8TI=";
15+
};
16+
17+
buildInputs = if isNull withQlExtractor then [ ] else [ withQlExtractor ];
18+
inherit withQlExtractor;
19+
20+
installPhase = ''
21+
# codeql directory should not be top-level, otherwise,
22+
# it'll include /nix/store to resolve extractors.
23+
env
24+
mkdir -p $out/{codeql,bin}
25+
cp -R * $out/codeql/
26+
27+
ln -s $out/codeql/codeql $out/bin/
28+
29+
if [ -n "$withQlExtractor" ]; then
30+
# Copy the extractor, because CodeQL doesn't follow symlinks.
31+
cp -R $withQlExtractor $out/codeql/ql
32+
fi
33+
'';
34+
35+
36+
meta = with lib; {
37+
description = "Semantic code analysis engine";
38+
homepage = "https://codeql.github.com";
39+
platforms = lib.platforms.linux ++ lib.platforms.darwin;
40+
license = licenses.unfree;
41+
};
42+
}

scripts/codeql/default.nix

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let
2+
pkgs = import <nixpkgs> {};
3+
in
4+
rec {
5+
ql-extractor_0_0_1 = pkgs.callPackage ./ql-extractor/0.0.1.nix {inherit codeql-cli_2_16_0;};
6+
codeql-cli_2_16_0 = pkgs.callPackage ./codeql-cli/2.16.0.nix {};
7+
codeql-cli_2_16_0_with_ql_extractor = pkgs.callPackage ./codeql-cli/2.16.0.nix { withQlExtractor = ql-extractor_0_0_1; };
8+
9+
}

scripts/codeql/ql-extractor/0.0.1.nix

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{ stdenv, lib, fetchFromGitHub, rustPlatform, gh, libiconv, which, jq, codeql-cli_2_16_0}:
2+
3+
rustPlatform.buildRustPackage rec {
4+
pname = "codeql-ql-extractor";
5+
version = "0.0.1";
6+
7+
dontConfigure = true;
8+
dontStrip = true;
9+
10+
src = fetchFromGitHub {
11+
owner = "github";
12+
repo = "codeql";
13+
rev = "codeql-cli/v2.16.0";
14+
sha256 = "x2EFoOt1MZRXxIZt6hF86Z1Qu/hVUoOVla562TApVwo=";
15+
};
16+
17+
sourceRoot = "${src.name}/ql";
18+
19+
cargoLock = {
20+
lockFile = "${src.outPath}/ql/Cargo.lock";
21+
outputHashes = {
22+
"tree-sitter-json-0.20.0" = "sha256-fIh/bKxHMnok8D+xQlyyp5GaO2Ra/U2Y/5IjQ+t4+xY=";
23+
"tree-sitter-ql-0.19.0" = "sha256-2QOtNguYAIhIhGuVqyx/33gFu3OqcxAPBZOk85Q226M=";
24+
"tree-sitter-ql-dbscheme-0.0.1" = "sha256-wp0LtcbkP2lxbmE9rppO9cK+RATTjZxOb0EWfdKT884=";
25+
};
26+
};
27+
28+
nativeBuildInputs = [ gh libiconv which codeql-cli_2_16_0 jq];
29+
30+
platform = if stdenv.isLinux then "linux64" else "osx64";
31+
32+
installPhase = ''
33+
runHook preInstall
34+
mkdir -p $out/tools/$platform
35+
cargo run --profile release --bin codeql-extractor-ql -- generate --dbscheme ql/src/ql.dbscheme --library ql/src/codeql_ql/ast/internal/TreeSitter.qll
36+
codeql query format -i ql/src/codeql_ql/ast/internal/TreeSitter.qll
37+
# For some reason the fixupPhase isn't working, so we do it manually
38+
patchShebangs tools/
39+
cp -r codeql-extractor.yml tools ql/src/ql.dbscheme ql/src/ql.dbscheme.stats $out/
40+
cp $(cargo metadata --format-version 1 | jq -r '.target_directory')/release/codeql-extractor-ql $out/tools/$platform/extractor
41+
runHook postInstall
42+
'';
43+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
let
2+
pkgs = import <nixpkgs> {};
3+
in
4+
{
5+
ql-extractor_0_0_1 = pkgs.callPackage ./0.0.1.nix {};
6+
}

shell.nix

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let
2+
nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/archive/0e148322b344eab7c8d52f6e59b0d95ba73fb62e.tar.gz";
3+
pkgs = (import nixpkgs { config = {}; overlays = []; }) // (import ./scripts/codeql/default.nix);
4+
in
5+
6+
pkgs.mkShell {
7+
packages = with pkgs; [
8+
clang-tools_14
9+
python39
10+
git
11+
gh
12+
jq
13+
codeql-cli_2_16_0_with_ql_extractor
14+
];
15+
}

0 commit comments

Comments
 (0)