diff --git a/.gitignore b/.gitignore index 26e072c..9bf08c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.envrc +/.direnv /vendor/bundle /Gemfile.lock 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..f251759 --- /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_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" + ''; + }; + } + ); +} diff --git a/shell.nix b/shell.nix index 02307aa..950c1c8 100644 --- a/shell.nix +++ b/shell.nix @@ -1,22 +1,11 @@ -{ - pkgs ? import {}, - pinned ? import(fetchTarball("https://github.com/NixOS/nixpkgs/archive/47b604b07d1e.tar.gz")) {}, -}: -let - ruby = pinned.ruby_3_3; - llvm = pinned.llvmPackages_16; -in llvm.stdenv.mkDerivation { - name = "shell"; - - buildInputs = [ - ruby - pinned.libyaml.dev - ]; - - shellHook = '' - export RUBY_VERSION="$(ruby -e 'puts RUBY_VERSION.gsub(/\d+$/, "0")')" - export GEM_HOME="$(pwd)/vendor/bundle/ruby/$RUBY_VERSION" - export BUNDLE_PATH="$(pwd)/vendor/bundle" - 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