Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Nix flake #9

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.envrc
/.direnv
/vendor/bundle
/Gemfile.lock
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"
'';
};
}
);
}
33 changes: 11 additions & 22 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
{
pkgs ? import <nixpkgs> {},
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