NixOSaaS
← Back to the blog

6 November 2026 · NixOSaaS

Reproducible Go environments on NixOS: a beginner walkthrough

Nix is already installed. Build your first Go development environment in a NixOS VM, with every command and expected result.

Nix is already part of NixOS. You do not need another installer or a system-wide Go package to try a reproducible development environment. This walkthrough works inside a NixOS VM or on a NixOS workstation and creates every file from scratch.

1. Check the existing Nix installation

Open a terminal as your normal user and run:

nix --version
uname -m

For example, the NixOS VM used to try this exercise reported nix (Nix) 2.34.8. Your version may differ. x86_64 identifies an Intel/AMD guest and aarch64 an Arm guest. The flake below supports both.

Make sure the VM has working internet access and sufficient free disk space for package downloads. You do not need sudo, a change to configuration.nix, or nixos-rebuild for the project steps. Go being absent from the VM is expected: the development shell supplies it.

The commands below enable flakes explicitly for each invocation. This is enough to test the example without permanently changing your NixOS settings.

2. Create a fresh project directory

Run the following commands in a normal user terminal, not a root shell. Go does not need to be installed. Use a new directory outside any existing Git repository for this first exercise.

mkdir -p ~/projects/nix-go-demo
cd ~/projects/nix-go-demo
command -v go || echo "Go is not available in my normal shell"

Remember the result: if Go already exists, the final check should return to that original installation. Otherwise, Go being unavailable here is expected.

3. Describe the development environment

Copy the entire block below, including the final EOF, into your terminal. It creates flake.nix in the current directory. The quoted EOF keeps the shell from interpreting the Nix code. Use this in the fresh directory: it replaces an existing file with the same name.

cat > flake.nix <<'EOF'
{
  description = "Go project development shell";
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

  outputs = { nixpkgs, ... }:
    let
      systems = [
        "x86_64-linux"
        "aarch64-linux"
        "x86_64-darwin"
        "aarch64-darwin"
      ];
    in {
      devShells = nixpkgs.lib.genAttrs systems (system:
        let pkgs = nixpkgs.legacyPackages.${system};
        in {
          default = pkgs.mkShell {
            packages = [ pkgs.go pkgs.gopls ];
            GOTOOLCHAIN = "local";
          };
        });
    };
}
EOF

Here is what the main parts mean:

SettingPurpose
nixpkgsThe package collection supplying Go and its dependencies.
systemsSupported Linux and macOS CPU platforms; Nix selects the current one.
mkShellDefines the tools and variables available in the development shell.
pkgs.goSupplies the Go compiler and commands.
pkgs.goplsSupplies the Go language server for editors.
GOTOOLCHAIN = "local"Keeps Go from automatically downloading a different compiler.

The branch name chooses the initial package source. The lock file created next records the exact revision. This example uses the rolling nixos-unstable branch; later updates should be deliberate and tested.

4. Pin the inputs and enter the shell

These commands explicitly enable nix-command and flakes, so you do not need to edit system settings first:

nix --extra-experimental-features 'nix-command flakes' flake lock
nix --extra-experimental-features 'nix-command flakes' develop

The first command creates flake.lock. The second fetches the tools and opens a Bash development shell. Wait for the shell prompt to return before entering the next commands.

On the first run, you may see a message such as:

copying '«github:NixOS/nixpkgs/... »/' to the store

Downloading and unpacking Nixpkgs can take several minutes, particularly in a VM. The progress display may pause while files are processed. That alone does not mean the command is stuck. If it ends with an error, read that error before retrying; check internet connectivity and free space with df -h /nix if relevant.

Inside the new shell, run:

go version
command -v go
go env GOTOOLCHAIN

The executable path should begin with /nix/store/, and the final command should print local. The exact Go version depends on the revision recorded in your lock file. Your prompt may look similar to the original prompt: checking the executable path is more useful than relying on its appearance.

5. Create the Go module and application

Run these commands inside the development shell. Initialise the module once:

