6 November 2026 · NixOSaaS
Reproducible Go environments with Nix on Debian, Ubuntu and Fedora
Start from Nix installation, create a complete Go project and test it without installing Go through APT or DNF.
You can give a Go project its own toolchain on Debian, Ubuntu or Fedora without replacing your distribution or installing Go through APT or DNF. This walkthrough starts with Nix installation and ends with a passing test. No prior Go knowledge is needed.
1. Prepare your Linux machine
Use a supported Linux installation with internet access, spare disk space and administrator access for installation. The project commands later run as your normal user. Check first:
nix --version
If it prints a version, skip installation. If the command is missing, install the download prerequisites for your distribution only.
Debian or Ubuntu:
sudo apt update
sudo apt install curl xz-utils ca-certificates
Fedora:
sudo dnf install curl xz ca-certificates
For a conventional systemd-based desktop or server, the official Nix documentation provides this multi-user installation route:
curl -L https://nixos.org/nix/install | sh -s -- --daemon
This executes a downloaded installer and makes system changes. Review its prompts; it will request administrator privileges when needed. It sets up Nix alongside your existing package manager. If it reports a platform or security-policy incompatibility, consult the installation documentation rather than disabling security controls or installing a second Nix distribution over it.
After installation, close the terminal, open a new one and run nix --version again. Continue only when it succeeds. You do not need to install Go separately.
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:
| Setting | Purpose |
|---|---|
nixpkgs | The package collection supplying Go and its dependencies. |
systems | Supported Linux and macOS CPU platforms; Nix selects the current one. |
mkShell | Defines the tools and variables available in the development shell. |
pkgs.go | Supplies the Go compiler and commands. |
pkgs.gopls | Supplies 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
| Symptom | What to check |
|---|---|
go: command not found | Confirm that nix develop finished successfully and that you are inside its shell, or use the one-command test from step 6. |
| Experimental feature is disabled | Include --extra-experimental-features 'nix-command flakes' exactly as shown. |
Nix cannot find flake.nix | Run pwd and ls; change to the project directory. If it is tracked by Git, stage the flake file. |
The shell shows > after pasting | The shell may still be waiting for the final EOF. It must be alone on a line, without leading spaces. |
| Go cannot find the main module | Check that go.mod exists in this directory; run step 5’s initialisation if needed. |
| A dirty Git tree warning appears | Uncommitted 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.
When would NixOS help further?
Nix already solves the project-toolchain problem demonstrated here. NixOS extends declarative configuration to a Linux host’s users, services and system packages. Consider it when you also want to manage those host settings consistently. A successful project-shell pilot does not require migrating your existing operating system.
Choose another platform
- Reproducible Go environments with Nix on Debian, Ubuntu and Fedora
- Reproducible Go environments with Nix on macOS
- Reproducible Go environments on NixOS: a beginner walkthrough
Further reading: Installing Nix, declarative shell environments, and nix develop.