Nix Golang

Share on:

A quick blog post on using nix with Go.

Wait go isnt your jam 🤯, then swap out Go package with your favourite language 😂

Nix-Shell is my favourite part of Nix. Using Nix-Shell, you can temporarily install packages, but keep that new machine feel.

Go excels at building fast, efficient applications. Nix excels at managing complex dependencies and environments. When used together, they provide a robust and predictable development workflow. Nix allows you to create isolated Go environments, preventing version conflicts and simplifying dependency management. This ensures that your Go projects are always built and deployed in a consistent and reliable manner, regardless of the target platform.

Getting Started

  1. Lets define a basic Go environment
1nix-shell --pure -p go 

In the above command we ask nix to install the latest available Go package using -p go. We also ask Nix to give us a pure environment (i.e. ignore existing host configuration). The --pure argument is optional, I personally prefer this setting when using the command line option.

Expected Output:

1[nix-shell:~/]$
  1. Check the version
1go version

Expected Output:

1go version go1.24.1 darwin/arm64

Nice we now have a access to Go on our system. At this point Go is configured on your host, so you can use it to build, fmt, etc.

However its a bit much typing this command each time you want go. Enter the nix script...

Nix Scripts

In the last section, we used Nix Shell to run go. Alternatively we can define a script to represent the environment requirement.

  1. Lets create a shell.nix script
 1with import <nixpkgs> {};
 2
 3pkgs.mkShell {
 4  name = "go-dev";
 5
 6  nativeBuildInputs = with pkgs; [
 7    go
 8  ];
 9
10  LANGUAGE = "Go";
11  VERSION  = "go version";
12
13  shellHook = ''
14    # Optional: Script environment start up
15    echo "Welcome to $LANGUAGE Development Environment"
16    $VERSION
17  '';
18}

The above script will still give us a go environment, but this time it will also output a nice message to let us know the shell is running.

  1. Run a Pure environment

Remember a pure environment tries to ignore the existing host configuration 😂

Nix will automatically look for the file shell.nix so you dont need to added this parameter 🥰

1nix-shell --pure

Expected Output:

1Welcome to Go Development Environment
2go version go1.24.1 darwin/arm64

Awesome, Go is configured and ready to go!

  1. Exit the Nix Shell
1exit

Nice we now have a super easy way to run Go (or your choice of runtime language) on a Nix machine.