Skip to content

Nix Flake Installation

Ali edited this page Dec 2, 2023 · 1 revision

Example flake for development

The flake below is an example template that will assist in building a go-capnp module for various system architectures. If you're unfamiliar with nix flakes please read here before continuing.

{
  description = "go-capnp example flake";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
  };

  outputs = { self, nixpkgs, ... }:
    let
      supportedSystems = [ "x86_64-linux" ];
      forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
      nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; });
    in
    {
      packages = forAllSystems (system:
        let
          pkgs = nixpkgsFor.${system};
          go-capnp = pkgs.fetchFromGitHub {
            name = "go-capnp";
            owner = "capnproto";
            repo = "go-capnp";
            rev = "main";
            hash = "sha256-P6YP5b5Bz5/rS1ulkt1tSr3mhLyxxwgCin4WRFErPGM="; # change accordingly
          };
        in
        {
          capnpc-go = pkgs.buildGoModule {
            pname = "capnpc-go";
            src = go-capnp;
            version = "1.2.3";
            sourceRoot = "go-capnp";
            vendorHash = "sha256-DRNbv+jhIHzoBItuiQykrp/pD/46uysFbazLJ59qbqY="; # change accordingly
            buildPhase = ''
              go install ./capnpc-go
            '';
          };
          dev = pkgs.stdenv.mkDerivation {
            pname = "example_project_name";
            version = "1.2.3";
            srcs = [ go-capnp ];
            sourceRoot = "example_project-src/"; # Change to project name
            shellHook = ''
              echo "Entered devshell"
              export CAPNPC_GO_STD="${capnpc-go.src}/std" # Setting the path for capnp
            '';
            nativeBuildInputs = with pkgs; [ capnpc-go go gopls gotools ];
          };
        });
    };
} 

Notes

hash and vendorHash can be found by adding a placeholder hash(or just something full of zeroes) e.g. hash = “0000000” and running nix build or nix develop which will error out and then output the correct hash. Copy that into the fields. Consult the docs for more info.