go mod init example.com/nix-go-demo

This creates go.mod. If you already ran it successfully, skip it; “go.mod already exists” simply means the module is already initialised.

Create main.go:

cat > main.go <<'EOF'
package main

import "fmt"

func Greeting(name string) string {
    return "Hello, " + name + "!"
}

func main() {
    fmt.Println(Greeting("NixOSaaS"))
}
EOF

Create main_test.go:

cat > main_test.go <<'EOF'
package main

import "testing"

func TestGreeting(t *testing.T) {
    got := Greeting("NixOSaaS")
    want := "Hello, NixOSaaS!"
    if got != want {
        t.Fatalf("got %q; want %q", got, want)
    }
}
EOF

Format, run and test the code:

gofmt -w main.go main_test.go
go run .
go test -v ./...

Expected output includes:

Hello, NixOSaaS!
=== RUN   TestGreeting
--- PASS: TestGreeting (0.00s)
PASS
ok      example.com/nix-go-demo    0.001s

Timing varies. A later test may report (cached). Use go test -count=1 -v ./... when you want a fresh test execution.

6. Leave the shell and test the boundary

Run exit once to leave the development shell:

exit
command -v go || echo "Go is not available in my normal shell"

If Go was absent before this exercise, it should be absent again. If it was already installed, the original executable should be available again. Nix has not removed that installation.

You can run the tests without opening an interactive shell. From the same project directory, run:

nix --extra-experimental-features 'nix-command flakes' develop -c go test -count=1 -v ./...

This supplies the project tools, runs the test and returns to your original shell automatically. The same command can be used on a CI runner with Nix installed.

7. Repeat on another machine

Keep these five files together:

  • flake.nix — the environment definition.
  • flake.lock — the pinned Nix inputs.
  • go.mod — the Go module definition.
  • main.go — the application.
  • main_test.go — its test.

This example uses only Go’s standard library, so it does not need a go.sum file. For projects with external dependencies, keep the generated go.sum as well.

Copy the directory to another machine with Nix installed and run the command from step 6. Reuse the existing lock file rather than generating an unrelated one. Linux and macOS select their own platform builds from the same pinned package collection; this does not make their kernels or runtime behaviour identical.

If Git is installed, you can save the project in a repository:

git init
git add flake.nix flake.lock go.mod main.go main_test.go
git commit -m "Add reproducible Go development example"

Git may ask you to configure your author name and email before committing. Once a directory is a Git repository, stage newly created Nix configuration files with git add; untracked files may be omitted from the flake source. Commit the lock file with the configuration.

Troubleshooting and limits

SymptomWhat to check
go: command not foundConfirm that nix develop finished successfully and that you are inside its shell, or use the one-command test from step 6.
Experimental feature is disabledInclude --extra-experimental-features 'nix-command flakes' exactly as shown.
Nix cannot find flake.nixRun pwd and ls; change to the project directory. If it is tracked by Git, stage the flake file.
The shell shows > after pastingThe shell may still be waiting for the final EOF. It must be alone on a line, without leading spaces.
Go cannot find the main moduleCheck that go.mod exists in this directory; run step 5’s initialisation if needed.
A dirty Git tree warning appearsUncommitted changes are normal while experimenting. Review and commit them when ready.

A Nix development shell is not a security sandbox. Commands can write to your project and home directory, access the network and use permissions your account already has. Nix packages remain in /nix/store after you exit, and Go may create caches in your home directory. Exiting does not undo application changes or reclaim all disk space.

Keep the lock file stable for repeatability. Update it deliberately, then rerun the tests. The environment definition supplies tools; it does not automatically execute tests, and this example does not define flake checks outputs.

Project tools versus system configuration

Use this project shell for repository-specific compilers and developer tools. Keep host services, users and machine settings in your NixOS configuration. The two layers complement each other: sharing a workstation baseline does not require every project to use the same compiler version.

Choose another platform

Further reading: Installing Nix, declarative shell environments, and nix develop.