Skip to content

Commit

Permalink
Update to 2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
manveru committed Jul 5, 2017
1 parent d2c4deb commit 930a29e
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.bundle/
*.gem
tmp
result
36 changes: 13 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ applications with the [Nix](http://nixos.org/nix/) package manager.

## Installation

You can either get this via rubygems:

gem install bundix

Or once it gets into nixpkgs:
Installing from this repo:

nix-env -iA bundix

Expand All @@ -26,8 +22,6 @@ regret it.

Change to your project's directory and run this:

bundler lock
bundler package --path vendor/cache --no-install
bundix

This will generate a `gemset.nix` file that you then can use in your
Expand All @@ -40,16 +34,14 @@ To try your package in `nix-shell`, create a `default.nix` like this:
```nix
with (import <nixpkgs> {});
let
env = bundlerEnv {
gems = bundlerEnv {
name = "your-package";
inherit ruby;
gemfile = ./Gemfile;
lockfile = ./Gemfile.lock;
gemset = ./gemset.nix;
gemdir = ./.;
};
in stdenv.mkDerivation {
name = "your-package";
buildInputs = [env ruby];
buildInputs = [gems ruby];
}
```

Expand All @@ -62,18 +54,16 @@ To make a package for nixpkgs, you can try something like this:
```nix
{ stdenv, bundlerEnv, ruby }:
let
env = bundlerEnv {
name = "your-package";
inherit ruby;
gemfile = ./Gemfile;
lockfile = ./Gemfile.lock;
gemset = ./gemset.nix;
};
gems = bundlerEnv {
name = "your-package";
inherit ruby;
gemdir = ./.;
};
in stdenv.mkDerivation {
name = "your-package";
src = ./.;
buildInputs = [ ruby env ];
installPhase = ''
name = "your-package";
src = ./.;
buildInputs = [gems ruby];
installPhase = ''
mkdir -p $out
cp -r $src $out
'';
Expand Down
30 changes: 27 additions & 3 deletions bin/bundix
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ require_relative '../lib/bundix'
options = {
ruby: 'ruby',
bundle_pack_path: 'vendor/bundle',
gemfile: 'Gemfile',
lockfile: 'Gemfile.lock',
gemset: 'gemset.nix'
gemset: 'gemset.nix',
}

op = OptionParser.new do |o|
Expand Down Expand Up @@ -39,18 +40,32 @@ op = OptionParser.new do |o|
options[:lockfile] = File.expand_path(value)
end

o.on '-d', '--dependencies', 'include gem dependencies' do
options[:deps] = true
o.on '--gemfile=Gemfile', 'path to the Gemfile' do |value|
options[:gemfile] = File.expand_path(value)
end

o.on '-d', '--dependencies', 'include gem dependencies (deprecated)' do
warn '--dependencies/-d is deprecated because'
warn 'dependencies will always be fetched'
end

o.on '-q', '--quiet', 'only output errors' do
options[:quiet] = true
end

o.on '-l', '--lock', 'generate Gemfile.lock first' do
options[:lock] = true
end

o.on '-v', '--version', 'show the version of bundix' do
puts Bundix::VERSION
exit
end

o.on '--env', 'show the environment in bundix' do
system('env')
exit
end
end

op.parse!
Expand Down Expand Up @@ -80,6 +95,15 @@ if options[:init]
end
end

if options[:lock]
lock = !File.file?(options[:lockfile])
lock ||= File.mtime(options[:gemfile]) > File.mtime(options[:lockfile])
if lock
system('bundle', 'lock')
fail 'bundle lock failed' unless $?.success?
end
end

gemset = Bundix.new(options).convert

tempfile = Tempfile.new('gemset.nix', encoding: 'UTF-8')
Expand Down
19 changes: 14 additions & 5 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
with (import <nixpkgs> {});
stdenv.mkDerivation {
{
pkgs ? (import <nixpkgs> {}),
ruby ? pkgs.ruby_2_4_1,
bundler ? (pkgs.bundler.override { inherit ruby; }),
nix ? pkgs.nix,
nix-prefetch-git ? pkgs.nix-prefetch-git,
}:
pkgs.stdenv.mkDerivation rec {
version = "2.3.0";
name = "bundix";
src = ./.;
phases = "installPhase";
Expand All @@ -8,10 +15,12 @@ stdenv.mkDerivation {
makeWrapper $src/bin/bundix $out/bin/bundix \
--prefix PATH : "${nix.out}/bin" \
--prefix PATH : "${nix-prefetch-git.out}/bin" \
--prefix PATH : "${bundler.out}/bin" \
--set GEM_PATH "${bundler}/${bundler.ruby.gemPath}"
'';

nativeBuildInputs = [makeWrapper];
nativeBuildInputs = [ pkgs.makeWrapper ];
buildInputs = [ ruby bundler ];

meta = {
inherit version;
Expand All @@ -24,7 +33,7 @@ stdenv.mkDerivation {
'';
homepage = "https://github.com/manveru/bundix";
license = "MIT";
maintainers = with lib.maintainers; [ manveru zimbatm ];
platforms = lib.platforms.all;
maintainers = with pkgs.lib.maintainers; [ manveru zimbatm ];
platforms = pkgs.lib.platforms.all;
};
}
10 changes: 2 additions & 8 deletions lib/bundix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ class Bundix
attr_reader :options

def initialize(options)
@options = {
quiet: false,
tempfile: nil,
deps: false
}.merge(options)
@options = { quiet: false, tempfile: nil }.merge(options)
end

def convert
Expand All @@ -42,9 +38,7 @@ def convert
gem = find_cached_spec(spec, cache) || convert_spec(spec, cache)
gems.merge!(gem)

if options[:deps] && spec.dependencies.any?
gems[spec.name]['dependencies'] = spec.dependencies.map(&:name) - ['bundler']
end
gems[spec.name]['dependencies'] = spec.dependencies.map(&:name) - ['bundler']
end
end

Expand Down
37 changes: 32 additions & 5 deletions lib/bundix/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,37 @@ def sh(*args, &block)
Bundix.sh(*args, &block)
end

def download(file, url)
warn "Downloading #{file} from #{url}"
uri = URI(url)
open_options = {}
if uri.user && uri.password
open_options[:http_basic_authentication] = [uri.user, uri.password]
uri.user = nil
uri.password = nil
end
open(uri, 'r', 0600, open_options) do |net|
File.open(file, 'wb+') { |local|
File.copy_stream(net, local)
}
end
end

def nix_prefetch_url(url)
sh(NIX_BUILD, '--argstr', 'url', url, FETCHURL_FORCE, &FETCHURL_FORCE_CHECK)
.force_encoding('UTF-8')
rescue
dir = File.expand_path('~/.cache/bundix')
FileUtils.mkdir_p dir
file = File.join(dir, url.gsub(/[^\w-]+/, '_'))

unless File.size?(file)
download(file, url)
end

return unless File.size?(file)

sh('nix-prefetch-url', '--type', 'sha256', "file://#{file}")
.force_encoding('UTF-8').strip
rescue => ex
puts ex
nil
end

Expand Down Expand Up @@ -55,7 +82,7 @@ def fetch_remote_hash(spec, remote)
uri = "#{remote}/gems/#{spec.name}-#{spec.version}.gem"
result = nix_prefetch_url(uri)
return unless result
result.force_encoding('UTF-8')[SHA256_32]
result[SHA256_32]
rescue => e
puts "ignoring error during fetching: #{e}"
puts e.backtrace
Expand All @@ -66,8 +93,8 @@ def convert_rubygems
remotes = spec.source.remotes.map{|remote| remote.to_s.sub(/\/+$/, '') }
hash = fetch_local_hash(spec)
remote, hash = fetch_remotes_hash(spec, remotes) unless hash
hash = sh(NIX_HASH, '--type', 'sha256', '--to-base32', hash)[SHA256_32]
fail "couldn't fetch hash for #{spec.name}-#{spec.version}" unless hash
hash = sh(NIX_HASH, '--type', 'sha256', '--to-base32', hash)[SHA256_32]
puts "#{hash} => #{spec.name}-#{spec.version}.gem" if $VERBOSE

{ type: 'gem',
Expand Down
2 changes: 1 addition & 1 deletion lib/bundix/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Bundix
VERSION = '2.2.1'
VERSION = '2.3.0'
end
19 changes: 19 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
with (import <nixpkgs> {});
let
ruby = pkgs.ruby_2_4_1;
bundler = pkgs.bundler.override { inherit ruby; };
gems = bundlerEnv {
inherit ruby;
name = "bundix-gems";
gemdir = ./.;
};
in
stdenv.mkDerivation {
name = "bundix-shell";
buildInputs = [
bundler
(lowPrio gems)
ruby
postgresql96
];
}

0 comments on commit 930a29e

Please sign in to comment.