Skip to content

Instantly share code, notes, and snippets.

@mitchellh
Created May 10, 2020 16:29
Show Gist options
  • Select an option

  • Save mitchellh/c47e3333bb78f57836ba2aa8064ad993 to your computer and use it in GitHub Desktop.

Select an option

Save mitchellh/c47e3333bb78f57836ba2aa8064ad993 to your computer and use it in GitHub Desktop.

Revisions

  1. mitchellh created this gist May 10, 2020.
    9 changes: 9 additions & 0 deletions example.nix
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    self: super: {
    consul-bin = self.callPackage ./hashicorp-generic.nix {
    name = "consul";
    version = "1.7.3";
    sha256 = "0k03n7h5h8miqhh3n3y47894vhwdcp8m611w55f826swmq9angl1";
    };

    # and so on...
    }
    47 changes: 47 additions & 0 deletions hashicorp-generic.nix
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    /* This function creates a derivation for installing binaries directly
    * from releases.hashicorp.com.
    */
    { name
    , version
    , sha256
    , system ? builtins.currentSystem
    , pname ? "${name}-bin"

    , lib
    , stdenv
    , fetchurl
    , unzip
    , ...
    }:

    let
    # Mapping of Nix systems to the GOOS/GOARCH pairs.
    systemMap = {
    x86_64-linux = "linux_amd64";
    i686-linux = "linux_386";
    x86_64-darwin = "darwin_amd64";
    i686-darwin = "darwin_386";
    aarch64-linux = "linux_arm64";
    };

    # Get our system
    goSystem = systemMap.${system} or (throw "unsupported system: ${system}");

    # url for downloading composed of all the other stuff we built up.
    url = "https://releases.hashicorp.com/${name}/${version}/${name}_${version}_${goSystem}.zip";
    in stdenv.mkDerivation {
    inherit pname version;
    src = fetchurl { inherit url sha256; };

    # Our source is right where the unzip happens, not in a "src/" directory (default)
    sourceRoot = ".";

    # Stripping breaks darwin Go binaries
    dontStrip = lib.strings.hasPrefix "darwin" goSystem;

    nativeBuildInputs = [ unzip ];
    installPhase = ''
    mkdir -p $out/bin
    mv ${name} $out/bin
    '';
    }