In this tutorial, you will learn how to add flake.nix
to a Go project.
Initialize a new project
You can start developing a Go project by simply adding main.go
and go.mod
to an empty directory. Refer to GitHub - golang-standards/project-layout: Standard Go Project Layout for a complex project layout.
Add flake.nix to the project
Initialize the template from the root directory of the project:
Add
.envrc
:Allow direnv:
Developing
Exposing the executable from the flake
To expose the program as a Nix flake package, comment out the stub inside packages
in the flake.nix
.
Adapt
pname
andversion
to match your package information:Then build the package with Nix:
It will show an error on the hash, so update
vendorHash
to the value shown in the output.It is also recommended to push your artifacts to a binary cache server and add its settings to the
flake.nix
:
Creating a justfile for local workflows
You can create the following justfile
for convenience.