Skip to content

Commit

Permalink
Add support for building and development using nix (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelchcki authored Dec 31, 2024
1 parent 6f412c0 commit 110cac5
Show file tree
Hide file tree
Showing 18 changed files with 489 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.venv
result
*.Dockerfile
6 changes: 6 additions & 0 deletions .envrc.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# use https://github.com/nix-community/nix-direnv to load all tools needed to develop in the repository
# $ cp .envrc.example .envrc
# $ direnv allow .
use flake --impure .

# alternatively just use `nix develop` or `nix-shell`
33 changes: 33 additions & 0 deletions .github/workflows/nix-auto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: CI tasks from Nix config

on:
pull_request:
push:
branches:
- master
- main

jobs:
nix-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v27
- id: set-matrix
name: Generate Nix Matrix
run: |
set -Eeu
matrix="$(nix eval --json '.#githubActions.matrix')"
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"
nix:
needs: nix-matrix
runs-on: ${{ matrix.os }}
strategy:
matrix: ${{fromJSON(needs.nix-matrix.outputs.matrix)}}
steps:
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v27
- run: nix build -L '.#githubActions.checks.${{ matrix.attr }}'
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ htmlcov/
.coverage
.coverage.*
.cache

# Automated dev env setup - see .envrc.example
/.envrc
/.direnv/

# nix
/result
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ See the [Development](#development) section for how to get the test agent runnin

## Installation

The test agent can be installed using [nix](https://docs.determinate.systems/getting-started/):

nix profile install github:datadog/dd-apm-test-agent#ddapm-test-agent
# nix profile upgrade ddapm-test-agent # to upgrade

The test agent can be installed from PyPI:

pip install ddapm-test-agent
Expand Down
28 changes: 28 additions & 0 deletions build-on-darwin.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# simple hack to build the same image on OSX. otherwise the Nix docker build will by default not cross compile to linux
# in CI or on linux systems - use `nix build .#image | docker load -i result` instead

FROM nixos/nix AS builder
RUN set -xe; \
echo 'sandbox = true' >> /etc/nix/nix.conf; \
echo 'filter-syscalls = false' >> /etc/nix/nix.conf; \
echo 'max-jobs = auto' >> /etc/nix/nix.conf; \
echo 'experimental-features = nix-command flakes' >> /etc/nix/nix.conf

WORKDIR /build
COPY flake.lock *.nix .

RUN nix flake show


COPY . .

RUN nix build .#image
RUN mkdir -p /output; mkdir -p /img
# very simplistic way to unpack image - relies only on there being a single layer
RUN tar -xf result --strip-components=1 -C /img
RUN tar -xf /img/layer.tar -C /output

FROM scratch AS final
COPY --from=builder /output/ /

ENTRYPOINT ["/bin/ddapm-test-agent"]
32 changes: 32 additions & 0 deletions ddsketch.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{ python, pkgs, ... }:
python.pkgs.buildPythonPackage rec {
name = "ddsketch";
version = "3.0.1";

src = pkgs.fetchFromGitHub {
owner = "datadog";
repo = "sketches-py";
rev = "refs/tags/v${version}";
hash = "sha256-SmdKq5aXi5B3FNBxPQDNKNBujGGEPXF132YGadGFPpo=";
};

propagatedBuildInputs = with python.pkgs; [
six
protobuf
setuptools
];
nativeBuildInputs = with python.pkgs; [ setuptools_scm ];
checkInputs = with python.pkgs; [
pytest
numpy
];
env.SETUPTOOLS_SCM_PRETEND_VERSION = version;

pythonImportsCheck = [ "ddsketch" ];

postPatch = ''
patchShebangs setup.py
ls -lah
echo version=\"${version}\" > ddsketch/__version.py
'';
}
72 changes: 72 additions & 0 deletions ddtrace.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
python,
pkgs,
ddsketch,
...
}:

let
envier = python.pkgs.buildPythonPackage rec {
pname = "envier";
version = "0.5.2";

pyproject = true;
propagatedBuildInputs = with python.pkgs; [
hatchling
hatch-vcs
];

src = pkgs.fetchPypi {
inherit pname version;
sha256 = "sha256-Tn45jLCajdNgUI734SURoVI1VCbSVEuEh6NNrSfMIK0=";
};
};

ddtrace = python.pkgs.buildPythonPackage rec {
pname = "ddtrace";
version = "2.9.2";
pyproject = true;

nativeBuildInputs =
[ pkgs.cmake ]
++ (with python.pkgs; [
cmake
setuptools
setuptools_scm
cython
]);

propagatedBuildInputs = with python.pkgs; [
attrs
cattrs
ddsketch
envier
opentelemetry-api
protobuf
six
xmltodict
bytecode
];

buildInputs = pkgs.lib.optionals pkgs.stdenv.isDarwin [ pkgs.darwin.apple_sdk.frameworks.IOKit ];

postPatch = ''
substituteInPlace setup.py --replace "cmake>=3.24.2,<3.28" "cmake"
# downloading artifacts is impossible in sandboxed build
substituteInPlace setup.py --replace "cls.download_artifacts()" "pass"
substituteInPlace pyproject.toml --replace "cmake>=3.24.2,<3.28" "cmake"
'';

dontUseCmakeConfigure = true;

src = pkgs.fetchFromGitHub {
owner = "datadog";
repo = "dd-trace-py";
rev = "refs/tags/v${version}";
hash = "sha256-Ax220/uBNwSZNBFYxbxAe0rmLrqYYf3a8K/PIuSE150=";
};
};
in
(ddtrace)
117 changes: 117 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 110cac5

Please sign in to comment.