nixpkgs/doc/first-package.chapter.md
Johannes Kirschbauer acfc12be47
doc/manual: init example packaging guide
Used go as example, python and rust where not found suitable due to
theory overhead and multiple competing builder choices
2026-07-03 14:50:03 +02:00

1.8 KiB

Package your first application

Package an application with Nixpkgs by picking the build helper for its language and setting a few attributes.

Each language ecosystem has its own build helper. See for the full set.

Package a Go application

buildGoModule builds Go programs that use Go modules.

Write the package to package.nix:

:::{.example #ex-first-package-go}

Package pet with buildGoModule

# package.nix
{
  buildGoModule,
  fetchFromGitHub,
  lib,
}:
buildGoModule (finalAttrs: {
  pname = "pet";
  version = "0.3.4";

  src = fetchFromGitHub {
    owner = "knqyf263";
    repo = "pet";
    tag = "v${finalAttrs.version}";
    hash = "sha256-Gjw1dRrgM8D3G7v6WIM2+50r4HmTXvx0Xxme2fH9TlQ=";
  };

  vendorHash = "sha256-6hCgv2/8UIRHw1kCe3nLkxF23zE/7t5RDwEjSzX3pBQ=";

  meta = {
    description = "Simple command-line snippet manager, written in Go";
    homepage = "https://github.com/knqyf263/pet";
    license = lib.licenses.mit;
    maintainers = with lib.maintainers; [ kalbasit ];
  };
})

:::

buildGoModule needs pname, version, src, and vendorHash.

Pin Nixpkgs and call the package from default.nix:

# default.nix
let
  nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz";
  pkgs = import nixpkgs { };
in
pkgs.callPackage ./package.nix { }

Build it:

$ nix-build ./default.nix
# or equivalent
$ nix-build

Run it:

$ ./result/bin/pet --help
pet - Simple command-line snippet manager.

vendorHash pins the fetched dependencies. To find its value:

  1. Set vendorHash to an empty string "".
  2. Run nix-build.
  3. Copy the correct value from the error into vendorHash.

See the Go reference for every attribute and advanced usage.