Posts tagged "nix":
A Solidity Compilers Collection in Nix
I have written solc.nix
for a while, this article is a summary of it.
It is a collection of solidity compilers as nix expressions. While most solidity development toolchains (foundry,
truffle, hardhat, etc.) have their bespoke mechanisms of downloading solidity compiler (solc
) binaries, there are
situations you would want not to rely on them:
- In a CI environment, you provide the compiler during setup instead of letting toolchains download them again.
- You have a development process that does not have the
solc
resolution mechanism. - You are juggling with multiple versions of solidity compilers.
- You are using NixOS or the "Nix package manager".
If any of these cases apply to you, you should consider using solc.nix
. Here are some tips on how.
Install Nix
Here is a write-up of why I think you should use Nix.
The fastest way to have it installed on your system is to follow the official instruction.
Once installed, you are good to go!
Using Nix Shell
If you just wanted to have some version of solc
ad-hoc, you just need one simple line:
$ nix shell github:hellwolf/solc.nix#solc_0_4_26 github:hellwolf/solc.nix#solc_0_8_19
Then you will have two different solc
binary available for you:
$ solc-0.4.26 --version solc, the solidity compiler commandline interface Version: 0.4.26+commit.4563c3fc.Linux.g++ $ solc-0.8.19 --version solc, the solidity compiler commandline interface Version: 0.8.19+commit.7dd6d404.Linux.g++
Using Nix Flake
If you want a deeper integration of it, you should consider using Nix Flake, here is an example of how to have multiple
solc
installed and one of them as the default one:
{ inputs = { solc = { url = "github:hellwolf/solc.nix"; inputs.nixpkgs.follows = "nixpkgs"; }; }; outputs = { self, nixpkgs, solc }: let pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ solc.overlay ]; }; in { devShell.x86_64-linux = with pkgs; mkShell { buildInputs = [ solc_0_4_26 solc_0_7_6 solc_0_8_19 (solc.mkDefault pkgs solc_0_8_19) ]; }; }; }
Contribute
If you find this tool useful, please follow up at the git repo, contribute to keep this up to date, and share with people you might think it can help them too!
Stop Writing Instructions and Use Nix
If you are still writing development environment setup instruction for your project, time to use Nix!
With Nix:
- Any developers and CI systems can have reproducible builds and deployments regardless of the platforms (any Linux, MacOS or even Windows using WSL2) they use, thanks to Nix: the package manager.
- You can access a large collection of packages (over 80,000 as of today) maintained by thousands of contributors, thanks to Nixpkgs.
- There is even a dedicated Linux distribution using Nixpkgs: NixOS.
More over, you should use Nix Flakes. This upcoming new feature of Nix will power-up your build environment with a lock
file that is similar to your yarn.lock/package-json.lock/Cargo.lock/etc.
, but for all types of projects.
It is a experience changer for many engineering organizations, e.g. at shopify.
Personally, each time I want to contribute to an open source project, the first thing I would do is to add a flake.nix
to their project and decorate their CONTRIBUTING.md with a much more concise development environment setup using Nix
section.
For more reasons why you should use Nix, I recommend these readings:
After you are "nix pilled" inevitably, Nix official website has a pretty decent collection of learning materials from getting started to comprehensive references.
There are also some good materials done by the community:
Further more, Nix Flakes have also its own interesting materials to read:
- Serokell: Practical Nix Flakes
- TWEAG: Nix Flakes, by Eelco Dolstra. (Eelco Dolstra wrote the paper about Nix in 2004)
Happy Nix!