Skip to content

Commit

Permalink
Merge pull request #4003 from DataDog/lloeki/migrate-to-nix-flake
Browse files Browse the repository at this point in the history
Migrate to Nix Flake
  • Loading branch information
lloeki authored Oct 22, 2024
2 parents df97f48 + aebdbd1 commit 646cec2
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 55 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/nix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Test Nix

on:
push:
branches:
- "**"

jobs:
test:
strategy:
fail-fast: false
matrix:
platform:
- os: darwin
cpu: x86_64
base: macos-13 # always x86_64-darwin
- os: darwin
cpu: arm64
base: macos-14 # always arm64-darwin
- os: linux
cpu: x86_64
base: ubuntu-24.04 # always x86_64-linux-gnu
- os: linux
cpu: aarch64
base: arm-4core-linux-ubuntu24.04 # always aarch64-linux-gnu
nix:
- 24.05

name: Test Nix (${{ matrix.platform.cpu }}-${{ matrix.platform.os }}, ${{ matrix.nix }})
runs-on: ${{ matrix.platform.base }}

permissions:
contents: read
id-token: write

env:
SKIP_SIMPLECOV: 1
DD_INSTRUMENTATION_TELEMETRY_ENABLED: false
DD_REMOTE_CONFIGURATION_ENABLED: false

steps:
- name: Check CPU arch
run: |
test "$(uname -m)" = "${{ matrix.platform.cpu }}"
- uses: actions/checkout@v4
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main
- name: Print ruby version
run: |
nix develop --command which ruby
nix develop --command ruby --version
- name: Bundle install
run: nix develop --command bundle install
- name: Run spec:main
run: nix develop --command bundle exec rake spec:main
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ ext/**/skipped_reason.txt
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
# Ignore local variables
.envrc
/.envrc
/.direnv

# lock files
Gemfile.lock
Expand Down
11 changes: 11 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# flake-compat shim for usage without flakes
(import
(
let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in
fetchTarball {
url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{ src = ./.; }
).defaultNix
76 changes: 76 additions & 0 deletions flake.lock

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

57 changes: 57 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/master";

# cross-platform convenience
flake-utils.url = "github:numtide/flake-utils";

# backwards compatibility with nix-build and nix-shell
flake-compat.url = "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz";
};

outputs = { self, nixpkgs, flake-utils, flake-compat }:
# resolve for all platforms in turn
flake-utils.lib.eachDefaultSystem (system:
let
# packages for this system platform
pkgs = nixpkgs.legacyPackages.${system};

# control versions
ruby = pkgs.ruby_3_3;
llvm = pkgs.llvmPackages_16;
gcc = pkgs.gcc13;
in {
devShell = pkgs.llvm.stdenv.mkDerivation {
name = "devshell";

buildInputs = with pkgs; [
ruby
libyaml.dev

# TODO: some gems insist on using `gcc` on Linux, satisfy them for now:
# - json
# - protobuf
# - ruby-prof
gcc
];

shellHook = ''
# get major.minor.0 ruby version
export RUBY_VERSION="$(ruby -e 'puts RUBY_VERSION.gsub(/\d+$/, "0")')"
# make gem install work in-project, compatibly with bundler
export GEM_HOME="$(pwd)/vendor/bundle/ruby/$RUBY_VERSION"
# make bundle work in-project
export BUNDLE_PATH="$(pwd)/vendor/bundle"
# enable calling gem scripts without bundle exec
export PATH="$GEM_HOME/bin:$PATH"
# enable implicitly resolving gems to bundled version
export RUBYGEMS_GEMDEPS="$(pwd)/Gemfile"
'';
};
}
);
}
20 changes: 10 additions & 10 deletions lib/datadog/core/telemetry/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ def self.from(exception)
return unless backtrace
return if backtrace.empty?

stack_trace = +''
backtrace.each do |line|
stack_trace << if line.start_with?(GEM_ROOT)
line[GEM_ROOT.length..-1] || ''
else
'REDACTED'
end
stack_trace << ','
end
# vendored deps
vendored_deps = Gem.path.any? { |p| p.start_with?(GEM_ROOT) }

stack_trace.chomp(',')
backtrace.map do |line|
if !vendored_deps && line.start_with?(GEM_ROOT) ||
vendored_deps && line.start_with?(GEM_ROOT) && Gem.path.none? { |p| line.start_with?(p) }
line[GEM_ROOT.length..-1] || ''
else
'REDACTED'
end
end.join(',')
end
end

Expand Down
55 changes: 11 additions & 44 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,44 +1,11 @@
{
# use the environment channel
pkgs ? import <nixpkgs> {},

# use a pinned package state
pinned ? import(fetchTarball("https://github.com/NixOS/nixpkgs/archive/25865a40d14b.tar.gz")) {},
}:
let
# specify ruby version to use
ruby = pinned.ruby_3_3;

# control llvm/clang version (e.g for packages built from source)
llvm = pinned.llvmPackages_16;

# control gcc version (e.g for packages built from source)
gcc = pinned.gcc13;
in llvm.stdenv.mkDerivation {
# unique project name for this environment derivation
name = "dd-trace-rb.devshell";

buildInputs = [
ruby

# TODO: some gems insist on using `gcc` on Linux, satisfy them for now:
# - json
# - protobuf
# - ruby-prof
gcc
];

shellHook = ''
# get major.minor.0 ruby version
export RUBY_VERSION="$(ruby -e 'puts RUBY_VERSION.gsub(/\d+$/, "0")')"
# make gem install work in-project, compatibly with bundler
export GEM_HOME="$(pwd)/vendor/bundle/ruby/$RUBY_VERSION"
# make bundle work in-project
export BUNDLE_PATH="$(pwd)/vendor/bundle"
# enable calling gem scripts without bundle exec
export PATH="$GEM_HOME/bin:$PATH"
'';
}
# flake-compat shim for usage without flakes
(import
(
let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in
fetchTarball {
url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{ src = ./.; }
).shellNix
12 changes: 12 additions & 0 deletions spec/datadog/core/telemetry/logging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def log!(_event)
logs: [{ message: 'RuntimeError', level: 'ERROR',
stack_trace: a_string_including('REDACTED') }]
)
expect(event.payload).to include(
logs: [{ message: 'RuntimeError', level: 'ERROR',
stack_trace: a_string_including(',/spec/') }]
)
end

begin
Expand All @@ -38,6 +42,10 @@ def log!(_event)
logs: [{ message: 'RuntimeError:Must not contain PII', level: 'ERROR',
stack_trace: a_string_including('REDACTED') }]
)
expect(event.payload).to include(
logs: [{ message: 'RuntimeError:Must not contain PII', level: 'ERROR',
stack_trace: a_string_including(',/spec/') }]
)
end

begin
Expand All @@ -56,6 +64,10 @@ def log!(_event)
logs: [{ message: /#<Class:/, level: 'ERROR',
stack_trace: a_string_including('REDACTED') }]
)
expect(event.payload).to include(
logs: [{ message: /#<Class:/, level: 'ERROR',
stack_trace: a_string_including(',/spec/') }]
)
end

customer_exception = Class.new(StandardError)
Expand Down
3 changes: 3 additions & 0 deletions spec/datadog/release_gem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
|datadog\.gemspec
|docker-compose\.yml
|shell\.nix
|default\.nix
|flake\.nix
|flake\.lock
|static-analysis\.datadog\.yml
|\.standard\.yml
|\.standard_todo\.yml
Expand Down

0 comments on commit 646cec2

Please sign in to comment.