From ac3df27d87efedee9fe98569edc49e8dee89968a Mon Sep 17 00:00:00 2001 From: Loic Nageleisen Date: Tue, 22 Oct 2024 11:55:20 +0200 Subject: [PATCH] Migrate to Nix Flake --- default.nix | 11 ++++++++ flake.lock | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 57 ++++++++++++++++++++++++++++++++++++++++ shell.nix | 58 ++++++++-------------------------------- 4 files changed, 155 insertions(+), 47 deletions(-) create mode 100644 default.nix create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..fb3934b --- /dev/null +++ b/default.nix @@ -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 diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..3ba015b --- /dev/null +++ b/flake.lock @@ -0,0 +1,76 @@ +{ + "nodes": { + "flake-compat": { + "locked": { + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "revCount": 57, + "type": "tarball", + "url": "https://api.flakehub.com/f/pinned/edolstra/flake-compat/1.0.1/018afb31-abd1-7bff-a5e4-cff7e18efb7a/source.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1726560853, + "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1729076285, + "narHash": "sha256-0pKZR4g2X3YTOcLQexuDrN2vIdFQJ1djqXSBfD0gEgE=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "2a3b139da1ffe0cdd6ab82583e147a75738ba4f8", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "master", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..8cd5303 --- /dev/null +++ b/flake.nix @@ -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_18; + gcc = pkgs.gcc14; + 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" + ''; + }; + } + ); +} diff --git a/shell.nix b/shell.nix index c7ae979..950c1c8 100644 --- a/shell.nix +++ b/shell.nix @@ -1,47 +1,11 @@ -{ - # use the environment channel - pkgs ? import {}, - - # use a pinned package state - pinned ? import(fetchTarball("https://github.com/NixOS/nixpkgs/archive/b87dfeec0dd1.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_18; - - # control gcc version (e.g for packages built from source) - gcc = pinned.gcc14; -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" - - # enable `bundle exec`-less commands - export RUBYGEMS_GEMDEPS="$(pwd)/Gemfile - ''; -} +# 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